ws_dfuService.cpp 177 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning (disable : 4786)
  14. #include <math.h>
  15. #include "daclient.hpp"
  16. #include "daft.hpp"
  17. #include "daftcfg.hpp"
  18. #include "fterror.hpp"
  19. #include "fverror.hpp"
  20. #include "daftprogress.hpp"
  21. #include "mpbase.hpp"
  22. #include "daclient.hpp"
  23. #include "dadfs.hpp"
  24. #include "dafdesc.hpp"
  25. #include "dasds.hpp"
  26. #include "danqs.hpp"
  27. #include "dalienv.hpp"
  28. #include "dautils.hpp"
  29. #include "jfile.hpp"
  30. #include "wshelpers.hpp"
  31. #include "LogicFileWrapper.hpp"
  32. #include "rmtfile.hpp"
  33. #include "dfuutil.hpp"
  34. #include "TpWrapper.hpp"
  35. #include "WUWrapper.hpp"
  36. #include "portlist.h"
  37. #include "roxiecommlibscm.hpp"
  38. #include "dfuwu.hpp"
  39. #include "fverror.hpp"
  40. #include "jstring.hpp"
  41. #include "exception_util.hpp"
  42. #include "ws_dfuService.hpp"
  43. #include "hqlerror.hpp"
  44. #include "eclrtl.hpp"
  45. #define Action_Delete "Delete"
  46. #define Action_AddtoSuperfile "Add To Superfile"
  47. static const char* FEATURE_URL="DfuAccess";
  48. #define FILE_NEWEST 1
  49. #define FILE_OLDEST 2
  50. #define FILE_LARGEST 3
  51. #define FILE_SMALLEST 4
  52. #define COUNTBY_SCOPE "Scope"
  53. #define COUNTBY_OWNER "Owner"
  54. #define COUNTBY_DATE "Date"
  55. #define COUNTBY_YEAR "Year"
  56. #define COUNTBY_QUARTER "Quarter"
  57. #define COUNTBY_MONTH "Month"
  58. #define COUNTBY_DAY "Day"
  59. #define REMOVE_FILE_SDS_CONNECT_TIMEOUT (1000*15) // 15 seconds
  60. const int DESCRIPTION_DISPLAY_LENGTH = 12;
  61. const unsigned MAX_VIEWKEYFILE_ROWS = 1000;
  62. const unsigned MAX_KEY_ROWS = 20;
  63. short days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  64. void CWsDfuEx::init(IPropertyTree *cfg, const char *process, const char *service)
  65. {
  66. StringBuffer xpath;
  67. DBGLOG("Initializing %s service [process = %s]", service, process);
  68. xpath.appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/DefaultScope", process, service);
  69. cfg->getProp(xpath.str(), defaultScope_);
  70. xpath.clear().appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/User", process, service);
  71. cfg->getProp(xpath.str(), user_);
  72. xpath.clear().appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/Password", process, service);
  73. cfg->getProp(xpath.str(), password_);
  74. StringBuffer disableUppercaseTranslation;
  75. xpath.clear().appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/DisableUppercaseTranslation", process, service);
  76. cfg->getProp(xpath.str(), disableUppercaseTranslation);
  77. m_clusterName.clear();
  78. xpath.clear().appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/ClusterName", process, service);
  79. cfg->getProp(xpath.str(), m_clusterName);
  80. Linked<IPropertyTree> globals;
  81. globals.set(cfg->queryPropTree(StringBuffer("Software/EspProcess[@name=\"").append(process).append("\"]/EspService[@name=\"").append(service).append("\"]").str()));
  82. const char * plugins = globals->queryProp("Plugins/@path");
  83. if (plugins)
  84. queryTransformerRegistry().addPlugins(plugins);
  85. m_disableUppercaseTranslation = false;
  86. if (streq(disableUppercaseTranslation.str(), "true"))
  87. m_disableUppercaseTranslation = true;
  88. if (!daliClientActive())
  89. throw MakeStringException(-1, "No Dali Connection Active. Please Specify a Dali to connect to in you configuration file");
  90. setDaliServixSocketCaching(true);
  91. }
  92. bool CWsDfuEx::onDFUSearch(IEspContext &context, IEspDFUSearchRequest & req, IEspDFUSearchResponse & resp)
  93. {
  94. try
  95. {
  96. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  97. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to Search Logical Files. Permission denied.");
  98. StringBuffer username;
  99. context.getUserID(username);
  100. DBGLOG("CWsDfuEx::onDFUSearch User=%s",username.str());
  101. Owned<IUserDescriptor> userdesc;
  102. if(username.length() > 0)
  103. {
  104. const char* passwd = context.queryPassword();
  105. userdesc.setown(createUserDescriptor());
  106. userdesc->set(username.str(), passwd);
  107. }
  108. CTpWrapper dummy;
  109. IArrayOf<IEspTpCluster> clusters;
  110. dummy.getClusterProcessList(eqThorCluster, clusters);
  111. dummy.getHthorClusterList(clusters);
  112. StringArray dfuclusters;
  113. ForEachItemIn(k, clusters)
  114. {
  115. IEspTpCluster& cluster = clusters.item(k);
  116. dfuclusters.append(cluster.getName());
  117. }
  118. IArrayOf<IEspTpCluster> clusters1;
  119. dummy.getClusterProcessList(eqRoxieCluster, clusters1);
  120. ForEachItemIn(k1, clusters1)
  121. {
  122. IEspTpCluster& cluster = clusters1.item(k1);
  123. StringBuffer slaveName = cluster.getName();
  124. dfuclusters.append(slaveName.str());
  125. }
  126. StringArray ftarray;
  127. ftarray.append("Logical Files and Superfiles");
  128. ftarray.append("Logical Files Only");
  129. ftarray.append("Superfiles Only");
  130. ftarray.append("Not in Superfiles");
  131. if (req.getShowExample() && *req.getShowExample())
  132. resp.setShowExample(req.getShowExample());
  133. resp.setClusterNames(dfuclusters);
  134. resp.setFileTypes(ftarray);
  135. }
  136. catch(IException* e)
  137. {
  138. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  139. }
  140. return true;
  141. }
  142. void addToQueryString(StringBuffer &queryString, const char *name, const char *value)
  143. {
  144. if (queryString.length() > 0)
  145. {
  146. queryString.append("&");
  147. }
  148. queryString.append(name);
  149. queryString.append("=");
  150. queryString.append(value);
  151. }
  152. void addToQueryStringFromInt(StringBuffer &queryString, const char *name, __int64 value)
  153. {
  154. if (queryString.length() > 0)
  155. {
  156. queryString.append("&");
  157. }
  158. queryString.append(name);
  159. queryString.append("=");
  160. queryString.append(value);
  161. }
  162. void parseTwoStringArrays(const char *input, StringArray& strarray1, StringArray& strarray2)
  163. {
  164. if (!input && strlen(input) > 2)
  165. return;
  166. char c0[2], c1[2];
  167. c0[0] = input[0], c0[1] = 0;
  168. c1[0] = input[1], c1[1] = 0;
  169. //the first string usually is a name; the second is a value
  170. unsigned int name_len = atoi(c0);
  171. unsigned int value_len = atoi(c1);
  172. if (name_len > 0 && value_len > 0)
  173. {
  174. char * inputText = (char *) input;
  175. inputText += 2; //skip 2 chars
  176. loop
  177. {
  178. if (!inputText || strlen(inputText) < name_len + value_len)
  179. break;
  180. StringBuffer columnNameLenStr, columnValueLenStr;
  181. for (unsigned i_name = 0; i_name < name_len; i_name++)
  182. {
  183. columnNameLenStr.append(inputText[0]);
  184. inputText++;
  185. }
  186. for (unsigned i_value = 0; i_value < value_len; i_value++)
  187. {
  188. columnValueLenStr.append(inputText[0]);
  189. inputText++;
  190. }
  191. unsigned columnNameLen = atoi(columnNameLenStr.str());
  192. unsigned columnValueLen = atoi(columnValueLenStr.str());
  193. if (!inputText || strlen(inputText) < columnNameLen + columnValueLen)
  194. break;
  195. char * colon = inputText + columnNameLen;
  196. if (!colon)
  197. break;
  198. StringAttr tmp;
  199. tmp.set(inputText, columnNameLen);
  200. strarray1.append(tmp.get());
  201. tmp.set(colon, columnValueLen);
  202. //tmp.toUpperCase();
  203. strarray2.append(tmp.get());
  204. inputText = colon + columnValueLen;
  205. }
  206. }
  207. return;
  208. }
  209. bool CWsDfuEx::onDFUQuery(IEspContext &context, IEspDFUQueryRequest & req, IEspDFUQueryResponse & resp)
  210. {
  211. try
  212. {
  213. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  214. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to Browse Logical Files. Permission denied.");
  215. StringBuffer username;
  216. context.getUserID(username);
  217. DBGLOG("CWsDfuEx::onDFUQuery User=%s",username.str());
  218. Owned<IUserDescriptor> userdesc;
  219. if(username.length() > 0)
  220. {
  221. const char* passwd = context.queryPassword();
  222. userdesc.setown(createUserDescriptor());
  223. userdesc->set(username.str(), passwd);
  224. }
  225. doLogicalFileSearch(context, userdesc.get(), req, resp);
  226. }
  227. catch(IException* e)
  228. {
  229. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  230. }
  231. return true;
  232. }
  233. bool CWsDfuEx::onDFUInfo(IEspContext &context, IEspDFUInfoRequest &req, IEspDFUInfoResponse &resp)
  234. {
  235. try
  236. {
  237. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  238. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to access DFUInfo. Permission denied.");
  239. StringBuffer username;
  240. context.getUserID(username);
  241. DBGLOG("CWsDfuEx::onDFUInfo User=%s",username.str());
  242. Owned<IUserDescriptor> userdesc;
  243. if(username.length() > 0)
  244. {
  245. const char* passwd = context.queryPassword();
  246. userdesc.setown(createUserDescriptor());
  247. userdesc->set(username.str(), passwd);
  248. }
  249. if (req.getUpdateDescription())
  250. {
  251. doGetFileDetails(context, userdesc.get(), req.getFileName(), req.getCluster(), req.getFileDesc(), resp.updateFileDetail());
  252. }
  253. else
  254. {
  255. doGetFileDetails(context, userdesc.get(), req.getName(), req.getCluster(), NULL, resp.updateFileDetail());
  256. }
  257. }
  258. catch(IException* e)
  259. {
  260. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  261. }
  262. return true;
  263. }
  264. bool CWsDfuEx::onDFUSpace(IEspContext &context, IEspDFUSpaceRequest & req, IEspDFUSpaceResponse & resp)
  265. {
  266. try
  267. {
  268. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  269. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to Browse Space Usage. Permission denied.");
  270. StringBuffer username;
  271. context.getUserID(username);
  272. DBGLOG("CWsDfuEx::onDFUSpace User=%s",username.str());
  273. Owned<IUserDescriptor> userdesc;
  274. if(username.length() > 0)
  275. {
  276. const char* passwd = context.queryPassword();
  277. userdesc.setown(createUserDescriptor());
  278. userdesc->set(username.str(), passwd);
  279. }
  280. const char *countby = req.getCountBy();
  281. if (!countby || strlen(countby) < 1)
  282. return true;
  283. char *scopeName = NULL;
  284. StringBuffer filter;
  285. if(req.getScopeUnder() && *req.getScopeUnder())
  286. {
  287. scopeName = (char *) req.getScopeUnder();
  288. filter.appendf("%s::*", scopeName);
  289. resp.setScopeUnder(req.getScopeUnder());
  290. }
  291. else
  292. {
  293. filter.append("*");
  294. }
  295. Owned<IDFAttributesIterator> fi = queryDistributedFileDirectory().getDFAttributesIterator(filter, userdesc.get(), true, false, NULL);
  296. if(!fi)
  297. throw MakeStringException(ECLWATCH_CANNOT_GET_FILE_ITERATOR,"Cannot get information from file system.");
  298. const char *ownerUnder = NULL;
  299. if(req.getOwnerUnder() && *req.getOwnerUnder())
  300. {
  301. ownerUnder = req.getOwnerUnder();
  302. resp.setOwnerUnder(ownerUnder);
  303. }
  304. StringBuffer wuFrom, wuTo, interval;
  305. unsigned yearFrom = 0, monthFrom, dayFrom, yearTo = 0, monthTo, dayTo, hour, minute, second, nano;
  306. if(req.getStartDate() && *req.getStartDate())
  307. {
  308. CDateTime wuTime;
  309. wuTime.setString(req.getStartDate(),NULL,true);
  310. wuTime.getDate(yearFrom, monthFrom, dayFrom, true);
  311. wuTime.getTime(hour, minute, second, nano, true);
  312. wuFrom.appendf("%4d-%02d-%02d %02d:%02d:%02d",yearFrom,monthFrom,dayFrom,hour,minute,second);
  313. StringBuffer startDate;
  314. startDate.appendf("%02d/%02d/%04d", monthFrom, dayFrom, yearFrom);
  315. resp.setStartDate(startDate.str());
  316. }
  317. if(req.getEndDate() && *req.getEndDate())
  318. {
  319. CDateTime wuTime;
  320. wuTime.setString(req.getEndDate(),NULL,true);
  321. wuTime.getDate(yearTo, monthTo, dayTo, true);
  322. wuTime.getTime(hour, minute, second, nano, true);
  323. wuTo.appendf("%4d-%02d-%02d %02d:%02d:%02d",yearTo,monthTo,dayTo,hour,minute,second);
  324. StringBuffer endDate;
  325. endDate.appendf("%02d/%02d/%04d", monthTo, dayTo, yearTo);
  326. resp.setEndDate(endDate.str());
  327. }
  328. unsigned i = 0;
  329. IArrayOf<IEspSpaceItem> SpaceItems64;
  330. if (!stricmp(countby, COUNTBY_DATE))
  331. {
  332. if (yearFrom < 1 || yearTo < 1)
  333. {
  334. StringBuffer wuFrom, wuTo;
  335. bool bFirst = true;
  336. ForEach(*fi)
  337. {
  338. IPropertyTree &attr=fi->query();
  339. StringBuffer modf(attr.queryProp("@modified"));
  340. //char* t=strchr(modf.str(),'T');
  341. //if(t) *t=' ';
  342. if (bFirst)
  343. {
  344. bFirst = false;
  345. wuFrom = modf.str();
  346. wuTo = modf.str();
  347. continue;
  348. }
  349. if (strcmp(modf.str(),wuFrom.str())<0)
  350. wuFrom = modf.str();
  351. if (strcmp(modf.str(),wuTo.str())>0)
  352. wuTo = modf.str();
  353. }
  354. if (yearFrom < 1)
  355. {
  356. CDateTime wuTime;
  357. wuTime.setString(wuFrom.str(),NULL,true);
  358. wuTime.getDate(yearFrom, monthFrom, dayFrom, true);
  359. }
  360. if (yearTo < 1)
  361. {
  362. CDateTime wuTime;
  363. wuTime.setString(wuTo.str(),NULL,true);
  364. wuTime.getDate(yearTo, monthTo, dayTo, true);
  365. }
  366. }
  367. interval = req.getInterval();
  368. resp.setInterval(interval);
  369. createSpaceItemsByDate(SpaceItems64, interval, yearFrom, monthFrom, dayFrom, yearTo, monthTo, dayTo);
  370. }
  371. else
  372. {
  373. Owned<IEspSpaceItem> item64 = createSpaceItem();
  374. if (stricmp(countby, COUNTBY_OWNER))
  375. {
  376. if (scopeName)
  377. item64->setName(scopeName);
  378. else
  379. item64->setName("(root)");
  380. }
  381. else
  382. {
  383. item64->setName("(empty)");
  384. }
  385. item64->setNumOfFilesInt(0);
  386. item64->setNumOfFilesIntUnknown(0);
  387. item64->setTotalSizeInt(0);
  388. item64->setLargestSizeInt(0);
  389. item64->setSmallestSizeInt(0);
  390. item64->setLargestFile("");
  391. item64->setSmallestFile("");
  392. SpaceItems64.append(*item64.getClear());
  393. }
  394. ForEach(*fi)
  395. {
  396. IPropertyTree &attr=fi->query();
  397. if (attr.hasProp("@numsubfiles"))
  398. continue; //exclude superfiles
  399. if (ownerUnder)
  400. {
  401. const char* owner=attr.queryProp("@owner");
  402. if (owner && stricmp(owner, ownerUnder))
  403. continue;
  404. }
  405. StringBuffer modf(attr.queryProp("@modified"));
  406. char* t= (char *) strchr(modf.str(),'T');
  407. if(t) *t=' ';
  408. if (wuFrom.length() && strcmp(modf.str(),wuFrom.str())<0)
  409. continue;
  410. if (wuTo.length() && strcmp(modf.str(),wuTo.str())>0)
  411. continue;
  412. if (!stricmp(countby, COUNTBY_DATE))
  413. {
  414. setSpaceItemByDate(SpaceItems64, interval, attr.queryProp("@modified"), attr.queryProp("@name"), attr.getPropInt64("@size",-1));
  415. }
  416. else if (!stricmp(countby, COUNTBY_OWNER))
  417. {
  418. setSpaceItemByOwner(SpaceItems64, attr.queryProp("@owner"), attr.queryProp("@name"), attr.getPropInt64("@size",-1));
  419. }
  420. else
  421. {
  422. setSpaceItemByScope(SpaceItems64, scopeName, attr.queryProp("@name"), attr.getPropInt64("@size",-1));
  423. }
  424. }
  425. i = 0;
  426. IEspSpaceItem& item0 = SpaceItems64.item(0);
  427. if (item0.getNumOfFilesInt() < 1)
  428. {
  429. i++;
  430. }
  431. IArrayOf<IEspDFUSpaceItem> SpaceItems;
  432. for(; i < SpaceItems64.length();i++)
  433. {
  434. IEspSpaceItem& item64 = SpaceItems64.item(i);
  435. if (item64.getNumOfFilesInt() < 1)
  436. continue;
  437. StringBuffer buf;
  438. Owned<IEspDFUSpaceItem> item1 = createDFUSpaceItem("","");
  439. item1->setName(item64.getName());
  440. buf << comma(item64.getNumOfFilesInt());
  441. item1->setNumOfFiles(buf.str());
  442. buf.clear();
  443. buf << comma(item64.getNumOfFilesIntUnknown());
  444. item1->setNumOfFilesUnknown(buf.str());
  445. buf.clear();
  446. buf << comma(item64.getTotalSizeInt());
  447. item1->setTotalSize(buf.str());
  448. buf.clear();
  449. buf << comma(item64.getLargestSizeInt());
  450. item1->setLargestSize(buf.str());
  451. buf.clear();
  452. buf << comma(item64.getSmallestSizeInt());
  453. item1->setSmallestSize(buf.str());
  454. item1->setLargestFile(item64.getLargestFile());
  455. item1->setSmallestFile(item64.getSmallestFile());
  456. SpaceItems.append(*item1.getClear());
  457. }
  458. resp.setDFUSpaceItems(SpaceItems);
  459. resp.setCountBy(req.getCountBy());
  460. }
  461. catch(IException* e)
  462. {
  463. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  464. }
  465. return true;
  466. }
  467. bool CWsDfuEx::setSpaceItemByScope(IArrayOf<IEspSpaceItem>& SpaceItems64, const char*scopeName, const char*logicalName, __int64 size)
  468. {
  469. char scope[1024];
  470. scope[0] = 0;
  471. const char* pName = NULL;
  472. if (!scopeName)
  473. {
  474. pName = strstr(logicalName, "::");
  475. if (!pName)
  476. return false;
  477. strncpy(scope, logicalName, pName - logicalName);
  478. scope[pName - logicalName] = 0;
  479. }
  480. else
  481. {
  482. if (strlen(logicalName) <= strlen(scopeName)+2)
  483. return false;
  484. char* ppName = (char*) logicalName + strlen(scopeName) + 2;
  485. pName = strstr(ppName, "::");
  486. if (pName)
  487. {
  488. strncpy(scope, logicalName, pName - logicalName);
  489. scope[pName - logicalName] = 0;
  490. }
  491. }
  492. if (strlen(scope) > 0)
  493. {
  494. IEspSpaceItem *item0 = NULL;
  495. for(unsigned i = 0; i < SpaceItems64.length();i++)
  496. {
  497. IEspSpaceItem& item1 = SpaceItems64.item(i);
  498. if (!stricmp(item1.getName(), scope))
  499. {
  500. item0 = &item1;
  501. break;
  502. }
  503. }
  504. if (!item0)
  505. {
  506. Owned<IEspSpaceItem> item1 = createSpaceItem();
  507. item1->setName(scope);
  508. item1->setNumOfFilesInt(1);
  509. if (size < 0)
  510. {
  511. item1->setNumOfFilesIntUnknown(1);
  512. item1->setTotalSizeInt(0);
  513. item1->setLargestSizeInt(0);
  514. item1->setSmallestSizeInt(0);
  515. item1->setLargestFile("");
  516. item1->setSmallestFile("");
  517. }
  518. else
  519. {
  520. item1->setNumOfFilesIntUnknown(0);
  521. item1->setTotalSizeInt(size);
  522. item1->setLargestSizeInt(size);
  523. item1->setSmallestSizeInt(size);
  524. item1->setLargestFile(logicalName);
  525. item1->setSmallestFile(logicalName);
  526. }
  527. SpaceItems64.append(*item1.getClear());
  528. }
  529. else if (size < 0)
  530. {
  531. item0->setNumOfFilesIntUnknown(item0->getNumOfFilesIntUnknown() + 1);
  532. item0->setNumOfFilesInt(item0->getNumOfFilesInt() + 1);
  533. }
  534. else
  535. {
  536. if (item0->getNumOfFilesInt() == item0->getNumOfFilesIntUnknown() || size > item0->getLargestSizeInt())
  537. {
  538. item0->setLargestSizeInt(size);
  539. item0->setLargestFile(logicalName);
  540. }
  541. if (item0->getNumOfFilesInt() == item0->getNumOfFilesIntUnknown() || size < item0->getSmallestSizeInt())
  542. {
  543. item0->setSmallestSizeInt(size);
  544. item0->setSmallestFile(logicalName);
  545. }
  546. item0->setNumOfFilesInt(item0->getNumOfFilesInt() + 1);
  547. item0->setTotalSizeInt(item0->getTotalSizeInt() + size);
  548. }
  549. }
  550. else
  551. {
  552. IEspSpaceItem& item0 = SpaceItems64.item(0);
  553. if (size < 0)
  554. {
  555. item0.setNumOfFilesInt(item0.getNumOfFilesInt() + 1);
  556. item0.setNumOfFilesIntUnknown(item0.getNumOfFilesIntUnknown() + 1);
  557. }
  558. else
  559. {
  560. if ((item0.getNumOfFilesInt() == item0.getNumOfFilesIntUnknown()) || (size > item0.getLargestSizeInt()))
  561. {
  562. item0.setLargestSizeInt(size);
  563. item0.setLargestFile(logicalName);
  564. }
  565. if ((item0.getNumOfFilesInt() == item0.getNumOfFilesIntUnknown()) || (size < item0.getSmallestSizeInt()))
  566. {
  567. item0.setSmallestSizeInt(size);
  568. item0.setSmallestFile(logicalName);
  569. }
  570. item0.setNumOfFilesInt(item0.getNumOfFilesInt() + 1);
  571. item0.setTotalSizeInt(item0.getTotalSizeInt() + size);
  572. }
  573. }
  574. return true;
  575. }
  576. bool CWsDfuEx::setSpaceItemByOwner(IArrayOf<IEspSpaceItem>& SpaceItems64, const char *owner, const char *logicalName, __int64 size)
  577. {
  578. if (owner && *owner)
  579. {
  580. IEspSpaceItem *item0 = NULL;
  581. for(unsigned i = 0; i < SpaceItems64.length();i++)
  582. {
  583. IEspSpaceItem& item1 = SpaceItems64.item(i);
  584. if (!stricmp(item1.getName(), owner))
  585. {
  586. item0 = &item1;
  587. break;
  588. }
  589. }
  590. if (!item0)
  591. {
  592. Owned<IEspSpaceItem> item1 = createSpaceItem();
  593. item1->setName(owner);
  594. item1->setNumOfFilesInt(1);
  595. if (size < 0)
  596. {
  597. item1->setNumOfFilesIntUnknown(1);
  598. item1->setTotalSizeInt(0);
  599. item1->setLargestSizeInt(0);
  600. item1->setSmallestSizeInt(0);
  601. item1->setLargestFile("");
  602. item1->setSmallestFile("");
  603. }
  604. else
  605. {
  606. item1->setNumOfFilesIntUnknown(0);
  607. item1->setTotalSizeInt(size);
  608. item1->setLargestSizeInt(size);
  609. item1->setSmallestSizeInt(size);
  610. item1->setLargestFile(logicalName);
  611. item1->setSmallestFile(logicalName);
  612. }
  613. SpaceItems64.append(*item1.getClear());
  614. }
  615. else if (size < 0)
  616. {
  617. item0->setNumOfFilesIntUnknown(item0->getNumOfFilesIntUnknown() + 1);
  618. item0->setNumOfFilesInt(item0->getNumOfFilesInt() + 1);
  619. }
  620. else
  621. {
  622. if (item0->getNumOfFilesInt() == item0->getNumOfFilesIntUnknown() || size > item0->getLargestSizeInt())
  623. {
  624. item0->setLargestSizeInt(size);
  625. item0->setLargestFile(logicalName);
  626. }
  627. if (item0->getNumOfFilesInt() == item0->getNumOfFilesIntUnknown() || size < item0->getSmallestSizeInt())
  628. {
  629. item0->setSmallestSizeInt(size);
  630. item0->setSmallestFile(logicalName);
  631. }
  632. item0->setNumOfFilesInt(item0->getNumOfFilesInt() + 1);
  633. item0->setTotalSizeInt(item0->getTotalSizeInt() + size);
  634. }
  635. }
  636. else
  637. {
  638. IEspSpaceItem& item0 = SpaceItems64.item(0);
  639. if (size < 0)
  640. {
  641. item0.setNumOfFilesInt(item0.getNumOfFilesInt() + 1);
  642. item0.setNumOfFilesIntUnknown(item0.getNumOfFilesIntUnknown() + 1);
  643. }
  644. else
  645. {
  646. if ((item0.getNumOfFilesInt() == item0.getNumOfFilesIntUnknown()) || (size > item0.getLargestSizeInt()))
  647. {
  648. item0.setLargestSizeInt(size);
  649. item0.setLargestFile(logicalName);
  650. }
  651. if ((item0.getNumOfFilesInt() == item0.getNumOfFilesIntUnknown()) || (size < item0.getSmallestSizeInt()))
  652. {
  653. item0.setSmallestSizeInt(size);
  654. item0.setSmallestFile(logicalName);
  655. }
  656. item0.setNumOfFilesInt(item0.getNumOfFilesInt() + 1);
  657. item0.setTotalSizeInt(item0.getTotalSizeInt() + size);
  658. }
  659. }
  660. return true;
  661. }
  662. bool CWsDfuEx::createSpaceItemsByDate(IArrayOf<IEspSpaceItem>& SpaceItems, StringBuffer interval, unsigned& yearFrom,
  663. unsigned& monthFrom, unsigned& dayFrom, unsigned& yearTo, unsigned& monthTo, unsigned& dayTo)
  664. {
  665. if (!stricmp(interval, COUNTBY_YEAR))
  666. {
  667. for (unsigned i = yearFrom; i <= yearTo; i++)
  668. {
  669. Owned<IEspSpaceItem> item64 = createSpaceItem();
  670. StringBuffer name;
  671. name.appendf("%04d", i);
  672. item64->setName(name.str());
  673. item64->setNumOfFilesInt(0);
  674. item64->setNumOfFilesIntUnknown(0);
  675. item64->setTotalSizeInt(0);
  676. item64->setLargestSizeInt(0);
  677. item64->setSmallestSizeInt(0);
  678. item64->setLargestFile("");
  679. item64->setSmallestFile("");
  680. SpaceItems.append(*item64.getClear());
  681. }
  682. }
  683. else if (!stricmp(interval, COUNTBY_QUARTER))
  684. {
  685. for (unsigned i = yearFrom; i <= yearTo; i++)
  686. {
  687. int quartStart = 1;
  688. int quartEnd = 4;
  689. if (i == yearFrom)
  690. {
  691. if (monthFrom > 9)
  692. {
  693. quartStart = 4;
  694. }
  695. else if (monthFrom > 6)
  696. {
  697. quartStart = 3;
  698. }
  699. else if (monthFrom > 3)
  700. {
  701. quartStart = 2;
  702. }
  703. }
  704. if (i == yearTo)
  705. {
  706. if (monthTo > 9)
  707. {
  708. quartEnd = 4;
  709. }
  710. else if (monthTo > 6)
  711. {
  712. quartEnd = 3;
  713. }
  714. else if (monthTo > 3)
  715. {
  716. quartEnd = 2;
  717. }
  718. }
  719. for (int j = quartStart; j <= quartEnd; j++)
  720. {
  721. Owned<IEspSpaceItem> item64 = createSpaceItem();
  722. StringBuffer name;
  723. name.appendf("%04d quarter: %d", i, j);
  724. item64->setName(name.str());
  725. item64->setNumOfFilesInt(0);
  726. item64->setNumOfFilesIntUnknown(0);
  727. item64->setTotalSizeInt(0);
  728. item64->setLargestSizeInt(0);
  729. item64->setSmallestSizeInt(0);
  730. item64->setLargestFile("");
  731. item64->setSmallestFile("");
  732. SpaceItems.append(*item64.getClear());
  733. }
  734. }
  735. }
  736. else if (!stricmp(interval, COUNTBY_MONTH))
  737. {
  738. for (unsigned i = yearFrom; i <= yearTo; i++)
  739. {
  740. int jFrom = (i != yearFrom) ? 1 : monthFrom;
  741. int jTo = (i != yearTo) ? 12 : monthTo;
  742. for (int j = jFrom; j <= jTo; j++)
  743. {
  744. Owned<IEspSpaceItem> item64 = createSpaceItem();
  745. StringBuffer name;
  746. name.appendf("%04d-%02d", i, j);
  747. item64->setName(name.str());
  748. item64->setNumOfFilesInt(0);
  749. item64->setNumOfFilesIntUnknown(0);
  750. item64->setTotalSizeInt(0);
  751. item64->setLargestSizeInt(0);
  752. item64->setSmallestSizeInt(0);
  753. item64->setLargestFile("");
  754. item64->setSmallestFile("");
  755. SpaceItems.append(*item64.getClear());
  756. }
  757. }
  758. }
  759. else
  760. {
  761. for (unsigned i = yearFrom; i <= yearTo; i++)
  762. {
  763. int jFrom = (i != yearFrom) ? 1 : monthFrom;
  764. int jTo = (i != yearTo) ? 12 : monthTo;
  765. for (int j = jFrom; j <= jTo; j++)
  766. {
  767. int dayStart = 1;
  768. int dayEnd = days[j-1];
  769. if (i == yearFrom && j == monthFrom)
  770. {
  771. dayStart = dayFrom;
  772. }
  773. else if (i == yearTo && j == monthTo)
  774. {
  775. dayEnd = dayTo;
  776. }
  777. for (int k = dayStart; k <= dayEnd; k++)
  778. {
  779. Owned<IEspSpaceItem> item64 = createSpaceItem();
  780. StringBuffer name;
  781. name.appendf("%04d-%02d-%02d", i, j, k);
  782. item64->setName(name.str());
  783. item64->setNumOfFilesInt(0);
  784. item64->setNumOfFilesIntUnknown(0);
  785. item64->setTotalSizeInt(0);
  786. item64->setLargestSizeInt(0);
  787. item64->setSmallestSizeInt(0);
  788. item64->setLargestFile("");
  789. item64->setSmallestFile("");
  790. SpaceItems.append(*item64.getClear());
  791. }
  792. }
  793. }
  794. }
  795. return true;
  796. }
  797. bool CWsDfuEx::setSpaceItemByDate(IArrayOf<IEspSpaceItem>& SpaceItems, StringBuffer interval, StringBuffer mod, const char*logicalName, __int64 size)
  798. {
  799. unsigned year, month, day;
  800. CDateTime wuTime;
  801. wuTime.setString(mod.str(),NULL,true);
  802. wuTime.getDate(year, month, day, true);
  803. StringBuffer name;
  804. if (!stricmp(interval, COUNTBY_YEAR))
  805. {
  806. name.appendf("%04d", year);
  807. }
  808. else if (!stricmp(interval, COUNTBY_QUARTER))
  809. {
  810. int quart = 1;
  811. if (month > 9)
  812. {
  813. quart = 4;
  814. }
  815. else if (month > 6)
  816. {
  817. quart = 3;
  818. }
  819. else if (month > 3)
  820. {
  821. quart = 2;
  822. }
  823. name.appendf("%04d quarter: %d", year, quart);
  824. }
  825. else if (!stricmp(interval, COUNTBY_MONTH))
  826. {
  827. name.appendf("%04d-%02d", year, month);
  828. }
  829. else
  830. {
  831. name.appendf("%04d-%02d-%02d", year, month, day);
  832. }
  833. for (unsigned i = 0; i < SpaceItems.length(); i++)
  834. {
  835. IEspSpaceItem& item0 = SpaceItems.item(i);
  836. if (!stricmp(item0.getName(), name))
  837. {
  838. if (size < 0)
  839. {
  840. item0.setNumOfFilesIntUnknown(item0.getNumOfFilesIntUnknown() + 1);
  841. }
  842. else
  843. {
  844. if ((item0.getNumOfFilesInt() == item0.getNumOfFilesIntUnknown()) || (size > item0.getLargestSizeInt()))
  845. {
  846. item0.setLargestSizeInt(size);
  847. item0.setLargestFile(logicalName);
  848. }
  849. if ((item0.getNumOfFilesInt() == item0.getNumOfFilesIntUnknown()) || (size < item0.getSmallestSizeInt()))
  850. {
  851. item0.setSmallestSizeInt(size);
  852. item0.setSmallestFile(logicalName);
  853. }
  854. item0.setTotalSizeInt(item0.getTotalSizeInt() + size);
  855. }
  856. item0.setNumOfFilesInt(item0.getNumOfFilesInt() + 1);
  857. break;
  858. }
  859. }
  860. return true;
  861. }
  862. void CWsDfuEx::parseStringArray(const char *input, StringArray& strarray)
  863. {
  864. if (!input || !*input)
  865. return;
  866. const char *ptr = input;
  867. const char *pptr = ptr;
  868. while (pptr[0])
  869. {
  870. if (pptr[0] == ',')
  871. {
  872. StringAttr tmp;
  873. tmp.set(ptr, pptr-ptr);
  874. strarray.append(tmp.get());
  875. ptr = pptr + 1;
  876. }
  877. pptr++;
  878. }
  879. if (pptr > ptr)
  880. {
  881. StringAttr tmp;
  882. tmp.set(ptr, pptr-ptr);
  883. strarray.append(tmp.get());
  884. }
  885. }
  886. int CWsDfuEx::superfileAction(IEspContext &context, const char* action, const char* superfile, StringArray& subfiles,
  887. const char* beforeSubFile, bool existingSuperfile, bool autocreatesuper, bool deleteFile, bool removeSuperfile)
  888. {
  889. if (!action || !*action)
  890. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Superfile action not specified");
  891. if(!strieq(action, "add") && !strieq(action, "remove"))
  892. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Only Add or Remove is allowed.");
  893. if (!superfile || !*superfile)
  894. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Superfile name not specified");
  895. StringBuffer username;
  896. context.getUserID(username);
  897. Owned<IUserDescriptor> userdesc;
  898. if(username.length() > 0)
  899. {
  900. const char* passwd = context.queryPassword();
  901. userdesc.setown(createUserDescriptor());
  902. userdesc->set(username.str(), passwd);
  903. }
  904. if (!autocreatesuper)
  905. {//a file lock created by the lookup() will be released after '}'
  906. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(superfile, userdesc.get(), true);
  907. if (existingSuperfile)
  908. {
  909. if (!df)
  910. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find file %s.",superfile);
  911. if(!df->querySuperFile())
  912. throw MakeStringException(ECLWATCH_NOT_SUPERFILE,"%s is not a superfile.",superfile);
  913. }
  914. else if (df)
  915. throw MakeStringException(ECLWATCH_FILE_ALREADY_EXISTS,"The file %s already exists.",superfile);
  916. }
  917. PointerArrayOf<char> subfileArray;
  918. unsigned num = subfiles.length();
  919. if (num > 0)
  920. {
  921. StringBuffer msgHead;
  922. if(username.length() > 0)
  923. msgHead.appendf("CWsDfuEx::SuperfileAction User=%s Action=%s, Superfile=%s Subfile(s)= ", username.str(), action, superfile);
  924. else
  925. msgHead.appendf("CWsDfuEx::SuperfileAction User=<unknown> Action=%s, Superfile=%s Subfile(s)= ", action, superfile);
  926. unsigned filesInMsgBuf = 0;
  927. StringBuffer msgBuf = msgHead;
  928. for(unsigned i = 0; i < num; i++)
  929. {
  930. subfileArray.append((char*) subfiles.item(i));
  931. msgBuf.appendf("%s, ", subfiles.item(i));
  932. filesInMsgBuf++;
  933. if (filesInMsgBuf > 9)
  934. {
  935. PROGLOG("%s",msgBuf.str());
  936. msgBuf = msgHead;
  937. filesInMsgBuf = 0;
  938. }
  939. }
  940. if (filesInMsgBuf > 0)
  941. PROGLOG("%s", msgBuf.str());
  942. }
  943. Owned<IDFUhelper> dfuhelper = createIDFUhelper();
  944. synchronized block(m_superfilemutex);
  945. if(strieq(action, "add"))
  946. dfuhelper->addSuper(superfile, userdesc.get(), num, (const char**) subfileArray.getArray(), beforeSubFile, true);
  947. else
  948. dfuhelper->removeSuper(superfile, userdesc.get(), num, (const char**) subfileArray.getArray(), deleteFile, removeSuperfile);
  949. return num;
  950. }
  951. bool CWsDfuEx::onAddtoSuperfile(IEspContext &context, IEspAddtoSuperfileRequest &req, IEspAddtoSuperfileResponse &resp)
  952. {
  953. try
  954. {
  955. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Write, false))
  956. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to AddtoSuperfile. Permission denied.");
  957. double version = context.getClientVersion();
  958. if (version > 1.17)
  959. {
  960. const char* backTo = req.getBackToPage();
  961. if (backTo && *backTo)
  962. resp.setBackToPage(backTo);
  963. }
  964. resp.setSubfiles(req.getSubfiles());
  965. const char* superfile = req.getSuperfile();
  966. if (!superfile || !*superfile)
  967. {
  968. if (version > 1.15)
  969. {//Display the subfiles inside a table
  970. const char* files = req.getSubfiles();
  971. if (files && *files)
  972. {
  973. StringArray subfileNames;
  974. parseStringArray(files, subfileNames);
  975. if (subfileNames.length() > 0)
  976. resp.setSubfileNames(subfileNames);
  977. }
  978. }
  979. return true;//Display a form for user to specify superfile
  980. }
  981. if (version > 1.15)
  982. {
  983. superfileAction(context, "add", superfile, req.getNames(), NULL, req.getExistingFile(), false, false);
  984. }
  985. else
  986. {
  987. StringArray subfileNames;
  988. const char *subfilesStr = req.getSubfiles();
  989. if (subfilesStr && *subfilesStr)
  990. parseStringArray(subfilesStr, subfileNames);
  991. superfileAction(context, "add", superfile, subfileNames, NULL, req.getExistingFile(), false, false);
  992. }
  993. resp.setRedirectUrl(StringBuffer("/WsDFU/DFUInfo?Name=").append(superfile));
  994. }
  995. catch(IException* e)
  996. {
  997. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  998. }
  999. return true;
  1000. }
  1001. bool CWsDfuEx::DFUDeleteFiles(IEspContext &context, IEspDFUArrayActionRequest &req, IEspDFUArrayActionResponse &resp)
  1002. {
  1003. StringBuffer username;
  1004. context.getUserID(username);
  1005. Owned<IUserDescriptor> userdesc;
  1006. if(username.length() > 0)
  1007. {
  1008. const char* passwd = context.queryPassword();
  1009. userdesc.setown(createUserDescriptor());
  1010. userdesc->set(username.str(), passwd);
  1011. }
  1012. StringBuffer returnStr;
  1013. StringArray superFileNames, filesCannotBeDeleted;
  1014. for(int j = 0; j < 2; j++) //j=0: delete superfiles first
  1015. {
  1016. for(unsigned i = 0; i < req.getLogicalFiles().length(); i++)
  1017. {
  1018. const char* filename = req.getLogicalFiles().item(i);
  1019. if(!filename || !*filename)
  1020. continue;
  1021. if (j>0)
  1022. { // 2nd pass, now we want to skip superfiles and the files which cannot do the lookup.
  1023. if (superFileNames.contains(filename))
  1024. continue;
  1025. if (filesCannotBeDeleted.contains(filename))
  1026. continue;
  1027. }
  1028. Owned<IDistributedFile> df;
  1029. try
  1030. {
  1031. df.setown(queryDistributedFileDirectory().lookup(filename, userdesc, true));
  1032. if(!df)
  1033. {
  1034. returnStr.appendf("<Message><Value>Cannot delete %s: file not found</Value></Message>", filename);
  1035. filesCannotBeDeleted.append(filename);
  1036. continue;
  1037. }
  1038. }
  1039. catch(IException* e)
  1040. {
  1041. filesCannotBeDeleted.append(filename);
  1042. StringBuffer emsg;
  1043. e->errorMessage(emsg);
  1044. if((e->errorCode() == DFSERR_CreateAccessDenied) && (req.getType() != NULL))
  1045. emsg.replaceString("Create ", "Delete ");
  1046. returnStr.appendf("<Message><Value>Cannot delete %s: %s</Value></Message>", filename, emsg.str());
  1047. }
  1048. catch(...)
  1049. {
  1050. returnStr.appendf("<Message><Value>Cannot delete %s: unknown exception.</Value></Message>", filename);
  1051. }
  1052. if (df)
  1053. {
  1054. if (0==j) // skip non-super files on 1st pass
  1055. {
  1056. if(!df->querySuperFile())
  1057. continue;
  1058. superFileNames.append(filename);
  1059. }
  1060. DBGLOG("CWsDfuEx::DFUDeleteFiles User=%s Action=Delete File=%s",username.str(), filename);
  1061. try
  1062. {
  1063. df->detach(REMOVE_FILE_SDS_CONNECT_TIMEOUT);
  1064. PROGLOG("Deleted Logical File: %s\n",filename);
  1065. returnStr.appendf("<Message><Value>Deleted File %s</Value></Message>", filename);
  1066. }
  1067. catch (IException *e)
  1068. {
  1069. StringBuffer errorMsg;
  1070. e->errorMessage(errorMsg);
  1071. PROGLOG("%s", errorMsg.str());
  1072. e->Release();
  1073. returnStr.appendf("<Message><Value>%s</Value></Message>", errorMsg.str());
  1074. }
  1075. }
  1076. }
  1077. }
  1078. resp.setDFUArrayActionResult(returnStr.str());
  1079. return true;
  1080. }
  1081. bool CWsDfuEx::onDFUArrayAction(IEspContext &context, IEspDFUArrayActionRequest &req, IEspDFUArrayActionResponse &resp)
  1082. {
  1083. try
  1084. {
  1085. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Write, false))
  1086. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to update Logical Files. Permission denied.");
  1087. double version = context.getClientVersion();
  1088. if (version > 1.03)
  1089. {
  1090. StringBuffer backToPage = req.getBackToPage();
  1091. if (backToPage.length() > 0)
  1092. {
  1093. const char* oldStr = "&";
  1094. const char* newStr = "&amp;";
  1095. backToPage.replaceString(oldStr, newStr);
  1096. resp.setBackToPage(backToPage.str());
  1097. }
  1098. }
  1099. if (strcmp(req.getType(), Action_Delete) == 0)
  1100. {
  1101. /*StringArray roxieQueries;
  1102. checkRoxieQueryFilesOnDelete(req, roxieQueries);
  1103. if (roxieQueries.length() > 0)
  1104. {
  1105. returnStr.append("<Message><Value>Cannot delete the files because of the following roxie queries: ");
  1106. for(int i = 0; i < roxieQueries.length();i++)
  1107. {
  1108. const char* query = roxieQueries.item(i);
  1109. if(!query || !*query)
  1110. continue;
  1111. if (i==0)
  1112. returnStr.append(query);
  1113. else
  1114. returnStr.appendf(",%s", query);
  1115. }
  1116. returnStr.append("</Value></Message>");
  1117. resp.setDFUArrayActionResult(returnStr.str());
  1118. return true;
  1119. }*/
  1120. return DFUDeleteFiles(context, req, resp);
  1121. }
  1122. StringBuffer username;
  1123. context.getUserID(username);
  1124. Owned<IUserDescriptor> userdesc;
  1125. if(username.length() > 0)
  1126. {
  1127. const char* passwd = context.queryPassword();
  1128. userdesc.setown(createUserDescriptor());
  1129. userdesc->set(username.str(), passwd);
  1130. }
  1131. StringBuffer returnStr;
  1132. for(unsigned i = 0; i < req.getLogicalFiles().length();i++)
  1133. {
  1134. const char* file = req.getLogicalFiles().item(i);
  1135. if(!file || !*file)
  1136. continue;
  1137. unsigned len = strlen(file);
  1138. char* curfile = new char[len+1];
  1139. const char* cluster = NULL;
  1140. const char *pCh = strchr(file, '@');
  1141. if (pCh)
  1142. {
  1143. len = pCh - file;
  1144. if (len+1 < strlen(file))
  1145. cluster = pCh + 1;
  1146. }
  1147. strncpy(curfile, file, len);
  1148. curfile[len] = 0;
  1149. DBGLOG("CWsDfuEx::onDFUArrayAction User=%s Action=%s File=%s",username.str(),req.getType(), file);
  1150. try
  1151. {
  1152. onDFUAction(userdesc.get(), curfile, cluster, req.getType(), returnStr);
  1153. }
  1154. catch(IException* e)
  1155. {
  1156. StringBuffer emsg;
  1157. e->errorMessage(emsg);
  1158. if((e->errorCode() == DFSERR_CreateAccessDenied) && (req.getType() != NULL))
  1159. {
  1160. if (strcmp(req.getType(), Action_AddtoSuperfile) == 0)
  1161. {
  1162. emsg.replaceString("Create ", "AddtoSuperfile ");
  1163. }
  1164. }
  1165. returnStr.appendf("<Message><Value>%s</Value></Message>", emsg.str());
  1166. }
  1167. catch(...)
  1168. {
  1169. returnStr.appendf("<Message><Value>Unknown exception onDFUArrayAction %s</Value></Message>", curfile);
  1170. }
  1171. delete [] curfile;
  1172. }
  1173. if (strcmp(Action_AddtoSuperfile ,req.getType()) == 0)
  1174. {
  1175. returnStr.replaceString("#", "%23");
  1176. if (version < 1.18)
  1177. resp.setRedirectUrl(StringBuffer("/WsDFU/AddtoSuperfile?Subfiles=").append(returnStr.str()));
  1178. else
  1179. {
  1180. resp.setRedirectTo(returnStr.str());
  1181. }
  1182. }
  1183. else
  1184. {
  1185. resp.setDFUArrayActionResult(returnStr.str());
  1186. }
  1187. }
  1188. catch(IException* e)
  1189. {
  1190. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1191. }
  1192. return true;
  1193. }
  1194. bool CWsDfuEx::onDFUAction(IUserDescriptor* udesc, const char* LogicalFileName, const char* ClusterName, const char* ActionType, StringBuffer& returnStr)
  1195. {
  1196. //No 'try/catch' is needed for this method since it will be called internally.
  1197. if (strcmp(Action_Delete ,ActionType) == 0)
  1198. {
  1199. LogicFileWrapper Logicfile;
  1200. if (!Logicfile.doDeleteFile(LogicalFileName,ClusterName, returnStr, udesc))
  1201. return false;
  1202. }
  1203. else if (strcmp(Action_AddtoSuperfile ,ActionType) == 0)
  1204. {
  1205. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(LogicalFileName, udesc, true);
  1206. if (df)
  1207. {
  1208. if (returnStr.length() > 0)
  1209. returnStr.appendf(",%s", LogicalFileName);
  1210. else
  1211. returnStr.appendf("%s", LogicalFileName);
  1212. return false;
  1213. }
  1214. }
  1215. else
  1216. DBGLOG("Unknown Action type:%s\n",ActionType);
  1217. return true;
  1218. }
  1219. bool CWsDfuEx::onDFUDefFile(IEspContext &context,IEspDFUDefFileRequest &req, IEspDFUDefFileResponse &resp)
  1220. {
  1221. try
  1222. {
  1223. DBGLOG("CWsDfuEx::onDFUDefFile\n");
  1224. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  1225. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to access DFUDefFile. Permission denied.");
  1226. StringBuffer username;
  1227. context.getUserID(username);
  1228. DBGLOG("CWsDfuEx::onDFUDefFile User=%s",username.str());
  1229. StringBuffer rawStr,returnStr;
  1230. Owned<IUserDescriptor> userdesc;
  1231. if(username.length() > 0)
  1232. {
  1233. const char* passwd = context.queryPassword();
  1234. userdesc.setown(createUserDescriptor());
  1235. userdesc->set(username.str(), passwd);
  1236. }
  1237. getDefFile(userdesc.get(), req.getName(),rawStr);
  1238. StringBuffer xsltFile;
  1239. xsltFile.append(getCFD()).append("smc_xslt/").append(req.getFormat()).append("_def_file.xslt");
  1240. xsltTransformer(xsltFile.str(),rawStr,returnStr);
  1241. //set the file
  1242. MemoryBuffer buff;
  1243. buff.setBuffer(returnStr.length(), (void*)returnStr.toCharArray());
  1244. resp.setDefFile(buff);
  1245. //set the type
  1246. StringBuffer type;
  1247. const char* format = req.getFormat();
  1248. if (!stricmp(format, "def"))
  1249. format = "plain";
  1250. type.append("text/").append(format);
  1251. resp.setDefFile_mimetype(type.str());
  1252. }
  1253. catch(IException* e)
  1254. {
  1255. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1256. }
  1257. return true;
  1258. }
  1259. void CWsDfuEx::xsltTransformer(const char* xsltPath,StringBuffer& source,StringBuffer& returnStr)
  1260. {
  1261. if (m_xsl.get() == 0)
  1262. {
  1263. m_xsl.setown(getXslProcessor());
  1264. }
  1265. Owned<IXslTransform> xform = m_xsl->createXslTransform();
  1266. xform->loadXslFromFile(xsltPath);
  1267. xform->setXmlSource(source.str(), source.length()+1);
  1268. xform->transform(returnStr.clear());
  1269. }
  1270. void CWsDfuEx::getDefFile(IUserDescriptor* udesc, const char* FileName,StringBuffer& returnStr)
  1271. {
  1272. DBGLOG("CWsDfuEx::getDefFile\n");
  1273. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(FileName, udesc);
  1274. if(!df)
  1275. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find file %s.",FileName);
  1276. if(!df->queryAttributes().hasProp("ECL"))
  1277. throw MakeStringException(ECLWATCH_MISSING_PARAMS,"No record definition for file %s.",FileName);
  1278. StringBuffer text;
  1279. text.append(df->queryAttributes().queryProp("ECL"));
  1280. MultiErrorReceiver errs;
  1281. OwnedHqlExpr record = parseQuery(text.str(), &errs);
  1282. if (errs.errCount())
  1283. {
  1284. StringBuffer errtext;
  1285. IECLError *first = errs.firstError();
  1286. first->toString(errtext);
  1287. throw MakeStringException(ECLWATCH_CANNOT_PARSE_ECL_QUERY, "Failed in parsing ECL query: %s @ %d:%d.", errtext.str(), first->getColumn(), first->getLine());
  1288. }
  1289. if(!record)
  1290. throw MakeStringException(ECLWATCH_CANNOT_PARSE_ECL_QUERY, "Failed in parsing ECL query.");
  1291. Owned<IPropertyTree> data = createPTree("Table", ipt_caseInsensitive);
  1292. exportData(data, record);
  1293. const char* fname=strrchr(FileName,':');
  1294. data->setProp("filename",fname ? fname+1 : FileName);
  1295. toXML(data, returnStr, 0, 0);
  1296. }
  1297. bool CWsDfuEx::checkFileContent(IEspContext &context, IUserDescriptor* udesc, const char * logicalName, const char * cluster)
  1298. {
  1299. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName, udesc);
  1300. if (!df)
  1301. return false;
  1302. if (!cluster || !stricmp(cluster, ""))
  1303. {
  1304. char *eclCluster = NULL;
  1305. const char* wuid = df->queryAttributes().queryProp("@workunit");
  1306. if (wuid && *wuid)
  1307. {
  1308. try
  1309. {
  1310. Owned<IWorkUnitFactory> factory = (context.querySecManager() ? getSecWorkUnitFactory(*context.querySecManager(), *context.queryUser()) : getWorkUnitFactory());
  1311. if (factory)
  1312. {
  1313. IConstWorkUnit* wu = factory->openWorkUnit(wuid, false);
  1314. if (wu)
  1315. {
  1316. SCMStringBuffer cluster;
  1317. eclCluster = (char *)wu->getClusterName(cluster).str();
  1318. }
  1319. }
  1320. }
  1321. catch(...)
  1322. {
  1323. return false;
  1324. }
  1325. }
  1326. if (!eclCluster || !stricmp(eclCluster, ""))
  1327. return false;
  1328. }
  1329. bool blocked;
  1330. if (df->isCompressed(&blocked) && !blocked)
  1331. return false;
  1332. IPropertyTree & properties = df->queryAttributes();
  1333. const char * format = properties.queryProp("@format");
  1334. if (format && (stricmp(format,"csv")==0 || memicmp(format, "utf", 3) == 0))
  1335. {
  1336. return true;
  1337. }
  1338. const char * recordEcl = properties.queryProp("ECL");
  1339. if (!recordEcl)
  1340. return false;
  1341. MultiErrorReceiver errs;
  1342. Owned< IHqlExpression> ret = ::parseQuery(recordEcl, &errs);
  1343. return errs.errCount() == 0;
  1344. }
  1345. bool FindInStringArray(StringArray& clusters, const char *cluster)
  1346. {
  1347. bool bFound = false;
  1348. if(cluster && *cluster)
  1349. {
  1350. if (clusters.ordinality())
  1351. {
  1352. ForEachItemIn(i, clusters)
  1353. {
  1354. const char* cluster0 = clusters.item(i);
  1355. if(cluster0 && *cluster0 && !stricmp(cluster, cluster0))
  1356. return true;
  1357. }
  1358. }
  1359. }
  1360. else
  1361. {
  1362. #if 0 //Comment out since clusters are not set for some old files
  1363. if (!clusters.ordinality())
  1364. return true;
  1365. ForEachItemIn(i, clusters)
  1366. {
  1367. const char* cluster0 = clusters.item(i);
  1368. if(cluster0 && !*cluster0)
  1369. {
  1370. return true;
  1371. }
  1372. }
  1373. #else
  1374. return true;
  1375. #endif
  1376. }
  1377. return bFound;
  1378. }
  1379. static void getFilePermission(const CDfsLogicalFileName &dlfn, ISecUser* user, IUserDescriptor* udesc, ISecManager* secmgr, int& permission)
  1380. {
  1381. if (dlfn.isMulti())
  1382. {
  1383. unsigned i = dlfn.multiOrdinality();
  1384. while (i--)
  1385. {
  1386. getFilePermission(dlfn.multiItem(i), user, udesc, secmgr, permission);
  1387. }
  1388. }
  1389. else
  1390. {
  1391. int permissionTemp;
  1392. if (dlfn.isForeign())
  1393. {
  1394. permissionTemp = queryDistributedFileDirectory().getFilePermissions(dlfn.get(), udesc);
  1395. }
  1396. else
  1397. {
  1398. StringBuffer scopes;
  1399. dlfn.getScopes(scopes);
  1400. permissionTemp = secmgr->authorizeFileScope(*user, scopes.str());
  1401. }
  1402. //Descrease the permission whenever a component has a lower permission.
  1403. if (permissionTemp < permission)
  1404. permission = permissionTemp;
  1405. }
  1406. return;
  1407. }
  1408. bool CWsDfuEx::getUserFilePermission(IEspContext &context, IUserDescriptor* udesc, const char* logicalName, int& permission)
  1409. {
  1410. ISecManager* secmgr = context.querySecManager();
  1411. if (!secmgr)
  1412. {
  1413. return false;
  1414. }
  1415. StringBuffer username;
  1416. StringBuffer password;
  1417. udesc->getUserName(username);
  1418. if (username.length() < 1)
  1419. {
  1420. DBGLOG("User Name not defined\n");
  1421. return false;
  1422. }
  1423. udesc->getPassword(password);
  1424. Owned<ISecUser> user = secmgr->createUser(username);
  1425. if (!user)
  1426. {
  1427. DBGLOG("User %s not found\n", username.str());
  1428. return false;
  1429. }
  1430. if (password.length() > 0)
  1431. user->credentials().setPassword(password);
  1432. CDfsLogicalFileName dlfn;
  1433. dlfn.set(logicalName);
  1434. //Start from the SecAccess_Full. Descrease the permission whenever a component has a lower permission.
  1435. permission = SecAccess_Full;
  1436. getFilePermission(dlfn, user, udesc, secmgr, permission);
  1437. return true;
  1438. }
  1439. void CWsDfuEx::doGetFileDetails(IEspContext &context, IUserDescriptor* udesc, const char *name, const char *cluster,
  1440. const char *description,IEspDFUFileDetail& FileDetails)
  1441. {
  1442. DBGLOG("CWsDfuEx::doGetFileDetails\n");
  1443. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(name, udesc);
  1444. if(!df)
  1445. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find file %s.",name);
  1446. StringArray clusters;
  1447. if (cluster && *cluster)
  1448. {
  1449. df->getClusterNames(clusters);
  1450. if(!FindInStringArray(clusters, cluster))
  1451. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find file %s.",name);
  1452. }
  1453. double version = context.getClientVersion();
  1454. offset_t size=queryDistributedFileSystem().getSize(df), recordSize=df->queryAttributes().getPropInt64("@recordSize",0);
  1455. CDateTime dt;
  1456. df->getModificationTime(dt);
  1457. const char* lname=df->queryLogicalName(), *fname=strrchr(lname,':');
  1458. FileDetails.setName(lname);
  1459. FileDetails.setFilename(fname ? fname+1 : lname);
  1460. FileDetails.setDir(df->queryDefaultDir());
  1461. FileDetails.setPathMask(df->queryPartMask());
  1462. StringBuffer strDesc = df->queryAttributes().queryProp("@description");
  1463. if (description)
  1464. {
  1465. DistributedFilePropertyLock lock(df);
  1466. lock.queryAttributes().setProp("@description",description);
  1467. strDesc = description;
  1468. }
  1469. FileDetails.setDescription(strDesc);
  1470. comma c1(size);
  1471. StringBuffer tmpstr;
  1472. tmpstr<<c1;
  1473. FileDetails.setFilesize(tmpstr.str());
  1474. if(df->isCompressed())
  1475. {
  1476. FileDetails.setZipFile(true);
  1477. }
  1478. comma c2(recordSize);
  1479. tmpstr.clear();
  1480. tmpstr<<c2;
  1481. FileDetails.setRecordSize(tmpstr.str());
  1482. tmpstr.clear();
  1483. if (df->queryAttributes().hasProp("@recordCount"))
  1484. {
  1485. comma c3(df->queryAttributes().getPropInt64("@recordCount"));
  1486. tmpstr<<c3;
  1487. }
  1488. else if (recordSize)
  1489. {
  1490. comma c3(size/recordSize);
  1491. tmpstr<<c3;
  1492. }
  1493. FileDetails.setRecordCount(tmpstr.str());
  1494. FileDetails.setOwner(df->queryAttributes().queryProp("@owner"));
  1495. FileDetails.setJobName(df->queryAttributes().queryProp("@job"));
  1496. //#14280
  1497. IDistributedSuperFile *sf = df->querySuperFile();
  1498. if(sf)
  1499. {
  1500. StringArray farray;
  1501. Owned<IDistributedFileIterator> iter=sf->getSubFileIterator();
  1502. ForEach(*iter)
  1503. {
  1504. StringBuffer subfileName;
  1505. iter->getName(subfileName);
  1506. farray.append(subfileName.str());
  1507. }
  1508. if(farray.length() > 0)
  1509. {
  1510. FileDetails.setSubfiles(farray);
  1511. }
  1512. FileDetails.setIsSuperfile(true);
  1513. return;
  1514. }
  1515. //#14280
  1516. FileDetails.setWuid(df->queryAttributes().queryProp("@workunit"));
  1517. //#17430
  1518. {
  1519. IArrayOf<IEspDFULogicalFile> LogicalFiles;
  1520. Owned<IDistributedSuperFileIterator> iter = df->getOwningSuperFiles();
  1521. if(iter.get() != NULL)
  1522. {
  1523. ForEach(*iter)
  1524. {
  1525. //printf("%s,%s\n",iter->query().queryLogicalName(),lname);
  1526. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  1527. File->setName(iter->queryName());
  1528. LogicalFiles.append(*File.getClear());
  1529. }
  1530. }
  1531. if(LogicalFiles.length() > 0)
  1532. {
  1533. FileDetails.setSuperfiles(LogicalFiles);
  1534. }
  1535. }
  1536. //#17430
  1537. //new (optional) attribute on a logical file (@persistent)
  1538. //indicates the ESP page that shows the details of a file. It indicates
  1539. //whether the file was created with a PERSIST() ecl attribute.
  1540. FileDetails.setPersistent(df->queryAttributes().queryProp("@persistent"));
  1541. //@format - what format the file is (if not fixed with)
  1542. FileDetails.setFormat(df->queryAttributes().queryProp("@format"));
  1543. //@maxRecordSize - what the maximum length of records is
  1544. FileDetails.setMaxRecordSize(df->queryAttributes().queryProp("@maxRecordSize"));
  1545. //@csvSeparate - separators between fields for a CSV/utf file
  1546. FileDetails.setCsvSeparate(df->queryAttributes().queryProp("@csvSeparate"));
  1547. //@csvQuote - character used to quote fields for a csv/utf file.
  1548. FileDetails.setCsvQuote(df->queryAttributes().queryProp("@csvQuote"));
  1549. //@csvTerminate - characters used to terminate a record in a csv.utf file
  1550. FileDetails.setCsvTerminate(df->queryAttributes().queryProp("@csvTerminate"));
  1551. //@csvEscape - character used to define escape for a csv/utf file.
  1552. if (version >= 1.20)
  1553. FileDetails.setCsvEscape(df->queryAttributes().queryProp("@csvEscape"));
  1554. //Time and date of the file
  1555. tmpstr.clear();
  1556. dt.getDateString(tmpstr);
  1557. tmpstr.append(" ");
  1558. dt.getTimeString(tmpstr);
  1559. FileDetails.setModified(tmpstr.str());
  1560. if(df->queryAttributes().hasProp("ECL"))
  1561. FileDetails.setEcl(df->queryAttributes().queryProp("ECL"));
  1562. StringBuffer clusterStr;
  1563. if ((!cluster || !*cluster) && clusters.ordinality())
  1564. {
  1565. clusterStr.append(clusters.item(0));
  1566. }
  1567. else if (cluster && *cluster)
  1568. {
  1569. clusterStr.append(cluster);
  1570. }
  1571. if (clusterStr.length() > 0)
  1572. {
  1573. FileDetails.setCluster(clusterStr.str());
  1574. if (!checkFileContent(context, udesc, name, clusterStr.str()))
  1575. FileDetails.setShowFileContent(false);
  1576. if (version > 1.05)
  1577. {
  1578. FileDetails.setFromRoxieCluster(false);
  1579. StringArray roxieClusterNames;
  1580. IArrayOf<IEspTpCluster> roxieclusters;
  1581. CTpWrapper dummy;
  1582. dummy.getClusterProcessList(eqRoxieCluster, roxieclusters);
  1583. ForEachItemIn(k, roxieclusters)
  1584. {
  1585. IEspTpCluster& r_cluster = roxieclusters.item(k);
  1586. StringBuffer sName = r_cluster.getName();
  1587. if (!stricmp(sName.str(), clusterStr.str()))
  1588. {
  1589. FileDetails.setFromRoxieCluster(true);
  1590. break;
  1591. }
  1592. }
  1593. }
  1594. }
  1595. IArrayOf<IConstDFUPart>& PartList = FileDetails.getDFUFileParts();
  1596. Owned<IDistributedFilePartIterator> pi = df->getIterator();
  1597. offset_t mn=LLC(0x7fffffffffffffff), mx=0, sum=0, count=0;
  1598. ForEach(*pi)
  1599. {
  1600. Owned<IDistributedFilePart> part = &pi->get();
  1601. for (unsigned int i=0; i<part->numCopies(); i++)
  1602. {
  1603. Owned<IEspDFUPart> FilePart = createDFUPart("","");
  1604. StringBuffer b;
  1605. part->queryNode(i)->endpoint().getUrlStr(b);
  1606. FilePart->setId(part->getPartIndex()+1);
  1607. FilePart->setCopy(i+1);
  1608. FilePart->setIp(b.str());
  1609. FilePart->setPartsize("<N/A>");
  1610. try
  1611. {
  1612. offset_t size=queryDistributedFileSystem().getSize(part);
  1613. comma c4(size);
  1614. tmpstr.clear();
  1615. tmpstr<<c4;
  1616. FilePart->setPartsize(tmpstr.str());
  1617. if(size!=-1)
  1618. {
  1619. count+=1;
  1620. sum+=size;
  1621. if(size>mx) mx=size;
  1622. if(size<mn) mn=size;
  1623. }
  1624. }
  1625. catch(IException *e)
  1626. {
  1627. StringBuffer msg;
  1628. ERRLOG("Exception %d:%s in WS_DFU queryDistributedFileSystem().getSize()", e->errorCode(), e->errorMessage(msg).str());
  1629. e->Release();
  1630. }
  1631. catch(...)
  1632. {
  1633. ERRLOG("Unknown exception in WS_DFU queryDistributedFileSystem().getSize()");
  1634. }
  1635. PartList.append(*FilePart.getClear());
  1636. }
  1637. }
  1638. if(count)
  1639. {
  1640. IEspDFUFileStat& Stat = FileDetails.updateStat();
  1641. offset_t avg=sum/count;
  1642. comma c5(avg-mn);
  1643. tmpstr.clear();
  1644. tmpstr<<c5;
  1645. Stat.setMinSkew(tmpstr.str());
  1646. comma c6(mx-avg);
  1647. tmpstr.clear();
  1648. tmpstr<<c6;
  1649. Stat.setMaxSkew(tmpstr.str());
  1650. }
  1651. if (version > 1.06)
  1652. {
  1653. const char *wuid = df->queryAttributes().queryProp("@workunit");
  1654. if (wuid && *wuid && (wuid[0]=='W'))
  1655. {
  1656. try
  1657. {
  1658. CWUWrapper wu(wuid, context);
  1659. StringArray graphs;
  1660. Owned<IPropertyTreeIterator> f=&wu->getFileIterator();
  1661. ForEach(*f)
  1662. {
  1663. IPropertyTree &query = f->query();
  1664. const char *fileName = query.queryProp("@name");
  1665. const char *graphName = query.queryProp("@graph");
  1666. if (!fileName || !graphName || !*graphName || stricmp(fileName, name))
  1667. continue;
  1668. graphs.append(graphName);
  1669. }
  1670. FileDetails.setGraphs(graphs);
  1671. }
  1672. catch(...)
  1673. {
  1674. DBGLOG("Failed in retrieving graphs from workunit %s", wuid);
  1675. }
  1676. }
  1677. }
  1678. if (version > 1.08 && udesc)
  1679. {
  1680. int permission;
  1681. if (getUserFilePermission(context, udesc, name, permission))
  1682. {
  1683. switch (permission)
  1684. {
  1685. case SecAccess_Full:
  1686. FileDetails.setUserPermission("Full Access Permission");
  1687. break;
  1688. case SecAccess_Write:
  1689. FileDetails.setUserPermission("Write Access Permission");
  1690. break;
  1691. case SecAccess_Read:
  1692. FileDetails.setUserPermission("Read Access Permission");
  1693. break;
  1694. case SecAccess_Access:
  1695. FileDetails.setUserPermission("Access Permission");
  1696. break;
  1697. case SecAccess_None:
  1698. FileDetails.setUserPermission("None Access Permission");
  1699. break;
  1700. default:
  1701. FileDetails.setUserPermission("Permission Unknown");
  1702. break;
  1703. }
  1704. }
  1705. }
  1706. }
  1707. void CWsDfuEx::getLogicalFileAndDirectory(IUserDescriptor* udesc, const char *dirname, IArrayOf<IEspDFULogicalFile>& LogicalFiles, int& numFiles, int& numDirs)
  1708. {
  1709. DBGLOG("CWsDfuEx::getLogicalFileAndDirectory\n");
  1710. StringArray roxieClusterNames;
  1711. IArrayOf<IEspTpCluster> roxieclusters;
  1712. CTpWrapper dummy;
  1713. dummy.getClusterProcessList(eqRoxieCluster, roxieclusters);
  1714. ForEachItemIn(k, roxieclusters)
  1715. {
  1716. IEspTpCluster& cluster = roxieclusters.item(k);
  1717. StringBuffer sName = cluster.getName();
  1718. roxieClusterNames.append(sName.str());
  1719. }
  1720. numFiles = 0;
  1721. numDirs = 0;
  1722. if (dirname && *dirname)
  1723. {
  1724. StringBuffer filter;
  1725. filter.append(dirname);
  1726. filter.append("::*");
  1727. Owned<IDFAttributesIterator> fi = queryDistributedFileDirectory().getDFAttributesIterator(filter.toLowerCase().str(), udesc, false,true, NULL);
  1728. if(fi)
  1729. {
  1730. StringBuffer size;
  1731. ForEach(*fi)
  1732. {
  1733. StringBuffer pref;
  1734. IPropertyTree &attr=fi->query();
  1735. const char* logicalName=attr.queryProp("@name");
  1736. const char *c=strstr(logicalName, "::");
  1737. if (c)
  1738. pref.append(c-logicalName, logicalName);
  1739. else
  1740. pref.append(logicalName);
  1741. const char* owner=attr.queryProp("@owner");
  1742. #if 0
  1743. char* clusterName=(char*)attr.queryProp("@group");
  1744. #else //Handling for multiple clusters
  1745. StringArray clusters;
  1746. if (getFileGroups(&attr,clusters)==0)
  1747. {
  1748. clusters.append("");
  1749. }
  1750. #endif
  1751. ForEachItemIn(i, clusters)
  1752. {
  1753. const char* clusterName = clusters.item(i);
  1754. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  1755. File->setPrefix(pref);
  1756. File->setClusterName(clusterName);
  1757. File->setName(logicalName);
  1758. File->setOwner(owner);
  1759. File->setReplicate(true);
  1760. ForEachItemIn(j, roxieClusterNames)
  1761. {
  1762. const char* roxieClusterName = roxieClusterNames.item(j);
  1763. if (roxieClusterName && clusterName && !stricmp(roxieClusterName, clusterName))
  1764. {
  1765. File->setFromRoxieCluster(true);
  1766. break;
  1767. }
  1768. }
  1769. if(!attr.hasProp("@numsubfiles"))
  1770. {
  1771. File->setDirectory(attr.queryProp("@directory"));
  1772. File->setParts(attr.queryProp("@numparts"));
  1773. File->setIsSuperfile(false);
  1774. }
  1775. else
  1776. {
  1777. File->setIsSuperfile(true);
  1778. }
  1779. int numSubFiles = attr.hasProp("@numsubfiles");
  1780. if (numSubFiles > 1) //Bug 41379 - ViewKeyFile Cannot handle superfile with multiple subfiles
  1781. File->setBrowseData(false);
  1782. else
  1783. File->setBrowseData(true);
  1784. StringBuffer modf(attr.queryProp("@modified"));
  1785. char* t= (char *) strchr(modf.str(),'T');
  1786. if(t) *t=' ';
  1787. File->setModified(modf.str());
  1788. __int64 recordSize=attr.getPropInt64("@recordSize",0), size=attr.getPropInt64("@size",-1);
  1789. if(!isCompressed(attr))
  1790. {
  1791. File->setIsZipfile(false);
  1792. }
  1793. else
  1794. {
  1795. File->setIsZipfile(true);
  1796. }
  1797. StringBuffer buf;
  1798. buf << comma(size);
  1799. File->setIntSize(size);
  1800. File->setTotalsize(buf.str());
  1801. if (attr.hasProp("@recordCount"))
  1802. {
  1803. File->setRecordCount((buf.clear()<<comma(attr.getPropInt64("@recordCount"))).str());
  1804. File->setIntRecordCount(attr.getPropInt64("@recordCount"));
  1805. }
  1806. else if(recordSize)
  1807. {
  1808. File->setRecordCount((buf.clear()<<comma(size/recordSize)).str());
  1809. File->setIntRecordCount(size/recordSize);
  1810. }
  1811. LogicalFiles.append(*File.getClear());
  1812. numFiles++;
  1813. }
  1814. }
  1815. }
  1816. }
  1817. Owned<IDFScopeIterator> iter = queryDistributedFileDirectory().getScopeIterator(udesc,dirname,false);
  1818. if(iter)
  1819. {
  1820. ForEach(*iter)
  1821. {
  1822. const char *scope = iter->query();
  1823. if (scope && *scope)
  1824. {
  1825. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  1826. File->setDirectory(scope);
  1827. File->setIsDirectory(true);
  1828. LogicalFiles.append(*File.getClear());
  1829. numDirs++;
  1830. }
  1831. }
  1832. }
  1833. }
  1834. bool CWsDfuEx::onDFUFileView(IEspContext &context, IEspDFUFileViewRequest &req, IEspDFUFileViewResponse &resp)
  1835. {
  1836. try
  1837. {
  1838. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  1839. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to Browse Files by Scope. Permission denied.");
  1840. Owned<IUserDescriptor> userdesc;
  1841. StringBuffer username;
  1842. context.getUserID(username);
  1843. DBGLOG("CWsDfuEx::onDFUFileView User=%s",username.str());
  1844. if(username.length() > 0)
  1845. {
  1846. const char* passwd = context.queryPassword();
  1847. userdesc.setown(createUserDescriptor());
  1848. userdesc->set(username.str(), passwd);
  1849. }
  1850. int numDirs = 0;
  1851. int numFiles = 0;
  1852. IArrayOf<IEspDFULogicalFile> logicalFiles;
  1853. getLogicalFileAndDirectory(userdesc.get(), req.getScope(), logicalFiles, numFiles, numDirs);
  1854. if (numFiles > 0)
  1855. resp.setNumFiles(numFiles);
  1856. if (req.getScope() && *req.getScope())
  1857. resp.setScope(req.getScope());
  1858. else
  1859. resp.setScope("");
  1860. resp.setDFULogicalFiles(logicalFiles);
  1861. }
  1862. catch(IException* e)
  1863. {
  1864. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1865. }
  1866. return true;
  1867. }
  1868. __int64 CWsDfuEx::findPositionBySize(const __int64 size, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1869. {
  1870. __int64 addToPos = -1;
  1871. ForEachItemIn(i, LogicalFiles)
  1872. {
  1873. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1874. const char* sSize = File.getLongSize();
  1875. __int64 nSize = atoi64_l(sSize,strlen(sSize));
  1876. if (descend && size > nSize)
  1877. {
  1878. addToPos = i;
  1879. break;
  1880. }
  1881. if (!descend && size < nSize)
  1882. {
  1883. addToPos = i;
  1884. break;
  1885. }
  1886. }
  1887. return addToPos;
  1888. }
  1889. __int64 CWsDfuEx::findPositionByParts(const __int64 parts, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1890. {
  1891. __int64 addToPos = -1;
  1892. ForEachItemIn(i, LogicalFiles)
  1893. {
  1894. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1895. const char* sParts = File.getParts();
  1896. __int64 nParts = atoi64_l(sParts,strlen(sParts));
  1897. if (descend && parts > nParts)
  1898. {
  1899. addToPos = i;
  1900. break;
  1901. }
  1902. if (!descend && parts < nParts)
  1903. {
  1904. addToPos = i;
  1905. break;
  1906. }
  1907. }
  1908. return addToPos;
  1909. }
  1910. __int64 CWsDfuEx::findPositionByRecords(const __int64 records, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1911. {
  1912. __int64 addToPos = -1;
  1913. ForEachItemIn(i, LogicalFiles)
  1914. {
  1915. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1916. const char* sRecords = File.getLongRecordCount();
  1917. __int64 nRecords = atoi64_l(sRecords,strlen(sRecords));
  1918. if (descend && records > nRecords)
  1919. {
  1920. addToPos = i;
  1921. break;
  1922. }
  1923. if (!descend && records < nRecords)
  1924. {
  1925. addToPos = i;
  1926. break;
  1927. }
  1928. }
  1929. return addToPos;
  1930. }
  1931. __int64 CWsDfuEx::findPositionByName(const char *name, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1932. {
  1933. if (!name || (strlen(name) < 1))
  1934. {
  1935. if (descend)
  1936. return -1;
  1937. else
  1938. return 0;
  1939. }
  1940. __int64 addToPos = -1;
  1941. ForEachItemIn(i, LogicalFiles)
  1942. {
  1943. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1944. const char *Name = File.getName();
  1945. if (!Name)
  1946. continue;
  1947. if (descend && strcmp(name, Name)>0)
  1948. {
  1949. addToPos = i;
  1950. break;
  1951. }
  1952. if (!descend && strcmp(name, Name)<0)
  1953. {
  1954. addToPos = i;
  1955. break;
  1956. }
  1957. }
  1958. return addToPos;
  1959. }
  1960. __int64 CWsDfuEx::findPositionByCluster(const char *cluster, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1961. {
  1962. if (!cluster || (strlen(cluster) < 1))
  1963. {
  1964. if (descend)
  1965. return -1;
  1966. else
  1967. return 0;
  1968. }
  1969. __int64 addToPos = -1;
  1970. ForEachItemIn(i, LogicalFiles)
  1971. {
  1972. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1973. const char *ClusterName = File.getClusterName();
  1974. if (!ClusterName)
  1975. continue;
  1976. if (descend && strcmp(cluster, ClusterName)>0)
  1977. {
  1978. addToPos = i;
  1979. break;
  1980. }
  1981. if (!descend && strcmp(cluster, ClusterName)<0)
  1982. {
  1983. addToPos = i;
  1984. break;
  1985. }
  1986. }
  1987. return addToPos;
  1988. }
  1989. __int64 CWsDfuEx::findPositionByOwner(const char *owner, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1990. {
  1991. if (!owner || (strlen(owner) < 1))
  1992. {
  1993. if (descend)
  1994. return -1;
  1995. else
  1996. return 0;
  1997. }
  1998. __int64 addToPos = -1;
  1999. ForEachItemIn(i, LogicalFiles)
  2000. {
  2001. IEspDFULogicalFile& File = LogicalFiles.item(i);
  2002. const char *Owner = File.getOwner();
  2003. if (!Owner)
  2004. continue;
  2005. if (descend && strcmp(owner, Owner)>0)
  2006. {
  2007. addToPos = i;
  2008. break;
  2009. }
  2010. if (!descend && strcmp(owner, Owner)<0)
  2011. {
  2012. addToPos = i;
  2013. break;
  2014. }
  2015. }
  2016. return addToPos;
  2017. }
  2018. __int64 CWsDfuEx::findPositionByDate(const char *datetime, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  2019. {
  2020. if (!datetime || (strlen(datetime) < 1))
  2021. {
  2022. if (descend)
  2023. return -1;
  2024. else
  2025. return 0;
  2026. }
  2027. __int64 addToPos = -1;
  2028. ForEachItemIn(i, LogicalFiles)
  2029. {
  2030. IEspDFULogicalFile& File = LogicalFiles.item(i);
  2031. const char *modDate = File.getModified();
  2032. if (!modDate)
  2033. continue;
  2034. if (descend && strcmp(datetime, modDate)>0)
  2035. {
  2036. addToPos = i;
  2037. break;
  2038. }
  2039. if (!descend && strcmp(datetime, modDate)<0)
  2040. {
  2041. addToPos = i;
  2042. break;
  2043. }
  2044. }
  2045. return addToPos;
  2046. }
  2047. __int64 CWsDfuEx::findPositionByDescription(const char *description, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  2048. {
  2049. if (!description || (strlen(description) < 1))
  2050. {
  2051. if (descend)
  2052. return -1;
  2053. else
  2054. return 0;
  2055. }
  2056. __int64 addToPos = -1;
  2057. ForEachItemIn(i, LogicalFiles)
  2058. {
  2059. IEspDFULogicalFile& File = LogicalFiles.item(i);
  2060. const char *Description = File.getDescription();
  2061. if (!Description)
  2062. continue;
  2063. if (descend && strcmp(description, Description)>0)
  2064. {
  2065. addToPos = i;
  2066. break;
  2067. }
  2068. if (!descend && strcmp(description, Description)<0)
  2069. {
  2070. addToPos = i;
  2071. break;
  2072. }
  2073. }
  2074. return addToPos;
  2075. }
  2076. bool CWsDfuEx::checkDescription(const char *description, const char *descriptionFilter)
  2077. {
  2078. if (!descriptionFilter || (descriptionFilter[0] == 0))
  2079. return true;
  2080. if (!description || (description[0] == 0))
  2081. return false;
  2082. int len = strlen(descriptionFilter);
  2083. int filterType = 0;
  2084. if (descriptionFilter[0] == '*')
  2085. filterType += 1;
  2086. if (descriptionFilter[len - 1] == '*')
  2087. filterType += 2;
  2088. char descFilter[256];
  2089. if (filterType < 1)
  2090. strcpy(descFilter, descriptionFilter);
  2091. else if (filterType < 2)
  2092. strcpy(descFilter, descriptionFilter+1);
  2093. else if (filterType < 3)
  2094. {
  2095. strncpy(descFilter, descriptionFilter, len - 1);
  2096. descFilter[len -2] = 0;
  2097. }
  2098. else
  2099. {
  2100. strncpy(descFilter, descriptionFilter+1, len - 2);
  2101. descFilter[len -3] = 0;
  2102. }
  2103. const char *pos = strstr(description, descFilter);
  2104. if (!pos)
  2105. return false;
  2106. if ((pos != description) && (descriptionFilter[0] != '*'))
  2107. return false;
  2108. if ((pos + strlen(descFilter) != description + strlen(description)) && (descriptionFilter[len - 1] != '*'))
  2109. return false;
  2110. return true;
  2111. }
  2112. bool CWsDfuEx::doLogicalFileSearch(IEspContext &context, IUserDescriptor* udesc, IEspDFUQueryRequest & req, IEspDFUQueryResponse & resp)
  2113. {
  2114. DBGLOG("CWsDfuEx::doLogicalFileSearch\n");
  2115. double version = context.getClientVersion();
  2116. IArrayOf<IEspDFULogicalFile> LogicalFiles;
  2117. if (req.getOneLevelDirFileReturn())
  2118. {
  2119. int numDirs = 0;
  2120. int numFiles = 0;
  2121. getLogicalFileAndDirectory(udesc, req.getLogicalName(), LogicalFiles, numFiles, numDirs);
  2122. }
  2123. else
  2124. {
  2125. StringBuffer filter;
  2126. const char* fname = req.getLogicalName();
  2127. if(fname && *fname)
  2128. {
  2129. filter.append(fname);
  2130. }
  2131. else
  2132. {
  2133. if(req.getPrefix() && *req.getPrefix())
  2134. {
  2135. filter.append(req.getPrefix());
  2136. filter.append("::");
  2137. }
  2138. filter.append("*");
  2139. }
  2140. Owned<IDFAttributesIterator> fi;
  2141. bool bNotInSuperfile = false;
  2142. const char* sFileType = req.getFileType();
  2143. if (sFileType && !stricmp(sFileType, "Not in Superfiles"))
  2144. {
  2145. bNotInSuperfile = true;
  2146. }
  2147. if (bNotInSuperfile)
  2148. {
  2149. fi.setown(createSubFileFilter(
  2150. queryDistributedFileDirectory().getDFAttributesIterator(filter.toLowerCase().str(),udesc,true,true, NULL),udesc,false)); // NB wrapper owns wrapped iterator
  2151. }
  2152. else
  2153. {
  2154. fi.setown(queryDistributedFileDirectory().getDFAttributesIterator(filter.toLowerCase().str(), udesc,true,true, NULL));
  2155. }
  2156. if(!fi)
  2157. throw MakeStringException(ECLWATCH_CANNOT_GET_FILE_ITERATOR,"Cannot get information from file system.");
  2158. StringBuffer wuFrom, wuTo;
  2159. if(req.getStartDate() && *req.getStartDate())
  2160. {
  2161. CDateTime wuTime;
  2162. wuTime.setString(req.getStartDate(),NULL,true);
  2163. unsigned year, month, day, hour, minute, second, nano;
  2164. wuTime.getDate(year, month, day, true);
  2165. wuTime.getTime(hour, minute, second, nano, true);
  2166. wuFrom.appendf("%4d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second);
  2167. }
  2168. if(req.getEndDate() && *req.getEndDate())
  2169. {
  2170. CDateTime wuTime;
  2171. wuTime.setString(req.getEndDate(),NULL,true);
  2172. unsigned year, month, day, hour, minute, second, nano;
  2173. wuTime.getDate(year, month, day, true);
  2174. wuTime.getTime(hour, minute, second, nano, true);
  2175. wuTo.appendf("%4d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second);
  2176. }
  2177. char sortBy[256];
  2178. if(req.getSortby() && *req.getSortby())
  2179. {
  2180. strcpy(sortBy, req.getSortby());
  2181. }
  2182. unsigned pagesize = req.getPageSize();
  2183. if (pagesize < 1)
  2184. {
  2185. pagesize = 100;
  2186. }
  2187. //DBGLOG("pagesize=%d\n", pagesize);
  2188. __int64 displayStartReq = 1;
  2189. if (req.getPageStartFrom() > 0)
  2190. displayStartReq = req.getPageStartFrom();
  2191. __int64 displayStart = displayStartReq - 1;
  2192. __int64 displayEnd = displayStart + pagesize;
  2193. bool descending = req.getDescending();
  2194. const int nFirstN = req.getFirstN();
  2195. const char* sFirstNType = req.getFirstNType();
  2196. const __int64 nFileSizeFrom = req.getFileSizeFrom();
  2197. const __int64 nFileSizeTo = req.getFileSizeTo();
  2198. if (nFirstN > 0)
  2199. {
  2200. displayStart = 0;
  2201. displayEnd = nFirstN;
  2202. if (!stricmp(sFirstNType, "newest"))
  2203. {
  2204. strcpy(sortBy, "Modified");
  2205. descending = true;
  2206. }
  2207. else if (!stricmp(sFirstNType, "oldest"))
  2208. {
  2209. strcpy(sortBy, "Modified");
  2210. descending = false;
  2211. }
  2212. else if (!stricmp(sFirstNType, "largest"))
  2213. {
  2214. strcpy(sortBy, "Size");
  2215. descending = true;
  2216. }
  2217. else if (!stricmp(sFirstNType, "smallest"))
  2218. {
  2219. strcpy(sortBy, "Size");
  2220. descending = false;
  2221. }
  2222. pagesize = nFirstN;
  2223. }
  2224. StringArray roxieClusterNames;
  2225. IArrayOf<IEspTpCluster> roxieclusters;
  2226. CTpWrapper dummy;
  2227. dummy.getClusterProcessList(eqRoxieCluster, roxieclusters);
  2228. ForEachItemIn(k, roxieclusters)
  2229. {
  2230. IEspTpCluster& cluster = roxieclusters.item(k);
  2231. StringBuffer sName = cluster.getName();
  2232. roxieClusterNames.append(sName.str());
  2233. }
  2234. StringArray clustersReq;
  2235. const char* clustersReq0 = req.getClusterName();
  2236. if (clustersReq0 && *clustersReq0)
  2237. {
  2238. char* pStr = (char*) clustersReq0;
  2239. while (pStr)
  2240. {
  2241. char clusterName[256];
  2242. char* ppStr = strchr(pStr, ',');
  2243. if (!ppStr)
  2244. {
  2245. strcpy(clusterName, pStr);
  2246. pStr = NULL;
  2247. }
  2248. else
  2249. {
  2250. strncpy(clusterName, pStr, ppStr - pStr );
  2251. clusterName[ppStr - pStr] = 0;
  2252. pStr = ppStr+1;
  2253. }
  2254. clustersReq.append(clusterName);
  2255. }
  2256. }
  2257. StringBuffer size;
  2258. __int64 totalFiles = 0;
  2259. IArrayOf<IEspDFULogicalFile> LogicalFileList;
  2260. ForEach(*fi)
  2261. {
  2262. IPropertyTree &attr=fi->query();
  2263. const char* logicalName=attr.queryProp("@name");
  2264. if (!logicalName || (logicalName[0] == 0))
  2265. continue;
  2266. StringBuffer pref;
  2267. const char *c=strstr(logicalName, "::");
  2268. if (c)
  2269. pref.append(c-logicalName, logicalName);
  2270. else
  2271. pref.append(logicalName);
  2272. const char* owner=attr.queryProp("@owner");
  2273. if (req.getOwner() && *req.getOwner()!=0)
  2274. {
  2275. if (!owner || stricmp(owner, req.getOwner()))
  2276. continue;
  2277. }
  2278. #if 0
  2279. char* clusterName = (char*)attr.queryProp("@group"); // ** TBD - Handling for multiple clusters?
  2280. if (clusterName)
  2281. {//special process for roxie cluster names
  2282. unsigned len = strlen(clusterName);
  2283. if (len > 8)
  2284. {
  2285. char *pName = clusterName + len - 8;
  2286. if (!stricmp(pName, "__slaves"))
  2287. {
  2288. pName[0] = 0;//we did not specify slaves when copy/spray the file
  2289. }
  2290. }
  2291. }
  2292. if (req.getClusterName() && *req.getClusterName()!=0)
  2293. {
  2294. if (!clusterName || stricmp(clusterName, req.getClusterName()))
  2295. continue;
  2296. }
  2297. #else
  2298. StringArray clusters;
  2299. StringArray clusters1;
  2300. if (getFileGroups(&attr,clusters1)==0)
  2301. {
  2302. if (clustersReq.length() < 1)
  2303. {
  2304. clusters.append("");
  2305. }
  2306. }
  2307. else
  2308. {
  2309. // check specified cluster name in list
  2310. if (clustersReq.length() > 0)
  2311. {
  2312. ForEachItemIn(ii,clustersReq)
  2313. {
  2314. StringBuffer clusterFound;
  2315. const char * cluster0 = clustersReq.item(ii);
  2316. ForEachItemIn(i,clusters1)
  2317. {
  2318. if (!stricmp(clusters1.item(i), cluster0))
  2319. {
  2320. clusterFound.append(cluster0);
  2321. break;
  2322. }
  2323. }
  2324. if (clusterFound.length() > 0)
  2325. clusters.append(clusterFound);
  2326. }
  2327. }
  2328. else
  2329. {
  2330. if (clusters1.length() > 0)
  2331. {
  2332. ForEachItemIn(i,clusters1)
  2333. {
  2334. const char * cluster0 = clusters1.item(i);
  2335. clusters.append(cluster0);
  2336. }
  2337. }
  2338. }
  2339. }
  2340. #endif
  2341. const char* desc = attr.queryProp("@description");
  2342. if(req.getDescription() && *req.getDescription())
  2343. {
  2344. if (!checkDescription(desc, req.getDescription()))
  2345. continue;
  2346. }
  2347. if (sFileType && *sFileType)
  2348. {
  2349. bool bHasSubFiles = attr.hasProp("@numsubfiles");
  2350. if (bHasSubFiles && (bNotInSuperfile || !stricmp(sFileType, "Logical Files Only")))
  2351. continue;
  2352. else if (!bHasSubFiles && !stricmp(sFileType, "Superfiles Only"))
  2353. continue;
  2354. }
  2355. __int64 recordSize=attr.getPropInt64("@recordSize",0), size=attr.getPropInt64("@size",-1);
  2356. if (nFileSizeFrom > 0 && size < nFileSizeFrom)
  2357. continue;
  2358. if (nFileSizeTo > 0 && size > nFileSizeTo)
  2359. continue;
  2360. StringBuffer modf(attr.queryProp("@modified"));
  2361. char* t=(char *) strchr(modf.str(),'T');
  2362. if(t) *t=' ';
  2363. if (wuFrom.length() && strcmp(modf.str(),wuFrom.str())<0)
  2364. continue;
  2365. if (wuTo.length() && strcmp(modf.str(),wuTo.str())>0)
  2366. continue;
  2367. __int64 parts = 0;
  2368. if(!attr.hasProp("@numsubfiles"))
  2369. parts = attr.getPropInt64("@numparts");
  2370. __int64 records = 0;
  2371. if (attr.hasProp("@recordCount"))
  2372. records = attr.getPropInt64("@recordCount");
  2373. else if(recordSize)
  2374. records = size/recordSize;
  2375. char description[DESCRIPTION_DISPLAY_LENGTH + 1];
  2376. description[0] = 0;
  2377. if (desc && *desc)
  2378. {
  2379. if (strlen(desc) <= DESCRIPTION_DISPLAY_LENGTH) //Only 12 characters is required for display
  2380. {
  2381. strcpy(description, desc);
  2382. }
  2383. else
  2384. {
  2385. strncpy(description, desc, DESCRIPTION_DISPLAY_LENGTH - 3);
  2386. description[DESCRIPTION_DISPLAY_LENGTH - 3] = 0;
  2387. strcat(description, "...");
  2388. }
  2389. }
  2390. ForEachItemIn(i, clusters)
  2391. {
  2392. const char* clusterName = clusters.item(i);
  2393. __int64 addToPos = -1; //Add to tail
  2394. if (stricmp(sortBy, "Size")==0)
  2395. {
  2396. addToPos = findPositionBySize(size, descending, LogicalFileList);
  2397. }
  2398. else if (stricmp(sortBy, "Parts")==0)
  2399. {
  2400. addToPos = findPositionByParts(parts, descending, LogicalFileList);
  2401. }
  2402. else if (stricmp(sortBy, "Owner")==0)
  2403. {
  2404. addToPos = findPositionByOwner(owner, descending, LogicalFileList);
  2405. }
  2406. else if (stricmp(sortBy, "Cluster")==0)
  2407. {
  2408. addToPos = findPositionByCluster(clusterName, descending, LogicalFileList);
  2409. }
  2410. else if (stricmp(sortBy, "Records")==0)
  2411. {
  2412. addToPos = findPositionByRecords(records, descending, LogicalFileList);
  2413. }
  2414. else if (stricmp(sortBy, "Modified")==0)
  2415. {
  2416. addToPos = findPositionByDate(modf.str(), descending, LogicalFileList);
  2417. }
  2418. else if (stricmp(sortBy, "Description")==0)
  2419. {
  2420. addToPos = findPositionByDescription(description, descending, LogicalFileList);
  2421. }
  2422. else
  2423. {
  2424. addToPos = findPositionByName(logicalName, descending, LogicalFileList);
  2425. }
  2426. totalFiles++;
  2427. if (addToPos < 0 && (totalFiles > displayEnd))
  2428. continue;
  2429. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  2430. File->setPrefix(pref);
  2431. File->setClusterName(clusterName);
  2432. File->setName(logicalName);
  2433. File->setOwner(owner);
  2434. File->setDescription(description);
  2435. File->setModified(modf.str());
  2436. File->setReplicate(true);
  2437. ForEachItemIn(j, roxieClusterNames)
  2438. {
  2439. const char* roxieClusterName = roxieClusterNames.item(j);
  2440. if (roxieClusterName && clusterName && !stricmp(roxieClusterName, clusterName))
  2441. {
  2442. File->setFromRoxieCluster(true);
  2443. break;
  2444. }
  2445. }
  2446. bool bSuperfile = false;
  2447. int numSubFiles = attr.hasProp("@numsubfiles");
  2448. if(!numSubFiles)
  2449. {
  2450. File->setDirectory(attr.queryProp("@directory"));
  2451. File->setParts(attr.queryProp("@numparts"));
  2452. }
  2453. else
  2454. {
  2455. bSuperfile = true;
  2456. }
  2457. File->setIsSuperfile(bSuperfile);
  2458. if(!isCompressed(attr))
  2459. {
  2460. File->setIsZipfile(false);
  2461. }
  2462. else
  2463. {
  2464. File->setIsZipfile(true);
  2465. }
  2466. //File->setBrowseData(bKeyFile); //Bug: 39750 - All files should be viewable through ViewKeyFile function
  2467. if (numSubFiles > 1) //Bug 41379 - ViewKeyFile Cannot handle superfile with multiple subfiles
  2468. File->setBrowseData(false);
  2469. else
  2470. File->setBrowseData(true);
  2471. if (version > 1.13)
  2472. {
  2473. bool bKeyFile = false;
  2474. const char * kind = attr.queryProp("@kind");
  2475. if (kind && (stricmp(kind, "key") == 0))
  2476. {
  2477. bKeyFile = true;
  2478. }
  2479. File->setIsKeyFile(bKeyFile);
  2480. }
  2481. StringBuffer buf;
  2482. buf << comma(size);
  2483. File->setTotalsize(buf.str());
  2484. char temp[64];
  2485. numtostr(temp, size);
  2486. File->setLongSize(temp);
  2487. numtostr(temp, records);
  2488. File->setLongRecordCount(temp);
  2489. if (records > 0)
  2490. File->setRecordCount((buf.clear()<<comma(records)).str());
  2491. if (addToPos < 0)
  2492. LogicalFileList.append(*File.getClear());
  2493. else
  2494. LogicalFileList.add(*File.getClear(), (int) addToPos);
  2495. if (LogicalFileList.length() > displayEnd)
  2496. LogicalFileList.pop();
  2497. }
  2498. }
  2499. if (displayEnd > LogicalFileList.length())
  2500. displayEnd = LogicalFileList.length();
  2501. for (int i = (int) displayStart; i < (int) displayEnd; i++)
  2502. {
  2503. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  2504. IEspDFULogicalFile& File0 = LogicalFileList.item(i);
  2505. File->copy(File0);
  2506. LogicalFiles.append(*File.getClear());
  2507. }
  2508. resp.setNumFiles(totalFiles);
  2509. resp.setPageSize(pagesize);
  2510. resp.setPageStartFrom(displayStart+1);
  2511. resp.setPageEndAt(displayEnd);
  2512. if (displayStart - pagesize > 0)
  2513. resp.setPrevPageFrom(displayStart - pagesize + 1);
  2514. else if(displayStart > 0)
  2515. resp.setPrevPageFrom(1);
  2516. if(displayEnd < totalFiles)
  2517. {
  2518. resp.setNextPageFrom(displayEnd+1);
  2519. resp.setLastPageFrom((int)(pagesize * floor((double) ((totalFiles-1) / pagesize)) + 1));
  2520. }
  2521. StringBuffer basicQuery;
  2522. if (req.getClusterName() && *req.getClusterName())
  2523. {
  2524. resp.setClusterName(req.getClusterName());
  2525. addToQueryString(basicQuery, "ClusterName", req.getClusterName());
  2526. }
  2527. if (req.getOwner() && *req.getOwner())
  2528. {
  2529. resp.setOwner(req.getOwner());
  2530. addToQueryString(basicQuery, "Owner", req.getOwner());
  2531. }
  2532. if (req.getPrefix() && *req.getPrefix())
  2533. {
  2534. resp.setPrefix(req.getPrefix());
  2535. addToQueryString(basicQuery, "Prefix", req.getPrefix());
  2536. }
  2537. if (req.getLogicalName() && *req.getLogicalName())
  2538. {
  2539. resp.setLogicalName(req.getLogicalName());
  2540. addToQueryString(basicQuery, "LogicalName", req.getLogicalName());
  2541. }
  2542. if (req.getDescription() && *req.getDescription())
  2543. {
  2544. resp.setDescription(req.getDescription());
  2545. addToQueryString(basicQuery, "Description", req.getDescription());
  2546. }
  2547. if (req.getStartDate() && *req.getStartDate())
  2548. {
  2549. resp.setStartDate(req.getStartDate());
  2550. addToQueryString(basicQuery, "StartDate", req.getStartDate());
  2551. }
  2552. if (req.getEndDate() && *req.getEndDate())
  2553. {
  2554. resp.setEndDate(req.getEndDate());
  2555. addToQueryString(basicQuery, "EndDate", req.getEndDate());
  2556. }
  2557. if (req.getFileType() && *req.getFileType())
  2558. {
  2559. resp.setFileType(req.getFileType());
  2560. addToQueryString(basicQuery, "FileType", req.getFileType());
  2561. }
  2562. if (req.getFileSizeFrom())
  2563. {
  2564. resp.setFileSizeFrom(req.getFileSizeFrom());
  2565. addToQueryStringFromInt(basicQuery, "FileSizeFrom", req.getFileSizeFrom());
  2566. }
  2567. if (req.getFileSizeTo())
  2568. {
  2569. resp.setFileSizeTo(req.getFileSizeTo());
  2570. addToQueryStringFromInt(basicQuery, "FileSizeTo", req.getFileSizeTo());
  2571. }
  2572. StringBuffer ParametersForFilters = basicQuery;
  2573. StringBuffer ParametersForPaging = basicQuery;
  2574. addToQueryStringFromInt(ParametersForFilters, "PageSize",pagesize);
  2575. addToQueryStringFromInt(ParametersForPaging, "PageSize", pagesize);
  2576. if (ParametersForFilters.length() > 0)
  2577. resp.setFilters(ParametersForFilters.str());
  2578. sortBy[0] = 0;
  2579. descending = false;
  2580. if ((req.getFirstN() > 0) && req.getFirstNType() && *req.getFirstNType())
  2581. {
  2582. const char *sFirstNType = req.getFirstNType();
  2583. if (!stricmp(sFirstNType, "newest"))
  2584. {
  2585. strcpy(sortBy, "Modified");
  2586. descending = true;
  2587. }
  2588. else if (!stricmp(sFirstNType, "oldest"))
  2589. {
  2590. strcpy(sortBy, "Modified");
  2591. descending = false;
  2592. }
  2593. else if (!stricmp(sFirstNType, "largest"))
  2594. {
  2595. strcpy(sortBy, "Size");
  2596. descending = true;
  2597. }
  2598. else if (!stricmp(sFirstNType, "smallest"))
  2599. {
  2600. strcpy(sortBy, "Size");
  2601. descending = false;
  2602. }
  2603. }
  2604. else if (req.getSortby() && *req.getSortby())
  2605. {
  2606. strcpy(sortBy, req.getSortby());
  2607. if (req.getDescending())
  2608. descending = req.getDescending();
  2609. }
  2610. if (sortBy && *sortBy)
  2611. {
  2612. resp.setSortby(sortBy);
  2613. resp.setDescending(descending);
  2614. StringBuffer strbuf = sortBy;
  2615. strbuf.append("=");
  2616. String str1(strbuf.str());
  2617. String str(basicQuery.str());
  2618. if (str.indexOf(str1) < 0)
  2619. {
  2620. addToQueryString(ParametersForPaging, "Sortby", sortBy);
  2621. addToQueryString(basicQuery, "Sortby", sortBy);
  2622. if (descending)
  2623. {
  2624. addToQueryString(ParametersForPaging, "Descending", "1");
  2625. addToQueryString(basicQuery, "Descending", "1");
  2626. }
  2627. }
  2628. }
  2629. if (basicQuery.length() > 0)
  2630. resp.setBasicQuery(basicQuery.str());
  2631. if (ParametersForPaging.length() > 0)
  2632. resp.setParametersForPaging(ParametersForPaging.str());
  2633. //DBGLOG("basicQuery=%s\n", basicQuery);
  2634. }
  2635. resp.setDFULogicalFiles(LogicalFiles);
  2636. return true;
  2637. }
  2638. bool CWsDfuEx::onSuperfileList(IEspContext &context, IEspSuperfileListRequest &req, IEspSuperfileListResponse &resp)
  2639. {
  2640. try
  2641. {
  2642. StringBuffer username;
  2643. context.getUserID(username);
  2644. Owned<IUserDescriptor> userdesc;
  2645. if(username.length() > 0)
  2646. {
  2647. const char* passwd = context.queryPassword();
  2648. userdesc.setown(createUserDescriptor());
  2649. userdesc->set(username.str(), passwd);
  2650. }
  2651. Owned<IDFUhelper> dfuhelper = createIDFUhelper();
  2652. StringArray farray;
  2653. StringAttrArray subfiles;
  2654. dfuhelper->listSubFiles(req.getSuperfile(), subfiles, userdesc.get());
  2655. for(unsigned i = 0; i < subfiles.length(); i++)
  2656. {
  2657. StringAttrItem& subfile = subfiles.item(i);
  2658. farray.append(subfile.text);
  2659. }
  2660. if(farray.length() > 0)
  2661. resp.setSubfiles(farray);
  2662. resp.setSuperfile(req.getSuperfile());
  2663. }
  2664. catch(IException* e)
  2665. {
  2666. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2667. }
  2668. return true;
  2669. }
  2670. bool CWsDfuEx::onSuperfileAction(IEspContext &context, IEspSuperfileActionRequest &req, IEspSuperfileActionResponse &resp)
  2671. {
  2672. try
  2673. {
  2674. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Write, false))
  2675. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to Superfile action. Permission denied.");
  2676. const char* action = req.getAction();
  2677. const char* superfile = req.getSuperfile();
  2678. superfileAction(context, action, superfile, req.getSubfiles(), req.getBefore(), true, true, req.getDelete(), req.getRemoveSuperfile());
  2679. resp.setRetcode(0);
  2680. if (superfile && *superfile && action && strieq(action, "remove"))
  2681. {
  2682. Owned<IUserDescriptor> udesc;
  2683. udesc.setown(createUserDescriptor());
  2684. {
  2685. StringBuffer userID;
  2686. StringBuffer pw;
  2687. context.getUserID(userID);
  2688. context.getPassword(pw);
  2689. udesc->set(userID.str(), pw.str());
  2690. }
  2691. Owned<IDistributedSuperFile> fp = queryDistributedFileDirectory().lookupSuperFile(superfile,udesc);
  2692. if (!fp)
  2693. resp.setRetcode(-1); //Superfile has been removed.
  2694. }
  2695. resp.setSuperfile(req.getSuperfile());
  2696. }
  2697. catch(IException* e)
  2698. {
  2699. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2700. }
  2701. return true;
  2702. }
  2703. bool CWsDfuEx::onSavexml(IEspContext &context, IEspSavexmlRequest &req, IEspSavexmlResponse &resp)
  2704. {
  2705. try
  2706. {
  2707. StringBuffer username;
  2708. context.getUserID(username);
  2709. Owned<IUserDescriptor> userdesc;
  2710. if(username.length() > 0)
  2711. {
  2712. const char* passwd = context.queryPassword();
  2713. userdesc.setown(createUserDescriptor());
  2714. userdesc->set(username.str(), passwd);
  2715. }
  2716. Owned<IDFUhelper> dfuhelper = createIDFUhelper();
  2717. StringBuffer out;
  2718. dfuhelper->getFileXML(req.getName(), out, userdesc.get());
  2719. MemoryBuffer xmlmap;
  2720. int len = out.length();
  2721. xmlmap.setBuffer(len, out.detach(), true);
  2722. resp.setXmlmap(xmlmap);
  2723. }
  2724. catch(IException* e)
  2725. {
  2726. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2727. }
  2728. return true;
  2729. }
  2730. bool CWsDfuEx::onAdd(IEspContext &context, IEspAddRequest &req, IEspAddResponse &resp)
  2731. {
  2732. try
  2733. {
  2734. StringBuffer username;
  2735. context.getUserID(username);
  2736. Owned<IUserDescriptor> userdesc;
  2737. if(username.length() > 0)
  2738. {
  2739. const char* passwd = context.queryPassword();
  2740. userdesc.setown(createUserDescriptor());
  2741. userdesc->set(username.str(), passwd);
  2742. }
  2743. Owned<IDFUhelper> dfuhelper = createIDFUhelper();
  2744. StringBuffer xmlstr(req.getXmlmap().length(),(const char*)req.getXmlmap().bufferBase());
  2745. dfuhelper->addFileXML(req.getDstname(), xmlstr, userdesc.get());
  2746. }
  2747. catch(IException* e)
  2748. {
  2749. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2750. }
  2751. return true;
  2752. }
  2753. bool CWsDfuEx::onAddRemote(IEspContext &context, IEspAddRemoteRequest &req, IEspAddRemoteResponse &resp)
  2754. {
  2755. try
  2756. {
  2757. StringBuffer username;
  2758. context.getUserID(username);
  2759. Owned<IUserDescriptor> userdesc;
  2760. if(username.length() > 0)
  2761. {
  2762. const char* passwd = context.queryPassword();
  2763. userdesc.setown(createUserDescriptor());
  2764. userdesc->set(username.str(), passwd);
  2765. }
  2766. const char* srcusername = req.getSrcusername();
  2767. Owned<IUserDescriptor> srcuserdesc;
  2768. if(srcusername && *srcusername)
  2769. {
  2770. const char* srcpasswd = req.getSrcpassword();
  2771. srcuserdesc.setown(createUserDescriptor());
  2772. srcuserdesc->set(srcusername, srcpasswd);
  2773. }
  2774. const char* srcname = req.getSrcname();
  2775. if(srcname == NULL || *srcname == '\0')
  2776. throw MakeStringException(ECLWATCH_INVALID_INPUT, "srcname can't be empty.");
  2777. const char* srcdali = req.getSrcdali();
  2778. if(srcdali == NULL || *srcdali == '\0')
  2779. throw MakeStringException(ECLWATCH_INVALID_INPUT, "srcdali can't be empty.");
  2780. const char* dstname = req.getDstname();
  2781. if(dstname == NULL || *dstname == '\0')
  2782. throw MakeStringException(ECLWATCH_INVALID_INPUT, "dstname can't be empty.");
  2783. SocketEndpoint ep(srcdali);
  2784. Owned<IDFUhelper> dfuhelper = createIDFUhelper();
  2785. dfuhelper->addFileRemote(dstname, ep, srcname, srcuserdesc.get(), userdesc.get());
  2786. }
  2787. catch(IException* e)
  2788. {
  2789. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2790. }
  2791. return true;
  2792. }
  2793. const int INTEGELSIZE = 20;
  2794. const int REALSIZE = 32;
  2795. const int STRINGSIZE = 128;
  2796. bool CWsDfuEx::onDFUGetDataColumns(IEspContext &context, IEspDFUGetDataColumnsRequest &req, IEspDFUGetDataColumnsResponse &resp)
  2797. {
  2798. try
  2799. {
  2800. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  2801. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to View Data File. Permission denied.");
  2802. StringBuffer logicalNameStr;
  2803. char* logicalName0 = (char*) req.getOpenLogicalName();
  2804. if (logicalName0 && *logicalName0)
  2805. {
  2806. logicalNameStr.append(logicalName0);
  2807. logicalNameStr.trim();
  2808. }
  2809. if (logicalNameStr.length() > 0)
  2810. {
  2811. __int64 startIndex = req.getStartIndex();
  2812. __int64 endIndex = req.getEndIndex();
  2813. if (startIndex < 1)
  2814. startIndex = 1;
  2815. if (endIndex < 1)
  2816. endIndex = 100;
  2817. StringArray filterByNames, filterByValues;
  2818. double version = context.getClientVersion();
  2819. if (version > 1.04)
  2820. {
  2821. const char* filterBy = req.getFilterBy();
  2822. if (filterBy && *filterBy)
  2823. {
  2824. parseTwoStringArrays(filterBy, filterByNames, filterByValues);
  2825. }
  2826. const char* showColumns = req.getShowColumns();
  2827. if (showColumns && *showColumns)
  2828. {
  2829. resp.setShowColumns(showColumns);
  2830. }
  2831. }
  2832. StringBuffer username;
  2833. context.getUserID(username);
  2834. const char* passwd = context.queryPassword();
  2835. Owned<IUserDescriptor> userdesc;
  2836. userdesc.setown(createUserDescriptor());
  2837. userdesc->set(username.str(), passwd);
  2838. {
  2839. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalNameStr.str(), userdesc);
  2840. if(!df)
  2841. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Could not find file %s.", logicalNameStr.str());
  2842. IDistributedSuperFile *sf = df->querySuperFile();
  2843. if (sf && (sf->numSubFiles() > 1))
  2844. throw MakeStringException(ECLWATCH_INVALID_ACTION,"This feature is not designed to work with a superfile which contains multiple subfiles.");
  2845. }
  2846. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
  2847. Owned<INewResultSet> result;
  2848. if (m_clusterName.length() > 0)
  2849. {
  2850. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), m_clusterName.str()));
  2851. }
  2852. else
  2853. {
  2854. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), NULL));
  2855. }
  2856. __int64 total=result->getNumRows();
  2857. {
  2858. IArrayOf<IEspDFUDataColumn> dataKeyedColumns[MAX_KEY_ROWS];
  2859. IArrayOf<IEspDFUDataColumn> dataNonKeyedColumns[MAX_KEY_ROWS];
  2860. Owned<IResultSetCursor> cursor = result->createCursor();
  2861. const IResultSetMetaData & meta = cursor->queryResultSet()->getMetaData();
  2862. int columnCount = meta.getColumnCount();
  2863. int keyedColumnCount = meta.getNumKeyedColumns();
  2864. unsigned columnSize = 0;
  2865. int lineSizeCount = 0;
  2866. int lineCount = 0;
  2867. for (int i = 0; i < keyedColumnCount; i++)
  2868. {
  2869. Owned<IEspDFUDataColumn> item = createDFUDataColumn("","");
  2870. bool bNaturalColumn = true;
  2871. SCMStringBuffer columnLabel;
  2872. if (meta.hasSetTranslation(i))
  2873. {
  2874. meta.getNaturalColumnLabel(columnLabel, i);
  2875. }
  2876. if (columnLabel.length() < 1)
  2877. {
  2878. meta.getColumnLabel(columnLabel, i);
  2879. bNaturalColumn = false;
  2880. }
  2881. item->setColumnLabel(columnLabel.str());
  2882. if (version > 1.04 && filterByNames.length() > 0)
  2883. {
  2884. for (unsigned ii = 0; ii < filterByNames.length(); ii++)
  2885. {
  2886. const char* name = filterByNames.item(ii);
  2887. if (name && !stricmp(name, columnLabel.str()))
  2888. {
  2889. const char* value = filterByValues.item(ii);
  2890. if (value && *value)
  2891. {
  2892. item->setColumnValue(value);
  2893. break;
  2894. }
  2895. }
  2896. }
  2897. }
  2898. DisplayType columnType = meta.getColumnDisplayType(i);
  2899. if (bNaturalColumn)
  2900. {
  2901. item->setColumnType("Others");
  2902. item->setColumnSize(STRINGSIZE);
  2903. columnSize = STRINGSIZE;
  2904. item->setMaxSize(columnSize);
  2905. }
  2906. else if (columnType == TypeBoolean)
  2907. {
  2908. item->setColumnType("Boolean");
  2909. item->setMaxSize(1);
  2910. item->setColumnSize(strlen(columnLabel.str()));
  2911. columnSize = 2;
  2912. }
  2913. else
  2914. {
  2915. if (columnType == TypeInteger || columnType == TypeUnsignedInteger)
  2916. {
  2917. item->setColumnType("Integer");
  2918. item->setMaxSize(INTEGELSIZE);
  2919. columnSize = INTEGELSIZE;
  2920. if (strlen(columnLabel.str()) > columnSize)
  2921. columnSize = strlen(columnLabel.str());
  2922. item->setColumnSize(columnSize);
  2923. }
  2924. else if (columnType == TypeReal)
  2925. {
  2926. item->setColumnType("Real");
  2927. item->setMaxSize(REALSIZE);
  2928. columnSize = REALSIZE;
  2929. if (strlen(columnLabel.str()) > columnSize)
  2930. columnSize = strlen(columnLabel.str());
  2931. item->setColumnSize(columnSize);
  2932. }
  2933. else if (columnType == TypeString)
  2934. {
  2935. columnSize = meta.getColumnRawSize(i);
  2936. columnSize = rtlQStrLength(columnSize);
  2937. if (columnSize < 1)
  2938. columnSize = STRINGSIZE;
  2939. else if (columnSize > STRINGSIZE)
  2940. columnSize = STRINGSIZE;
  2941. item->setColumnType("String");
  2942. item->setMaxSize(columnSize);
  2943. if (strlen(columnLabel.str()) > columnSize)
  2944. columnSize = strlen(columnLabel.str());
  2945. item->setColumnSize(columnSize);
  2946. }
  2947. else if (columnType == TypeUnicode)
  2948. {
  2949. item->setColumnType("Others");
  2950. columnSize = (int) (meta.getColumnRawSize(i) * 0.5);
  2951. if (columnSize > STRINGSIZE)
  2952. columnSize = STRINGSIZE;
  2953. item->setColumnSize(columnSize);
  2954. item->setMaxSize(columnSize);
  2955. }
  2956. else
  2957. {
  2958. item->setColumnType("Others");
  2959. columnSize = STRINGSIZE;
  2960. item->setColumnSize(columnSize);
  2961. item->setMaxSize(columnSize);
  2962. }
  2963. }
  2964. columnSize += 7;
  2965. if ((lineSizeCount == 0) && (columnSize > STRINGSIZE)) //One field is big enough to use one line
  2966. {
  2967. if (lineCount >= MAX_KEY_ROWS)
  2968. break;
  2969. dataKeyedColumns[lineCount].append(*item.getLink());
  2970. lineCount++;
  2971. }
  2972. else
  2973. {
  2974. if (lineSizeCount + columnSize < STRINGSIZE)
  2975. {
  2976. lineSizeCount += columnSize;
  2977. }
  2978. else //too big in this line...so, switch to another line
  2979. {
  2980. lineCount++;
  2981. lineSizeCount = columnSize;
  2982. }
  2983. if (lineCount >= MAX_KEY_ROWS)
  2984. break;
  2985. dataKeyedColumns[lineCount].append(*item.getLink());
  2986. }
  2987. }
  2988. columnSize = 0;
  2989. lineSizeCount = 0;
  2990. lineCount = 0;
  2991. for (int ii = keyedColumnCount; ii < columnCount; ii++)
  2992. {
  2993. Owned<IEspDFUDataColumn> item = createDFUDataColumn("","");
  2994. bool bNaturalColumn = true;
  2995. SCMStringBuffer columnLabel;
  2996. if (meta.hasSetTranslation(ii))
  2997. {
  2998. meta.getNaturalColumnLabel(columnLabel, ii);
  2999. }
  3000. if (columnLabel.length() < 1)
  3001. {
  3002. meta.getColumnLabel(columnLabel, ii);
  3003. bNaturalColumn = false;
  3004. }
  3005. item->setColumnLabel(columnLabel.str());
  3006. if (version > 1.04 && filterByNames.length() > 0)
  3007. {
  3008. for (unsigned ii = 0; ii < filterByNames.length(); ii++)
  3009. {
  3010. const char* name = filterByNames.item(ii);
  3011. if (name && !stricmp(name, columnLabel.str()))
  3012. {
  3013. const char* value = filterByValues.item(ii);
  3014. if (value && *value)
  3015. {
  3016. item->setColumnValue(value);
  3017. break;
  3018. }
  3019. }
  3020. }
  3021. }
  3022. DisplayType columnType = meta.getColumnDisplayType(ii);
  3023. if (bNaturalColumn)
  3024. {
  3025. item->setColumnType("Others");
  3026. item->setColumnSize(STRINGSIZE);
  3027. columnSize = STRINGSIZE;
  3028. }
  3029. else if (columnType == TypeBoolean)
  3030. {
  3031. item->setColumnType("Boolean");
  3032. item->setMaxSize(1);
  3033. item->setColumnSize(strlen(columnLabel.str()));
  3034. columnSize = 2;
  3035. }
  3036. else
  3037. {
  3038. if (columnType == TypeInteger || columnType == TypeUnsignedInteger)
  3039. {
  3040. item->setColumnType("Integer");
  3041. item->setMaxSize(INTEGELSIZE);
  3042. columnSize = INTEGELSIZE;
  3043. if (strlen(columnLabel.str()) > columnSize)
  3044. columnSize = strlen(columnLabel.str());
  3045. item->setColumnSize(columnSize);
  3046. }
  3047. else if (columnType == TypeReal)
  3048. {
  3049. item->setColumnType("Real");
  3050. item->setMaxSize(REALSIZE);
  3051. columnSize = REALSIZE;
  3052. if (strlen(columnLabel.str()) > columnSize)
  3053. columnSize = strlen(columnLabel.str());
  3054. item->setColumnSize(columnSize);
  3055. }
  3056. else if (columnType == TypeString)
  3057. {
  3058. columnSize = meta.getColumnRawSize(ii);
  3059. columnSize = rtlQStrLength(columnSize);
  3060. if (columnSize < 1)
  3061. columnSize = STRINGSIZE;
  3062. else if (columnSize > STRINGSIZE)
  3063. columnSize = STRINGSIZE;
  3064. item->setColumnType("String");
  3065. item->setMaxSize(columnSize);
  3066. if (strlen(columnLabel.str()) > columnSize)
  3067. columnSize = strlen(columnLabel.str());
  3068. item->setColumnSize(columnSize);
  3069. }
  3070. else if (columnType == TypeUnicode)
  3071. {
  3072. item->setColumnType("Others");
  3073. columnSize = (int) (meta.getColumnRawSize(ii) * 0.5);
  3074. if (columnSize > STRINGSIZE)
  3075. columnSize = STRINGSIZE;
  3076. item->setColumnSize(columnSize);
  3077. item->setMaxSize(columnSize);
  3078. }
  3079. else
  3080. {
  3081. item->setColumnType("Others");
  3082. columnSize = STRINGSIZE;
  3083. item->setColumnSize(columnSize);
  3084. item->setMaxSize(columnSize);
  3085. }
  3086. }
  3087. columnSize += 7;
  3088. if ((lineSizeCount == 0) && (columnSize > STRINGSIZE))
  3089. {
  3090. if (lineCount >= MAX_KEY_ROWS)
  3091. break;
  3092. dataNonKeyedColumns[lineCount].append(*item.getLink());
  3093. lineCount++;
  3094. }
  3095. else
  3096. {
  3097. if (lineSizeCount + columnSize < STRINGSIZE)
  3098. {
  3099. lineSizeCount += columnSize;
  3100. }
  3101. else
  3102. {
  3103. lineCount++;
  3104. lineSizeCount = columnSize;
  3105. }
  3106. if (lineCount >= MAX_KEY_ROWS)
  3107. break;
  3108. dataNonKeyedColumns[lineCount].append(*item.getLink());
  3109. }
  3110. }
  3111. if (dataKeyedColumns[0].length() > 0)
  3112. resp.setDFUDataKeyedColumns1(dataKeyedColumns[0]);
  3113. if (dataKeyedColumns[1].length() > 0)
  3114. resp.setDFUDataKeyedColumns2(dataKeyedColumns[1]);
  3115. if (dataKeyedColumns[2].length() > 0)
  3116. resp.setDFUDataKeyedColumns3(dataKeyedColumns[2]);
  3117. if (dataKeyedColumns[3].length() > 0)
  3118. resp.setDFUDataKeyedColumns4(dataKeyedColumns[3]);
  3119. if (dataKeyedColumns[4].length() > 0)
  3120. resp.setDFUDataKeyedColumns5(dataKeyedColumns[4]);
  3121. if (dataKeyedColumns[5].length() > 0)
  3122. resp.setDFUDataKeyedColumns6(dataKeyedColumns[5]);
  3123. if (dataKeyedColumns[6].length() > 0)
  3124. resp.setDFUDataKeyedColumns7(dataKeyedColumns[6]);
  3125. if (dataKeyedColumns[7].length() > 0)
  3126. resp.setDFUDataKeyedColumns8(dataKeyedColumns[7]);
  3127. if (dataKeyedColumns[8].length() > 0)
  3128. resp.setDFUDataKeyedColumns9(dataKeyedColumns[8]);
  3129. if (dataKeyedColumns[9].length() > 0)
  3130. resp.setDFUDataKeyedColumns10(dataKeyedColumns[9]);
  3131. if (version > 1.14)
  3132. {
  3133. if (dataKeyedColumns[10].length() > 0)
  3134. resp.setDFUDataKeyedColumns11(dataKeyedColumns[10]);
  3135. if (dataKeyedColumns[11].length() > 0)
  3136. resp.setDFUDataKeyedColumns12(dataKeyedColumns[11]);
  3137. if (dataKeyedColumns[12].length() > 0)
  3138. resp.setDFUDataKeyedColumns13(dataKeyedColumns[12]);
  3139. if (dataKeyedColumns[13].length() > 0)
  3140. resp.setDFUDataKeyedColumns14(dataKeyedColumns[13]);
  3141. if (dataKeyedColumns[14].length() > 0)
  3142. resp.setDFUDataKeyedColumns15(dataKeyedColumns[14]);
  3143. if (dataKeyedColumns[15].length() > 0)
  3144. resp.setDFUDataKeyedColumns16(dataKeyedColumns[15]);
  3145. if (dataKeyedColumns[16].length() > 0)
  3146. resp.setDFUDataKeyedColumns17(dataKeyedColumns[16]);
  3147. if (dataKeyedColumns[17].length() > 0)
  3148. resp.setDFUDataKeyedColumns18(dataKeyedColumns[17]);
  3149. if (dataKeyedColumns[18].length() > 0)
  3150. resp.setDFUDataKeyedColumns19(dataKeyedColumns[18]);
  3151. if (dataKeyedColumns[19].length() > 0)
  3152. resp.setDFUDataKeyedColumns20(dataKeyedColumns[19]);
  3153. }
  3154. if (dataNonKeyedColumns[0].length() > 0)
  3155. resp.setDFUDataNonKeyedColumns1(dataNonKeyedColumns[0]);
  3156. if (dataNonKeyedColumns[1].length() > 0)
  3157. resp.setDFUDataNonKeyedColumns2(dataNonKeyedColumns[1]);
  3158. if (dataNonKeyedColumns[2].length() > 0)
  3159. resp.setDFUDataNonKeyedColumns3(dataNonKeyedColumns[2]);
  3160. if (dataNonKeyedColumns[3].length() > 0)
  3161. resp.setDFUDataNonKeyedColumns4(dataNonKeyedColumns[3]);
  3162. if (dataNonKeyedColumns[4].length() > 0)
  3163. resp.setDFUDataNonKeyedColumns5(dataNonKeyedColumns[4]);
  3164. if (dataNonKeyedColumns[5].length() > 0)
  3165. resp.setDFUDataNonKeyedColumns6(dataNonKeyedColumns[5]);
  3166. if (dataNonKeyedColumns[6].length() > 0)
  3167. resp.setDFUDataNonKeyedColumns7(dataNonKeyedColumns[6]);
  3168. if (dataNonKeyedColumns[7].length() > 0)
  3169. resp.setDFUDataNonKeyedColumns8(dataNonKeyedColumns[7]);
  3170. if (dataNonKeyedColumns[8].length() > 0)
  3171. resp.setDFUDataNonKeyedColumns9(dataNonKeyedColumns[8]);
  3172. if (dataNonKeyedColumns[9].length() > 0)
  3173. resp.setDFUDataNonKeyedColumns10(dataNonKeyedColumns[9]);
  3174. if (version > 1.14)
  3175. {
  3176. if (dataNonKeyedColumns[10].length() > 0)
  3177. resp.setDFUDataNonKeyedColumns11(dataNonKeyedColumns[10]);
  3178. if (dataNonKeyedColumns[11].length() > 0)
  3179. resp.setDFUDataNonKeyedColumns12(dataNonKeyedColumns[11]);
  3180. if (dataNonKeyedColumns[12].length() > 0)
  3181. resp.setDFUDataNonKeyedColumns13(dataNonKeyedColumns[12]);
  3182. if (dataNonKeyedColumns[13].length() > 0)
  3183. resp.setDFUDataNonKeyedColumns14(dataNonKeyedColumns[13]);
  3184. if (dataNonKeyedColumns[14].length() > 0)
  3185. resp.setDFUDataNonKeyedColumns15(dataNonKeyedColumns[14]);
  3186. if (dataNonKeyedColumns[15].length() > 0)
  3187. resp.setDFUDataNonKeyedColumns16(dataNonKeyedColumns[15]);
  3188. if (dataNonKeyedColumns[16].length() > 0)
  3189. resp.setDFUDataNonKeyedColumns17(dataNonKeyedColumns[16]);
  3190. if (dataNonKeyedColumns[17].length() > 0)
  3191. resp.setDFUDataNonKeyedColumns18(dataNonKeyedColumns[17]);
  3192. if (dataNonKeyedColumns[18].length() > 0)
  3193. resp.setDFUDataNonKeyedColumns19(dataNonKeyedColumns[18]);
  3194. if (dataNonKeyedColumns[19].length() > 0)
  3195. resp.setDFUDataNonKeyedColumns20(dataNonKeyedColumns[19]);
  3196. }
  3197. //resp.setColumnCount(columnCount);
  3198. resp.setRowCount(total);
  3199. }
  3200. resp.setLogicalName(logicalNameStr.str());
  3201. resp.setStartIndex(startIndex);
  3202. resp.setEndIndex(endIndex);
  3203. if (version > 1.11)
  3204. {
  3205. if (req.getCluster() && *req.getCluster())
  3206. {
  3207. resp.setCluster(req.getCluster());
  3208. }
  3209. if (req.getClusterType() && *req.getClusterType())
  3210. {
  3211. resp.setClusterType(req.getClusterType());
  3212. }
  3213. }
  3214. }
  3215. if (req.getChooseFile())
  3216. resp.setChooseFile(1);
  3217. else
  3218. resp.setChooseFile(0);
  3219. }
  3220. catch(IException* e)
  3221. {
  3222. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3223. }
  3224. return true;
  3225. }
  3226. bool CWsDfuEx::onDFUSearchData(IEspContext &context, IEspDFUSearchDataRequest &req, IEspDFUSearchDataResponse &resp)
  3227. {
  3228. try
  3229. {
  3230. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  3231. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to View Data File. Permission denied.");
  3232. double version = context.getClientVersion();
  3233. resp.setCluster(req.getCluster());
  3234. resp.setClusterType(req.getClusterType());
  3235. resp.setFile(req.getFile());
  3236. resp.setKey(req.getKey());
  3237. const char* selectedKey = req.getSelectedKey();
  3238. if (strlen(selectedKey) > 0)
  3239. {
  3240. resp.setSelectedKey(req.getSelectedKey());
  3241. }
  3242. else
  3243. {
  3244. resp.setSelectedKey(req.getKey());
  3245. }
  3246. resp.setParentName(req.getParentName());
  3247. resp.setRoxieSelections(req.getRoxieSelections());
  3248. resp.setDisableUppercaseTranslation(req.getDisableUppercaseTranslation());
  3249. const char* openLogicalName = req.getOpenLogicalName();
  3250. if (strlen(openLogicalName) > 0)
  3251. {
  3252. Owned<IEspDFUGetDataColumnsRequest> DataColumnsRequest = createDFUGetDataColumnsRequest();
  3253. Owned<IEspDFUGetDataColumnsResponse> DataColumnsResponse = createDFUGetDataColumnsResponse();
  3254. DataColumnsRequest->setOpenLogicalName(req.getOpenLogicalName());
  3255. DataColumnsRequest->setFilterBy(req.getFilterBy());
  3256. DataColumnsRequest->setShowColumns(req.getShowColumns());
  3257. DataColumnsRequest->setChooseFile(req.getChooseFile());
  3258. DataColumnsRequest->setCluster(req.getCluster());
  3259. DataColumnsRequest->setClusterType(req.getClusterType());
  3260. try
  3261. {
  3262. onDFUGetDataColumns(context, *DataColumnsRequest, *DataColumnsResponse);
  3263. }
  3264. catch(IException* e)
  3265. {
  3266. if (version < 1.08)
  3267. throw e;
  3268. StringBuffer emsg;
  3269. e->errorMessage(emsg);
  3270. e->Release();
  3271. resp.setMsgToDisplay(emsg);
  3272. return true;
  3273. }
  3274. resp.setOpenLogicalName(req.getOpenLogicalName());
  3275. resp.setLogicalName(DataColumnsResponse->getLogicalName());
  3276. resp.setStartIndex(DataColumnsResponse->getStartIndex());
  3277. resp.setEndIndex(DataColumnsResponse->getEndIndex());
  3278. resp.setDFUDataKeyedColumns1(DataColumnsResponse->getDFUDataKeyedColumns1());
  3279. resp.setDFUDataKeyedColumns2(DataColumnsResponse->getDFUDataKeyedColumns2());
  3280. resp.setDFUDataKeyedColumns3(DataColumnsResponse->getDFUDataKeyedColumns3());
  3281. resp.setDFUDataKeyedColumns4(DataColumnsResponse->getDFUDataKeyedColumns4());
  3282. resp.setDFUDataKeyedColumns5(DataColumnsResponse->getDFUDataKeyedColumns5());
  3283. resp.setDFUDataKeyedColumns6(DataColumnsResponse->getDFUDataKeyedColumns6());
  3284. resp.setDFUDataKeyedColumns7(DataColumnsResponse->getDFUDataKeyedColumns7());
  3285. resp.setDFUDataKeyedColumns8(DataColumnsResponse->getDFUDataKeyedColumns8());
  3286. resp.setDFUDataKeyedColumns9(DataColumnsResponse->getDFUDataKeyedColumns9());
  3287. resp.setDFUDataKeyedColumns10(DataColumnsResponse->getDFUDataKeyedColumns10());
  3288. if (version > 1.14)
  3289. {
  3290. resp.setDFUDataKeyedColumns11(DataColumnsResponse->getDFUDataKeyedColumns11());
  3291. resp.setDFUDataKeyedColumns12(DataColumnsResponse->getDFUDataKeyedColumns12());
  3292. resp.setDFUDataKeyedColumns13(DataColumnsResponse->getDFUDataKeyedColumns13());
  3293. resp.setDFUDataKeyedColumns14(DataColumnsResponse->getDFUDataKeyedColumns14());
  3294. resp.setDFUDataKeyedColumns15(DataColumnsResponse->getDFUDataKeyedColumns15());
  3295. resp.setDFUDataKeyedColumns16(DataColumnsResponse->getDFUDataKeyedColumns16());
  3296. resp.setDFUDataKeyedColumns17(DataColumnsResponse->getDFUDataKeyedColumns17());
  3297. resp.setDFUDataKeyedColumns18(DataColumnsResponse->getDFUDataKeyedColumns18());
  3298. resp.setDFUDataKeyedColumns19(DataColumnsResponse->getDFUDataKeyedColumns19());
  3299. resp.setDFUDataKeyedColumns20(DataColumnsResponse->getDFUDataKeyedColumns20());
  3300. }
  3301. resp.setDFUDataNonKeyedColumns1(DataColumnsResponse->getDFUDataNonKeyedColumns1());
  3302. resp.setDFUDataNonKeyedColumns2(DataColumnsResponse->getDFUDataNonKeyedColumns2());
  3303. resp.setDFUDataNonKeyedColumns3(DataColumnsResponse->getDFUDataNonKeyedColumns3());
  3304. resp.setDFUDataNonKeyedColumns4(DataColumnsResponse->getDFUDataNonKeyedColumns4());
  3305. resp.setDFUDataNonKeyedColumns5(DataColumnsResponse->getDFUDataNonKeyedColumns5());
  3306. resp.setDFUDataNonKeyedColumns6(DataColumnsResponse->getDFUDataNonKeyedColumns6());
  3307. resp.setDFUDataNonKeyedColumns7(DataColumnsResponse->getDFUDataNonKeyedColumns7());
  3308. resp.setDFUDataNonKeyedColumns8(DataColumnsResponse->getDFUDataNonKeyedColumns8());
  3309. resp.setDFUDataNonKeyedColumns9(DataColumnsResponse->getDFUDataNonKeyedColumns9());
  3310. resp.setDFUDataNonKeyedColumns10(DataColumnsResponse->getDFUDataNonKeyedColumns10());
  3311. if (version > 1.14)
  3312. {
  3313. resp.setDFUDataNonKeyedColumns11(DataColumnsResponse->getDFUDataNonKeyedColumns11());
  3314. resp.setDFUDataNonKeyedColumns12(DataColumnsResponse->getDFUDataNonKeyedColumns12());
  3315. resp.setDFUDataNonKeyedColumns13(DataColumnsResponse->getDFUDataNonKeyedColumns13());
  3316. resp.setDFUDataNonKeyedColumns14(DataColumnsResponse->getDFUDataNonKeyedColumns14());
  3317. resp.setDFUDataNonKeyedColumns15(DataColumnsResponse->getDFUDataNonKeyedColumns15());
  3318. resp.setDFUDataNonKeyedColumns16(DataColumnsResponse->getDFUDataNonKeyedColumns16());
  3319. resp.setDFUDataNonKeyedColumns17(DataColumnsResponse->getDFUDataNonKeyedColumns17());
  3320. resp.setDFUDataNonKeyedColumns18(DataColumnsResponse->getDFUDataNonKeyedColumns18());
  3321. resp.setDFUDataNonKeyedColumns19(DataColumnsResponse->getDFUDataNonKeyedColumns19());
  3322. resp.setDFUDataNonKeyedColumns20(DataColumnsResponse->getDFUDataNonKeyedColumns20());
  3323. }
  3324. resp.setRowCount(DataColumnsResponse->getRowCount());
  3325. resp.setShowColumns(DataColumnsResponse->getShowColumns());
  3326. resp.setChooseFile(DataColumnsResponse->getChooseFile());
  3327. }
  3328. const char* logicalName = req.getLogicalName();
  3329. if (strlen(logicalName) == 0 && strlen(openLogicalName) > 0)
  3330. {
  3331. logicalName = openLogicalName;
  3332. }
  3333. if (strlen(logicalName) > 0)
  3334. {
  3335. Owned<IEspDFUBrowseDataRequest> browseDataRequest = createDFUBrowseDataRequest();
  3336. Owned<IEspDFUBrowseDataResponse> browseDataResponse = createDFUBrowseDataResponse();
  3337. browseDataRequest->setLogicalName(logicalName);
  3338. const char* parentName = req.getParentName();
  3339. if (parentName && *parentName)
  3340. browseDataRequest->setParentName(parentName);
  3341. browseDataRequest->setFilterBy(req.getFilterBy());
  3342. browseDataRequest->setShowColumns(req.getShowColumns());
  3343. browseDataRequest->setStartForGoback(req.getStartForGoback());
  3344. browseDataRequest->setCountForGoback(req.getCountForGoback());
  3345. browseDataRequest->setChooseFile(req.getChooseFile());
  3346. browseDataRequest->setStart(req.getStart());
  3347. browseDataRequest->setCount(req.getCount());
  3348. browseDataRequest->setSchemaOnly(req.getSchemaOnly());
  3349. browseDataRequest->setCluster(req.getCluster());
  3350. browseDataRequest->setClusterType(req.getClusterType());
  3351. browseDataRequest->setDisableUppercaseTranslation(req.getDisableUppercaseTranslation());
  3352. onDFUBrowseData(context, *browseDataRequest, *browseDataResponse);
  3353. resp.setName(browseDataResponse->getName());
  3354. resp.setLogicalName(browseDataResponse->getLogicalName());
  3355. resp.setFilterBy(browseDataResponse->getFilterBy());
  3356. resp.setFilterForGoBack(browseDataResponse->getFilterForGoBack());
  3357. resp.setColumnsHidden(browseDataResponse->getColumnsHidden());
  3358. resp.setColumnsHidden(browseDataResponse->getColumnsHidden());
  3359. resp.setColumnCount(browseDataResponse->getColumnCount());
  3360. resp.setStartForGoback(browseDataResponse->getStartForGoback());
  3361. resp.setCountForGoback(browseDataResponse->getCountForGoback());
  3362. resp.setChooseFile(browseDataResponse->getChooseFile());
  3363. resp.setStart(browseDataResponse->getStart());
  3364. resp.setCount(browseDataResponse->getCount());
  3365. resp.setPageSize(browseDataResponse->getPageSize());
  3366. resp.setTotal(browseDataResponse->getTotal());
  3367. resp.setResult(browseDataResponse->getResult());
  3368. resp.setMsgToDisplay(browseDataResponse->getMsgToDisplay());
  3369. resp.setSchemaOnly(browseDataResponse->getSchemaOnly());
  3370. resp.setAutoUppercaseTranslation(!m_disableUppercaseTranslation);
  3371. }
  3372. }
  3373. catch(IException* e)
  3374. {
  3375. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3376. }
  3377. return true;
  3378. }
  3379. bool CWsDfuEx::onDFUBrowseData(IEspContext &context, IEspDFUBrowseDataRequest &req, IEspDFUBrowseDataResponse &resp)
  3380. {
  3381. try
  3382. {
  3383. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  3384. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to View Data File. Permission denied.");
  3385. const char* logicalName0 = req.getLogicalName();
  3386. const char* parentName = req.getParentName();
  3387. if (!logicalName0 || !*logicalName0)
  3388. throw MakeStringException(ECLWATCH_INVALID_INPUT,"No LogicalName defined.");
  3389. StringBuffer logicalNameStr;
  3390. if (logicalName0 && *logicalName0)
  3391. {
  3392. logicalNameStr.append(logicalName0);
  3393. logicalNameStr.trim();
  3394. if (logicalNameStr.length() < 1)
  3395. throw MakeStringException(ECLWATCH_INVALID_INPUT,"No LogicalName defined.");
  3396. }
  3397. __int64 start = req.getStart() > 0 ? req.getStart() : 0;
  3398. __int64 count=req.getCount() ? req.getCount() : 20, requested=count;
  3399. if (count > MAX_VIEWKEYFILE_ROWS)
  3400. throw MakeStringException(ECLWATCH_TOO_MANY_DATA_ROWS,"Browser Cannot display more than %d data rows.", MAX_VIEWKEYFILE_ROWS);
  3401. bool bSchemaOnly=req.getSchemaOnly() ? req.getSchemaOnly() : false;
  3402. bool bDisableUppercaseTranslation = req.getDisableUppercaseTranslation() ? req.getDisableUppercaseTranslation() : false;
  3403. #define HPCCBROWSER 1
  3404. #ifdef HPCCBROWSER
  3405. const char* filterBy = req.getFilterBy();
  3406. const char* showColumns = req.getShowColumns();
  3407. __int64 read=0;
  3408. __int64 total = 0;
  3409. StringBuffer msg;
  3410. StringArray columnLabels, columnLabelsType;
  3411. IArrayOf<IEspDFUData> DataList;
  3412. int iRet = GetIndexData(context, bSchemaOnly, logicalNameStr.str(), parentName, filterBy, start, count, read, total, msg, columnLabels, columnLabelsType, DataList, bDisableUppercaseTranslation);
  3413. if (iRet > 0)
  3414. resp.setMsgToDisplay("This search has timed out due to the restrictive filter. There may be more records.");
  3415. //GetIndexData(context, bSchemaOnly, logicalNameStr.str(), "roxie::thor_data400::key::bankruptcyv2::20090721::search::tmsid", filterBy, start, count, read, total, msg, columnLabels, columnLabelsType, DataList);
  3416. resp.setResult(DataList.item(0).getData());
  3417. unsigned int max_name_length = 3; //max length for name length
  3418. unsigned int max_value_length = 4; //max length for value length:
  3419. StringBuffer filterByStr, filterByStr0;
  3420. filterByStr0.appendf("%d%d", max_name_length, max_value_length);
  3421. unsigned columnCount = columnLabels.length();
  3422. IArrayOf<IEspDFUDataColumn> dataColumns;
  3423. double version = context.getClientVersion();
  3424. if (version > 1.04 && columnCount > 0)
  3425. {
  3426. //Find out which columns need to be displayed
  3427. int lenShowCols = 0, showCols[1024];
  3428. const char* showColumns = req.getShowColumns();
  3429. char *pShowColumns = (char*) showColumns;
  3430. while (pShowColumns && *pShowColumns)
  3431. {
  3432. StringBuffer buf;
  3433. while (pShowColumns && isdigit(pShowColumns[0]))
  3434. {
  3435. buf.append(pShowColumns[0]);
  3436. pShowColumns++;
  3437. }
  3438. if (buf.length() > 0)
  3439. {
  3440. showCols[lenShowCols] = atoi(buf.str());
  3441. lenShowCols++;
  3442. }
  3443. if (!pShowColumns || !*pShowColumns)
  3444. break;
  3445. pShowColumns++;
  3446. }
  3447. for(unsigned col = 0; col < columnCount; col++)
  3448. {
  3449. const char* label = columnLabels.item(col);
  3450. const char* type = columnLabelsType.item(col);
  3451. if (!label || !*label || !type || !*type)
  3452. continue;
  3453. Owned<IEspDFUDataColumn> item = createDFUDataColumn("","");
  3454. item->setColumnLabel(label);
  3455. item->setColumnType(type);
  3456. item->setColumnSize(0); //not show this column
  3457. if (!showColumns || !*showColumns)
  3458. {
  3459. item->setColumnSize(1); //Show this column
  3460. }
  3461. else
  3462. {
  3463. for(int col1 = 0; col1 < lenShowCols; col1++)
  3464. {
  3465. if (col == showCols[col1])
  3466. {
  3467. item->setColumnSize(1); //Show this column
  3468. break;
  3469. }
  3470. }
  3471. }
  3472. dataColumns.append(*item.getLink());
  3473. }
  3474. //Re-build filters
  3475. if (filterBy && *filterBy)
  3476. {
  3477. StringArray filterByNames, filterByValues;
  3478. parseTwoStringArrays(filterBy, filterByNames, filterByValues);
  3479. if (filterByNames.length() > 0)
  3480. {
  3481. for (unsigned ii = 0; ii < filterByNames.length(); ii++)
  3482. {
  3483. const char* columnName = filterByNames.item(ii);
  3484. const char* columnValue = filterByValues.item(ii);
  3485. if (columnName && *columnName && columnValue && *columnValue)
  3486. {
  3487. filterByStr.appendf("%s[%s]", columnName, columnValue);
  3488. filterByStr0.appendf("%03d%04d%s%s", (int) strlen(columnName), (int) strlen(columnValue), columnName, columnValue);
  3489. }
  3490. }
  3491. }
  3492. }
  3493. if (req.getStartForGoback())
  3494. resp.setStartForGoback(req.getStartForGoback());
  3495. if (req.getCountForGoback())
  3496. resp.setCountForGoback(req.getCountForGoback());
  3497. }
  3498. #else
  3499. StringBuffer username;
  3500. context.getUserID(username);
  3501. double version = context.getClientVersion();
  3502. const char* passwd = context.queryPassword();
  3503. StringBuffer eclqueue, cluster;
  3504. Owned<IUserDescriptor> userdesc;
  3505. try
  3506. {
  3507. userdesc.setown(createUserDescriptor());
  3508. userdesc->set(username.str(), passwd);
  3509. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalNameStr.str(), userdesc);
  3510. if(df)
  3511. {
  3512. const char* wuid = df->queryAttributes().queryProp("@workunit");
  3513. if (wuid && *wuid)
  3514. {
  3515. CWUWrapper wu(wuid, context);
  3516. if (wu)
  3517. {
  3518. SCMStringBuffer eclqueue0, cluster0;
  3519. eclqueue.append(wu->getQueue(eclqueue0).str());
  3520. cluster.append(wu->getClusterName(cluster0).str());
  3521. }
  3522. }
  3523. }
  3524. }
  3525. catch(...)
  3526. {
  3527. ;
  3528. }
  3529. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), *context.queryUser());
  3530. Owned<INewResultSet> result;
  3531. if (eclqueue && *eclqueue && cluster && *cluster)
  3532. {
  3533. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), eclqueue, cluster));
  3534. }
  3535. else if (m_clusterName.length() > 0 && m_eclServerQueue.length() > 0)
  3536. {
  3537. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), m_eclServerQueue.str(), m_clusterName.str()));
  3538. }
  3539. else
  3540. {
  3541. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), NULL, NULL));
  3542. }
  3543. const IResultSetMetaData &meta = result->getMetaData();
  3544. unsigned columnCount = meta.getColumnCount();
  3545. StringArray filterByNames, filterByValues;
  3546. IArrayOf<IEspDFUDataColumn> dataColumns;
  3547. if (version > 1.04 && columnCount > 0)
  3548. {
  3549. int lenShowCols = 0, showCols[1024];
  3550. const char* showColumns = req.getShowColumns();
  3551. char *pShowColumns = (char*) showColumns;
  3552. while (pShowColumns && *pShowColumns)
  3553. {
  3554. StringBuffer buf;
  3555. while (pShowColumns && isdigit(pShowColumns[0]))
  3556. {
  3557. buf.append(pShowColumns[0]);
  3558. pShowColumns++;
  3559. }
  3560. if (buf.length() > 0)
  3561. {
  3562. showCols[lenShowCols] = atoi(buf.str());
  3563. lenShowCols++;
  3564. }
  3565. if (!pShowColumns || !*pShowColumns)
  3566. break;
  3567. pShowColumns++;
  3568. }
  3569. for(int col = 0; col < columnCount; col++)
  3570. {
  3571. Owned<IEspDFUDataColumn> item = createDFUDataColumn("","");
  3572. SCMStringBuffer scmbuf;
  3573. meta.getColumnLabel(scmbuf, col);
  3574. item->setColumnLabel(scmbuf.str());
  3575. if (!showColumns || !*showColumns)
  3576. {
  3577. item->setColumnSize(1); //Show this column
  3578. dataColumns.append(*item.getLink());
  3579. continue;
  3580. }
  3581. else
  3582. {
  3583. item->setColumnSize(0); //not show this column
  3584. }
  3585. for(int col1 = 0; col1 < lenShowCols; col1++)
  3586. {
  3587. if (col == showCols[col1])
  3588. {
  3589. item->setColumnSize(1); //Show this column
  3590. break;
  3591. }
  3592. }
  3593. dataColumns.append(*item.getLink());
  3594. }
  3595. const char* filterBy = req.getFilterBy();
  3596. if (filterBy && *filterBy)
  3597. {
  3598. parseTwoStringArrays(filterBy, filterByNames, filterByValues);
  3599. }
  3600. if (req.getStartForGoback())
  3601. resp.setStartForGoback(req.getStartForGoback());
  3602. if (req.getCountForGoback())
  3603. resp.setCountForGoback(req.getCountForGoback());
  3604. }
  3605. StringBuffer filterByStr, filterByStr0;
  3606. unsigned int max_name_length = 3; //max length for name length
  3607. unsigned int max_value_length = 4; //max length for value length:
  3608. filterByStr0.appendf("%d%d", max_name_length, max_value_length);
  3609. if (columnCount > 0 && filterByNames.length() > 0)
  3610. {
  3611. Owned<IFilteredResultSet> filter = result->createFiltered();
  3612. for (int ii = 0; ii < filterByNames.length(); ii++)
  3613. {
  3614. const char* columnName = filterByNames.item(ii);
  3615. const char* columnValue = filterByValues.item(ii);
  3616. if (columnName && *columnName && columnValue && *columnValue)
  3617. {
  3618. int col = 0;
  3619. for(col = 0; col < columnCount; col++)
  3620. {
  3621. SCMStringBuffer scmbuf;
  3622. meta.getColumnLabel(scmbuf, col);
  3623. if (stricmp(scmbuf.str(), columnName) == 0)
  3624. {
  3625. filter->addFilter(col, columnValue);
  3626. filterByStr.appendf("%s[%s]", columnName, columnValue);
  3627. filterByStr0.appendf("%03d%04d%s%s", strlen(columnName), strlen(columnValue), columnName, columnValue);
  3628. break;
  3629. }
  3630. }
  3631. if (col == columnCount)
  3632. {
  3633. throw MakeStringException(0,"The filter %s not defined", columnName);
  3634. }
  3635. }
  3636. }
  3637. result.setown(filter->create());
  3638. }
  3639. StringBuffer text;
  3640. const char* schemaName = "myschema";
  3641. Owned<IResultSetCursor> cursor = result->createCursor();
  3642. text.append("<XmlSchema name=\"").append(schemaName).append("\">");
  3643. const IResultSetMetaData & meta1 = cursor->queryResultSet()->getMetaData();
  3644. StringBufferAdaptor adaptor(text);
  3645. meta1.getXmlSchema(adaptor, false);
  3646. text.append("</XmlSchema>").newline();
  3647. text.append("<Dataset");
  3648. //if (name)
  3649. // text.append(" name=\"").append(name).append("\" ");
  3650. text.append(" xmlSchema=\"").append(schemaName).append("\" ");
  3651. text.append(">").newline();
  3652. //__int64 total=0;
  3653. __int64 total=result->getNumRows();
  3654. __int64 read=0;
  3655. try
  3656. {
  3657. for(bool ok=cursor->absolute(start);ok;ok=cursor->next())
  3658. {
  3659. //total++;
  3660. //if(read < count)
  3661. {
  3662. text.append(" ");
  3663. StringBufferAdaptor adaptor2(text);
  3664. cursor->getXmlRow(adaptor2);
  3665. text.newline();
  3666. read++;
  3667. }
  3668. if(read>=count)
  3669. break;
  3670. }
  3671. }
  3672. catch(IException* e)
  3673. {
  3674. if ((version < 1.08) || (e->errorCode() != FVERR_FilterTooRestrictive))
  3675. throw e;
  3676. e->Release();
  3677. resp.setMsgToDisplay("This search is timed out due to the restrictive filter. There may be more records.");
  3678. }
  3679. if (count > read)
  3680. count = read;
  3681. text.append("</Dataset>").newline();
  3682. ///DBGLOG("Dataset:%s", text.str());
  3683. MemoryBuffer buf;
  3684. struct MemoryBuffer2IStringVal : public CInterface, implements IStringVal
  3685. {
  3686. MemoryBuffer2IStringVal(MemoryBuffer & _buffer) : buffer(_buffer) {}
  3687. IMPLEMENT_IINTERFACE;
  3688. virtual const char * str() const { UNIMPLEMENTED; }
  3689. virtual void set(const char *val) { buffer.append(strlen(val),val); }
  3690. virtual void clear() { } // clearing when appending does nothing
  3691. virtual void setLen(const char *val, unsigned length) { buffer.append(length, val); }
  3692. virtual unsigned length() const { return buffer.length(); };
  3693. MemoryBuffer & buffer;
  3694. } adaptor0(buf);
  3695. adaptor0.set(text.str());
  3696. buf.append(0);
  3697. resp.setResult(buf.toByteArray());
  3698. #endif
  3699. //resp.setFilterBy(filterByStr.str());
  3700. if (filterByStr.length() > 0)
  3701. {
  3702. const char* oldStr = "&";
  3703. const char* newStr = "&amp;";
  3704. filterByStr.replaceString(oldStr, newStr);
  3705. resp.setFilterBy(filterByStr.str());
  3706. }
  3707. if (version > 1.04)
  3708. {
  3709. //resp.setFilterForGoBack(filterByStr0.str());
  3710. if (filterByStr0.length() > 0)
  3711. {
  3712. const char* oldStr = "&";
  3713. const char* newStr = "&amp;";
  3714. filterByStr0.replaceString(oldStr, newStr);
  3715. resp.setFilterForGoBack(filterByStr0.str());
  3716. }
  3717. resp.setColumnCount(columnCount);
  3718. if (dataColumns.length() > 0)
  3719. resp.setColumnsHidden(dataColumns);
  3720. }
  3721. if (version > 1.10)
  3722. {
  3723. resp.setSchemaOnly(bSchemaOnly);
  3724. }
  3725. //resp.setName(name.str());
  3726. resp.setLogicalName(logicalNameStr.str());
  3727. resp.setStart(start);
  3728. //if (requested > read)
  3729. // requested = read;
  3730. resp.setPageSize(requested);
  3731. if (count > read)
  3732. {
  3733. count = read;
  3734. }
  3735. resp.setCount(count);
  3736. if (total != UNKNOWN_NUM_ROWS)
  3737. resp.setTotal(total);
  3738. else
  3739. resp.setTotal(-1);
  3740. if (req.getChooseFile())
  3741. resp.setChooseFile(1);
  3742. else
  3743. resp.setChooseFile(0);
  3744. if (version > 1.11)
  3745. {
  3746. if (req.getCluster() && *req.getCluster())
  3747. {
  3748. resp.setCluster(req.getCluster());
  3749. }
  3750. if (req.getClusterType() && *req.getClusterType())
  3751. {
  3752. resp.setClusterType(req.getClusterType());
  3753. }
  3754. }
  3755. if ((version > 1.12) && parentName && *parentName)
  3756. {
  3757. resp.setParentName(parentName);
  3758. }
  3759. }
  3760. catch(IException* e)
  3761. {
  3762. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3763. }
  3764. return true;
  3765. }
  3766. void CWsDfuEx::getRoxieClusterConfig(char const * clusterType, char const * clusterName, char const * processName, StringBuffer& netAddress, int& port)
  3767. {
  3768. #if 0
  3769. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  3770. Owned<IConstEnvironment> environment = factory->openEnvironment();
  3771. Owned<IPropertyTree> pRoot = &environment->getPTree();
  3772. #else
  3773. CTpWrapper dummy;
  3774. Owned<IPropertyTree> pRoot = dummy.getEnvironment("");
  3775. if (!pRoot)
  3776. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO,"Failed to get environment information.");
  3777. #endif
  3778. StringBuffer xpath;
  3779. xpath.appendf("Software/%s[@name='%s']", clusterType, clusterName);
  3780. IPropertyTree* pCluster = pRoot->queryPropTree( xpath.str() );
  3781. if (!pCluster)
  3782. throw MakeStringException(ECLWATCH_CLUSTER_NOT_IN_ENV_INFO, "'%s %s' is not defined!", clusterType, clusterName);
  3783. xpath.clear().append(processName);
  3784. xpath.append("@computer");
  3785. const char* computer = pCluster->queryProp(xpath.str());
  3786. if (!computer || strlen(computer) < 1)
  3787. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_INFO, "'%s %s: %s' is not defined!", clusterType, clusterName, processName);
  3788. xpath.clear().append(processName);
  3789. xpath.append("@port");
  3790. const char* portStr = pCluster->queryProp(xpath.str());
  3791. port = ROXIE_SERVER_PORT;
  3792. if (portStr && *portStr)
  3793. {
  3794. port = atoi(portStr);
  3795. }
  3796. #if 0
  3797. Owned<IConstMachineInfo> pMachine = environment->getMachine(computer);
  3798. if (pMachine)
  3799. {
  3800. SCMStringBuffer scmNetAddress;
  3801. pMachine->getNetAddress(scmNetAddress);
  3802. netAddress = scmNetAddress.str();
  3803. }
  3804. #else
  3805. xpath.clear().appendf("Hardware/Computer[@name=\"%s\"]", computer);
  3806. IPropertyTree* pMachine = pRoot->queryPropTree( xpath.str() );
  3807. if (pMachine)
  3808. {
  3809. const char* addr = pMachine->queryProp("@netAddress");
  3810. if (addr && *addr)
  3811. netAddress.append(addr);
  3812. }
  3813. #endif
  3814. return;
  3815. }
  3816. //////////////////////HPCC Browser//////////////////////////
  3817. static const char* SCHEMANAME = "myschema";
  3818. //void CWsDfuEx::setRootFilter(INewResultSet* result, const char* filterBy, IFilteredResultSet* filter)
  3819. void CWsDfuEx::setRootFilter(INewResultSet* result, const char* filterBy, IResultSetFilter* filter, bool disableUppercaseTranslation)
  3820. {
  3821. if (!filterBy || !*filterBy || !result)
  3822. return;
  3823. //Owned<IFilteredResultSet> filter = result->createFiltered();
  3824. filter->clearFilters();
  3825. const IResultSetMetaData &meta = result->getMetaData();
  3826. unsigned columnCount = meta.getColumnCount();
  3827. if (columnCount < 1)
  3828. return;
  3829. StringArray filterByNames, filterByValues;
  3830. parseTwoStringArrays(filterBy, filterByNames, filterByValues);
  3831. if (filterByNames.length() < 1)
  3832. return;
  3833. for (unsigned ii = 0; ii < filterByNames.length(); ii++)
  3834. {
  3835. const char* columnName = filterByNames.item(ii);
  3836. const char* columnValue0 = filterByValues.item(ii);
  3837. if (!columnName || !*columnName || !columnValue0 || !*columnValue0)
  3838. continue;
  3839. StringBuffer buf(columnValue0);
  3840. if (!disableUppercaseTranslation)
  3841. buf.toUpperCase();
  3842. const char* columnValue = buf.str();
  3843. for(unsigned col = 0; col < columnCount; col++)
  3844. {
  3845. bool hasSetTranslation = false;
  3846. SCMStringBuffer scmbuf;
  3847. if (meta.hasSetTranslation(col))
  3848. {
  3849. hasSetTranslation = true;
  3850. meta.getNaturalColumnLabel(scmbuf, col);
  3851. }
  3852. if (scmbuf.length() < 1)
  3853. {
  3854. meta.getColumnLabel(scmbuf, col);
  3855. }
  3856. if (!stricmp(scmbuf.str(), columnName))
  3857. {
  3858. //filter->addFilter(col, columnValue);
  3859. //filterByStr.appendf("%s[%s]", columnName, columnValue);
  3860. //filterByStr0.appendf("%03d%04d%s%s", strlen(columnName), strlen(columnValue), columnName, columnValue);
  3861. if (hasSetTranslation)
  3862. filter->addNaturalFilter(col, strlen(columnValue), columnValue);
  3863. else
  3864. filter->addFilter(col, strlen(columnValue), columnValue);
  3865. break;
  3866. }
  3867. }
  3868. }
  3869. //result.setown(filter->create());
  3870. return;
  3871. }
  3872. void CWsDfuEx::getMappingColumns(IRelatedBrowseFile * file, bool isPrimary, UnsignedArray& cols)
  3873. {
  3874. const char* logicalName = file->queryDefinition()->queryDistributedFile()->queryLogicalName();
  3875. const char* primaryName = file->queryParentRelation()->queryFileRelationship()->queryPrimaryFilename();
  3876. if (!logicalName || !primaryName || strcmp(logicalName, primaryName))
  3877. return;
  3878. IViewRelation* parentRelation = file->queryParentRelation();
  3879. for (unsigned i=0; i < parentRelation->numMappingFields(); i++)
  3880. {
  3881. //find out the column numbers to remove
  3882. unsigned col = parentRelation->queryMappingField(i, isPrimary);
  3883. cols.append(col);
  3884. }
  3885. #ifdef TESTDATASET
  3886. cols.kill();
  3887. cols.append(2);
  3888. cols.append(3);
  3889. #endif
  3890. return;
  3891. }
  3892. void CWsDfuEx::readColumnsForDisplay(StringBuffer& schemaText, StringArray& columnsDisplay, StringArray& columnsDisplayType)
  3893. {
  3894. if (schemaText.length() < 1)
  3895. return;
  3896. Owned<IPropertyTree> schema = createPTreeFromXMLString(schemaText.str());
  3897. if (!schema)
  3898. return;
  3899. //Find out labels from the second schema used for column mapping
  3900. columnsDisplay.kill();
  3901. Owned<IPropertyTreeIterator> rows4 = schema->getElements("xs:element[@name=\"Dataset\"]/xs:complexType/xs:sequence/xs:element[@name=\"Row\"]/xs:complexType/xs:sequence/*");
  3902. ForEach(*rows4)
  3903. {
  3904. IPropertyTree &e = rows4->query();
  3905. const char* name = e.queryProp("@name");
  3906. const char* type = e.queryProp("@type");
  3907. bool hasChildren = e.hasChildren();
  3908. if (!name || !*name)
  3909. continue;
  3910. columnsDisplay.append(name); //Display this column
  3911. if (type && *type)
  3912. columnsDisplayType.append(type);
  3913. else if (hasChildren)
  3914. columnsDisplayType.append("Object");
  3915. else
  3916. columnsDisplayType.append("Unknown");
  3917. }
  3918. return;
  3919. }
  3920. void CWsDfuEx::mergeSchema(IRelatedBrowseFile * file, StringBuffer& schemaText, StringBuffer schemaText2,
  3921. StringArray& columnsDisplay, StringArray& columnsDisplayType, StringArray& columnsHide)
  3922. {
  3923. if (schemaText.length() < 1)
  3924. return;
  3925. if (schemaText2.length() < 1)
  3926. return;
  3927. //DBGLOG("First schema returns:%s", schemaText.str());
  3928. //DBGLOG("Second schema returns:%s", schemaText2.str());
  3929. Owned<IPropertyTree> schema = createPTreeFromXMLString(schemaText.str());
  3930. Owned<IPropertyTree> schema2 = createPTreeFromXMLString(schemaText2.str());
  3931. if (!schema || !schema2)
  3932. return;
  3933. //Process simpleType part
  3934. Owned<IPropertyTreeIterator> rows1 = schema->getElements("xs:simpleType");
  3935. Owned<IPropertyTreeIterator> rows2 = schema2->getElements("xs:simpleType");
  3936. if (!rows1 || !rows2)
  3937. return;
  3938. ForEach(*rows2)
  3939. {
  3940. IPropertyTree &e = rows2->query();
  3941. const char* name = e.queryProp("@name");
  3942. if (!name || !*name)
  3943. continue;
  3944. bool bFound = false;
  3945. ForEach(*rows1)
  3946. {
  3947. IPropertyTree &e1 = rows1->query();
  3948. const char* name1 = e1.queryProp("@name");
  3949. if (!name1 || !*name1 || stricmp(name1, name))
  3950. continue;
  3951. bFound = true;
  3952. break;
  3953. }
  3954. if (!bFound)
  3955. schema->addPropTree(e.queryName(), LINK(&e));
  3956. }
  3957. IPropertyTree* rows = schema->queryBranch("xs:element[@name=\"Dataset\"]/xs:complexType/xs:sequence/xs:element[@name=\"Row\"]/xs:complexType/xs:sequence");
  3958. if (!rows)
  3959. return;
  3960. // StringBuffer schemaText4;
  3961. // toXML(schema, schemaText4);
  3962. //DBGLOG("First schema returns:%s", schemaText4.str());
  3963. //Find out labels used for column mapping
  3964. columnsDisplay.kill();
  3965. columnsDisplayType.kill();
  3966. columnsHide.kill();
  3967. Owned<IPropertyTreeIterator> rows4 = schema->getElements("xs:element[@name=\"Dataset\"]/xs:complexType/xs:sequence/xs:element[@name=\"Row\"]/xs:complexType/xs:sequence/*");
  3968. ForEach(*rows4)
  3969. {
  3970. IPropertyTree &e = rows4->query();
  3971. const char* name = e.queryProp("@name");
  3972. const char* type = e.queryProp("@type");
  3973. bool hasChildren = e.hasChildren();
  3974. if (!name || !*name)
  3975. continue;
  3976. columnsDisplay.append(name); //Display this column
  3977. if (type && *type)
  3978. columnsDisplayType.append(type);
  3979. else if (hasChildren)
  3980. columnsDisplayType.append("Object");
  3981. else
  3982. columnsDisplayType.append("Unknown");
  3983. }
  3984. UnsignedArray cols;
  3985. bool isPrimary = true;
  3986. getMappingColumns(file, isPrimary, cols);
  3987. //Process complexType part for labels
  3988. unsigned col0 = 0;
  3989. Owned<IPropertyTreeIterator> rows3 = schema2->getElements("xs:element[@name=\"Dataset\"]/xs:complexType/xs:sequence/xs:element[@name=\"Row\"]/xs:complexType/xs:sequence/*");
  3990. ForEach(*rows3)
  3991. {
  3992. IPropertyTree &e = rows3->query();
  3993. const char* name = e.queryProp("@name");
  3994. const char* type = e.queryProp("@type");
  3995. bool hasChildren = e.hasChildren();
  3996. if (!name || !*name)
  3997. continue;
  3998. bool bAdd = true;
  3999. bool bRename = false;
  4000. if (cols.ordinality() != 0)
  4001. {
  4002. ForEachItemIn(i1,cols)
  4003. {
  4004. unsigned col = cols.item(i1);
  4005. if (col == col0)
  4006. {
  4007. bAdd = false;
  4008. break;
  4009. }
  4010. }
  4011. }
  4012. #define RENAMESAMECOLUMN
  4013. #ifdef RENAMESAMECOLUMN
  4014. if (columnsDisplay.length() > 0)
  4015. {
  4016. for (unsigned i = 0; i < columnsDisplay.length(); i++)
  4017. {
  4018. const char* label = columnsDisplay.item(i);
  4019. if (!label || strcmp(label, name))
  4020. continue;
  4021. bRename = true;
  4022. break;
  4023. }
  4024. }
  4025. #endif
  4026. if (!bAdd)
  4027. {
  4028. columnsHide.append(name); //hide this column
  4029. }
  4030. else
  4031. {
  4032. if (type && *type)
  4033. columnsDisplayType.append(type);
  4034. else if (hasChildren)
  4035. columnsDisplayType.append("Object");
  4036. else
  4037. columnsDisplayType.append("Unknown");
  4038. #ifdef RENAMESAMECOLUMN
  4039. if (bRename)
  4040. {
  4041. StringBuffer newName(name);
  4042. newName.append("-2");
  4043. columnsDisplay.append(newName.str()); //Display this column
  4044. e.setProp("@name", newName.str());
  4045. rows->addPropTree(e.queryName(), LINK(&e));
  4046. }
  4047. else
  4048. {
  4049. #endif
  4050. columnsDisplay.append(name); //Display this column
  4051. rows->addPropTree(e.queryName(), LINK(&e));
  4052. #ifdef RENAMESAMECOLUMN
  4053. }
  4054. #endif
  4055. }
  4056. col0++;
  4057. }
  4058. //Convert schema tree to schame now
  4059. schemaText.clear();
  4060. toXML(schema, schemaText);
  4061. //DBGLOG("Merged schema returns:%s", schemaText.str());
  4062. return;
  4063. }
  4064. void CWsDfuEx::mergeDataRow(StringBuffer& newRow, int depth, IPropertyTreeIterator* it, StringArray& columnsHide, StringArray& columnsUsed)
  4065. {
  4066. if (!it)
  4067. return;
  4068. it->first();
  4069. while(it->isValid())
  4070. {
  4071. IPropertyTree* e = &it->query();
  4072. if (e)
  4073. {
  4074. const char* label = e->queryName();
  4075. if (label && *label)
  4076. {
  4077. #ifdef RENAMESAMECOLUMN
  4078. if (depth < 1)
  4079. columnsUsed.append(label);
  4080. #endif
  4081. bool bHide = false;
  4082. if (columnsHide.length() > 0)
  4083. {
  4084. for (unsigned i = 0 ; i < columnsHide.length(); i++)
  4085. {
  4086. const char* key = columnsHide.item(i);
  4087. if (!key || strcmp(key, label))
  4088. continue;
  4089. bHide = true;
  4090. break;
  4091. }
  4092. }
  4093. #ifdef RENAMESAMECOLUMN
  4094. if (!bHide && depth > 0 && columnsUsed.length() > 0)
  4095. {
  4096. for (unsigned i = 0 ; i < columnsUsed.length(); i++)
  4097. {
  4098. const char* key = columnsUsed.item(i);
  4099. if (!key || strcmp(key, label))
  4100. continue;
  4101. StringBuffer newName(label);
  4102. newName.append("-2");
  4103. e->renameProp("/", newName.str());
  4104. break;
  4105. }
  4106. }
  4107. #endif
  4108. if (!bHide)
  4109. {
  4110. StringBuffer dataRow;
  4111. toXML(e, dataRow);
  4112. newRow.append(dataRow);
  4113. }
  4114. }
  4115. }
  4116. it->next();
  4117. }
  4118. return;
  4119. }
  4120. void CWsDfuEx::mergeDataRow(StringBuffer& newRow, StringBuffer dataRow1, StringBuffer dataRow2, StringArray& columnsHide)
  4121. {
  4122. if (dataRow1.length() < 1)
  4123. return;
  4124. if (dataRow2.length() < 1)
  4125. return;
  4126. Owned<IPropertyTree> data1 = createPTreeFromXMLString(dataRow1.str());
  4127. Owned<IPropertyTree> data2 = createPTreeFromXMLString(dataRow2.str());
  4128. if (!data1 || !data2)
  4129. return;
  4130. newRow.clear();
  4131. newRow.append("<Row>");
  4132. StringArray columnLabels;
  4133. IPropertyTreeIterator* it = data1->getElements("*");
  4134. if (it)
  4135. {
  4136. StringArray columnLabels0;
  4137. mergeDataRow(newRow, 0, it, columnLabels0, columnLabels);
  4138. }
  4139. IPropertyTreeIterator* it2 = data2->getElements("*");
  4140. if (it2)
  4141. {
  4142. mergeDataRow(newRow, 1, it2, columnsHide, columnLabels);
  4143. }
  4144. newRow.append("</Row>");
  4145. return;
  4146. }
  4147. void CWsDfuEx::browseRelatedFileSchema(IRelatedBrowseFile * file, const char* parentName, unsigned depth, StringBuffer& schemaText,
  4148. StringArray& columnsDisplay, StringArray& columnsDisplayType, StringArray& columnsHide)
  4149. {
  4150. //if (file in set of files to display or iterate)
  4151. IResultSetCursor * cursor = file->queryCursor();
  4152. if (cursor && cursor->first())
  4153. {
  4154. if (depth < 1)
  4155. {
  4156. const IResultSetMetaData & meta = cursor->queryResultSet()->getMetaData();
  4157. StringBufferAdaptor adaptor(schemaText);
  4158. meta.getXmlSchema(adaptor, false);
  4159. #ifdef TESTDATASET
  4160. schemaText.clear();
  4161. schemaText.append("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\"");
  4162. schemaText.append(" attributeFormDefault=\"unqualified\">");
  4163. schemaText.append("<xs:element name=\"Dataset\"><xs:complexType><xs:sequence minOccurs=\"0\" maxOccurs=\"unbounded\">");
  4164. schemaText.append("<xs:element name=\"Row\"><xs:complexType><xs:sequence>");
  4165. schemaText.append("<xs:element name=\"state\" type=\"string2\"/>");
  4166. schemaText.append("<xs:element name=\"rtype\" type=\"string2\"/>");
  4167. schemaText.append("<xs:element name=\"id\" type=\"string20\"/>");
  4168. schemaText.append("<xs:element name=\"seq\" type=\"xs:nonNegativeInteger\"/>");
  4169. schemaText.append("<xs:element name=\"num\" type=\"xs:nonNegativeInteger\"/>");
  4170. schemaText.append("<xs:element name=\"date\" type=\"string8\"/>");
  4171. schemaText.append("<xs:element name=\"imglength\" type=\"xs:nonNegativeInteger\"/>");
  4172. schemaText.append("<xs:element name=\"__filepos\" type=\"xs:nonNegativeInteger\"/>");
  4173. schemaText.append("</xs:sequence></xs:complexType></xs:element>");
  4174. schemaText.append("</xs:sequence></xs:complexType></xs:element>");
  4175. schemaText.append("<xs:simpleType name=\"string2\"><xs:restriction base=\"xs:string\"><xs:maxLength value=\"2\"/>");
  4176. schemaText.append("</xs:restriction></xs:simpleType>");
  4177. schemaText.append("<xs:simpleType name=\"string20\"><xs:restriction base=\"xs:string\"><xs:maxLength value=\"20\"/>");
  4178. schemaText.append("</xs:restriction></xs:simpleType>");
  4179. schemaText.append("<xs:simpleType name=\"string8\"><xs:restriction base=\"xs:string\"><xs:maxLength value=\"8\"/>");
  4180. schemaText.append("</xs:restriction></xs:simpleType>");
  4181. schemaText.append("</xs:schema>");
  4182. #endif
  4183. readColumnsForDisplay(schemaText, columnsDisplay, columnsDisplayType);
  4184. }
  4185. else
  4186. {
  4187. StringBuffer schemaText0;
  4188. const IResultSetMetaData & meta = cursor->queryResultSet()->getMetaData();
  4189. StringBufferAdaptor adaptor(schemaText0);
  4190. meta.getXmlSchema(adaptor, false);
  4191. #ifdef TESTDATASET
  4192. schemaText0.clear();
  4193. schemaText0.append("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\"");
  4194. schemaText0.append(" attributeFormDefault=\"unqualified\">");
  4195. schemaText0.append("<xs:element name=\"Dataset\"><xs:complexType><xs:sequence minOccurs=\"0\" maxOccurs=\"unbounded\">");
  4196. schemaText0.append("<xs:element name=\"Row\"><xs:complexType><xs:sequence>");
  4197. schemaText0.append("<xs:element name=\"date_first_reported\" type=\"string12\"/>");
  4198. schemaText0.append("<xs:element name=\"msa\" type=\"string8\"/>");
  4199. schemaText0.append("<xs:element name=\"sid\" type=\"string20\"/>");
  4200. schemaText0.append("<xs:element name=\"seq\" type=\"xs:nonNegativeInteger\"/>"); //not add
  4201. schemaText0.append("</xs:sequence></xs:complexType></xs:element>");
  4202. schemaText0.append("</xs:sequence></xs:complexType></xs:element>");
  4203. schemaText0.append("<xs:simpleType name=\"string12\"><xs:restriction base=\"xs:string\"><xs:maxLength value=\"12\"/>");
  4204. schemaText0.append("</xs:restriction></xs:simpleType>");
  4205. schemaText0.append("</xs:schema>");
  4206. #endif
  4207. mergeSchema(file, schemaText, schemaText0, columnsDisplay, columnsDisplayType, columnsHide);
  4208. }
  4209. if (parentName && *parentName)
  4210. {
  4211. for (unsigned i = 0;;i++)
  4212. {
  4213. IRelatedBrowseFile * next = file->queryChild(i);
  4214. if (!next)
  4215. break;
  4216. IViewRelatedFile * viewRelatedFile = next->queryDefinition();
  4217. if (!viewRelatedFile)
  4218. continue;
  4219. IDistributedFile * file = viewRelatedFile->queryDistributedFile();
  4220. if (!file)
  4221. continue;
  4222. const char* logicName0 = file->queryLogicalName();
  4223. if (logicName0 && !strcmp(logicName0, parentName))
  4224. browseRelatedFileSchema(next, NULL, depth+1, schemaText, columnsDisplay, columnsDisplayType, columnsHide);
  4225. }
  4226. }
  4227. }
  4228. return;
  4229. }
  4230. int CWsDfuEx::browseRelatedFileDataSet(double version, IRelatedBrowseFile * file, const char* parentName, unsigned depth, __int64 start, __int64& count, __int64& read,
  4231. StringArray& columnsHide, StringArray& dataSetOutput)
  4232. {
  4233. int iRet = 0;
  4234. int rows = 0;
  4235. try
  4236. {
  4237. //if (file in set of files to display or iterate)
  4238. IResultSetCursor * cursor = file->queryCursor();
  4239. if (cursor->first())
  4240. {
  4241. for(bool ok=cursor->absolute(start);ok;ok=cursor->next())
  4242. {
  4243. if (rows > 200)
  4244. throw MakeStringException(ECLWATCH_TOO_MANY_DATA_ROWS,"Too many data rows selected.");
  4245. StringBuffer text;
  4246. StringBufferAdaptor adaptor2(text);
  4247. cursor->getXmlRow(adaptor2);
  4248. //DBGLOG("Data row returns:%s", text.str());
  4249. #ifdef TESTDATASET
  4250. text.clear();
  4251. if (depth < 1)
  4252. {
  4253. if (rows < 1)
  4254. {
  4255. text.append("<Row><state>AA</state><rtype>ab</rtype><id>abc</id><seq>12</seq></Row>");
  4256. }
  4257. else if (rows < 2)
  4258. {
  4259. text.append("<Row><state>BB</state><rtype>ba</rtype><id>abc</id><seq>13</seq></Row>");
  4260. }
  4261. else if (rows < 3)
  4262. {
  4263. text.append("<Row><state>CC</state><rtype>ca</rtype><id>bcd</id><seq>13</seq></Row>");
  4264. }
  4265. else
  4266. {
  4267. break;
  4268. }
  4269. }
  4270. else
  4271. {
  4272. if (read < 1)
  4273. {
  4274. if (rows > 0)
  4275. break;
  4276. text.append("<Row><date_first_reported>20090511</date_first_reported><msa>6200</msa><sid>abc</sid><seq>12</seq></Row>");
  4277. }
  4278. else if (read < 2)
  4279. {
  4280. if (rows > 0)
  4281. break;
  4282. text.append("<Row><date_first_reported>20090512</date_first_reported><msa>6201</msa><sid>abc</sid><seq>13</seq></Row>");
  4283. }
  4284. else if (read < 3)
  4285. {
  4286. if (rows > 1)
  4287. break;
  4288. else if (rows > 0)
  4289. text.append("<Row><date_first_reported>20090514</date_first_reported><msa>6203</msa><sid>bcd</sid><seq>13</seq></Row>");
  4290. else
  4291. text.append("<Row><date_first_reported>20090513</date_first_reported><msa>6202</msa><sid>bcd</sid><seq>13</seq></Row>");
  4292. }
  4293. }
  4294. rows++;
  4295. #endif
  4296. StringArray dataSetOutput0;
  4297. if (parentName && *parentName)
  4298. {
  4299. for (unsigned i = 0;;i++)
  4300. {
  4301. IRelatedBrowseFile * next = file->queryChild(i);
  4302. if (!next)
  4303. break;
  4304. IViewRelatedFile * viewRelatedFile = next->queryDefinition();
  4305. if (!viewRelatedFile)
  4306. continue;
  4307. IDistributedFile * file = viewRelatedFile->queryDistributedFile();
  4308. if (!file)
  4309. continue;
  4310. const char* logicName0 = file->queryLogicalName();
  4311. if (logicName0 && !strcmp(logicName0, parentName))
  4312. iRet = browseRelatedFileDataSet(version, next, NULL, depth+1, 0, count, read, columnsHide, dataSetOutput0);
  4313. }
  4314. }
  4315. if (dataSetOutput0.length() < 1)
  4316. {
  4317. dataSetOutput.append(text);
  4318. }
  4319. else
  4320. {
  4321. for (unsigned ii = 0; ii<dataSetOutput0.length(); ii++)
  4322. {
  4323. StringBuffer text0;
  4324. StringBuffer text1 = dataSetOutput0.item(ii);
  4325. if (text1.length() > 0)
  4326. {
  4327. mergeDataRow(text0, text, text1, columnsHide);
  4328. //DBGLOG("New row returns:%s", text0.str());
  4329. }
  4330. dataSetOutput.append(text0);
  4331. }
  4332. }
  4333. if (depth < 1)
  4334. {
  4335. read++;
  4336. if(read>=count)
  4337. break;
  4338. }
  4339. }
  4340. if (depth < 1)
  4341. {
  4342. if (count > read)
  4343. count = read;
  4344. }
  4345. }
  4346. }
  4347. catch(IException* e)
  4348. {
  4349. if ((version < 1.08) || (e->errorCode() != FVERR_FilterTooRestrictive))
  4350. throw e;
  4351. e->Release();
  4352. iRet = 1;
  4353. }
  4354. return iRet;
  4355. }
  4356. //sample filterBy: 340020001id1
  4357. //sample data: <XmlSchema name="myschema">...</XmlSchema><Dataset xmlSchema="myschema">...</Dataset>
  4358. int CWsDfuEx::GetIndexData(IEspContext &context, bool bSchemaOnly, const char* indexName, const char* parentName, const char* filterBy, __int64 start,
  4359. __int64& count, __int64& read, __int64& total, StringBuffer& message, StringArray& columnLabels,
  4360. StringArray& columnLabelsType, IArrayOf<IEspDFUData>& DataList, bool webDisableUppercaseTranslation)
  4361. {
  4362. if (!indexName || !*indexName)
  4363. return -1;
  4364. double version = context.getClientVersion();
  4365. StringBuffer username;
  4366. context.getUserID(username);
  4367. const char* passwd = context.queryPassword();
  4368. StringBuffer cluster;
  4369. Owned<IUserDescriptor> userdesc;
  4370. bool disableUppercaseTranslation = false;
  4371. Owned<IDistributedFile> df;
  4372. try
  4373. {
  4374. userdesc.setown(createUserDescriptor());
  4375. userdesc->set(username.str(), passwd);
  4376. df.setown(queryDistributedFileDirectory().lookup(indexName, userdesc));
  4377. if(!df)
  4378. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Could not find file %s.", indexName);
  4379. //Check disableUppercaseTranslation
  4380. StringBuffer mapping;
  4381. df->getColumnMapping(mapping);
  4382. if (mapping.length() > 37 && strstr(mapping.str(), "word{set(stringlib.StringToLowerCase)}"))
  4383. disableUppercaseTranslation = true;
  4384. else if (webDisableUppercaseTranslation)
  4385. disableUppercaseTranslation = webDisableUppercaseTranslation;
  4386. else
  4387. disableUppercaseTranslation = m_disableUppercaseTranslation;
  4388. const char* wuid = df->queryAttributes().queryProp("@workunit");
  4389. if (wuid && *wuid)
  4390. {
  4391. CWUWrapper wu(wuid, context);
  4392. if (wu)
  4393. {
  4394. SCMStringBuffer cluster0;
  4395. cluster.append(wu->getClusterName(cluster0).str());
  4396. }
  4397. }
  4398. }
  4399. catch (IException *e)
  4400. {
  4401. DBGLOG(e);
  4402. e->Release();
  4403. }
  4404. catch(...)
  4405. {
  4406. DBGLOG("Unknown Exception - view data file: %s", indexName);
  4407. }
  4408. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
  4409. Owned<IViewFileWeb> web;
  4410. Owned<IUserDescriptor> udesc;
  4411. ISecUser * secUser = context.queryUser();
  4412. if(secUser && secUser->getName() && *secUser->getName())
  4413. {
  4414. udesc.setown(createUserDescriptor());
  4415. udesc->set(secUser->getName(), secUser->credentials().getPassword());
  4416. }
  4417. if (cluster && *cluster)
  4418. {
  4419. web.setown(createViewFileWeb(*resultSetFactory, cluster, udesc.getLink()));
  4420. }
  4421. else if (m_clusterName.length() > 0)
  4422. {
  4423. web.setown(createViewFileWeb(*resultSetFactory, m_clusterName.str(), udesc.getLink()));
  4424. }
  4425. else
  4426. {
  4427. web.setown(createViewFileWeb(*resultSetFactory, NULL, udesc.getLink()));
  4428. }
  4429. ViewGatherOptions options;
  4430. options.primaryDepth = 100; // we want to traverse secondary->primary, but not the reverse
  4431. options.secondaryDepth = 0;
  4432. options.setPayloadFilter(true); // we're only interested in payload links
  4433. char *indexName0 = (char *) indexName;
  4434. Owned<IFileTreeBrowser> browser;
  4435. try
  4436. {
  4437. web->gatherWeb(indexName0, df, options);
  4438. browser.setown(web->createBrowseTree(indexName0));
  4439. }
  4440. catch(IException* e)
  4441. {
  4442. if ((e->errorCode() != FVERR_CouldNotResolveX) || (indexName[0] != '~'))
  4443. {
  4444. throw e;
  4445. }
  4446. else
  4447. {
  4448. e->Release();
  4449. indexName0 = (char *) (indexName+1);
  4450. web->gatherWeb(indexName0, df, options);
  4451. browser.setown(web->createBrowseTree(indexName0));
  4452. }
  4453. }
  4454. Owned<INewResultSet> result;
  4455. if (cluster && *cluster)
  4456. {
  4457. result.setown(resultSetFactory->createNewFileResultSet(indexName0, cluster));
  4458. }
  4459. else if (m_clusterName.length() > 0)
  4460. {
  4461. result.setown(resultSetFactory->createNewFileResultSet(indexName0, m_clusterName.str()));
  4462. }
  4463. else
  4464. {
  4465. result.setown(resultSetFactory->createNewFileResultSet(indexName0, NULL));
  4466. }
  4467. // Apply the filter to the root node
  4468. if (filterBy && *filterBy)
  4469. {
  4470. //Owned<IFilteredResultSet> filter = result->createFiltered();
  4471. IResultSetFilter* filter = browser->queryRootFilter();
  4472. setRootFilter(result, filterBy, filter, disableUppercaseTranslation);
  4473. ///result.setown(filter->create());
  4474. }
  4475. StringBuffer text, schemaText;
  4476. StringArray columnsHide;
  4477. browseRelatedFileSchema(browser->queryRootFile(), parentName, 0, schemaText, columnLabels, columnLabelsType, columnsHide);
  4478. text.appendf("<XmlSchema name=\"%s\">", SCHEMANAME);
  4479. text.append(schemaText);
  4480. text.append("</XmlSchema>").newline();
  4481. int iRet = 0;
  4482. if (!bSchemaOnly)
  4483. {
  4484. StringArray dataSetOutput;
  4485. iRet = browseRelatedFileDataSet(version, browser->queryRootFile(), parentName, 0, start, count, read, columnsHide, dataSetOutput);
  4486. StringBuffer dataSetText;
  4487. dataSetText.appendf("<Dataset xmlSchema=\"%s\" >", SCHEMANAME);
  4488. dataSetText.newline();
  4489. for (unsigned i = 0; i<dataSetOutput.length(); i++)
  4490. {
  4491. StringBuffer text0 = dataSetOutput.item(i);
  4492. if (text0.length() > 0)
  4493. {
  4494. dataSetText.append(text0);
  4495. dataSetText.newline();
  4496. }
  4497. }
  4498. dataSetText.append("</Dataset>");
  4499. text.append(dataSetText.str());
  4500. }
  4501. MemoryBuffer data;
  4502. struct MemoryBuffer2IStringVal : public CInterface, implements IStringVal
  4503. {
  4504. MemoryBuffer2IStringVal(MemoryBuffer & _buffer) : buffer(_buffer) {}
  4505. IMPLEMENT_IINTERFACE;
  4506. virtual const char * str() const { UNIMPLEMENTED; }
  4507. virtual void set(const char *val) { buffer.append(strlen(val),val); }
  4508. virtual void clear() { } // clearing when appending does nothing
  4509. virtual void setLen(const char *val, unsigned length) { buffer.append(length, val); }
  4510. virtual unsigned length() const { return buffer.length(); };
  4511. MemoryBuffer & buffer;
  4512. } adaptor0(data);
  4513. adaptor0.set(text.str());
  4514. data.append(0);
  4515. total=result->getNumRows();
  4516. Owned<IEspDFUData> item = createDFUData("","");
  4517. item->setName(indexName);
  4518. item->setNumRows(total);
  4519. item->setData(data.toByteArray());
  4520. DataList.append(*item.getClear());
  4521. return iRet;
  4522. }
  4523. //////////////////////HPCC Browser//////////////////////////