ws_dfuService.cpp 182 KB

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