ws_dfuService.cpp 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106
  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) || filesCannotBeDeleted.contains(filename))
  1024. continue;
  1025. }
  1026. try
  1027. {
  1028. IDistributedFileDirectory &fdir = queryDistributedFileDirectory();
  1029. {
  1030. Owned<IDistributedFile> df = fdir.lookup(filename, userdesc, true);
  1031. if(!df)
  1032. {
  1033. returnStr.appendf("<Message><Value>Cannot delete %s: file not found</Value></Message>", filename);
  1034. filesCannotBeDeleted.append(filename);
  1035. continue;
  1036. }
  1037. if (0==j) // skip non-super files on 1st pass
  1038. {
  1039. if(!df->querySuperFile())
  1040. continue;
  1041. superFileNames.append(filename);
  1042. }
  1043. }
  1044. if (!fdir.removeEntry(filename, userdesc, NULL, REMOVE_FILE_SDS_CONNECT_TIMEOUT))
  1045. returnStr.appendf("<Message><Value>Failed to delete %s</Value></Message>", filename);
  1046. else
  1047. {
  1048. PROGLOG("Deleted Logical File: %s by: %s\n",filename, username.str());
  1049. returnStr.appendf("<Message><Value>Deleted File %s</Value></Message>", filename);
  1050. }
  1051. }
  1052. catch(IException* e)
  1053. {
  1054. filesCannotBeDeleted.append(filename);
  1055. StringBuffer emsg;
  1056. e->errorMessage(emsg);
  1057. if((e->errorCode() == DFSERR_CreateAccessDenied) && (req.getType() != NULL))
  1058. emsg.replaceString("Create ", "Delete ");
  1059. returnStr.appendf("<Message><Value>Cannot delete %s: %s</Value></Message>", filename, emsg.str());
  1060. }
  1061. catch(...)
  1062. {
  1063. returnStr.appendf("<Message><Value>Cannot delete %s: unknown exception.</Value></Message>", filename);
  1064. }
  1065. }
  1066. }
  1067. resp.setDFUArrayActionResult(returnStr.str());
  1068. return true;
  1069. }
  1070. bool CWsDfuEx::onDFUArrayAction(IEspContext &context, IEspDFUArrayActionRequest &req, IEspDFUArrayActionResponse &resp)
  1071. {
  1072. try
  1073. {
  1074. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Write, false))
  1075. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to update Logical Files. Permission denied.");
  1076. double version = context.getClientVersion();
  1077. if (version > 1.03)
  1078. {
  1079. StringBuffer backToPage = req.getBackToPage();
  1080. if (backToPage.length() > 0)
  1081. {
  1082. const char* oldStr = "&";
  1083. const char* newStr = "&amp;";
  1084. backToPage.replaceString(oldStr, newStr);
  1085. resp.setBackToPage(backToPage.str());
  1086. }
  1087. }
  1088. if (strcmp(req.getType(), Action_Delete) == 0)
  1089. {
  1090. /*StringArray roxieQueries;
  1091. checkRoxieQueryFilesOnDelete(req, roxieQueries);
  1092. if (roxieQueries.length() > 0)
  1093. {
  1094. returnStr.append("<Message><Value>Cannot delete the files because of the following roxie queries: ");
  1095. for(int i = 0; i < roxieQueries.length();i++)
  1096. {
  1097. const char* query = roxieQueries.item(i);
  1098. if(!query || !*query)
  1099. continue;
  1100. if (i==0)
  1101. returnStr.append(query);
  1102. else
  1103. returnStr.appendf(",%s", query);
  1104. }
  1105. returnStr.append("</Value></Message>");
  1106. resp.setDFUArrayActionResult(returnStr.str());
  1107. return true;
  1108. }*/
  1109. return DFUDeleteFiles(context, req, resp);
  1110. }
  1111. StringBuffer username;
  1112. context.getUserID(username);
  1113. Owned<IUserDescriptor> userdesc;
  1114. if(username.length() > 0)
  1115. {
  1116. const char* passwd = context.queryPassword();
  1117. userdesc.setown(createUserDescriptor());
  1118. userdesc->set(username.str(), passwd);
  1119. }
  1120. StringBuffer returnStr;
  1121. for(unsigned i = 0; i < req.getLogicalFiles().length();i++)
  1122. {
  1123. const char* file = req.getLogicalFiles().item(i);
  1124. if(!file || !*file)
  1125. continue;
  1126. unsigned len = strlen(file);
  1127. char* curfile = new char[len+1];
  1128. const char* cluster = NULL;
  1129. const char *pCh = strchr(file, '@');
  1130. if (pCh)
  1131. {
  1132. len = pCh - file;
  1133. if (len+1 < strlen(file))
  1134. cluster = pCh + 1;
  1135. }
  1136. strncpy(curfile, file, len);
  1137. curfile[len] = 0;
  1138. DBGLOG("CWsDfuEx::onDFUArrayAction User=%s Action=%s File=%s",username.str(),req.getType(), file);
  1139. try
  1140. {
  1141. onDFUAction(userdesc.get(), curfile, cluster, req.getType(), returnStr);
  1142. }
  1143. catch(IException* e)
  1144. {
  1145. StringBuffer emsg;
  1146. e->errorMessage(emsg);
  1147. if((e->errorCode() == DFSERR_CreateAccessDenied) && (req.getType() != NULL))
  1148. {
  1149. if (strcmp(req.getType(), Action_AddtoSuperfile) == 0)
  1150. {
  1151. emsg.replaceString("Create ", "AddtoSuperfile ");
  1152. }
  1153. }
  1154. returnStr.appendf("<Message><Value>%s</Value></Message>", emsg.str());
  1155. }
  1156. catch(...)
  1157. {
  1158. returnStr.appendf("<Message><Value>Unknown exception onDFUArrayAction %s</Value></Message>", curfile);
  1159. }
  1160. delete [] curfile;
  1161. }
  1162. if (strcmp(Action_AddtoSuperfile ,req.getType()) == 0)
  1163. {
  1164. returnStr.replaceString("#", "%23");
  1165. if (version < 1.18)
  1166. resp.setRedirectUrl(StringBuffer("/WsDFU/AddtoSuperfile?Subfiles=").append(returnStr.str()));
  1167. else
  1168. {
  1169. resp.setRedirectTo(returnStr.str());
  1170. }
  1171. }
  1172. else
  1173. {
  1174. resp.setDFUArrayActionResult(returnStr.str());
  1175. }
  1176. }
  1177. catch(IException* e)
  1178. {
  1179. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1180. }
  1181. return true;
  1182. }
  1183. bool CWsDfuEx::onDFUAction(IUserDescriptor* udesc, const char* LogicalFileName, const char* ClusterName, const char* ActionType, StringBuffer& returnStr)
  1184. {
  1185. //No 'try/catch' is needed for this method since it will be called internally.
  1186. if (strcmp(Action_Delete ,ActionType) == 0)
  1187. {
  1188. LogicFileWrapper Logicfile;
  1189. if (!Logicfile.doDeleteFile(LogicalFileName,ClusterName, returnStr, udesc))
  1190. return false;
  1191. }
  1192. else if (strcmp(Action_AddtoSuperfile ,ActionType) == 0)
  1193. {
  1194. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(LogicalFileName, udesc, true);
  1195. if (df)
  1196. {
  1197. if (returnStr.length() > 0)
  1198. returnStr.appendf(",%s", LogicalFileName);
  1199. else
  1200. returnStr.appendf("%s", LogicalFileName);
  1201. return false;
  1202. }
  1203. }
  1204. else
  1205. DBGLOG("Unknown Action type:%s\n",ActionType);
  1206. return true;
  1207. }
  1208. bool CWsDfuEx::onDFUDefFile(IEspContext &context,IEspDFUDefFileRequest &req, IEspDFUDefFileResponse &resp)
  1209. {
  1210. try
  1211. {
  1212. DBGLOG("CWsDfuEx::onDFUDefFile\n");
  1213. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  1214. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to access DFUDefFile. Permission denied.");
  1215. StringBuffer username;
  1216. context.getUserID(username);
  1217. DBGLOG("CWsDfuEx::onDFUDefFile User=%s",username.str());
  1218. StringBuffer rawStr,returnStr;
  1219. Owned<IUserDescriptor> userdesc;
  1220. if(username.length() > 0)
  1221. {
  1222. const char* passwd = context.queryPassword();
  1223. userdesc.setown(createUserDescriptor());
  1224. userdesc->set(username.str(), passwd);
  1225. }
  1226. getDefFile(userdesc.get(), req.getName(),rawStr);
  1227. StringBuffer xsltFile;
  1228. xsltFile.append(getCFD()).append("smc_xslt/").append(req.getFormat()).append("_def_file.xslt");
  1229. xsltTransformer(xsltFile.str(),rawStr,returnStr);
  1230. //set the file
  1231. MemoryBuffer buff;
  1232. buff.setBuffer(returnStr.length(), (void*)returnStr.toCharArray());
  1233. resp.setDefFile(buff);
  1234. //set the type
  1235. StringBuffer type;
  1236. const char* format = req.getFormat();
  1237. if (!stricmp(format, "def"))
  1238. format = "plain";
  1239. type.append("text/").append(format);
  1240. resp.setDefFile_mimetype(type.str());
  1241. }
  1242. catch(IException* e)
  1243. {
  1244. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1245. }
  1246. return true;
  1247. }
  1248. void CWsDfuEx::xsltTransformer(const char* xsltPath,StringBuffer& source,StringBuffer& returnStr)
  1249. {
  1250. if (m_xsl.get() == 0)
  1251. {
  1252. m_xsl.setown(getXslProcessor());
  1253. }
  1254. Owned<IXslTransform> xform = m_xsl->createXslTransform();
  1255. xform->loadXslFromFile(xsltPath);
  1256. xform->setXmlSource(source.str(), source.length()+1);
  1257. xform->transform(returnStr.clear());
  1258. }
  1259. void CWsDfuEx::getDefFile(IUserDescriptor* udesc, const char* FileName,StringBuffer& returnStr)
  1260. {
  1261. DBGLOG("CWsDfuEx::getDefFile\n");
  1262. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(FileName, udesc);
  1263. if(!df)
  1264. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find file %s.",FileName);
  1265. if(!df->queryAttributes().hasProp("ECL"))
  1266. throw MakeStringException(ECLWATCH_MISSING_PARAMS,"No record definition for file %s.",FileName);
  1267. StringBuffer text;
  1268. text.append(df->queryAttributes().queryProp("ECL"));
  1269. MultiErrorReceiver errs;
  1270. OwnedHqlExpr record = parseQuery(text.str(), &errs);
  1271. if (errs.errCount())
  1272. {
  1273. StringBuffer errtext;
  1274. IECLError *first = errs.firstError();
  1275. first->toString(errtext);
  1276. throw MakeStringException(ECLWATCH_CANNOT_PARSE_ECL_QUERY, "Failed in parsing ECL query: %s @ %d:%d.", errtext.str(), first->getColumn(), first->getLine());
  1277. }
  1278. if(!record)
  1279. throw MakeStringException(ECLWATCH_CANNOT_PARSE_ECL_QUERY, "Failed in parsing ECL query.");
  1280. Owned<IPropertyTree> data = createPTree("Table", ipt_caseInsensitive);
  1281. exportData(data, record);
  1282. const char* fname=strrchr(FileName,':');
  1283. data->setProp("filename",fname ? fname+1 : FileName);
  1284. toXML(data, returnStr, 0, 0);
  1285. }
  1286. bool CWsDfuEx::checkFileContent(IEspContext &context, IUserDescriptor* udesc, const char * logicalName, const char * cluster)
  1287. {
  1288. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName, udesc);
  1289. if (!df)
  1290. return false;
  1291. if (!cluster || !stricmp(cluster, ""))
  1292. {
  1293. char *eclCluster = NULL;
  1294. const char* wuid = df->queryAttributes().queryProp("@workunit");
  1295. if (wuid && *wuid)
  1296. {
  1297. try
  1298. {
  1299. Owned<IWorkUnitFactory> factory = (context.querySecManager() ? getSecWorkUnitFactory(*context.querySecManager(), *context.queryUser()) : getWorkUnitFactory());
  1300. if (factory)
  1301. {
  1302. IConstWorkUnit* wu = factory->openWorkUnit(wuid, false);
  1303. if (wu)
  1304. {
  1305. SCMStringBuffer cluster;
  1306. eclCluster = (char *)wu->getClusterName(cluster).str();
  1307. }
  1308. }
  1309. }
  1310. catch(...)
  1311. {
  1312. return false;
  1313. }
  1314. }
  1315. if (!eclCluster || !stricmp(eclCluster, ""))
  1316. return false;
  1317. }
  1318. bool blocked;
  1319. if (df->isCompressed(&blocked) && !blocked)
  1320. return false;
  1321. IPropertyTree & properties = df->queryAttributes();
  1322. const char * format = properties.queryProp("@format");
  1323. if (format && (stricmp(format,"csv")==0 || memicmp(format, "utf", 3) == 0))
  1324. {
  1325. return true;
  1326. }
  1327. const char * recordEcl = properties.queryProp("ECL");
  1328. if (!recordEcl)
  1329. return false;
  1330. MultiErrorReceiver errs;
  1331. Owned< IHqlExpression> ret = ::parseQuery(recordEcl, &errs);
  1332. return errs.errCount() == 0;
  1333. }
  1334. bool FindInStringArray(StringArray& clusters, const char *cluster)
  1335. {
  1336. bool bFound = false;
  1337. if(cluster && *cluster)
  1338. {
  1339. if (clusters.ordinality())
  1340. {
  1341. ForEachItemIn(i, clusters)
  1342. {
  1343. const char* cluster0 = clusters.item(i);
  1344. if(cluster0 && *cluster0 && !stricmp(cluster, cluster0))
  1345. return true;
  1346. }
  1347. }
  1348. }
  1349. else
  1350. {
  1351. #if 0 //Comment out since clusters are not set for some old files
  1352. if (!clusters.ordinality())
  1353. return true;
  1354. ForEachItemIn(i, clusters)
  1355. {
  1356. const char* cluster0 = clusters.item(i);
  1357. if(cluster0 && !*cluster0)
  1358. {
  1359. return true;
  1360. }
  1361. }
  1362. #else
  1363. return true;
  1364. #endif
  1365. }
  1366. return bFound;
  1367. }
  1368. static void getFilePermission(const CDfsLogicalFileName &dlfn, ISecUser* user, IUserDescriptor* udesc, ISecManager* secmgr, int& permission)
  1369. {
  1370. if (dlfn.isMulti())
  1371. {
  1372. unsigned i = dlfn.multiOrdinality();
  1373. while (i--)
  1374. {
  1375. getFilePermission(dlfn.multiItem(i), user, udesc, secmgr, permission);
  1376. }
  1377. }
  1378. else
  1379. {
  1380. int permissionTemp;
  1381. if (dlfn.isForeign())
  1382. {
  1383. permissionTemp = queryDistributedFileDirectory().getFilePermissions(dlfn.get(), udesc);
  1384. }
  1385. else
  1386. {
  1387. StringBuffer scopes;
  1388. dlfn.getScopes(scopes);
  1389. permissionTemp = secmgr->authorizeFileScope(*user, scopes.str());
  1390. }
  1391. //Descrease the permission whenever a component has a lower permission.
  1392. if (permissionTemp < permission)
  1393. permission = permissionTemp;
  1394. }
  1395. return;
  1396. }
  1397. bool CWsDfuEx::getUserFilePermission(IEspContext &context, IUserDescriptor* udesc, const char* logicalName, int& permission)
  1398. {
  1399. ISecManager* secmgr = context.querySecManager();
  1400. if (!secmgr)
  1401. {
  1402. return false;
  1403. }
  1404. StringBuffer username;
  1405. StringBuffer password;
  1406. udesc->getUserName(username);
  1407. if (username.length() < 1)
  1408. {
  1409. DBGLOG("User Name not defined\n");
  1410. return false;
  1411. }
  1412. udesc->getPassword(password);
  1413. Owned<ISecUser> user = secmgr->createUser(username);
  1414. if (!user)
  1415. {
  1416. DBGLOG("User %s not found\n", username.str());
  1417. return false;
  1418. }
  1419. if (password.length() > 0)
  1420. user->credentials().setPassword(password);
  1421. CDfsLogicalFileName dlfn;
  1422. dlfn.set(logicalName);
  1423. //Start from the SecAccess_Full. Descrease the permission whenever a component has a lower permission.
  1424. permission = SecAccess_Full;
  1425. getFilePermission(dlfn, user, udesc, secmgr, permission);
  1426. return true;
  1427. }
  1428. void CWsDfuEx::doGetFileDetails(IEspContext &context, IUserDescriptor* udesc, const char *name, const char *cluster,
  1429. const char *description,IEspDFUFileDetail& FileDetails)
  1430. {
  1431. DBGLOG("CWsDfuEx::doGetFileDetails\n");
  1432. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(name, udesc);
  1433. if(!df)
  1434. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find file %s.",name);
  1435. StringArray clusters;
  1436. if (cluster && *cluster)
  1437. {
  1438. df->getClusterNames(clusters);
  1439. if(!FindInStringArray(clusters, cluster))
  1440. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find file %s.",name);
  1441. }
  1442. double version = context.getClientVersion();
  1443. offset_t size=queryDistributedFileSystem().getSize(df), recordSize=df->queryAttributes().getPropInt64("@recordSize",0);
  1444. CDateTime dt;
  1445. df->getModificationTime(dt);
  1446. const char* lname=df->queryLogicalName(), *fname=strrchr(lname,':');
  1447. FileDetails.setName(lname);
  1448. FileDetails.setFilename(fname ? fname+1 : lname);
  1449. FileDetails.setDir(df->queryDefaultDir());
  1450. FileDetails.setPathMask(df->queryPartMask());
  1451. StringBuffer strDesc = df->queryAttributes().queryProp("@description");
  1452. if (description)
  1453. {
  1454. DistributedFilePropertyLock lock(df);
  1455. lock.queryAttributes().setProp("@description",description);
  1456. strDesc = description;
  1457. }
  1458. FileDetails.setDescription(strDesc);
  1459. comma c1(size);
  1460. StringBuffer tmpstr;
  1461. tmpstr<<c1;
  1462. FileDetails.setFilesize(tmpstr.str());
  1463. if(df->isCompressed())
  1464. {
  1465. FileDetails.setZipFile(true);
  1466. }
  1467. comma c2(recordSize);
  1468. tmpstr.clear();
  1469. tmpstr<<c2;
  1470. FileDetails.setRecordSize(tmpstr.str());
  1471. tmpstr.clear();
  1472. if (df->queryAttributes().hasProp("@recordCount"))
  1473. {
  1474. comma c3(df->queryAttributes().getPropInt64("@recordCount"));
  1475. tmpstr<<c3;
  1476. }
  1477. else if (recordSize)
  1478. {
  1479. comma c3(size/recordSize);
  1480. tmpstr<<c3;
  1481. }
  1482. FileDetails.setRecordCount(tmpstr.str());
  1483. FileDetails.setOwner(df->queryAttributes().queryProp("@owner"));
  1484. FileDetails.setJobName(df->queryAttributes().queryProp("@job"));
  1485. //#14280
  1486. IDistributedSuperFile *sf = df->querySuperFile();
  1487. if(sf)
  1488. {
  1489. StringArray farray;
  1490. Owned<IDistributedFileIterator> iter=sf->getSubFileIterator();
  1491. ForEach(*iter)
  1492. {
  1493. StringBuffer subfileName;
  1494. iter->getName(subfileName);
  1495. farray.append(subfileName.str());
  1496. }
  1497. if(farray.length() > 0)
  1498. {
  1499. FileDetails.setSubfiles(farray);
  1500. }
  1501. FileDetails.setIsSuperfile(true);
  1502. return;
  1503. }
  1504. //#14280
  1505. FileDetails.setWuid(df->queryAttributes().queryProp("@workunit"));
  1506. //#17430
  1507. {
  1508. IArrayOf<IEspDFULogicalFile> LogicalFiles;
  1509. Owned<IDistributedSuperFileIterator> iter = df->getOwningSuperFiles();
  1510. if(iter.get() != NULL)
  1511. {
  1512. ForEach(*iter)
  1513. {
  1514. //printf("%s,%s\n",iter->query().queryLogicalName(),lname);
  1515. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  1516. File->setName(iter->queryName());
  1517. LogicalFiles.append(*File.getClear());
  1518. }
  1519. }
  1520. if(LogicalFiles.length() > 0)
  1521. {
  1522. FileDetails.setSuperfiles(LogicalFiles);
  1523. }
  1524. }
  1525. //#17430
  1526. //new (optional) attribute on a logical file (@persistent)
  1527. //indicates the ESP page that shows the details of a file. It indicates
  1528. //whether the file was created with a PERSIST() ecl attribute.
  1529. FileDetails.setPersistent(df->queryAttributes().queryProp("@persistent"));
  1530. //@format - what format the file is (if not fixed with)
  1531. FileDetails.setFormat(df->queryAttributes().queryProp("@format"));
  1532. //@maxRecordSize - what the maximum length of records is
  1533. FileDetails.setMaxRecordSize(df->queryAttributes().queryProp("@maxRecordSize"));
  1534. //@csvSeparate - separators between fields for a CSV/utf file
  1535. FileDetails.setCsvSeparate(df->queryAttributes().queryProp("@csvSeparate"));
  1536. //@csvQuote - character used to quote fields for a csv/utf file.
  1537. FileDetails.setCsvQuote(df->queryAttributes().queryProp("@csvQuote"));
  1538. //@csvTerminate - characters used to terminate a record in a csv.utf file
  1539. FileDetails.setCsvTerminate(df->queryAttributes().queryProp("@csvTerminate"));
  1540. //@csvEscape - character used to define escape for a csv/utf file.
  1541. if (version >= 1.20)
  1542. FileDetails.setCsvEscape(df->queryAttributes().queryProp("@csvEscape"));
  1543. //Time and date of the file
  1544. tmpstr.clear();
  1545. dt.getDateString(tmpstr);
  1546. tmpstr.append(" ");
  1547. dt.getTimeString(tmpstr);
  1548. FileDetails.setModified(tmpstr.str());
  1549. if(df->queryAttributes().hasProp("ECL"))
  1550. FileDetails.setEcl(df->queryAttributes().queryProp("ECL"));
  1551. StringBuffer clusterStr;
  1552. if ((!cluster || !*cluster) && clusters.ordinality())
  1553. {
  1554. clusterStr.append(clusters.item(0));
  1555. }
  1556. else if (cluster && *cluster)
  1557. {
  1558. clusterStr.append(cluster);
  1559. }
  1560. if (clusterStr.length() > 0)
  1561. {
  1562. FileDetails.setCluster(clusterStr.str());
  1563. if (!checkFileContent(context, udesc, name, clusterStr.str()))
  1564. FileDetails.setShowFileContent(false);
  1565. if (version > 1.05)
  1566. {
  1567. FileDetails.setFromRoxieCluster(false);
  1568. StringArray roxieClusterNames;
  1569. IArrayOf<IEspTpCluster> roxieclusters;
  1570. CTpWrapper dummy;
  1571. dummy.getClusterProcessList(eqRoxieCluster, roxieclusters);
  1572. ForEachItemIn(k, roxieclusters)
  1573. {
  1574. IEspTpCluster& r_cluster = roxieclusters.item(k);
  1575. StringBuffer sName = r_cluster.getName();
  1576. if (!stricmp(sName.str(), clusterStr.str()))
  1577. {
  1578. FileDetails.setFromRoxieCluster(true);
  1579. break;
  1580. }
  1581. }
  1582. }
  1583. }
  1584. IArrayOf<IConstDFUPart>& PartList = FileDetails.getDFUFileParts();
  1585. Owned<IDistributedFilePartIterator> pi = df->getIterator();
  1586. offset_t mn=LLC(0x7fffffffffffffff), mx=0, sum=0, count=0;
  1587. ForEach(*pi)
  1588. {
  1589. Owned<IDistributedFilePart> part = &pi->get();
  1590. for (unsigned int i=0; i<part->numCopies(); i++)
  1591. {
  1592. Owned<IEspDFUPart> FilePart = createDFUPart("","");
  1593. StringBuffer b;
  1594. part->queryNode(i)->endpoint().getUrlStr(b);
  1595. FilePart->setId(part->getPartIndex()+1);
  1596. FilePart->setCopy(i+1);
  1597. FilePart->setIp(b.str());
  1598. FilePart->setPartsize("<N/A>");
  1599. try
  1600. {
  1601. offset_t size=queryDistributedFileSystem().getSize(part);
  1602. comma c4(size);
  1603. tmpstr.clear();
  1604. tmpstr<<c4;
  1605. FilePart->setPartsize(tmpstr.str());
  1606. if(size!=-1)
  1607. {
  1608. count+=1;
  1609. sum+=size;
  1610. if(size>mx) mx=size;
  1611. if(size<mn) mn=size;
  1612. }
  1613. }
  1614. catch(IException *e)
  1615. {
  1616. StringBuffer msg;
  1617. ERRLOG("Exception %d:%s in WS_DFU queryDistributedFileSystem().getSize()", e->errorCode(), e->errorMessage(msg).str());
  1618. e->Release();
  1619. }
  1620. catch(...)
  1621. {
  1622. ERRLOG("Unknown exception in WS_DFU queryDistributedFileSystem().getSize()");
  1623. }
  1624. PartList.append(*FilePart.getClear());
  1625. }
  1626. }
  1627. if(count)
  1628. {
  1629. IEspDFUFileStat& Stat = FileDetails.updateStat();
  1630. offset_t avg=sum/count;
  1631. comma c5(avg-mn);
  1632. tmpstr.clear();
  1633. tmpstr<<c5;
  1634. Stat.setMinSkew(tmpstr.str());
  1635. comma c6(mx-avg);
  1636. tmpstr.clear();
  1637. tmpstr<<c6;
  1638. Stat.setMaxSkew(tmpstr.str());
  1639. }
  1640. if (version > 1.06)
  1641. {
  1642. const char *wuid = df->queryAttributes().queryProp("@workunit");
  1643. if (wuid && *wuid && (wuid[0]=='W'))
  1644. {
  1645. try
  1646. {
  1647. CWUWrapper wu(wuid, context);
  1648. StringArray graphs;
  1649. Owned<IPropertyTreeIterator> f=&wu->getFileIterator();
  1650. ForEach(*f)
  1651. {
  1652. IPropertyTree &query = f->query();
  1653. const char *fileName = query.queryProp("@name");
  1654. const char *graphName = query.queryProp("@graph");
  1655. if (!fileName || !graphName || !*graphName || stricmp(fileName, name))
  1656. continue;
  1657. graphs.append(graphName);
  1658. }
  1659. FileDetails.setGraphs(graphs);
  1660. }
  1661. catch(...)
  1662. {
  1663. DBGLOG("Failed in retrieving graphs from workunit %s", wuid);
  1664. }
  1665. }
  1666. }
  1667. if (version > 1.08 && udesc)
  1668. {
  1669. int permission;
  1670. if (getUserFilePermission(context, udesc, name, permission))
  1671. {
  1672. switch (permission)
  1673. {
  1674. case SecAccess_Full:
  1675. FileDetails.setUserPermission("Full Access Permission");
  1676. break;
  1677. case SecAccess_Write:
  1678. FileDetails.setUserPermission("Write Access Permission");
  1679. break;
  1680. case SecAccess_Read:
  1681. FileDetails.setUserPermission("Read Access Permission");
  1682. break;
  1683. case SecAccess_Access:
  1684. FileDetails.setUserPermission("Access Permission");
  1685. break;
  1686. case SecAccess_None:
  1687. FileDetails.setUserPermission("None Access Permission");
  1688. break;
  1689. default:
  1690. FileDetails.setUserPermission("Permission Unknown");
  1691. break;
  1692. }
  1693. }
  1694. }
  1695. }
  1696. void CWsDfuEx::getLogicalFileAndDirectory(IUserDescriptor* udesc, const char *dirname, IArrayOf<IEspDFULogicalFile>& LogicalFiles, int& numFiles, int& numDirs)
  1697. {
  1698. DBGLOG("CWsDfuEx::getLogicalFileAndDirectory\n");
  1699. StringArray roxieClusterNames;
  1700. IArrayOf<IEspTpCluster> roxieclusters;
  1701. CTpWrapper dummy;
  1702. dummy.getClusterProcessList(eqRoxieCluster, roxieclusters);
  1703. ForEachItemIn(k, roxieclusters)
  1704. {
  1705. IEspTpCluster& cluster = roxieclusters.item(k);
  1706. StringBuffer sName = cluster.getName();
  1707. roxieClusterNames.append(sName.str());
  1708. }
  1709. numFiles = 0;
  1710. numDirs = 0;
  1711. if (dirname && *dirname)
  1712. {
  1713. StringBuffer filter;
  1714. filter.append(dirname);
  1715. filter.append("::*");
  1716. Owned<IDFAttributesIterator> fi = queryDistributedFileDirectory().getDFAttributesIterator(filter.toLowerCase().str(), udesc, false,true, NULL);
  1717. if(fi)
  1718. {
  1719. StringBuffer size;
  1720. ForEach(*fi)
  1721. {
  1722. StringBuffer pref;
  1723. IPropertyTree &attr=fi->query();
  1724. const char* logicalName=attr.queryProp("@name");
  1725. const char *c=strstr(logicalName, "::");
  1726. if (c)
  1727. pref.append(c-logicalName, logicalName);
  1728. else
  1729. pref.append(logicalName);
  1730. const char* owner=attr.queryProp("@owner");
  1731. #if 0
  1732. char* clusterName=(char*)attr.queryProp("@group");
  1733. #else //Handling for multiple clusters
  1734. StringArray clusters;
  1735. if (getFileGroups(&attr,clusters)==0)
  1736. {
  1737. clusters.append("");
  1738. }
  1739. #endif
  1740. ForEachItemIn(i, clusters)
  1741. {
  1742. const char* clusterName = clusters.item(i);
  1743. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  1744. File->setPrefix(pref);
  1745. File->setClusterName(clusterName);
  1746. File->setName(logicalName);
  1747. File->setOwner(owner);
  1748. File->setReplicate(true);
  1749. ForEachItemIn(j, roxieClusterNames)
  1750. {
  1751. const char* roxieClusterName = roxieClusterNames.item(j);
  1752. if (roxieClusterName && clusterName && !stricmp(roxieClusterName, clusterName))
  1753. {
  1754. File->setFromRoxieCluster(true);
  1755. break;
  1756. }
  1757. }
  1758. if(!attr.hasProp("@numsubfiles"))
  1759. {
  1760. File->setDirectory(attr.queryProp("@directory"));
  1761. File->setParts(attr.queryProp("@numparts"));
  1762. File->setIsSuperfile(false);
  1763. }
  1764. else
  1765. {
  1766. File->setIsSuperfile(true);
  1767. }
  1768. int numSubFiles = attr.hasProp("@numsubfiles");
  1769. if (numSubFiles > 1) //Bug 41379 - ViewKeyFile Cannot handle superfile with multiple subfiles
  1770. File->setBrowseData(false);
  1771. else
  1772. File->setBrowseData(true);
  1773. StringBuffer modf(attr.queryProp("@modified"));
  1774. char* t= (char *) strchr(modf.str(),'T');
  1775. if(t) *t=' ';
  1776. File->setModified(modf.str());
  1777. __int64 recordSize=attr.getPropInt64("@recordSize",0), size=attr.getPropInt64("@size",-1);
  1778. if(!isCompressed(attr))
  1779. {
  1780. File->setIsZipfile(false);
  1781. }
  1782. else
  1783. {
  1784. File->setIsZipfile(true);
  1785. }
  1786. StringBuffer buf;
  1787. buf << comma(size);
  1788. File->setIntSize(size);
  1789. File->setTotalsize(buf.str());
  1790. if (attr.hasProp("@recordCount"))
  1791. {
  1792. File->setRecordCount((buf.clear()<<comma(attr.getPropInt64("@recordCount"))).str());
  1793. File->setIntRecordCount(attr.getPropInt64("@recordCount"));
  1794. }
  1795. else if(recordSize)
  1796. {
  1797. File->setRecordCount((buf.clear()<<comma(size/recordSize)).str());
  1798. File->setIntRecordCount(size/recordSize);
  1799. }
  1800. LogicalFiles.append(*File.getClear());
  1801. numFiles++;
  1802. }
  1803. }
  1804. }
  1805. }
  1806. Owned<IDFScopeIterator> iter = queryDistributedFileDirectory().getScopeIterator(udesc,dirname,false);
  1807. if(iter)
  1808. {
  1809. ForEach(*iter)
  1810. {
  1811. const char *scope = iter->query();
  1812. if (scope && *scope)
  1813. {
  1814. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  1815. File->setDirectory(scope);
  1816. File->setIsDirectory(true);
  1817. LogicalFiles.append(*File.getClear());
  1818. numDirs++;
  1819. }
  1820. }
  1821. }
  1822. }
  1823. bool CWsDfuEx::onDFUFileView(IEspContext &context, IEspDFUFileViewRequest &req, IEspDFUFileViewResponse &resp)
  1824. {
  1825. try
  1826. {
  1827. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  1828. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to Browse Files by Scope. Permission denied.");
  1829. Owned<IUserDescriptor> userdesc;
  1830. StringBuffer username;
  1831. context.getUserID(username);
  1832. DBGLOG("CWsDfuEx::onDFUFileView User=%s",username.str());
  1833. if(username.length() > 0)
  1834. {
  1835. const char* passwd = context.queryPassword();
  1836. userdesc.setown(createUserDescriptor());
  1837. userdesc->set(username.str(), passwd);
  1838. }
  1839. int numDirs = 0;
  1840. int numFiles = 0;
  1841. IArrayOf<IEspDFULogicalFile> logicalFiles;
  1842. getLogicalFileAndDirectory(userdesc.get(), req.getScope(), logicalFiles, numFiles, numDirs);
  1843. if (numFiles > 0)
  1844. resp.setNumFiles(numFiles);
  1845. if (req.getScope() && *req.getScope())
  1846. resp.setScope(req.getScope());
  1847. else
  1848. resp.setScope("");
  1849. resp.setDFULogicalFiles(logicalFiles);
  1850. }
  1851. catch(IException* e)
  1852. {
  1853. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1854. }
  1855. return true;
  1856. }
  1857. __int64 CWsDfuEx::findPositionBySize(const __int64 size, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1858. {
  1859. __int64 addToPos = -1;
  1860. ForEachItemIn(i, LogicalFiles)
  1861. {
  1862. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1863. const char* sSize = File.getLongSize();
  1864. __int64 nSize = atoi64_l(sSize,strlen(sSize));
  1865. if (descend && size > nSize)
  1866. {
  1867. addToPos = i;
  1868. break;
  1869. }
  1870. if (!descend && size < nSize)
  1871. {
  1872. addToPos = i;
  1873. break;
  1874. }
  1875. }
  1876. return addToPos;
  1877. }
  1878. __int64 CWsDfuEx::findPositionByParts(const __int64 parts, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1879. {
  1880. __int64 addToPos = -1;
  1881. ForEachItemIn(i, LogicalFiles)
  1882. {
  1883. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1884. const char* sParts = File.getParts();
  1885. __int64 nParts = atoi64_l(sParts,strlen(sParts));
  1886. if (descend && parts > nParts)
  1887. {
  1888. addToPos = i;
  1889. break;
  1890. }
  1891. if (!descend && parts < nParts)
  1892. {
  1893. addToPos = i;
  1894. break;
  1895. }
  1896. }
  1897. return addToPos;
  1898. }
  1899. __int64 CWsDfuEx::findPositionByRecords(const __int64 records, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1900. {
  1901. __int64 addToPos = -1;
  1902. ForEachItemIn(i, LogicalFiles)
  1903. {
  1904. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1905. const char* sRecords = File.getLongRecordCount();
  1906. __int64 nRecords = atoi64_l(sRecords,strlen(sRecords));
  1907. if (descend && records > nRecords)
  1908. {
  1909. addToPos = i;
  1910. break;
  1911. }
  1912. if (!descend && records < nRecords)
  1913. {
  1914. addToPos = i;
  1915. break;
  1916. }
  1917. }
  1918. return addToPos;
  1919. }
  1920. __int64 CWsDfuEx::findPositionByName(const char *name, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1921. {
  1922. if (!name || (strlen(name) < 1))
  1923. {
  1924. if (descend)
  1925. return -1;
  1926. else
  1927. return 0;
  1928. }
  1929. __int64 addToPos = -1;
  1930. ForEachItemIn(i, LogicalFiles)
  1931. {
  1932. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1933. const char *Name = File.getName();
  1934. if (!Name)
  1935. continue;
  1936. if (descend && strcmp(name, Name)>0)
  1937. {
  1938. addToPos = i;
  1939. break;
  1940. }
  1941. if (!descend && strcmp(name, Name)<0)
  1942. {
  1943. addToPos = i;
  1944. break;
  1945. }
  1946. }
  1947. return addToPos;
  1948. }
  1949. __int64 CWsDfuEx::findPositionByCluster(const char *cluster, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1950. {
  1951. if (!cluster || (strlen(cluster) < 1))
  1952. {
  1953. if (descend)
  1954. return -1;
  1955. else
  1956. return 0;
  1957. }
  1958. __int64 addToPos = -1;
  1959. ForEachItemIn(i, LogicalFiles)
  1960. {
  1961. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1962. const char *ClusterName = File.getClusterName();
  1963. if (!ClusterName)
  1964. continue;
  1965. if (descend && strcmp(cluster, ClusterName)>0)
  1966. {
  1967. addToPos = i;
  1968. break;
  1969. }
  1970. if (!descend && strcmp(cluster, ClusterName)<0)
  1971. {
  1972. addToPos = i;
  1973. break;
  1974. }
  1975. }
  1976. return addToPos;
  1977. }
  1978. __int64 CWsDfuEx::findPositionByOwner(const char *owner, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  1979. {
  1980. if (!owner || (strlen(owner) < 1))
  1981. {
  1982. if (descend)
  1983. return -1;
  1984. else
  1985. return 0;
  1986. }
  1987. __int64 addToPos = -1;
  1988. ForEachItemIn(i, LogicalFiles)
  1989. {
  1990. IEspDFULogicalFile& File = LogicalFiles.item(i);
  1991. const char *Owner = File.getOwner();
  1992. if (!Owner)
  1993. continue;
  1994. if (descend && strcmp(owner, Owner)>0)
  1995. {
  1996. addToPos = i;
  1997. break;
  1998. }
  1999. if (!descend && strcmp(owner, Owner)<0)
  2000. {
  2001. addToPos = i;
  2002. break;
  2003. }
  2004. }
  2005. return addToPos;
  2006. }
  2007. __int64 CWsDfuEx::findPositionByDate(const char *datetime, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  2008. {
  2009. if (!datetime || (strlen(datetime) < 1))
  2010. {
  2011. if (descend)
  2012. return -1;
  2013. else
  2014. return 0;
  2015. }
  2016. __int64 addToPos = -1;
  2017. ForEachItemIn(i, LogicalFiles)
  2018. {
  2019. IEspDFULogicalFile& File = LogicalFiles.item(i);
  2020. const char *modDate = File.getModified();
  2021. if (!modDate)
  2022. continue;
  2023. if (descend && strcmp(datetime, modDate)>0)
  2024. {
  2025. addToPos = i;
  2026. break;
  2027. }
  2028. if (!descend && strcmp(datetime, modDate)<0)
  2029. {
  2030. addToPos = i;
  2031. break;
  2032. }
  2033. }
  2034. return addToPos;
  2035. }
  2036. __int64 CWsDfuEx::findPositionByDescription(const char *description, bool descend, IArrayOf<IEspDFULogicalFile>& LogicalFiles)
  2037. {
  2038. if (!description || (strlen(description) < 1))
  2039. {
  2040. if (descend)
  2041. return -1;
  2042. else
  2043. return 0;
  2044. }
  2045. __int64 addToPos = -1;
  2046. ForEachItemIn(i, LogicalFiles)
  2047. {
  2048. IEspDFULogicalFile& File = LogicalFiles.item(i);
  2049. const char *Description = File.getDescription();
  2050. if (!Description)
  2051. continue;
  2052. if (descend && strcmp(description, Description)>0)
  2053. {
  2054. addToPos = i;
  2055. break;
  2056. }
  2057. if (!descend && strcmp(description, Description)<0)
  2058. {
  2059. addToPos = i;
  2060. break;
  2061. }
  2062. }
  2063. return addToPos;
  2064. }
  2065. bool CWsDfuEx::checkDescription(const char *description, const char *descriptionFilter)
  2066. {
  2067. if (!descriptionFilter || (descriptionFilter[0] == 0))
  2068. return true;
  2069. if (!description || (description[0] == 0))
  2070. return false;
  2071. int len = strlen(descriptionFilter);
  2072. int filterType = 0;
  2073. if (descriptionFilter[0] == '*')
  2074. filterType += 1;
  2075. if (descriptionFilter[len - 1] == '*')
  2076. filterType += 2;
  2077. char descFilter[256];
  2078. if (filterType < 1)
  2079. strcpy(descFilter, descriptionFilter);
  2080. else if (filterType < 2)
  2081. strcpy(descFilter, descriptionFilter+1);
  2082. else if (filterType < 3)
  2083. {
  2084. strncpy(descFilter, descriptionFilter, len - 1);
  2085. descFilter[len -2] = 0;
  2086. }
  2087. else
  2088. {
  2089. strncpy(descFilter, descriptionFilter+1, len - 2);
  2090. descFilter[len -3] = 0;
  2091. }
  2092. const char *pos = strstr(description, descFilter);
  2093. if (!pos)
  2094. return false;
  2095. if ((pos != description) && (descriptionFilter[0] != '*'))
  2096. return false;
  2097. if ((pos + strlen(descFilter) != description + strlen(description)) && (descriptionFilter[len - 1] != '*'))
  2098. return false;
  2099. return true;
  2100. }
  2101. bool CWsDfuEx::doLogicalFileSearch(IEspContext &context, IUserDescriptor* udesc, IEspDFUQueryRequest & req, IEspDFUQueryResponse & resp)
  2102. {
  2103. DBGLOG("CWsDfuEx::doLogicalFileSearch\n");
  2104. double version = context.getClientVersion();
  2105. IArrayOf<IEspDFULogicalFile> LogicalFiles;
  2106. if (req.getOneLevelDirFileReturn())
  2107. {
  2108. int numDirs = 0;
  2109. int numFiles = 0;
  2110. getLogicalFileAndDirectory(udesc, req.getLogicalName(), LogicalFiles, numFiles, numDirs);
  2111. }
  2112. else
  2113. {
  2114. StringBuffer filter;
  2115. const char* fname = req.getLogicalName();
  2116. if(fname && *fname)
  2117. {
  2118. filter.append(fname);
  2119. }
  2120. else
  2121. {
  2122. if(req.getPrefix() && *req.getPrefix())
  2123. {
  2124. filter.append(req.getPrefix());
  2125. filter.append("::");
  2126. }
  2127. filter.append("*");
  2128. }
  2129. Owned<IDFAttributesIterator> fi;
  2130. bool bNotInSuperfile = false;
  2131. const char* sFileType = req.getFileType();
  2132. if (sFileType && !stricmp(sFileType, "Not in Superfiles"))
  2133. {
  2134. bNotInSuperfile = true;
  2135. }
  2136. if (bNotInSuperfile)
  2137. {
  2138. fi.setown(createSubFileFilter(
  2139. queryDistributedFileDirectory().getDFAttributesIterator(filter.toLowerCase().str(),udesc,true,true, NULL),udesc,false)); // NB wrapper owns wrapped iterator
  2140. }
  2141. else
  2142. {
  2143. fi.setown(queryDistributedFileDirectory().getDFAttributesIterator(filter.toLowerCase().str(), udesc,true,true, NULL));
  2144. }
  2145. if(!fi)
  2146. throw MakeStringException(ECLWATCH_CANNOT_GET_FILE_ITERATOR,"Cannot get information from file system.");
  2147. StringBuffer wuFrom, wuTo;
  2148. if(req.getStartDate() && *req.getStartDate())
  2149. {
  2150. CDateTime wuTime;
  2151. wuTime.setString(req.getStartDate(),NULL,true);
  2152. unsigned year, month, day, hour, minute, second, nano;
  2153. wuTime.getDate(year, month, day, true);
  2154. wuTime.getTime(hour, minute, second, nano, true);
  2155. wuFrom.appendf("%4d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second);
  2156. }
  2157. if(req.getEndDate() && *req.getEndDate())
  2158. {
  2159. CDateTime wuTime;
  2160. wuTime.setString(req.getEndDate(),NULL,true);
  2161. unsigned year, month, day, hour, minute, second, nano;
  2162. wuTime.getDate(year, month, day, true);
  2163. wuTime.getTime(hour, minute, second, nano, true);
  2164. wuTo.appendf("%4d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second);
  2165. }
  2166. char sortBy[256];
  2167. if(req.getSortby() && *req.getSortby())
  2168. {
  2169. strcpy(sortBy, req.getSortby());
  2170. }
  2171. unsigned pagesize = req.getPageSize();
  2172. if (pagesize < 1)
  2173. {
  2174. pagesize = 100;
  2175. }
  2176. //DBGLOG("pagesize=%d\n", pagesize);
  2177. __int64 displayStartReq = 1;
  2178. if (req.getPageStartFrom() > 0)
  2179. displayStartReq = req.getPageStartFrom();
  2180. __int64 displayStart = displayStartReq - 1;
  2181. __int64 displayEnd = displayStart + pagesize;
  2182. bool descending = req.getDescending();
  2183. const int nFirstN = req.getFirstN();
  2184. const char* sFirstNType = req.getFirstNType();
  2185. const __int64 nFileSizeFrom = req.getFileSizeFrom();
  2186. const __int64 nFileSizeTo = req.getFileSizeTo();
  2187. if (nFirstN > 0)
  2188. {
  2189. displayStart = 0;
  2190. displayEnd = nFirstN;
  2191. if (!stricmp(sFirstNType, "newest"))
  2192. {
  2193. strcpy(sortBy, "Modified");
  2194. descending = true;
  2195. }
  2196. else if (!stricmp(sFirstNType, "oldest"))
  2197. {
  2198. strcpy(sortBy, "Modified");
  2199. descending = false;
  2200. }
  2201. else if (!stricmp(sFirstNType, "largest"))
  2202. {
  2203. strcpy(sortBy, "Size");
  2204. descending = true;
  2205. }
  2206. else if (!stricmp(sFirstNType, "smallest"))
  2207. {
  2208. strcpy(sortBy, "Size");
  2209. descending = false;
  2210. }
  2211. pagesize = nFirstN;
  2212. }
  2213. StringArray roxieClusterNames;
  2214. IArrayOf<IEspTpCluster> roxieclusters;
  2215. CTpWrapper dummy;
  2216. dummy.getClusterProcessList(eqRoxieCluster, roxieclusters);
  2217. ForEachItemIn(k, roxieclusters)
  2218. {
  2219. IEspTpCluster& cluster = roxieclusters.item(k);
  2220. StringBuffer sName = cluster.getName();
  2221. roxieClusterNames.append(sName.str());
  2222. }
  2223. StringArray clustersReq;
  2224. const char* clustersReq0 = req.getClusterName();
  2225. if (clustersReq0 && *clustersReq0)
  2226. {
  2227. char* pStr = (char*) clustersReq0;
  2228. while (pStr)
  2229. {
  2230. char clusterName[256];
  2231. char* ppStr = strchr(pStr, ',');
  2232. if (!ppStr)
  2233. {
  2234. strcpy(clusterName, pStr);
  2235. pStr = NULL;
  2236. }
  2237. else
  2238. {
  2239. strncpy(clusterName, pStr, ppStr - pStr );
  2240. clusterName[ppStr - pStr] = 0;
  2241. pStr = ppStr+1;
  2242. }
  2243. clustersReq.append(clusterName);
  2244. }
  2245. }
  2246. StringBuffer size;
  2247. __int64 totalFiles = 0;
  2248. IArrayOf<IEspDFULogicalFile> LogicalFileList;
  2249. ForEach(*fi)
  2250. {
  2251. IPropertyTree &attr=fi->query();
  2252. const char* logicalName=attr.queryProp("@name");
  2253. if (!logicalName || (logicalName[0] == 0))
  2254. continue;
  2255. StringBuffer pref;
  2256. const char *c=strstr(logicalName, "::");
  2257. if (c)
  2258. pref.append(c-logicalName, logicalName);
  2259. else
  2260. pref.append(logicalName);
  2261. const char* owner=attr.queryProp("@owner");
  2262. if (req.getOwner() && *req.getOwner()!=0)
  2263. {
  2264. if (!owner || stricmp(owner, req.getOwner()))
  2265. continue;
  2266. }
  2267. #if 0
  2268. char* clusterName = (char*)attr.queryProp("@group"); // ** TBD - Handling for multiple clusters?
  2269. if (clusterName)
  2270. {//special process for roxie cluster names
  2271. unsigned len = strlen(clusterName);
  2272. if (len > 8)
  2273. {
  2274. char *pName = clusterName + len - 8;
  2275. if (!stricmp(pName, "__slaves"))
  2276. {
  2277. pName[0] = 0;//we did not specify slaves when copy/spray the file
  2278. }
  2279. }
  2280. }
  2281. if (req.getClusterName() && *req.getClusterName()!=0)
  2282. {
  2283. if (!clusterName || stricmp(clusterName, req.getClusterName()))
  2284. continue;
  2285. }
  2286. #else
  2287. StringArray clusters;
  2288. StringArray clusters1;
  2289. if (getFileGroups(&attr,clusters1)==0)
  2290. {
  2291. if (clustersReq.length() < 1)
  2292. {
  2293. clusters.append("");
  2294. }
  2295. }
  2296. else
  2297. {
  2298. // check specified cluster name in list
  2299. if (clustersReq.length() > 0)
  2300. {
  2301. ForEachItemIn(ii,clustersReq)
  2302. {
  2303. StringBuffer clusterFound;
  2304. const char * cluster0 = clustersReq.item(ii);
  2305. ForEachItemIn(i,clusters1)
  2306. {
  2307. if (!stricmp(clusters1.item(i), cluster0))
  2308. {
  2309. clusterFound.append(cluster0);
  2310. break;
  2311. }
  2312. }
  2313. if (clusterFound.length() > 0)
  2314. clusters.append(clusterFound);
  2315. }
  2316. }
  2317. else
  2318. {
  2319. if (clusters1.length() > 0)
  2320. {
  2321. ForEachItemIn(i,clusters1)
  2322. {
  2323. const char * cluster0 = clusters1.item(i);
  2324. clusters.append(cluster0);
  2325. }
  2326. }
  2327. }
  2328. }
  2329. #endif
  2330. const char* desc = attr.queryProp("@description");
  2331. if(req.getDescription() && *req.getDescription())
  2332. {
  2333. if (!checkDescription(desc, req.getDescription()))
  2334. continue;
  2335. }
  2336. if (sFileType && *sFileType)
  2337. {
  2338. bool bHasSubFiles = attr.hasProp("@numsubfiles");
  2339. if (bHasSubFiles && (bNotInSuperfile || !stricmp(sFileType, "Logical Files Only")))
  2340. continue;
  2341. else if (!bHasSubFiles && !stricmp(sFileType, "Superfiles Only"))
  2342. continue;
  2343. }
  2344. __int64 recordSize=attr.getPropInt64("@recordSize",0), size=attr.getPropInt64("@size",-1);
  2345. if (nFileSizeFrom > 0 && size < nFileSizeFrom)
  2346. continue;
  2347. if (nFileSizeTo > 0 && size > nFileSizeTo)
  2348. continue;
  2349. StringBuffer modf(attr.queryProp("@modified"));
  2350. char* t=(char *) strchr(modf.str(),'T');
  2351. if(t) *t=' ';
  2352. if (wuFrom.length() && strcmp(modf.str(),wuFrom.str())<0)
  2353. continue;
  2354. if (wuTo.length() && strcmp(modf.str(),wuTo.str())>0)
  2355. continue;
  2356. __int64 parts = 0;
  2357. if(!attr.hasProp("@numsubfiles"))
  2358. parts = attr.getPropInt64("@numparts");
  2359. __int64 records = 0;
  2360. if (attr.hasProp("@recordCount"))
  2361. records = attr.getPropInt64("@recordCount");
  2362. else if(recordSize)
  2363. records = size/recordSize;
  2364. char description[DESCRIPTION_DISPLAY_LENGTH + 1];
  2365. description[0] = 0;
  2366. if (desc && *desc)
  2367. {
  2368. if (strlen(desc) <= DESCRIPTION_DISPLAY_LENGTH) //Only 12 characters is required for display
  2369. {
  2370. strcpy(description, desc);
  2371. }
  2372. else
  2373. {
  2374. strncpy(description, desc, DESCRIPTION_DISPLAY_LENGTH - 3);
  2375. description[DESCRIPTION_DISPLAY_LENGTH - 3] = 0;
  2376. strcat(description, "...");
  2377. }
  2378. }
  2379. ForEachItemIn(i, clusters)
  2380. {
  2381. const char* clusterName = clusters.item(i);
  2382. __int64 addToPos = -1; //Add to tail
  2383. if (stricmp(sortBy, "Size")==0)
  2384. {
  2385. addToPos = findPositionBySize(size, descending, LogicalFileList);
  2386. }
  2387. else if (stricmp(sortBy, "Parts")==0)
  2388. {
  2389. addToPos = findPositionByParts(parts, descending, LogicalFileList);
  2390. }
  2391. else if (stricmp(sortBy, "Owner")==0)
  2392. {
  2393. addToPos = findPositionByOwner(owner, descending, LogicalFileList);
  2394. }
  2395. else if (stricmp(sortBy, "Cluster")==0)
  2396. {
  2397. addToPos = findPositionByCluster(clusterName, descending, LogicalFileList);
  2398. }
  2399. else if (stricmp(sortBy, "Records")==0)
  2400. {
  2401. addToPos = findPositionByRecords(records, descending, LogicalFileList);
  2402. }
  2403. else if (stricmp(sortBy, "Modified")==0)
  2404. {
  2405. addToPos = findPositionByDate(modf.str(), descending, LogicalFileList);
  2406. }
  2407. else if (stricmp(sortBy, "Description")==0)
  2408. {
  2409. addToPos = findPositionByDescription(description, descending, LogicalFileList);
  2410. }
  2411. else
  2412. {
  2413. addToPos = findPositionByName(logicalName, descending, LogicalFileList);
  2414. }
  2415. totalFiles++;
  2416. if (addToPos < 0 && (totalFiles > displayEnd))
  2417. continue;
  2418. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  2419. File->setPrefix(pref);
  2420. File->setClusterName(clusterName);
  2421. File->setName(logicalName);
  2422. File->setOwner(owner);
  2423. File->setDescription(description);
  2424. File->setModified(modf.str());
  2425. File->setReplicate(true);
  2426. ForEachItemIn(j, roxieClusterNames)
  2427. {
  2428. const char* roxieClusterName = roxieClusterNames.item(j);
  2429. if (roxieClusterName && clusterName && !stricmp(roxieClusterName, clusterName))
  2430. {
  2431. File->setFromRoxieCluster(true);
  2432. break;
  2433. }
  2434. }
  2435. bool bSuperfile = false;
  2436. int numSubFiles = attr.hasProp("@numsubfiles");
  2437. if(!numSubFiles)
  2438. {
  2439. File->setDirectory(attr.queryProp("@directory"));
  2440. File->setParts(attr.queryProp("@numparts"));
  2441. }
  2442. else
  2443. {
  2444. bSuperfile = true;
  2445. }
  2446. File->setIsSuperfile(bSuperfile);
  2447. if(!isCompressed(attr))
  2448. {
  2449. File->setIsZipfile(false);
  2450. }
  2451. else
  2452. {
  2453. File->setIsZipfile(true);
  2454. }
  2455. //File->setBrowseData(bKeyFile); //Bug: 39750 - All files should be viewable through ViewKeyFile function
  2456. if (numSubFiles > 1) //Bug 41379 - ViewKeyFile Cannot handle superfile with multiple subfiles
  2457. File->setBrowseData(false);
  2458. else
  2459. File->setBrowseData(true);
  2460. if (version > 1.13)
  2461. {
  2462. bool bKeyFile = false;
  2463. const char * kind = attr.queryProp("@kind");
  2464. if (kind && (stricmp(kind, "key") == 0))
  2465. {
  2466. bKeyFile = true;
  2467. }
  2468. File->setIsKeyFile(bKeyFile);
  2469. }
  2470. StringBuffer buf;
  2471. buf << comma(size);
  2472. File->setTotalsize(buf.str());
  2473. char temp[64];
  2474. numtostr(temp, size);
  2475. File->setLongSize(temp);
  2476. numtostr(temp, records);
  2477. File->setLongRecordCount(temp);
  2478. if (records > 0)
  2479. File->setRecordCount((buf.clear()<<comma(records)).str());
  2480. if (addToPos < 0)
  2481. LogicalFileList.append(*File.getClear());
  2482. else
  2483. LogicalFileList.add(*File.getClear(), (int) addToPos);
  2484. if (LogicalFileList.length() > displayEnd)
  2485. LogicalFileList.pop();
  2486. }
  2487. }
  2488. if (displayEnd > LogicalFileList.length())
  2489. displayEnd = LogicalFileList.length();
  2490. for (int i = (int) displayStart; i < (int) displayEnd; i++)
  2491. {
  2492. Owned<IEspDFULogicalFile> File = createDFULogicalFile("","");
  2493. IEspDFULogicalFile& File0 = LogicalFileList.item(i);
  2494. File->copy(File0);
  2495. LogicalFiles.append(*File.getClear());
  2496. }
  2497. resp.setNumFiles(totalFiles);
  2498. resp.setPageSize(pagesize);
  2499. resp.setPageStartFrom(displayStart+1);
  2500. resp.setPageEndAt(displayEnd);
  2501. if (displayStart - pagesize > 0)
  2502. resp.setPrevPageFrom(displayStart - pagesize + 1);
  2503. else if(displayStart > 0)
  2504. resp.setPrevPageFrom(1);
  2505. if(displayEnd < totalFiles)
  2506. {
  2507. resp.setNextPageFrom(displayEnd+1);
  2508. resp.setLastPageFrom((int)(pagesize * floor((double) ((totalFiles-1) / pagesize)) + 1));
  2509. }
  2510. StringBuffer basicQuery;
  2511. if (req.getClusterName() && *req.getClusterName())
  2512. {
  2513. resp.setClusterName(req.getClusterName());
  2514. addToQueryString(basicQuery, "ClusterName", req.getClusterName());
  2515. }
  2516. if (req.getOwner() && *req.getOwner())
  2517. {
  2518. resp.setOwner(req.getOwner());
  2519. addToQueryString(basicQuery, "Owner", req.getOwner());
  2520. }
  2521. if (req.getPrefix() && *req.getPrefix())
  2522. {
  2523. resp.setPrefix(req.getPrefix());
  2524. addToQueryString(basicQuery, "Prefix", req.getPrefix());
  2525. }
  2526. if (req.getLogicalName() && *req.getLogicalName())
  2527. {
  2528. resp.setLogicalName(req.getLogicalName());
  2529. addToQueryString(basicQuery, "LogicalName", req.getLogicalName());
  2530. }
  2531. if (req.getDescription() && *req.getDescription())
  2532. {
  2533. resp.setDescription(req.getDescription());
  2534. addToQueryString(basicQuery, "Description", req.getDescription());
  2535. }
  2536. if (req.getStartDate() && *req.getStartDate())
  2537. {
  2538. resp.setStartDate(req.getStartDate());
  2539. addToQueryString(basicQuery, "StartDate", req.getStartDate());
  2540. }
  2541. if (req.getEndDate() && *req.getEndDate())
  2542. {
  2543. resp.setEndDate(req.getEndDate());
  2544. addToQueryString(basicQuery, "EndDate", req.getEndDate());
  2545. }
  2546. if (req.getFileType() && *req.getFileType())
  2547. {
  2548. resp.setFileType(req.getFileType());
  2549. addToQueryString(basicQuery, "FileType", req.getFileType());
  2550. }
  2551. if (req.getFileSizeFrom())
  2552. {
  2553. resp.setFileSizeFrom(req.getFileSizeFrom());
  2554. addToQueryStringFromInt(basicQuery, "FileSizeFrom", req.getFileSizeFrom());
  2555. }
  2556. if (req.getFileSizeTo())
  2557. {
  2558. resp.setFileSizeTo(req.getFileSizeTo());
  2559. addToQueryStringFromInt(basicQuery, "FileSizeTo", req.getFileSizeTo());
  2560. }
  2561. StringBuffer ParametersForFilters = basicQuery;
  2562. StringBuffer ParametersForPaging = basicQuery;
  2563. addToQueryStringFromInt(ParametersForFilters, "PageSize",pagesize);
  2564. addToQueryStringFromInt(ParametersForPaging, "PageSize", pagesize);
  2565. if (ParametersForFilters.length() > 0)
  2566. resp.setFilters(ParametersForFilters.str());
  2567. sortBy[0] = 0;
  2568. descending = false;
  2569. if ((req.getFirstN() > 0) && req.getFirstNType() && *req.getFirstNType())
  2570. {
  2571. const char *sFirstNType = req.getFirstNType();
  2572. if (!stricmp(sFirstNType, "newest"))
  2573. {
  2574. strcpy(sortBy, "Modified");
  2575. descending = true;
  2576. }
  2577. else if (!stricmp(sFirstNType, "oldest"))
  2578. {
  2579. strcpy(sortBy, "Modified");
  2580. descending = false;
  2581. }
  2582. else if (!stricmp(sFirstNType, "largest"))
  2583. {
  2584. strcpy(sortBy, "Size");
  2585. descending = true;
  2586. }
  2587. else if (!stricmp(sFirstNType, "smallest"))
  2588. {
  2589. strcpy(sortBy, "Size");
  2590. descending = false;
  2591. }
  2592. }
  2593. else if (req.getSortby() && *req.getSortby())
  2594. {
  2595. strcpy(sortBy, req.getSortby());
  2596. if (req.getDescending())
  2597. descending = req.getDescending();
  2598. }
  2599. if (sortBy && *sortBy)
  2600. {
  2601. resp.setSortby(sortBy);
  2602. resp.setDescending(descending);
  2603. StringBuffer strbuf = sortBy;
  2604. strbuf.append("=");
  2605. String str1(strbuf.str());
  2606. String str(basicQuery.str());
  2607. if (str.indexOf(str1) < 0)
  2608. {
  2609. addToQueryString(ParametersForPaging, "Sortby", sortBy);
  2610. addToQueryString(basicQuery, "Sortby", sortBy);
  2611. if (descending)
  2612. {
  2613. addToQueryString(ParametersForPaging, "Descending", "1");
  2614. addToQueryString(basicQuery, "Descending", "1");
  2615. }
  2616. }
  2617. }
  2618. if (basicQuery.length() > 0)
  2619. resp.setBasicQuery(basicQuery.str());
  2620. if (ParametersForPaging.length() > 0)
  2621. resp.setParametersForPaging(ParametersForPaging.str());
  2622. //DBGLOG("basicQuery=%s\n", basicQuery);
  2623. }
  2624. resp.setDFULogicalFiles(LogicalFiles);
  2625. return true;
  2626. }
  2627. bool CWsDfuEx::onSuperfileList(IEspContext &context, IEspSuperfileListRequest &req, IEspSuperfileListResponse &resp)
  2628. {
  2629. try
  2630. {
  2631. StringBuffer username;
  2632. context.getUserID(username);
  2633. Owned<IUserDescriptor> userdesc;
  2634. if(username.length() > 0)
  2635. {
  2636. const char* passwd = context.queryPassword();
  2637. userdesc.setown(createUserDescriptor());
  2638. userdesc->set(username.str(), passwd);
  2639. }
  2640. Owned<IDFUhelper> dfuhelper = createIDFUhelper();
  2641. StringArray farray;
  2642. StringAttrArray subfiles;
  2643. dfuhelper->listSubFiles(req.getSuperfile(), subfiles, userdesc.get());
  2644. for(unsigned i = 0; i < subfiles.length(); i++)
  2645. {
  2646. StringAttrItem& subfile = subfiles.item(i);
  2647. farray.append(subfile.text);
  2648. }
  2649. if(farray.length() > 0)
  2650. resp.setSubfiles(farray);
  2651. resp.setSuperfile(req.getSuperfile());
  2652. }
  2653. catch(IException* e)
  2654. {
  2655. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2656. }
  2657. return true;
  2658. }
  2659. bool CWsDfuEx::onSuperfileAction(IEspContext &context, IEspSuperfileActionRequest &req, IEspSuperfileActionResponse &resp)
  2660. {
  2661. try
  2662. {
  2663. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Write, false))
  2664. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to Superfile action. Permission denied.");
  2665. const char* action = req.getAction();
  2666. const char* superfile = req.getSuperfile();
  2667. superfileAction(context, action, superfile, req.getSubfiles(), req.getBefore(), true, true, req.getDelete(), req.getRemoveSuperfile());
  2668. resp.setRetcode(0);
  2669. if (superfile && *superfile && action && strieq(action, "remove"))
  2670. {
  2671. Owned<IUserDescriptor> udesc;
  2672. udesc.setown(createUserDescriptor());
  2673. {
  2674. StringBuffer userID;
  2675. StringBuffer pw;
  2676. context.getUserID(userID);
  2677. context.getPassword(pw);
  2678. udesc->set(userID.str(), pw.str());
  2679. }
  2680. Owned<IDistributedSuperFile> fp = queryDistributedFileDirectory().lookupSuperFile(superfile,udesc);
  2681. if (!fp)
  2682. resp.setRetcode(-1); //Superfile has been removed.
  2683. }
  2684. resp.setSuperfile(req.getSuperfile());
  2685. }
  2686. catch(IException* e)
  2687. {
  2688. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2689. }
  2690. return true;
  2691. }
  2692. bool CWsDfuEx::onSavexml(IEspContext &context, IEspSavexmlRequest &req, IEspSavexmlResponse &resp)
  2693. {
  2694. try
  2695. {
  2696. StringBuffer username;
  2697. context.getUserID(username);
  2698. Owned<IUserDescriptor> userdesc;
  2699. if(username.length() > 0)
  2700. {
  2701. const char* passwd = context.queryPassword();
  2702. userdesc.setown(createUserDescriptor());
  2703. userdesc->set(username.str(), passwd);
  2704. }
  2705. Owned<IDFUhelper> dfuhelper = createIDFUhelper();
  2706. StringBuffer out;
  2707. dfuhelper->getFileXML(req.getName(), out, userdesc.get());
  2708. MemoryBuffer xmlmap;
  2709. int len = out.length();
  2710. xmlmap.setBuffer(len, out.detach(), true);
  2711. resp.setXmlmap(xmlmap);
  2712. }
  2713. catch(IException* e)
  2714. {
  2715. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2716. }
  2717. return true;
  2718. }
  2719. bool CWsDfuEx::onAdd(IEspContext &context, IEspAddRequest &req, IEspAddResponse &resp)
  2720. {
  2721. try
  2722. {
  2723. StringBuffer username;
  2724. context.getUserID(username);
  2725. Owned<IUserDescriptor> userdesc;
  2726. if(username.length() > 0)
  2727. {
  2728. const char* passwd = context.queryPassword();
  2729. userdesc.setown(createUserDescriptor());
  2730. userdesc->set(username.str(), passwd);
  2731. }
  2732. Owned<IDFUhelper> dfuhelper = createIDFUhelper();
  2733. StringBuffer xmlstr(req.getXmlmap().length(),(const char*)req.getXmlmap().bufferBase());
  2734. dfuhelper->addFileXML(req.getDstname(), xmlstr, userdesc.get());
  2735. }
  2736. catch(IException* e)
  2737. {
  2738. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2739. }
  2740. return true;
  2741. }
  2742. bool CWsDfuEx::onAddRemote(IEspContext &context, IEspAddRemoteRequest &req, IEspAddRemoteResponse &resp)
  2743. {
  2744. try
  2745. {
  2746. StringBuffer username;
  2747. context.getUserID(username);
  2748. Owned<IUserDescriptor> userdesc;
  2749. if(username.length() > 0)
  2750. {
  2751. const char* passwd = context.queryPassword();
  2752. userdesc.setown(createUserDescriptor());
  2753. userdesc->set(username.str(), passwd);
  2754. }
  2755. const char* srcusername = req.getSrcusername();
  2756. Owned<IUserDescriptor> srcuserdesc;
  2757. if(srcusername && *srcusername)
  2758. {
  2759. const char* srcpasswd = req.getSrcpassword();
  2760. srcuserdesc.setown(createUserDescriptor());
  2761. srcuserdesc->set(srcusername, srcpasswd);
  2762. }
  2763. const char* srcname = req.getSrcname();
  2764. if(srcname == NULL || *srcname == '\0')
  2765. throw MakeStringException(ECLWATCH_INVALID_INPUT, "srcname can't be empty.");
  2766. const char* srcdali = req.getSrcdali();
  2767. if(srcdali == NULL || *srcdali == '\0')
  2768. throw MakeStringException(ECLWATCH_INVALID_INPUT, "srcdali can't be empty.");
  2769. const char* dstname = req.getDstname();
  2770. if(dstname == NULL || *dstname == '\0')
  2771. throw MakeStringException(ECLWATCH_INVALID_INPUT, "dstname can't be empty.");
  2772. SocketEndpoint ep(srcdali);
  2773. Owned<IDFUhelper> dfuhelper = createIDFUhelper();
  2774. dfuhelper->addFileRemote(dstname, ep, srcname, srcuserdesc.get(), userdesc.get());
  2775. }
  2776. catch(IException* e)
  2777. {
  2778. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2779. }
  2780. return true;
  2781. }
  2782. const int INTEGELSIZE = 20;
  2783. const int REALSIZE = 32;
  2784. const int STRINGSIZE = 128;
  2785. bool CWsDfuEx::onDFUGetDataColumns(IEspContext &context, IEspDFUGetDataColumnsRequest &req, IEspDFUGetDataColumnsResponse &resp)
  2786. {
  2787. try
  2788. {
  2789. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  2790. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to View Data File. Permission denied.");
  2791. StringBuffer logicalNameStr;
  2792. char* logicalName0 = (char*) req.getOpenLogicalName();
  2793. if (logicalName0 && *logicalName0)
  2794. {
  2795. logicalNameStr.append(logicalName0);
  2796. logicalNameStr.trim();
  2797. }
  2798. if (logicalNameStr.length() > 0)
  2799. {
  2800. __int64 startIndex = req.getStartIndex();
  2801. __int64 endIndex = req.getEndIndex();
  2802. if (startIndex < 1)
  2803. startIndex = 1;
  2804. if (endIndex < 1)
  2805. endIndex = 100;
  2806. StringArray filterByNames, filterByValues;
  2807. double version = context.getClientVersion();
  2808. if (version > 1.04)
  2809. {
  2810. const char* filterBy = req.getFilterBy();
  2811. if (filterBy && *filterBy)
  2812. {
  2813. parseTwoStringArrays(filterBy, filterByNames, filterByValues);
  2814. }
  2815. const char* showColumns = req.getShowColumns();
  2816. if (showColumns && *showColumns)
  2817. {
  2818. resp.setShowColumns(showColumns);
  2819. }
  2820. }
  2821. StringBuffer username;
  2822. context.getUserID(username);
  2823. const char* passwd = context.queryPassword();
  2824. Owned<IUserDescriptor> userdesc;
  2825. userdesc.setown(createUserDescriptor());
  2826. userdesc->set(username.str(), passwd);
  2827. {
  2828. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalNameStr.str(), userdesc);
  2829. if(!df)
  2830. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Could not find file %s.", logicalNameStr.str());
  2831. IDistributedSuperFile *sf = df->querySuperFile();
  2832. if (sf && (sf->numSubFiles() > 1))
  2833. throw MakeStringException(ECLWATCH_INVALID_ACTION,"This feature is not designed to work with a superfile which contains multiple subfiles.");
  2834. }
  2835. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
  2836. Owned<INewResultSet> result;
  2837. if (m_clusterName.length() > 0)
  2838. {
  2839. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), m_clusterName.str()));
  2840. }
  2841. else
  2842. {
  2843. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), NULL));
  2844. }
  2845. __int64 total=result->getNumRows();
  2846. {
  2847. IArrayOf<IEspDFUDataColumn> dataKeyedColumns[MAX_KEY_ROWS];
  2848. IArrayOf<IEspDFUDataColumn> dataNonKeyedColumns[MAX_KEY_ROWS];
  2849. Owned<IResultSetCursor> cursor = result->createCursor();
  2850. const IResultSetMetaData & meta = cursor->queryResultSet()->getMetaData();
  2851. int columnCount = meta.getColumnCount();
  2852. int keyedColumnCount = meta.getNumKeyedColumns();
  2853. unsigned columnSize = 0;
  2854. int lineSizeCount = 0;
  2855. int lineCount = 0;
  2856. for (int i = 0; i < keyedColumnCount; i++)
  2857. {
  2858. Owned<IEspDFUDataColumn> item = createDFUDataColumn("","");
  2859. bool bNaturalColumn = true;
  2860. SCMStringBuffer columnLabel;
  2861. if (meta.hasSetTranslation(i))
  2862. {
  2863. meta.getNaturalColumnLabel(columnLabel, i);
  2864. }
  2865. if (columnLabel.length() < 1)
  2866. {
  2867. meta.getColumnLabel(columnLabel, i);
  2868. bNaturalColumn = false;
  2869. }
  2870. item->setColumnLabel(columnLabel.str());
  2871. if (version > 1.04 && filterByNames.length() > 0)
  2872. {
  2873. for (unsigned ii = 0; ii < filterByNames.length(); ii++)
  2874. {
  2875. const char* name = filterByNames.item(ii);
  2876. if (name && !stricmp(name, columnLabel.str()))
  2877. {
  2878. const char* value = filterByValues.item(ii);
  2879. if (value && *value)
  2880. {
  2881. item->setColumnValue(value);
  2882. break;
  2883. }
  2884. }
  2885. }
  2886. }
  2887. DisplayType columnType = meta.getColumnDisplayType(i);
  2888. if (bNaturalColumn)
  2889. {
  2890. item->setColumnType("Others");
  2891. item->setColumnSize(STRINGSIZE);
  2892. columnSize = STRINGSIZE;
  2893. item->setMaxSize(columnSize);
  2894. }
  2895. else if (columnType == TypeBoolean)
  2896. {
  2897. item->setColumnType("Boolean");
  2898. item->setMaxSize(1);
  2899. item->setColumnSize(strlen(columnLabel.str()));
  2900. columnSize = 2;
  2901. }
  2902. else
  2903. {
  2904. if (columnType == TypeInteger || columnType == TypeUnsignedInteger)
  2905. {
  2906. item->setColumnType("Integer");
  2907. item->setMaxSize(INTEGELSIZE);
  2908. columnSize = INTEGELSIZE;
  2909. if (strlen(columnLabel.str()) > columnSize)
  2910. columnSize = strlen(columnLabel.str());
  2911. item->setColumnSize(columnSize);
  2912. }
  2913. else if (columnType == TypeReal)
  2914. {
  2915. item->setColumnType("Real");
  2916. item->setMaxSize(REALSIZE);
  2917. columnSize = REALSIZE;
  2918. if (strlen(columnLabel.str()) > columnSize)
  2919. columnSize = strlen(columnLabel.str());
  2920. item->setColumnSize(columnSize);
  2921. }
  2922. else if (columnType == TypeString)
  2923. {
  2924. columnSize = meta.getColumnRawSize(i);
  2925. columnSize = rtlQStrLength(columnSize);
  2926. if (columnSize < 1)
  2927. columnSize = STRINGSIZE;
  2928. else if (columnSize > STRINGSIZE)
  2929. columnSize = STRINGSIZE;
  2930. item->setColumnType("String");
  2931. item->setMaxSize(columnSize);
  2932. if (strlen(columnLabel.str()) > columnSize)
  2933. columnSize = strlen(columnLabel.str());
  2934. item->setColumnSize(columnSize);
  2935. }
  2936. else if (columnType == TypeUnicode)
  2937. {
  2938. item->setColumnType("Others");
  2939. columnSize = (int) (meta.getColumnRawSize(i) * 0.5);
  2940. if (columnSize > STRINGSIZE)
  2941. columnSize = STRINGSIZE;
  2942. item->setColumnSize(columnSize);
  2943. item->setMaxSize(columnSize);
  2944. }
  2945. else
  2946. {
  2947. item->setColumnType("Others");
  2948. columnSize = STRINGSIZE;
  2949. item->setColumnSize(columnSize);
  2950. item->setMaxSize(columnSize);
  2951. }
  2952. }
  2953. columnSize += 7;
  2954. if ((lineSizeCount == 0) && (columnSize > STRINGSIZE)) //One field is big enough to use one line
  2955. {
  2956. if (lineCount >= MAX_KEY_ROWS)
  2957. break;
  2958. dataKeyedColumns[lineCount].append(*item.getLink());
  2959. lineCount++;
  2960. }
  2961. else
  2962. {
  2963. if (lineSizeCount + columnSize < STRINGSIZE)
  2964. {
  2965. lineSizeCount += columnSize;
  2966. }
  2967. else //too big in this line...so, switch to another line
  2968. {
  2969. lineCount++;
  2970. lineSizeCount = columnSize;
  2971. }
  2972. if (lineCount >= MAX_KEY_ROWS)
  2973. break;
  2974. dataKeyedColumns[lineCount].append(*item.getLink());
  2975. }
  2976. }
  2977. columnSize = 0;
  2978. lineSizeCount = 0;
  2979. lineCount = 0;
  2980. for (int ii = keyedColumnCount; ii < columnCount; ii++)
  2981. {
  2982. Owned<IEspDFUDataColumn> item = createDFUDataColumn("","");
  2983. bool bNaturalColumn = true;
  2984. SCMStringBuffer columnLabel;
  2985. if (meta.hasSetTranslation(ii))
  2986. {
  2987. meta.getNaturalColumnLabel(columnLabel, ii);
  2988. }
  2989. if (columnLabel.length() < 1)
  2990. {
  2991. meta.getColumnLabel(columnLabel, ii);
  2992. bNaturalColumn = false;
  2993. }
  2994. item->setColumnLabel(columnLabel.str());
  2995. if (version > 1.04 && filterByNames.length() > 0)
  2996. {
  2997. for (unsigned ii = 0; ii < filterByNames.length(); ii++)
  2998. {
  2999. const char* name = filterByNames.item(ii);
  3000. if (name && !stricmp(name, columnLabel.str()))
  3001. {
  3002. const char* value = filterByValues.item(ii);
  3003. if (value && *value)
  3004. {
  3005. item->setColumnValue(value);
  3006. break;
  3007. }
  3008. }
  3009. }
  3010. }
  3011. DisplayType columnType = meta.getColumnDisplayType(ii);
  3012. if (bNaturalColumn)
  3013. {
  3014. item->setColumnType("Others");
  3015. item->setColumnSize(STRINGSIZE);
  3016. columnSize = STRINGSIZE;
  3017. }
  3018. else if (columnType == TypeBoolean)
  3019. {
  3020. item->setColumnType("Boolean");
  3021. item->setMaxSize(1);
  3022. item->setColumnSize(strlen(columnLabel.str()));
  3023. columnSize = 2;
  3024. }
  3025. else
  3026. {
  3027. if (columnType == TypeInteger || columnType == TypeUnsignedInteger)
  3028. {
  3029. item->setColumnType("Integer");
  3030. item->setMaxSize(INTEGELSIZE);
  3031. columnSize = INTEGELSIZE;
  3032. if (strlen(columnLabel.str()) > columnSize)
  3033. columnSize = strlen(columnLabel.str());
  3034. item->setColumnSize(columnSize);
  3035. }
  3036. else if (columnType == TypeReal)
  3037. {
  3038. item->setColumnType("Real");
  3039. item->setMaxSize(REALSIZE);
  3040. columnSize = REALSIZE;
  3041. if (strlen(columnLabel.str()) > columnSize)
  3042. columnSize = strlen(columnLabel.str());
  3043. item->setColumnSize(columnSize);
  3044. }
  3045. else if (columnType == TypeString)
  3046. {
  3047. columnSize = meta.getColumnRawSize(ii);
  3048. columnSize = rtlQStrLength(columnSize);
  3049. if (columnSize < 1)
  3050. columnSize = STRINGSIZE;
  3051. else if (columnSize > STRINGSIZE)
  3052. columnSize = STRINGSIZE;
  3053. item->setColumnType("String");
  3054. item->setMaxSize(columnSize);
  3055. if (strlen(columnLabel.str()) > columnSize)
  3056. columnSize = strlen(columnLabel.str());
  3057. item->setColumnSize(columnSize);
  3058. }
  3059. else if (columnType == TypeUnicode)
  3060. {
  3061. item->setColumnType("Others");
  3062. columnSize = (int) (meta.getColumnRawSize(ii) * 0.5);
  3063. if (columnSize > STRINGSIZE)
  3064. columnSize = STRINGSIZE;
  3065. item->setColumnSize(columnSize);
  3066. item->setMaxSize(columnSize);
  3067. }
  3068. else
  3069. {
  3070. item->setColumnType("Others");
  3071. columnSize = STRINGSIZE;
  3072. item->setColumnSize(columnSize);
  3073. item->setMaxSize(columnSize);
  3074. }
  3075. }
  3076. columnSize += 7;
  3077. if ((lineSizeCount == 0) && (columnSize > STRINGSIZE))
  3078. {
  3079. if (lineCount >= MAX_KEY_ROWS)
  3080. break;
  3081. dataNonKeyedColumns[lineCount].append(*item.getLink());
  3082. lineCount++;
  3083. }
  3084. else
  3085. {
  3086. if (lineSizeCount + columnSize < STRINGSIZE)
  3087. {
  3088. lineSizeCount += columnSize;
  3089. }
  3090. else
  3091. {
  3092. lineCount++;
  3093. lineSizeCount = columnSize;
  3094. }
  3095. if (lineCount >= MAX_KEY_ROWS)
  3096. break;
  3097. dataNonKeyedColumns[lineCount].append(*item.getLink());
  3098. }
  3099. }
  3100. if (dataKeyedColumns[0].length() > 0)
  3101. resp.setDFUDataKeyedColumns1(dataKeyedColumns[0]);
  3102. if (dataKeyedColumns[1].length() > 0)
  3103. resp.setDFUDataKeyedColumns2(dataKeyedColumns[1]);
  3104. if (dataKeyedColumns[2].length() > 0)
  3105. resp.setDFUDataKeyedColumns3(dataKeyedColumns[2]);
  3106. if (dataKeyedColumns[3].length() > 0)
  3107. resp.setDFUDataKeyedColumns4(dataKeyedColumns[3]);
  3108. if (dataKeyedColumns[4].length() > 0)
  3109. resp.setDFUDataKeyedColumns5(dataKeyedColumns[4]);
  3110. if (dataKeyedColumns[5].length() > 0)
  3111. resp.setDFUDataKeyedColumns6(dataKeyedColumns[5]);
  3112. if (dataKeyedColumns[6].length() > 0)
  3113. resp.setDFUDataKeyedColumns7(dataKeyedColumns[6]);
  3114. if (dataKeyedColumns[7].length() > 0)
  3115. resp.setDFUDataKeyedColumns8(dataKeyedColumns[7]);
  3116. if (dataKeyedColumns[8].length() > 0)
  3117. resp.setDFUDataKeyedColumns9(dataKeyedColumns[8]);
  3118. if (dataKeyedColumns[9].length() > 0)
  3119. resp.setDFUDataKeyedColumns10(dataKeyedColumns[9]);
  3120. if (version > 1.14)
  3121. {
  3122. if (dataKeyedColumns[10].length() > 0)
  3123. resp.setDFUDataKeyedColumns11(dataKeyedColumns[10]);
  3124. if (dataKeyedColumns[11].length() > 0)
  3125. resp.setDFUDataKeyedColumns12(dataKeyedColumns[11]);
  3126. if (dataKeyedColumns[12].length() > 0)
  3127. resp.setDFUDataKeyedColumns13(dataKeyedColumns[12]);
  3128. if (dataKeyedColumns[13].length() > 0)
  3129. resp.setDFUDataKeyedColumns14(dataKeyedColumns[13]);
  3130. if (dataKeyedColumns[14].length() > 0)
  3131. resp.setDFUDataKeyedColumns15(dataKeyedColumns[14]);
  3132. if (dataKeyedColumns[15].length() > 0)
  3133. resp.setDFUDataKeyedColumns16(dataKeyedColumns[15]);
  3134. if (dataKeyedColumns[16].length() > 0)
  3135. resp.setDFUDataKeyedColumns17(dataKeyedColumns[16]);
  3136. if (dataKeyedColumns[17].length() > 0)
  3137. resp.setDFUDataKeyedColumns18(dataKeyedColumns[17]);
  3138. if (dataKeyedColumns[18].length() > 0)
  3139. resp.setDFUDataKeyedColumns19(dataKeyedColumns[18]);
  3140. if (dataKeyedColumns[19].length() > 0)
  3141. resp.setDFUDataKeyedColumns20(dataKeyedColumns[19]);
  3142. }
  3143. if (dataNonKeyedColumns[0].length() > 0)
  3144. resp.setDFUDataNonKeyedColumns1(dataNonKeyedColumns[0]);
  3145. if (dataNonKeyedColumns[1].length() > 0)
  3146. resp.setDFUDataNonKeyedColumns2(dataNonKeyedColumns[1]);
  3147. if (dataNonKeyedColumns[2].length() > 0)
  3148. resp.setDFUDataNonKeyedColumns3(dataNonKeyedColumns[2]);
  3149. if (dataNonKeyedColumns[3].length() > 0)
  3150. resp.setDFUDataNonKeyedColumns4(dataNonKeyedColumns[3]);
  3151. if (dataNonKeyedColumns[4].length() > 0)
  3152. resp.setDFUDataNonKeyedColumns5(dataNonKeyedColumns[4]);
  3153. if (dataNonKeyedColumns[5].length() > 0)
  3154. resp.setDFUDataNonKeyedColumns6(dataNonKeyedColumns[5]);
  3155. if (dataNonKeyedColumns[6].length() > 0)
  3156. resp.setDFUDataNonKeyedColumns7(dataNonKeyedColumns[6]);
  3157. if (dataNonKeyedColumns[7].length() > 0)
  3158. resp.setDFUDataNonKeyedColumns8(dataNonKeyedColumns[7]);
  3159. if (dataNonKeyedColumns[8].length() > 0)
  3160. resp.setDFUDataNonKeyedColumns9(dataNonKeyedColumns[8]);
  3161. if (dataNonKeyedColumns[9].length() > 0)
  3162. resp.setDFUDataNonKeyedColumns10(dataNonKeyedColumns[9]);
  3163. if (version > 1.14)
  3164. {
  3165. if (dataNonKeyedColumns[10].length() > 0)
  3166. resp.setDFUDataNonKeyedColumns11(dataNonKeyedColumns[10]);
  3167. if (dataNonKeyedColumns[11].length() > 0)
  3168. resp.setDFUDataNonKeyedColumns12(dataNonKeyedColumns[11]);
  3169. if (dataNonKeyedColumns[12].length() > 0)
  3170. resp.setDFUDataNonKeyedColumns13(dataNonKeyedColumns[12]);
  3171. if (dataNonKeyedColumns[13].length() > 0)
  3172. resp.setDFUDataNonKeyedColumns14(dataNonKeyedColumns[13]);
  3173. if (dataNonKeyedColumns[14].length() > 0)
  3174. resp.setDFUDataNonKeyedColumns15(dataNonKeyedColumns[14]);
  3175. if (dataNonKeyedColumns[15].length() > 0)
  3176. resp.setDFUDataNonKeyedColumns16(dataNonKeyedColumns[15]);
  3177. if (dataNonKeyedColumns[16].length() > 0)
  3178. resp.setDFUDataNonKeyedColumns17(dataNonKeyedColumns[16]);
  3179. if (dataNonKeyedColumns[17].length() > 0)
  3180. resp.setDFUDataNonKeyedColumns18(dataNonKeyedColumns[17]);
  3181. if (dataNonKeyedColumns[18].length() > 0)
  3182. resp.setDFUDataNonKeyedColumns19(dataNonKeyedColumns[18]);
  3183. if (dataNonKeyedColumns[19].length() > 0)
  3184. resp.setDFUDataNonKeyedColumns20(dataNonKeyedColumns[19]);
  3185. }
  3186. //resp.setColumnCount(columnCount);
  3187. resp.setRowCount(total);
  3188. }
  3189. resp.setLogicalName(logicalNameStr.str());
  3190. resp.setStartIndex(startIndex);
  3191. resp.setEndIndex(endIndex);
  3192. if (version > 1.11)
  3193. {
  3194. if (req.getCluster() && *req.getCluster())
  3195. {
  3196. resp.setCluster(req.getCluster());
  3197. }
  3198. if (req.getClusterType() && *req.getClusterType())
  3199. {
  3200. resp.setClusterType(req.getClusterType());
  3201. }
  3202. }
  3203. }
  3204. if (req.getChooseFile())
  3205. resp.setChooseFile(1);
  3206. else
  3207. resp.setChooseFile(0);
  3208. }
  3209. catch(IException* e)
  3210. {
  3211. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3212. }
  3213. return true;
  3214. }
  3215. bool CWsDfuEx::onDFUSearchData(IEspContext &context, IEspDFUSearchDataRequest &req, IEspDFUSearchDataResponse &resp)
  3216. {
  3217. try
  3218. {
  3219. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  3220. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to View Data File. Permission denied.");
  3221. double version = context.getClientVersion();
  3222. resp.setCluster(req.getCluster());
  3223. resp.setClusterType(req.getClusterType());
  3224. resp.setFile(req.getFile());
  3225. resp.setKey(req.getKey());
  3226. const char* selectedKey = req.getSelectedKey();
  3227. if (strlen(selectedKey) > 0)
  3228. {
  3229. resp.setSelectedKey(req.getSelectedKey());
  3230. }
  3231. else
  3232. {
  3233. resp.setSelectedKey(req.getKey());
  3234. }
  3235. resp.setParentName(req.getParentName());
  3236. resp.setRoxieSelections(req.getRoxieSelections());
  3237. resp.setDisableUppercaseTranslation(req.getDisableUppercaseTranslation());
  3238. const char* openLogicalName = req.getOpenLogicalName();
  3239. if (strlen(openLogicalName) > 0)
  3240. {
  3241. Owned<IEspDFUGetDataColumnsRequest> DataColumnsRequest = createDFUGetDataColumnsRequest();
  3242. Owned<IEspDFUGetDataColumnsResponse> DataColumnsResponse = createDFUGetDataColumnsResponse();
  3243. DataColumnsRequest->setOpenLogicalName(req.getOpenLogicalName());
  3244. DataColumnsRequest->setFilterBy(req.getFilterBy());
  3245. DataColumnsRequest->setShowColumns(req.getShowColumns());
  3246. DataColumnsRequest->setChooseFile(req.getChooseFile());
  3247. DataColumnsRequest->setCluster(req.getCluster());
  3248. DataColumnsRequest->setClusterType(req.getClusterType());
  3249. try
  3250. {
  3251. onDFUGetDataColumns(context, *DataColumnsRequest, *DataColumnsResponse);
  3252. }
  3253. catch(IException* e)
  3254. {
  3255. if (version < 1.08)
  3256. throw e;
  3257. StringBuffer emsg;
  3258. e->errorMessage(emsg);
  3259. e->Release();
  3260. resp.setMsgToDisplay(emsg);
  3261. return true;
  3262. }
  3263. resp.setOpenLogicalName(req.getOpenLogicalName());
  3264. resp.setLogicalName(DataColumnsResponse->getLogicalName());
  3265. resp.setStartIndex(DataColumnsResponse->getStartIndex());
  3266. resp.setEndIndex(DataColumnsResponse->getEndIndex());
  3267. resp.setDFUDataKeyedColumns1(DataColumnsResponse->getDFUDataKeyedColumns1());
  3268. resp.setDFUDataKeyedColumns2(DataColumnsResponse->getDFUDataKeyedColumns2());
  3269. resp.setDFUDataKeyedColumns3(DataColumnsResponse->getDFUDataKeyedColumns3());
  3270. resp.setDFUDataKeyedColumns4(DataColumnsResponse->getDFUDataKeyedColumns4());
  3271. resp.setDFUDataKeyedColumns5(DataColumnsResponse->getDFUDataKeyedColumns5());
  3272. resp.setDFUDataKeyedColumns6(DataColumnsResponse->getDFUDataKeyedColumns6());
  3273. resp.setDFUDataKeyedColumns7(DataColumnsResponse->getDFUDataKeyedColumns7());
  3274. resp.setDFUDataKeyedColumns8(DataColumnsResponse->getDFUDataKeyedColumns8());
  3275. resp.setDFUDataKeyedColumns9(DataColumnsResponse->getDFUDataKeyedColumns9());
  3276. resp.setDFUDataKeyedColumns10(DataColumnsResponse->getDFUDataKeyedColumns10());
  3277. if (version > 1.14)
  3278. {
  3279. resp.setDFUDataKeyedColumns11(DataColumnsResponse->getDFUDataKeyedColumns11());
  3280. resp.setDFUDataKeyedColumns12(DataColumnsResponse->getDFUDataKeyedColumns12());
  3281. resp.setDFUDataKeyedColumns13(DataColumnsResponse->getDFUDataKeyedColumns13());
  3282. resp.setDFUDataKeyedColumns14(DataColumnsResponse->getDFUDataKeyedColumns14());
  3283. resp.setDFUDataKeyedColumns15(DataColumnsResponse->getDFUDataKeyedColumns15());
  3284. resp.setDFUDataKeyedColumns16(DataColumnsResponse->getDFUDataKeyedColumns16());
  3285. resp.setDFUDataKeyedColumns17(DataColumnsResponse->getDFUDataKeyedColumns17());
  3286. resp.setDFUDataKeyedColumns18(DataColumnsResponse->getDFUDataKeyedColumns18());
  3287. resp.setDFUDataKeyedColumns19(DataColumnsResponse->getDFUDataKeyedColumns19());
  3288. resp.setDFUDataKeyedColumns20(DataColumnsResponse->getDFUDataKeyedColumns20());
  3289. }
  3290. resp.setDFUDataNonKeyedColumns1(DataColumnsResponse->getDFUDataNonKeyedColumns1());
  3291. resp.setDFUDataNonKeyedColumns2(DataColumnsResponse->getDFUDataNonKeyedColumns2());
  3292. resp.setDFUDataNonKeyedColumns3(DataColumnsResponse->getDFUDataNonKeyedColumns3());
  3293. resp.setDFUDataNonKeyedColumns4(DataColumnsResponse->getDFUDataNonKeyedColumns4());
  3294. resp.setDFUDataNonKeyedColumns5(DataColumnsResponse->getDFUDataNonKeyedColumns5());
  3295. resp.setDFUDataNonKeyedColumns6(DataColumnsResponse->getDFUDataNonKeyedColumns6());
  3296. resp.setDFUDataNonKeyedColumns7(DataColumnsResponse->getDFUDataNonKeyedColumns7());
  3297. resp.setDFUDataNonKeyedColumns8(DataColumnsResponse->getDFUDataNonKeyedColumns8());
  3298. resp.setDFUDataNonKeyedColumns9(DataColumnsResponse->getDFUDataNonKeyedColumns9());
  3299. resp.setDFUDataNonKeyedColumns10(DataColumnsResponse->getDFUDataNonKeyedColumns10());
  3300. if (version > 1.14)
  3301. {
  3302. resp.setDFUDataNonKeyedColumns11(DataColumnsResponse->getDFUDataNonKeyedColumns11());
  3303. resp.setDFUDataNonKeyedColumns12(DataColumnsResponse->getDFUDataNonKeyedColumns12());
  3304. resp.setDFUDataNonKeyedColumns13(DataColumnsResponse->getDFUDataNonKeyedColumns13());
  3305. resp.setDFUDataNonKeyedColumns14(DataColumnsResponse->getDFUDataNonKeyedColumns14());
  3306. resp.setDFUDataNonKeyedColumns15(DataColumnsResponse->getDFUDataNonKeyedColumns15());
  3307. resp.setDFUDataNonKeyedColumns16(DataColumnsResponse->getDFUDataNonKeyedColumns16());
  3308. resp.setDFUDataNonKeyedColumns17(DataColumnsResponse->getDFUDataNonKeyedColumns17());
  3309. resp.setDFUDataNonKeyedColumns18(DataColumnsResponse->getDFUDataNonKeyedColumns18());
  3310. resp.setDFUDataNonKeyedColumns19(DataColumnsResponse->getDFUDataNonKeyedColumns19());
  3311. resp.setDFUDataNonKeyedColumns20(DataColumnsResponse->getDFUDataNonKeyedColumns20());
  3312. }
  3313. resp.setRowCount(DataColumnsResponse->getRowCount());
  3314. resp.setShowColumns(DataColumnsResponse->getShowColumns());
  3315. resp.setChooseFile(DataColumnsResponse->getChooseFile());
  3316. }
  3317. const char* logicalName = req.getLogicalName();
  3318. if (strlen(logicalName) == 0 && strlen(openLogicalName) > 0)
  3319. {
  3320. logicalName = openLogicalName;
  3321. }
  3322. if (strlen(logicalName) > 0)
  3323. {
  3324. Owned<IEspDFUBrowseDataRequest> browseDataRequest = createDFUBrowseDataRequest();
  3325. Owned<IEspDFUBrowseDataResponse> browseDataResponse = createDFUBrowseDataResponse();
  3326. browseDataRequest->setLogicalName(logicalName);
  3327. const char* parentName = req.getParentName();
  3328. if (parentName && *parentName)
  3329. browseDataRequest->setParentName(parentName);
  3330. browseDataRequest->setFilterBy(req.getFilterBy());
  3331. browseDataRequest->setShowColumns(req.getShowColumns());
  3332. browseDataRequest->setStartForGoback(req.getStartForGoback());
  3333. browseDataRequest->setCountForGoback(req.getCountForGoback());
  3334. browseDataRequest->setChooseFile(req.getChooseFile());
  3335. browseDataRequest->setStart(req.getStart());
  3336. browseDataRequest->setCount(req.getCount());
  3337. browseDataRequest->setSchemaOnly(req.getSchemaOnly());
  3338. browseDataRequest->setCluster(req.getCluster());
  3339. browseDataRequest->setClusterType(req.getClusterType());
  3340. browseDataRequest->setDisableUppercaseTranslation(req.getDisableUppercaseTranslation());
  3341. onDFUBrowseData(context, *browseDataRequest, *browseDataResponse);
  3342. resp.setName(browseDataResponse->getName());
  3343. resp.setLogicalName(browseDataResponse->getLogicalName());
  3344. resp.setFilterBy(browseDataResponse->getFilterBy());
  3345. resp.setFilterForGoBack(browseDataResponse->getFilterForGoBack());
  3346. resp.setColumnsHidden(browseDataResponse->getColumnsHidden());
  3347. resp.setColumnsHidden(browseDataResponse->getColumnsHidden());
  3348. resp.setColumnCount(browseDataResponse->getColumnCount());
  3349. resp.setStartForGoback(browseDataResponse->getStartForGoback());
  3350. resp.setCountForGoback(browseDataResponse->getCountForGoback());
  3351. resp.setChooseFile(browseDataResponse->getChooseFile());
  3352. resp.setStart(browseDataResponse->getStart());
  3353. resp.setCount(browseDataResponse->getCount());
  3354. resp.setPageSize(browseDataResponse->getPageSize());
  3355. resp.setTotal(browseDataResponse->getTotal());
  3356. resp.setResult(browseDataResponse->getResult());
  3357. resp.setMsgToDisplay(browseDataResponse->getMsgToDisplay());
  3358. resp.setSchemaOnly(browseDataResponse->getSchemaOnly());
  3359. resp.setAutoUppercaseTranslation(!m_disableUppercaseTranslation);
  3360. }
  3361. }
  3362. catch(IException* e)
  3363. {
  3364. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3365. }
  3366. return true;
  3367. }
  3368. bool CWsDfuEx::onDFUBrowseData(IEspContext &context, IEspDFUBrowseDataRequest &req, IEspDFUBrowseDataResponse &resp)
  3369. {
  3370. try
  3371. {
  3372. if (!context.validateFeatureAccess(FEATURE_URL, SecAccess_Read, false))
  3373. throw MakeStringException(ECLWATCH_DFU_ACCESS_DENIED, "Failed to View Data File. Permission denied.");
  3374. const char* logicalName0 = req.getLogicalName();
  3375. const char* parentName = req.getParentName();
  3376. if (!logicalName0 || !*logicalName0)
  3377. throw MakeStringException(ECLWATCH_INVALID_INPUT,"No LogicalName defined.");
  3378. StringBuffer logicalNameStr;
  3379. if (logicalName0 && *logicalName0)
  3380. {
  3381. logicalNameStr.append(logicalName0);
  3382. logicalNameStr.trim();
  3383. if (logicalNameStr.length() < 1)
  3384. throw MakeStringException(ECLWATCH_INVALID_INPUT,"No LogicalName defined.");
  3385. }
  3386. __int64 start = req.getStart() > 0 ? req.getStart() : 0;
  3387. __int64 count=req.getCount() ? req.getCount() : 20, requested=count;
  3388. if (count > MAX_VIEWKEYFILE_ROWS)
  3389. throw MakeStringException(ECLWATCH_TOO_MANY_DATA_ROWS,"Browser Cannot display more than %d data rows.", MAX_VIEWKEYFILE_ROWS);
  3390. bool bSchemaOnly=req.getSchemaOnly() ? req.getSchemaOnly() : false;
  3391. bool bDisableUppercaseTranslation = req.getDisableUppercaseTranslation() ? req.getDisableUppercaseTranslation() : false;
  3392. #define HPCCBROWSER 1
  3393. #ifdef HPCCBROWSER
  3394. const char* filterBy = req.getFilterBy();
  3395. const char* showColumns = req.getShowColumns();
  3396. __int64 read=0;
  3397. __int64 total = 0;
  3398. StringBuffer msg;
  3399. StringArray columnLabels, columnLabelsType;
  3400. IArrayOf<IEspDFUData> DataList;
  3401. int iRet = GetIndexData(context, bSchemaOnly, logicalNameStr.str(), parentName, filterBy, start, count, read, total, msg, columnLabels, columnLabelsType, DataList, bDisableUppercaseTranslation);
  3402. if (iRet > 0)
  3403. resp.setMsgToDisplay("This search has timed out due to the restrictive filter. There may be more records.");
  3404. //GetIndexData(context, bSchemaOnly, logicalNameStr.str(), "roxie::thor_data400::key::bankruptcyv2::20090721::search::tmsid", filterBy, start, count, read, total, msg, columnLabels, columnLabelsType, DataList);
  3405. resp.setResult(DataList.item(0).getData());
  3406. unsigned int max_name_length = 3; //max length for name length
  3407. unsigned int max_value_length = 4; //max length for value length:
  3408. StringBuffer filterByStr, filterByStr0;
  3409. filterByStr0.appendf("%d%d", max_name_length, max_value_length);
  3410. unsigned columnCount = columnLabels.length();
  3411. IArrayOf<IEspDFUDataColumn> dataColumns;
  3412. double version = context.getClientVersion();
  3413. if (version > 1.04 && columnCount > 0)
  3414. {
  3415. //Find out which columns need to be displayed
  3416. int lenShowCols = 0, showCols[1024];
  3417. const char* showColumns = req.getShowColumns();
  3418. char *pShowColumns = (char*) showColumns;
  3419. while (pShowColumns && *pShowColumns)
  3420. {
  3421. StringBuffer buf;
  3422. while (pShowColumns && isdigit(pShowColumns[0]))
  3423. {
  3424. buf.append(pShowColumns[0]);
  3425. pShowColumns++;
  3426. }
  3427. if (buf.length() > 0)
  3428. {
  3429. showCols[lenShowCols] = atoi(buf.str());
  3430. lenShowCols++;
  3431. }
  3432. if (!pShowColumns || !*pShowColumns)
  3433. break;
  3434. pShowColumns++;
  3435. }
  3436. for(unsigned col = 0; col < columnCount; col++)
  3437. {
  3438. const char* label = columnLabels.item(col);
  3439. const char* type = columnLabelsType.item(col);
  3440. if (!label || !*label || !type || !*type)
  3441. continue;
  3442. Owned<IEspDFUDataColumn> item = createDFUDataColumn("","");
  3443. item->setColumnLabel(label);
  3444. item->setColumnType(type);
  3445. item->setColumnSize(0); //not show this column
  3446. if (!showColumns || !*showColumns)
  3447. {
  3448. item->setColumnSize(1); //Show this column
  3449. }
  3450. else
  3451. {
  3452. for(int col1 = 0; col1 < lenShowCols; col1++)
  3453. {
  3454. if (col == showCols[col1])
  3455. {
  3456. item->setColumnSize(1); //Show this column
  3457. break;
  3458. }
  3459. }
  3460. }
  3461. dataColumns.append(*item.getLink());
  3462. }
  3463. //Re-build filters
  3464. if (filterBy && *filterBy)
  3465. {
  3466. StringArray filterByNames, filterByValues;
  3467. parseTwoStringArrays(filterBy, filterByNames, filterByValues);
  3468. if (filterByNames.length() > 0)
  3469. {
  3470. for (unsigned ii = 0; ii < filterByNames.length(); ii++)
  3471. {
  3472. const char* columnName = filterByNames.item(ii);
  3473. const char* columnValue = filterByValues.item(ii);
  3474. if (columnName && *columnName && columnValue && *columnValue)
  3475. {
  3476. filterByStr.appendf("%s[%s]", columnName, columnValue);
  3477. filterByStr0.appendf("%03d%04d%s%s", (int) strlen(columnName), (int) strlen(columnValue), columnName, columnValue);
  3478. }
  3479. }
  3480. }
  3481. }
  3482. if (req.getStartForGoback())
  3483. resp.setStartForGoback(req.getStartForGoback());
  3484. if (req.getCountForGoback())
  3485. resp.setCountForGoback(req.getCountForGoback());
  3486. }
  3487. #else
  3488. StringBuffer username;
  3489. context.getUserID(username);
  3490. double version = context.getClientVersion();
  3491. const char* passwd = context.queryPassword();
  3492. StringBuffer eclqueue, cluster;
  3493. Owned<IUserDescriptor> userdesc;
  3494. try
  3495. {
  3496. userdesc.setown(createUserDescriptor());
  3497. userdesc->set(username.str(), passwd);
  3498. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalNameStr.str(), userdesc);
  3499. if(df)
  3500. {
  3501. const char* wuid = df->queryAttributes().queryProp("@workunit");
  3502. if (wuid && *wuid)
  3503. {
  3504. CWUWrapper wu(wuid, context);
  3505. if (wu)
  3506. {
  3507. SCMStringBuffer eclqueue0, cluster0;
  3508. eclqueue.append(wu->getQueue(eclqueue0).str());
  3509. cluster.append(wu->getClusterName(cluster0).str());
  3510. }
  3511. }
  3512. }
  3513. }
  3514. catch(...)
  3515. {
  3516. ;
  3517. }
  3518. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), *context.queryUser());
  3519. Owned<INewResultSet> result;
  3520. if (eclqueue && *eclqueue && cluster && *cluster)
  3521. {
  3522. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), eclqueue, cluster));
  3523. }
  3524. else if (m_clusterName.length() > 0 && m_eclServerQueue.length() > 0)
  3525. {
  3526. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), m_eclServerQueue.str(), m_clusterName.str()));
  3527. }
  3528. else
  3529. {
  3530. result.setown(resultSetFactory->createNewFileResultSet(logicalNameStr.str(), NULL, NULL));
  3531. }
  3532. const IResultSetMetaData &meta = result->getMetaData();
  3533. unsigned columnCount = meta.getColumnCount();
  3534. StringArray filterByNames, filterByValues;
  3535. IArrayOf<IEspDFUDataColumn> dataColumns;
  3536. if (version > 1.04 && columnCount > 0)
  3537. {
  3538. int lenShowCols = 0, showCols[1024];
  3539. const char* showColumns = req.getShowColumns();
  3540. char *pShowColumns = (char*) showColumns;
  3541. while (pShowColumns && *pShowColumns)
  3542. {
  3543. StringBuffer buf;
  3544. while (pShowColumns && isdigit(pShowColumns[0]))
  3545. {
  3546. buf.append(pShowColumns[0]);
  3547. pShowColumns++;
  3548. }
  3549. if (buf.length() > 0)
  3550. {
  3551. showCols[lenShowCols] = atoi(buf.str());
  3552. lenShowCols++;
  3553. }
  3554. if (!pShowColumns || !*pShowColumns)
  3555. break;
  3556. pShowColumns++;
  3557. }
  3558. for(int col = 0; col < columnCount; col++)
  3559. {
  3560. Owned<IEspDFUDataColumn> item = createDFUDataColumn("","");
  3561. SCMStringBuffer scmbuf;
  3562. meta.getColumnLabel(scmbuf, col);
  3563. item->setColumnLabel(scmbuf.str());
  3564. if (!showColumns || !*showColumns)
  3565. {
  3566. item->setColumnSize(1); //Show this column
  3567. dataColumns.append(*item.getLink());
  3568. continue;
  3569. }
  3570. else
  3571. {
  3572. item->setColumnSize(0); //not show this column
  3573. }
  3574. for(int col1 = 0; col1 < lenShowCols; col1++)
  3575. {
  3576. if (col == showCols[col1])
  3577. {
  3578. item->setColumnSize(1); //Show this column
  3579. break;
  3580. }
  3581. }
  3582. dataColumns.append(*item.getLink());
  3583. }
  3584. const char* filterBy = req.getFilterBy();
  3585. if (filterBy && *filterBy)
  3586. {
  3587. parseTwoStringArrays(filterBy, filterByNames, filterByValues);
  3588. }
  3589. if (req.getStartForGoback())
  3590. resp.setStartForGoback(req.getStartForGoback());
  3591. if (req.getCountForGoback())
  3592. resp.setCountForGoback(req.getCountForGoback());
  3593. }
  3594. StringBuffer filterByStr, filterByStr0;
  3595. unsigned int max_name_length = 3; //max length for name length
  3596. unsigned int max_value_length = 4; //max length for value length:
  3597. filterByStr0.appendf("%d%d", max_name_length, max_value_length);
  3598. if (columnCount > 0 && filterByNames.length() > 0)
  3599. {
  3600. Owned<IFilteredResultSet> filter = result->createFiltered();
  3601. for (int ii = 0; ii < filterByNames.length(); ii++)
  3602. {
  3603. const char* columnName = filterByNames.item(ii);
  3604. const char* columnValue = filterByValues.item(ii);
  3605. if (columnName && *columnName && columnValue && *columnValue)
  3606. {
  3607. int col = 0;
  3608. for(col = 0; col < columnCount; col++)
  3609. {
  3610. SCMStringBuffer scmbuf;
  3611. meta.getColumnLabel(scmbuf, col);
  3612. if (stricmp(scmbuf.str(), columnName) == 0)
  3613. {
  3614. filter->addFilter(col, columnValue);
  3615. filterByStr.appendf("%s[%s]", columnName, columnValue);
  3616. filterByStr0.appendf("%03d%04d%s%s", strlen(columnName), strlen(columnValue), columnName, columnValue);
  3617. break;
  3618. }
  3619. }
  3620. if (col == columnCount)
  3621. {
  3622. throw MakeStringException(0,"The filter %s not defined", columnName);
  3623. }
  3624. }
  3625. }
  3626. result.setown(filter->create());
  3627. }
  3628. StringBuffer text;
  3629. const char* schemaName = "myschema";
  3630. Owned<IResultSetCursor> cursor = result->createCursor();
  3631. text.append("<XmlSchema name=\"").append(schemaName).append("\">");
  3632. const IResultSetMetaData & meta1 = cursor->queryResultSet()->getMetaData();
  3633. StringBufferAdaptor adaptor(text);
  3634. meta1.getXmlSchema(adaptor, false);
  3635. text.append("</XmlSchema>").newline();
  3636. text.append("<Dataset");
  3637. //if (name)
  3638. // text.append(" name=\"").append(name).append("\" ");
  3639. text.append(" xmlSchema=\"").append(schemaName).append("\" ");
  3640. text.append(">").newline();
  3641. //__int64 total=0;
  3642. __int64 total=result->getNumRows();
  3643. __int64 read=0;
  3644. try
  3645. {
  3646. for(bool ok=cursor->absolute(start);ok;ok=cursor->next())
  3647. {
  3648. //total++;
  3649. //if(read < count)
  3650. {
  3651. text.append(" ");
  3652. StringBufferAdaptor adaptor2(text);
  3653. cursor->getXmlRow(adaptor2);
  3654. text.newline();
  3655. read++;
  3656. }
  3657. if(read>=count)
  3658. break;
  3659. }
  3660. }
  3661. catch(IException* e)
  3662. {
  3663. if ((version < 1.08) || (e->errorCode() != FVERR_FilterTooRestrictive))
  3664. throw e;
  3665. e->Release();
  3666. resp.setMsgToDisplay("This search is timed out due to the restrictive filter. There may be more records.");
  3667. }
  3668. if (count > read)
  3669. count = read;
  3670. text.append("</Dataset>").newline();
  3671. ///DBGLOG("Dataset:%s", text.str());
  3672. MemoryBuffer buf;
  3673. struct MemoryBuffer2IStringVal : public CInterface, implements IStringVal
  3674. {
  3675. MemoryBuffer2IStringVal(MemoryBuffer & _buffer) : buffer(_buffer) {}
  3676. IMPLEMENT_IINTERFACE;
  3677. virtual const char * str() const { UNIMPLEMENTED; }
  3678. virtual void set(const char *val) { buffer.append(strlen(val),val); }
  3679. virtual void clear() { } // clearing when appending does nothing
  3680. virtual void setLen(const char *val, unsigned length) { buffer.append(length, val); }
  3681. virtual unsigned length() const { return buffer.length(); };
  3682. MemoryBuffer & buffer;
  3683. } adaptor0(buf);
  3684. adaptor0.set(text.str());
  3685. buf.append(0);
  3686. resp.setResult(buf.toByteArray());
  3687. #endif
  3688. //resp.setFilterBy(filterByStr.str());
  3689. if (filterByStr.length() > 0)
  3690. {
  3691. const char* oldStr = "&";
  3692. const char* newStr = "&amp;";
  3693. filterByStr.replaceString(oldStr, newStr);
  3694. resp.setFilterBy(filterByStr.str());
  3695. }
  3696. if (version > 1.04)
  3697. {
  3698. //resp.setFilterForGoBack(filterByStr0.str());
  3699. if (filterByStr0.length() > 0)
  3700. {
  3701. const char* oldStr = "&";
  3702. const char* newStr = "&amp;";
  3703. filterByStr0.replaceString(oldStr, newStr);
  3704. resp.setFilterForGoBack(filterByStr0.str());
  3705. }
  3706. resp.setColumnCount(columnCount);
  3707. if (dataColumns.length() > 0)
  3708. resp.setColumnsHidden(dataColumns);
  3709. }
  3710. if (version > 1.10)
  3711. {
  3712. resp.setSchemaOnly(bSchemaOnly);
  3713. }
  3714. //resp.setName(name.str());
  3715. resp.setLogicalName(logicalNameStr.str());
  3716. resp.setStart(start);
  3717. //if (requested > read)
  3718. // requested = read;
  3719. resp.setPageSize(requested);
  3720. if (count > read)
  3721. {
  3722. count = read;
  3723. }
  3724. resp.setCount(count);
  3725. if (total != UNKNOWN_NUM_ROWS)
  3726. resp.setTotal(total);
  3727. else
  3728. resp.setTotal(-1);
  3729. if (req.getChooseFile())
  3730. resp.setChooseFile(1);
  3731. else
  3732. resp.setChooseFile(0);
  3733. if (version > 1.11)
  3734. {
  3735. if (req.getCluster() && *req.getCluster())
  3736. {
  3737. resp.setCluster(req.getCluster());
  3738. }
  3739. if (req.getClusterType() && *req.getClusterType())
  3740. {
  3741. resp.setClusterType(req.getClusterType());
  3742. }
  3743. }
  3744. if ((version > 1.12) && parentName && *parentName)
  3745. {
  3746. resp.setParentName(parentName);
  3747. }
  3748. }
  3749. catch(IException* e)
  3750. {
  3751. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3752. }
  3753. return true;
  3754. }
  3755. void CWsDfuEx::getRoxieClusterConfig(char const * clusterType, char const * clusterName, char const * processName, StringBuffer& netAddress, int& port)
  3756. {
  3757. #if 0
  3758. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  3759. Owned<IConstEnvironment> environment = factory->openEnvironment();
  3760. Owned<IPropertyTree> pRoot = &environment->getPTree();
  3761. #else
  3762. CTpWrapper dummy;
  3763. Owned<IPropertyTree> pRoot = dummy.getEnvironment("");
  3764. if (!pRoot)
  3765. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO,"Failed to get environment information.");
  3766. #endif
  3767. StringBuffer xpath;
  3768. xpath.appendf("Software/%s[@name='%s']", clusterType, clusterName);
  3769. IPropertyTree* pCluster = pRoot->queryPropTree( xpath.str() );
  3770. if (!pCluster)
  3771. throw MakeStringException(ECLWATCH_CLUSTER_NOT_IN_ENV_INFO, "'%s %s' is not defined!", clusterType, clusterName);
  3772. xpath.clear().append(processName);
  3773. xpath.append("@computer");
  3774. const char* computer = pCluster->queryProp(xpath.str());
  3775. if (!computer || strlen(computer) < 1)
  3776. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_INFO, "'%s %s: %s' is not defined!", clusterType, clusterName, processName);
  3777. xpath.clear().append(processName);
  3778. xpath.append("@port");
  3779. const char* portStr = pCluster->queryProp(xpath.str());
  3780. port = ROXIE_SERVER_PORT;
  3781. if (portStr && *portStr)
  3782. {
  3783. port = atoi(portStr);
  3784. }
  3785. #if 0
  3786. Owned<IConstMachineInfo> pMachine = environment->getMachine(computer);
  3787. if (pMachine)
  3788. {
  3789. SCMStringBuffer scmNetAddress;
  3790. pMachine->getNetAddress(scmNetAddress);
  3791. netAddress = scmNetAddress.str();
  3792. }
  3793. #else
  3794. xpath.clear().appendf("Hardware/Computer[@name=\"%s\"]", computer);
  3795. IPropertyTree* pMachine = pRoot->queryPropTree( xpath.str() );
  3796. if (pMachine)
  3797. {
  3798. const char* addr = pMachine->queryProp("@netAddress");
  3799. if (addr && *addr)
  3800. netAddress.append(addr);
  3801. }
  3802. #endif
  3803. return;
  3804. }
  3805. //////////////////////HPCC Browser//////////////////////////
  3806. static const char* SCHEMANAME = "myschema";
  3807. //void CWsDfuEx::setRootFilter(INewResultSet* result, const char* filterBy, IFilteredResultSet* filter)
  3808. void CWsDfuEx::setRootFilter(INewResultSet* result, const char* filterBy, IResultSetFilter* filter, bool disableUppercaseTranslation)
  3809. {
  3810. if (!filterBy || !*filterBy || !result)
  3811. return;
  3812. //Owned<IFilteredResultSet> filter = result->createFiltered();
  3813. filter->clearFilters();
  3814. const IResultSetMetaData &meta = result->getMetaData();
  3815. unsigned columnCount = meta.getColumnCount();
  3816. if (columnCount < 1)
  3817. return;
  3818. StringArray filterByNames, filterByValues;
  3819. parseTwoStringArrays(filterBy, filterByNames, filterByValues);
  3820. if (filterByNames.length() < 1)
  3821. return;
  3822. for (unsigned ii = 0; ii < filterByNames.length(); ii++)
  3823. {
  3824. const char* columnName = filterByNames.item(ii);
  3825. const char* columnValue0 = filterByValues.item(ii);
  3826. if (!columnName || !*columnName || !columnValue0 || !*columnValue0)
  3827. continue;
  3828. StringBuffer buf(columnValue0);
  3829. if (!disableUppercaseTranslation)
  3830. buf.toUpperCase();
  3831. const char* columnValue = buf.str();
  3832. for(unsigned col = 0; col < columnCount; col++)
  3833. {
  3834. bool hasSetTranslation = false;
  3835. SCMStringBuffer scmbuf;
  3836. if (meta.hasSetTranslation(col))
  3837. {
  3838. hasSetTranslation = true;
  3839. meta.getNaturalColumnLabel(scmbuf, col);
  3840. }
  3841. if (scmbuf.length() < 1)
  3842. {
  3843. meta.getColumnLabel(scmbuf, col);
  3844. }
  3845. if (!stricmp(scmbuf.str(), columnName))
  3846. {
  3847. //filter->addFilter(col, columnValue);
  3848. //filterByStr.appendf("%s[%s]", columnName, columnValue);
  3849. //filterByStr0.appendf("%03d%04d%s%s", strlen(columnName), strlen(columnValue), columnName, columnValue);
  3850. if (hasSetTranslation)
  3851. filter->addNaturalFilter(col, strlen(columnValue), columnValue);
  3852. else
  3853. filter->addFilter(col, strlen(columnValue), columnValue);
  3854. break;
  3855. }
  3856. }
  3857. }
  3858. //result.setown(filter->create());
  3859. return;
  3860. }
  3861. void CWsDfuEx::getMappingColumns(IRelatedBrowseFile * file, bool isPrimary, UnsignedArray& cols)
  3862. {
  3863. const char* logicalName = file->queryDefinition()->queryDistributedFile()->queryLogicalName();
  3864. const char* primaryName = file->queryParentRelation()->queryFileRelationship()->queryPrimaryFilename();
  3865. if (!logicalName || !primaryName || strcmp(logicalName, primaryName))
  3866. return;
  3867. IViewRelation* parentRelation = file->queryParentRelation();
  3868. for (unsigned i=0; i < parentRelation->numMappingFields(); i++)
  3869. {
  3870. //find out the column numbers to remove
  3871. unsigned col = parentRelation->queryMappingField(i, isPrimary);
  3872. cols.append(col);
  3873. }
  3874. #ifdef TESTDATASET
  3875. cols.kill();
  3876. cols.append(2);
  3877. cols.append(3);
  3878. #endif
  3879. return;
  3880. }
  3881. void CWsDfuEx::readColumnsForDisplay(StringBuffer& schemaText, StringArray& columnsDisplay, StringArray& columnsDisplayType)
  3882. {
  3883. if (schemaText.length() < 1)
  3884. return;
  3885. Owned<IPropertyTree> schema = createPTreeFromXMLString(schemaText.str());
  3886. if (!schema)
  3887. return;
  3888. //Find out labels from the second schema used for column mapping
  3889. columnsDisplay.kill();
  3890. Owned<IPropertyTreeIterator> rows4 = schema->getElements("xs:element[@name=\"Dataset\"]/xs:complexType/xs:sequence/xs:element[@name=\"Row\"]/xs:complexType/xs:sequence/*");
  3891. ForEach(*rows4)
  3892. {
  3893. IPropertyTree &e = rows4->query();
  3894. const char* name = e.queryProp("@name");
  3895. const char* type = e.queryProp("@type");
  3896. bool hasChildren = e.hasChildren();
  3897. if (!name || !*name)
  3898. continue;
  3899. columnsDisplay.append(name); //Display this column
  3900. if (type && *type)
  3901. columnsDisplayType.append(type);
  3902. else if (hasChildren)
  3903. columnsDisplayType.append("Object");
  3904. else
  3905. columnsDisplayType.append("Unknown");
  3906. }
  3907. return;
  3908. }
  3909. void CWsDfuEx::mergeSchema(IRelatedBrowseFile * file, StringBuffer& schemaText, StringBuffer schemaText2,
  3910. StringArray& columnsDisplay, StringArray& columnsDisplayType, StringArray& columnsHide)
  3911. {
  3912. if (schemaText.length() < 1)
  3913. return;
  3914. if (schemaText2.length() < 1)
  3915. return;
  3916. //DBGLOG("First schema returns:%s", schemaText.str());
  3917. //DBGLOG("Second schema returns:%s", schemaText2.str());
  3918. Owned<IPropertyTree> schema = createPTreeFromXMLString(schemaText.str());
  3919. Owned<IPropertyTree> schema2 = createPTreeFromXMLString(schemaText2.str());
  3920. if (!schema || !schema2)
  3921. return;
  3922. //Process simpleType part
  3923. Owned<IPropertyTreeIterator> rows1 = schema->getElements("xs:simpleType");
  3924. Owned<IPropertyTreeIterator> rows2 = schema2->getElements("xs:simpleType");
  3925. if (!rows1 || !rows2)
  3926. return;
  3927. ForEach(*rows2)
  3928. {
  3929. IPropertyTree &e = rows2->query();
  3930. const char* name = e.queryProp("@name");
  3931. if (!name || !*name)
  3932. continue;
  3933. bool bFound = false;
  3934. ForEach(*rows1)
  3935. {
  3936. IPropertyTree &e1 = rows1->query();
  3937. const char* name1 = e1.queryProp("@name");
  3938. if (!name1 || !*name1 || stricmp(name1, name))
  3939. continue;
  3940. bFound = true;
  3941. break;
  3942. }
  3943. if (!bFound)
  3944. schema->addPropTree(e.queryName(), LINK(&e));
  3945. }
  3946. IPropertyTree* rows = schema->queryBranch("xs:element[@name=\"Dataset\"]/xs:complexType/xs:sequence/xs:element[@name=\"Row\"]/xs:complexType/xs:sequence");
  3947. if (!rows)
  3948. return;
  3949. // StringBuffer schemaText4;
  3950. // toXML(schema, schemaText4);
  3951. //DBGLOG("First schema returns:%s", schemaText4.str());
  3952. //Find out labels used for column mapping
  3953. columnsDisplay.kill();
  3954. columnsDisplayType.kill();
  3955. columnsHide.kill();
  3956. Owned<IPropertyTreeIterator> rows4 = schema->getElements("xs:element[@name=\"Dataset\"]/xs:complexType/xs:sequence/xs:element[@name=\"Row\"]/xs:complexType/xs:sequence/*");
  3957. ForEach(*rows4)
  3958. {
  3959. IPropertyTree &e = rows4->query();
  3960. const char* name = e.queryProp("@name");
  3961. const char* type = e.queryProp("@type");
  3962. bool hasChildren = e.hasChildren();
  3963. if (!name || !*name)
  3964. continue;
  3965. columnsDisplay.append(name); //Display this column
  3966. if (type && *type)
  3967. columnsDisplayType.append(type);
  3968. else if (hasChildren)
  3969. columnsDisplayType.append("Object");
  3970. else
  3971. columnsDisplayType.append("Unknown");
  3972. }
  3973. UnsignedArray cols;
  3974. bool isPrimary = true;
  3975. getMappingColumns(file, isPrimary, cols);
  3976. //Process complexType part for labels
  3977. unsigned col0 = 0;
  3978. Owned<IPropertyTreeIterator> rows3 = schema2->getElements("xs:element[@name=\"Dataset\"]/xs:complexType/xs:sequence/xs:element[@name=\"Row\"]/xs:complexType/xs:sequence/*");
  3979. ForEach(*rows3)
  3980. {
  3981. IPropertyTree &e = rows3->query();
  3982. const char* name = e.queryProp("@name");
  3983. const char* type = e.queryProp("@type");
  3984. bool hasChildren = e.hasChildren();
  3985. if (!name || !*name)
  3986. continue;
  3987. bool bAdd = true;
  3988. bool bRename = false;
  3989. if (cols.ordinality() != 0)
  3990. {
  3991. ForEachItemIn(i1,cols)
  3992. {
  3993. unsigned col = cols.item(i1);
  3994. if (col == col0)
  3995. {
  3996. bAdd = false;
  3997. break;
  3998. }
  3999. }
  4000. }
  4001. #define RENAMESAMECOLUMN
  4002. #ifdef RENAMESAMECOLUMN
  4003. if (columnsDisplay.length() > 0)
  4004. {
  4005. for (unsigned i = 0; i < columnsDisplay.length(); i++)
  4006. {
  4007. const char* label = columnsDisplay.item(i);
  4008. if (!label || strcmp(label, name))
  4009. continue;
  4010. bRename = true;
  4011. break;
  4012. }
  4013. }
  4014. #endif
  4015. if (!bAdd)
  4016. {
  4017. columnsHide.append(name); //hide this column
  4018. }
  4019. else
  4020. {
  4021. if (type && *type)
  4022. columnsDisplayType.append(type);
  4023. else if (hasChildren)
  4024. columnsDisplayType.append("Object");
  4025. else
  4026. columnsDisplayType.append("Unknown");
  4027. #ifdef RENAMESAMECOLUMN
  4028. if (bRename)
  4029. {
  4030. StringBuffer newName(name);
  4031. newName.append("-2");
  4032. columnsDisplay.append(newName.str()); //Display this column
  4033. e.setProp("@name", newName.str());
  4034. rows->addPropTree(e.queryName(), LINK(&e));
  4035. }
  4036. else
  4037. {
  4038. #endif
  4039. columnsDisplay.append(name); //Display this column
  4040. rows->addPropTree(e.queryName(), LINK(&e));
  4041. #ifdef RENAMESAMECOLUMN
  4042. }
  4043. #endif
  4044. }
  4045. col0++;
  4046. }
  4047. //Convert schema tree to schame now
  4048. schemaText.clear();
  4049. toXML(schema, schemaText);
  4050. //DBGLOG("Merged schema returns:%s", schemaText.str());
  4051. return;
  4052. }
  4053. void CWsDfuEx::mergeDataRow(StringBuffer& newRow, int depth, IPropertyTreeIterator* it, StringArray& columnsHide, StringArray& columnsUsed)
  4054. {
  4055. if (!it)
  4056. return;
  4057. it->first();
  4058. while(it->isValid())
  4059. {
  4060. IPropertyTree* e = &it->query();
  4061. if (e)
  4062. {
  4063. const char* label = e->queryName();
  4064. if (label && *label)
  4065. {
  4066. #ifdef RENAMESAMECOLUMN
  4067. if (depth < 1)
  4068. columnsUsed.append(label);
  4069. #endif
  4070. bool bHide = false;
  4071. if (columnsHide.length() > 0)
  4072. {
  4073. for (unsigned i = 0 ; i < columnsHide.length(); i++)
  4074. {
  4075. const char* key = columnsHide.item(i);
  4076. if (!key || strcmp(key, label))
  4077. continue;
  4078. bHide = true;
  4079. break;
  4080. }
  4081. }
  4082. #ifdef RENAMESAMECOLUMN
  4083. if (!bHide && depth > 0 && columnsUsed.length() > 0)
  4084. {
  4085. for (unsigned i = 0 ; i < columnsUsed.length(); i++)
  4086. {
  4087. const char* key = columnsUsed.item(i);
  4088. if (!key || strcmp(key, label))
  4089. continue;
  4090. StringBuffer newName(label);
  4091. newName.append("-2");
  4092. e->renameProp("/", newName.str());
  4093. break;
  4094. }
  4095. }
  4096. #endif
  4097. if (!bHide)
  4098. {
  4099. StringBuffer dataRow;
  4100. toXML(e, dataRow);
  4101. newRow.append(dataRow);
  4102. }
  4103. }
  4104. }
  4105. it->next();
  4106. }
  4107. return;
  4108. }
  4109. void CWsDfuEx::mergeDataRow(StringBuffer& newRow, StringBuffer dataRow1, StringBuffer dataRow2, StringArray& columnsHide)
  4110. {
  4111. if (dataRow1.length() < 1)
  4112. return;
  4113. if (dataRow2.length() < 1)
  4114. return;
  4115. Owned<IPropertyTree> data1 = createPTreeFromXMLString(dataRow1.str());
  4116. Owned<IPropertyTree> data2 = createPTreeFromXMLString(dataRow2.str());
  4117. if (!data1 || !data2)
  4118. return;
  4119. newRow.clear();
  4120. newRow.append("<Row>");
  4121. StringArray columnLabels;
  4122. IPropertyTreeIterator* it = data1->getElements("*");
  4123. if (it)
  4124. {
  4125. StringArray columnLabels0;
  4126. mergeDataRow(newRow, 0, it, columnLabels0, columnLabels);
  4127. }
  4128. IPropertyTreeIterator* it2 = data2->getElements("*");
  4129. if (it2)
  4130. {
  4131. mergeDataRow(newRow, 1, it2, columnsHide, columnLabels);
  4132. }
  4133. newRow.append("</Row>");
  4134. return;
  4135. }
  4136. void CWsDfuEx::browseRelatedFileSchema(IRelatedBrowseFile * file, const char* parentName, unsigned depth, StringBuffer& schemaText,
  4137. StringArray& columnsDisplay, StringArray& columnsDisplayType, StringArray& columnsHide)
  4138. {
  4139. //if (file in set of files to display or iterate)
  4140. IResultSetCursor * cursor = file->queryCursor();
  4141. if (cursor && cursor->first())
  4142. {
  4143. if (depth < 1)
  4144. {
  4145. const IResultSetMetaData & meta = cursor->queryResultSet()->getMetaData();
  4146. StringBufferAdaptor adaptor(schemaText);
  4147. meta.getXmlSchema(adaptor, false);
  4148. #ifdef TESTDATASET
  4149. schemaText.clear();
  4150. schemaText.append("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\"");
  4151. schemaText.append(" attributeFormDefault=\"unqualified\">");
  4152. schemaText.append("<xs:element name=\"Dataset\"><xs:complexType><xs:sequence minOccurs=\"0\" maxOccurs=\"unbounded\">");
  4153. schemaText.append("<xs:element name=\"Row\"><xs:complexType><xs:sequence>");
  4154. schemaText.append("<xs:element name=\"state\" type=\"string2\"/>");
  4155. schemaText.append("<xs:element name=\"rtype\" type=\"string2\"/>");
  4156. schemaText.append("<xs:element name=\"id\" type=\"string20\"/>");
  4157. schemaText.append("<xs:element name=\"seq\" type=\"xs:nonNegativeInteger\"/>");
  4158. schemaText.append("<xs:element name=\"num\" type=\"xs:nonNegativeInteger\"/>");
  4159. schemaText.append("<xs:element name=\"date\" type=\"string8\"/>");
  4160. schemaText.append("<xs:element name=\"imglength\" type=\"xs:nonNegativeInteger\"/>");
  4161. schemaText.append("<xs:element name=\"__filepos\" type=\"xs:nonNegativeInteger\"/>");
  4162. schemaText.append("</xs:sequence></xs:complexType></xs:element>");
  4163. schemaText.append("</xs:sequence></xs:complexType></xs:element>");
  4164. schemaText.append("<xs:simpleType name=\"string2\"><xs:restriction base=\"xs:string\"><xs:maxLength value=\"2\"/>");
  4165. schemaText.append("</xs:restriction></xs:simpleType>");
  4166. schemaText.append("<xs:simpleType name=\"string20\"><xs:restriction base=\"xs:string\"><xs:maxLength value=\"20\"/>");
  4167. schemaText.append("</xs:restriction></xs:simpleType>");
  4168. schemaText.append("<xs:simpleType name=\"string8\"><xs:restriction base=\"xs:string\"><xs:maxLength value=\"8\"/>");
  4169. schemaText.append("</xs:restriction></xs:simpleType>");
  4170. schemaText.append("</xs:schema>");
  4171. #endif
  4172. readColumnsForDisplay(schemaText, columnsDisplay, columnsDisplayType);
  4173. }
  4174. else
  4175. {
  4176. StringBuffer schemaText0;
  4177. const IResultSetMetaData & meta = cursor->queryResultSet()->getMetaData();
  4178. StringBufferAdaptor adaptor(schemaText0);
  4179. meta.getXmlSchema(adaptor, false);
  4180. #ifdef TESTDATASET
  4181. schemaText0.clear();
  4182. schemaText0.append("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\"");
  4183. schemaText0.append(" attributeFormDefault=\"unqualified\">");
  4184. schemaText0.append("<xs:element name=\"Dataset\"><xs:complexType><xs:sequence minOccurs=\"0\" maxOccurs=\"unbounded\">");
  4185. schemaText0.append("<xs:element name=\"Row\"><xs:complexType><xs:sequence>");
  4186. schemaText0.append("<xs:element name=\"date_first_reported\" type=\"string12\"/>");
  4187. schemaText0.append("<xs:element name=\"msa\" type=\"string8\"/>");
  4188. schemaText0.append("<xs:element name=\"sid\" type=\"string20\"/>");
  4189. schemaText0.append("<xs:element name=\"seq\" type=\"xs:nonNegativeInteger\"/>"); //not add
  4190. schemaText0.append("</xs:sequence></xs:complexType></xs:element>");
  4191. schemaText0.append("</xs:sequence></xs:complexType></xs:element>");
  4192. schemaText0.append("<xs:simpleType name=\"string12\"><xs:restriction base=\"xs:string\"><xs:maxLength value=\"12\"/>");
  4193. schemaText0.append("</xs:restriction></xs:simpleType>");
  4194. schemaText0.append("</xs:schema>");
  4195. #endif
  4196. mergeSchema(file, schemaText, schemaText0, columnsDisplay, columnsDisplayType, columnsHide);
  4197. }
  4198. if (parentName && *parentName)
  4199. {
  4200. for (unsigned i = 0;;i++)
  4201. {
  4202. IRelatedBrowseFile * next = file->queryChild(i);
  4203. if (!next)
  4204. break;
  4205. IViewRelatedFile * viewRelatedFile = next->queryDefinition();
  4206. if (!viewRelatedFile)
  4207. continue;
  4208. IDistributedFile * file = viewRelatedFile->queryDistributedFile();
  4209. if (!file)
  4210. continue;
  4211. const char* logicName0 = file->queryLogicalName();
  4212. if (logicName0 && !strcmp(logicName0, parentName))
  4213. browseRelatedFileSchema(next, NULL, depth+1, schemaText, columnsDisplay, columnsDisplayType, columnsHide);
  4214. }
  4215. }
  4216. }
  4217. return;
  4218. }
  4219. int CWsDfuEx::browseRelatedFileDataSet(double version, IRelatedBrowseFile * file, const char* parentName, unsigned depth, __int64 start, __int64& count, __int64& read,
  4220. StringArray& columnsHide, StringArray& dataSetOutput)
  4221. {
  4222. int iRet = 0;
  4223. int rows = 0;
  4224. try
  4225. {
  4226. //if (file in set of files to display or iterate)
  4227. IResultSetCursor * cursor = file->queryCursor();
  4228. if (cursor->first())
  4229. {
  4230. for(bool ok=cursor->absolute(start);ok;ok=cursor->next())
  4231. {
  4232. if (rows > 200)
  4233. throw MakeStringException(ECLWATCH_TOO_MANY_DATA_ROWS,"Too many data rows selected.");
  4234. StringBuffer text;
  4235. StringBufferAdaptor adaptor2(text);
  4236. cursor->getXmlRow(adaptor2);
  4237. //DBGLOG("Data row returns:%s", text.str());
  4238. #ifdef TESTDATASET
  4239. text.clear();
  4240. if (depth < 1)
  4241. {
  4242. if (rows < 1)
  4243. {
  4244. text.append("<Row><state>AA</state><rtype>ab</rtype><id>abc</id><seq>12</seq></Row>");
  4245. }
  4246. else if (rows < 2)
  4247. {
  4248. text.append("<Row><state>BB</state><rtype>ba</rtype><id>abc</id><seq>13</seq></Row>");
  4249. }
  4250. else if (rows < 3)
  4251. {
  4252. text.append("<Row><state>CC</state><rtype>ca</rtype><id>bcd</id><seq>13</seq></Row>");
  4253. }
  4254. else
  4255. {
  4256. break;
  4257. }
  4258. }
  4259. else
  4260. {
  4261. if (read < 1)
  4262. {
  4263. if (rows > 0)
  4264. break;
  4265. text.append("<Row><date_first_reported>20090511</date_first_reported><msa>6200</msa><sid>abc</sid><seq>12</seq></Row>");
  4266. }
  4267. else if (read < 2)
  4268. {
  4269. if (rows > 0)
  4270. break;
  4271. text.append("<Row><date_first_reported>20090512</date_first_reported><msa>6201</msa><sid>abc</sid><seq>13</seq></Row>");
  4272. }
  4273. else if (read < 3)
  4274. {
  4275. if (rows > 1)
  4276. break;
  4277. else if (rows > 0)
  4278. text.append("<Row><date_first_reported>20090514</date_first_reported><msa>6203</msa><sid>bcd</sid><seq>13</seq></Row>");
  4279. else
  4280. text.append("<Row><date_first_reported>20090513</date_first_reported><msa>6202</msa><sid>bcd</sid><seq>13</seq></Row>");
  4281. }
  4282. }
  4283. rows++;
  4284. #endif
  4285. StringArray dataSetOutput0;
  4286. if (parentName && *parentName)
  4287. {
  4288. for (unsigned i = 0;;i++)
  4289. {
  4290. IRelatedBrowseFile * next = file->queryChild(i);
  4291. if (!next)
  4292. break;
  4293. IViewRelatedFile * viewRelatedFile = next->queryDefinition();
  4294. if (!viewRelatedFile)
  4295. continue;
  4296. IDistributedFile * file = viewRelatedFile->queryDistributedFile();
  4297. if (!file)
  4298. continue;
  4299. const char* logicName0 = file->queryLogicalName();
  4300. if (logicName0 && !strcmp(logicName0, parentName))
  4301. iRet = browseRelatedFileDataSet(version, next, NULL, depth+1, 0, count, read, columnsHide, dataSetOutput0);
  4302. }
  4303. }
  4304. if (dataSetOutput0.length() < 1)
  4305. {
  4306. dataSetOutput.append(text);
  4307. }
  4308. else
  4309. {
  4310. for (unsigned ii = 0; ii<dataSetOutput0.length(); ii++)
  4311. {
  4312. StringBuffer text0;
  4313. StringBuffer text1 = dataSetOutput0.item(ii);
  4314. if (text1.length() > 0)
  4315. {
  4316. mergeDataRow(text0, text, text1, columnsHide);
  4317. //DBGLOG("New row returns:%s", text0.str());
  4318. }
  4319. dataSetOutput.append(text0);
  4320. }
  4321. }
  4322. if (depth < 1)
  4323. {
  4324. read++;
  4325. if(read>=count)
  4326. break;
  4327. }
  4328. }
  4329. if (depth < 1)
  4330. {
  4331. if (count > read)
  4332. count = read;
  4333. }
  4334. }
  4335. }
  4336. catch(IException* e)
  4337. {
  4338. if ((version < 1.08) || (e->errorCode() != FVERR_FilterTooRestrictive))
  4339. throw e;
  4340. e->Release();
  4341. iRet = 1;
  4342. }
  4343. return iRet;
  4344. }
  4345. //sample filterBy: 340020001id1
  4346. //sample data: <XmlSchema name="myschema">...</XmlSchema><Dataset xmlSchema="myschema">...</Dataset>
  4347. int CWsDfuEx::GetIndexData(IEspContext &context, bool bSchemaOnly, const char* indexName, const char* parentName, const char* filterBy, __int64 start,
  4348. __int64& count, __int64& read, __int64& total, StringBuffer& message, StringArray& columnLabels,
  4349. StringArray& columnLabelsType, IArrayOf<IEspDFUData>& DataList, bool webDisableUppercaseTranslation)
  4350. {
  4351. if (!indexName || !*indexName)
  4352. return -1;
  4353. double version = context.getClientVersion();
  4354. StringBuffer username;
  4355. context.getUserID(username);
  4356. const char* passwd = context.queryPassword();
  4357. StringBuffer cluster;
  4358. Owned<IUserDescriptor> userdesc;
  4359. bool disableUppercaseTranslation = false;
  4360. Owned<IDistributedFile> df;
  4361. try
  4362. {
  4363. userdesc.setown(createUserDescriptor());
  4364. userdesc->set(username.str(), passwd);
  4365. df.setown(queryDistributedFileDirectory().lookup(indexName, userdesc));
  4366. if(!df)
  4367. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Could not find file %s.", indexName);
  4368. //Check disableUppercaseTranslation
  4369. StringBuffer mapping;
  4370. df->getColumnMapping(mapping);
  4371. if (mapping.length() > 37 && strstr(mapping.str(), "word{set(stringlib.StringToLowerCase)}"))
  4372. disableUppercaseTranslation = true;
  4373. else if (webDisableUppercaseTranslation)
  4374. disableUppercaseTranslation = webDisableUppercaseTranslation;
  4375. else
  4376. disableUppercaseTranslation = m_disableUppercaseTranslation;
  4377. const char* wuid = df->queryAttributes().queryProp("@workunit");
  4378. if (wuid && *wuid)
  4379. {
  4380. CWUWrapper wu(wuid, context);
  4381. if (wu)
  4382. {
  4383. SCMStringBuffer cluster0;
  4384. cluster.append(wu->getClusterName(cluster0).str());
  4385. }
  4386. }
  4387. }
  4388. catch (IException *e)
  4389. {
  4390. DBGLOG(e);
  4391. e->Release();
  4392. }
  4393. catch(...)
  4394. {
  4395. DBGLOG("Unknown Exception - view data file: %s", indexName);
  4396. }
  4397. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
  4398. Owned<IViewFileWeb> web;
  4399. Owned<IUserDescriptor> udesc;
  4400. ISecUser * secUser = context.queryUser();
  4401. if(secUser && secUser->getName() && *secUser->getName())
  4402. {
  4403. udesc.setown(createUserDescriptor());
  4404. udesc->set(secUser->getName(), secUser->credentials().getPassword());
  4405. }
  4406. if (cluster && *cluster)
  4407. {
  4408. web.setown(createViewFileWeb(*resultSetFactory, cluster, udesc.getLink()));
  4409. }
  4410. else if (m_clusterName.length() > 0)
  4411. {
  4412. web.setown(createViewFileWeb(*resultSetFactory, m_clusterName.str(), udesc.getLink()));
  4413. }
  4414. else
  4415. {
  4416. web.setown(createViewFileWeb(*resultSetFactory, NULL, udesc.getLink()));
  4417. }
  4418. ViewGatherOptions options;
  4419. options.primaryDepth = 100; // we want to traverse secondary->primary, but not the reverse
  4420. options.secondaryDepth = 0;
  4421. options.setPayloadFilter(true); // we're only interested in payload links
  4422. char *indexName0 = (char *) indexName;
  4423. Owned<IFileTreeBrowser> browser;
  4424. try
  4425. {
  4426. web->gatherWeb(indexName0, df, options);
  4427. browser.setown(web->createBrowseTree(indexName0));
  4428. }
  4429. catch(IException* e)
  4430. {
  4431. if ((e->errorCode() != FVERR_CouldNotResolveX) || (indexName[0] != '~'))
  4432. {
  4433. throw e;
  4434. }
  4435. else
  4436. {
  4437. e->Release();
  4438. indexName0 = (char *) (indexName+1);
  4439. web->gatherWeb(indexName0, df, options);
  4440. browser.setown(web->createBrowseTree(indexName0));
  4441. }
  4442. }
  4443. Owned<INewResultSet> result;
  4444. if (cluster && *cluster)
  4445. {
  4446. result.setown(resultSetFactory->createNewFileResultSet(indexName0, cluster));
  4447. }
  4448. else if (m_clusterName.length() > 0)
  4449. {
  4450. result.setown(resultSetFactory->createNewFileResultSet(indexName0, m_clusterName.str()));
  4451. }
  4452. else
  4453. {
  4454. result.setown(resultSetFactory->createNewFileResultSet(indexName0, NULL));
  4455. }
  4456. // Apply the filter to the root node
  4457. if (filterBy && *filterBy)
  4458. {
  4459. //Owned<IFilteredResultSet> filter = result->createFiltered();
  4460. IResultSetFilter* filter = browser->queryRootFilter();
  4461. setRootFilter(result, filterBy, filter, disableUppercaseTranslation);
  4462. ///result.setown(filter->create());
  4463. }
  4464. StringBuffer text, schemaText;
  4465. StringArray columnsHide;
  4466. browseRelatedFileSchema(browser->queryRootFile(), parentName, 0, schemaText, columnLabels, columnLabelsType, columnsHide);
  4467. text.appendf("<XmlSchema name=\"%s\">", SCHEMANAME);
  4468. text.append(schemaText);
  4469. text.append("</XmlSchema>").newline();
  4470. int iRet = 0;
  4471. if (!bSchemaOnly)
  4472. {
  4473. StringArray dataSetOutput;
  4474. iRet = browseRelatedFileDataSet(version, browser->queryRootFile(), parentName, 0, start, count, read, columnsHide, dataSetOutput);
  4475. StringBuffer dataSetText;
  4476. dataSetText.appendf("<Dataset xmlSchema=\"%s\" >", SCHEMANAME);
  4477. dataSetText.newline();
  4478. for (unsigned i = 0; i<dataSetOutput.length(); i++)
  4479. {
  4480. StringBuffer text0 = dataSetOutput.item(i);
  4481. if (text0.length() > 0)
  4482. {
  4483. dataSetText.append(text0);
  4484. dataSetText.newline();
  4485. }
  4486. }
  4487. dataSetText.append("</Dataset>");
  4488. text.append(dataSetText.str());
  4489. }
  4490. MemoryBuffer data;
  4491. struct MemoryBuffer2IStringVal : public CInterface, implements IStringVal
  4492. {
  4493. MemoryBuffer2IStringVal(MemoryBuffer & _buffer) : buffer(_buffer) {}
  4494. IMPLEMENT_IINTERFACE;
  4495. virtual const char * str() const { UNIMPLEMENTED; }
  4496. virtual void set(const char *val) { buffer.append(strlen(val),val); }
  4497. virtual void clear() { } // clearing when appending does nothing
  4498. virtual void setLen(const char *val, unsigned length) { buffer.append(length, val); }
  4499. virtual unsigned length() const { return buffer.length(); };
  4500. MemoryBuffer & buffer;
  4501. } adaptor0(data);
  4502. adaptor0.set(text.str());
  4503. data.append(0);
  4504. total=result->getNumRows();
  4505. Owned<IEspDFUData> item = createDFUData("","");
  4506. item->setName(indexName);
  4507. item->setNumRows(total);
  4508. item->setData(data.toByteArray());
  4509. DataList.append(*item.getClear());
  4510. return iRet;
  4511. }
  4512. //////////////////////HPCC Browser//////////////////////////