# # ITSV GmbH # CCDB - Command and Control Database # # FILE: dquermfile_movemfiles.txt # DESCRIPTION: DQUERY definition for CCDB DQUERY "movemfiles" # @querytitle Mehrere managed Files auf andere physische location verschieben @querydescription verschiebt mehrere Managed Files auf anderen physischen Pfad, Auswahl entweder durch Liste oder WHERE-Clause @group DATAFILE @attributenames whereclause:string:{{whereclauseoptions}},mfileids:string:{{mfileidsoptions}},newdir:string:{{newdiroptions}},newpaths:string:{{newpathsoptions}},newpathqexpression:string:{{newpathqexpressionoptions}},newlpath:string:{{newlpathoptions}},maxFilesToMove:integer:{{mftmoptions}},confmove:noyes:{{confmoveoptions}},ignfileerr:yesno:{{ignfileerroptions}} @whereclauseoptions { labeltext: "Auswahlkriterien", typedesc: "Ausdruck für SQL-WHERE-Clause zur Auswahl der zu verschiebenden managed Files", is_optional: true } @mfileidsoptions { labeltext: "Managed File ID(s)", typedesc: "Liste von Managed-File IDs, die verschoben werden sollen", is_optional: true } @newpathsoptions { labeltext: "Neue physische Pfad(e)", typedesc: "Liste von neuen physischen Pfaden, auf die die Dateien verschoben werden sollen", is_optional: true } @newdiroptions { labeltext: "Neues physisches Verzeichnis", typedesc: "Physisches Verzeichnis, in das die Dateien verschoben werden sollen (ersetzt alles VOR dem LETZTEN \"/\")", is_optional: true } @newpathqexpressionoptions { labeltext: "Berechnungs-Javascript für neuen Pfad", typedesc: "Ein Javascript-Fragment, das aus dem alten Pfad (in this.oldpath) als Ergebnis den neuen Pfad berechnet", is_optional: true } @newlpathoptions { labeltext: "neuer logischer Pfad", typedesc: "neuer logischer Pfad, zu dem die Managed Files verschoben werden sollen, logischer Pfad bleibt unverändert, wenn nicht angegeben", is_optional: true } @mftmoptions { labeltext: "Maximale Anzahl Dateien", typedesc: "Maximale Anzahl von Dateien, die verschoben werden sollen, ergibt die Auswahl mehr, so wird das Verschieben verweigert, Vorgabe ist 15", is_optional: true } @confmoveoptions { labeltext: "Bestätigung", typedesc: "Nur wenn Ja ausgewählt wird, wird das Verschieben auch durchgeführt" } @ignfileerroptions { labeltext: "Fehler bei physikalischer Datei ignorieren", typedesc: "Wenn Ja, dann wird ein Fehler beim Verschieben des physikalischen Datei ignoriert", is_optional: true} @querytype function @function seqtrans.seqtrans @formpagetitle Managed Files verschieben @init.qexpression this.inputresult = new aux.Result({ resulttype: 'dbresult', rows: [[0]], metaData: [ { name: "column0" } ] }); this.path = require('path'); this.options.maxrecsteps = 1000000; ~query.tsteps # # 00: check parameters # qexpression proc: { this.ignfileerr = this.query.hasOwnProperty("ignfileerr")?aux.boolFromString(this.query.ignfileerr):false; this.debug = (aux.boolFromString(this.query.debug) || (this.session && this.session.debug && (this.session.debug=="YES"))); logger.debug(this.phead("begin")+".debug="+this.debug) if (this.debug) logger.debug(this.phead("query: ")+aux.objTxt(this.query)); this.confmove = this.query.hasOwnProperty("confmove")?aux.boolFromString(this.query.confmove):false; if (!this.confmove) { this.errcoll.collect(null,"Verschieben der Managed Files nicht bestätigt (confmove="+this.confmove+")",this.query); break proc; } if (this.query.maxFilesToMove) { this.maxFilesToMove = this.query.maxFilesToMove; } else { this.maxFilesToMove = 15; } if (this.query.mfileids) { let mfids = this.query.mfileids.split(","); this.mfileidlist = new Array(); for (let ii=0; ii this.maxFilesToMove) { this.errcoll.collect(null,"selected number "+this.fmax+" exceeds maximum number of files allowed of "+this.maxFilesToMove,this.query); break proc; } this.fnum = 0; /* iteration counter */ logger.debug(this.phead("FILESTOPROCESS=")+this.fmax); } # # 02: begin of iteration number # nextfile: if (this.fnum>=this.fmax) goto alldone # # 03: prepare for loop number , get data for current MFILE to be moved # aexpression proc: { this.mfileid = this.selres.getCellValue(this.fnum,0); if (!this.mfileid) { this.errcoll.collect(null,"MFILE number "+this.fnum+" has no valid MFILEID",{ selres: this.selres }); break proc; } this.mfilename = this.selres.getCellValue(this.fnum,1); this.mfilepath = this.selres.getCellValue(this.fnum,2); this.mfilelpath = this.selres.getCellValue(this.fnum,4); this.mfileattribs = this.selres.getCellValue(this.fnum,3); this.mfileattributes = aux.txtObjDecode(this.mfileattribs); mfile.get_mfile_data(this.mfileid, function(err,res) { if (err) { this.errcoll.collect(err,this.phead("error in get_mfile_data"),{ err: err, res: res }); this(); } this.oldmfres = aux.deepCopy(res); this(); }.bind(this) ); } # # 04: check if to move to new physical path # if (!(this.newpathlist || this.newpathqexpression || this.newdir)) goto trymovelpath # # 05: move to new physical path, first calculate new path, then asynchronously execute move # domovepath: pre_qexpression proc: { let re = /\//g; this.oldpath = this.oldmfres.mfilepath.replace(re,this.path.sep); if (this.newpathlist) { this.newpath = this.newpathlist[this.fnum]; } else if (this.newdir) { let seppos = this.oldpath.lastIndexOf(this.path.sep); if (seppos<0) { this.newpath = this.newdir + this.path.sep + this.oldpath; } else { this.newpath = this.newdir + this.path.sep + this.oldpath.substring(seppos); } } else if (this.newpathqexpression) { try { this.newpath = eval(this.newpathqexpression); } catch (e) { this.errcoll.collect(e,"error processing newpath qexpression for MFILE number "+this.fnum, { mfileid: this.mfileid, mfilepath: this.mfilepath, oldpath: this.oldpath, newpath: this.newpath, newpathqexpression: this.newpathqexpression } ); break proc; } } else { this.errcoll.collect(null,"neither newpath-list nor newpathqexpression for MFILE number "+this.fnum,this.query); break proc; } } aexpression mfile.moveMfilePFile(this.mfileid,this.newpath,this); post_qexpression this.ppmoveinfo = "OLDPATH="+this.oldpath+", NEWPATH="+this.newpath; # # 06: check if to move to new logical path # trymovelpath: if (!(this.newlpath || this.newlpathqexpression)) goto endmove # # 07: move to new logical path; before, calculate new logical path # pre_qexpression proc: { if (this.newlpath) { this.nlpath = this.newlpath; } else if (this.newlpathqexpression) { try { this.nlpath = eval(this.newlpathqexpression); } catch (e) { this.errcoll.collect(e,"error processing new-logical-path qexpression for MFILE number "+this.fnum, { mfileid: this.mfileid, mfilepath: this.mfilepath, mfilelpath: this.mfilelpath, newlpathqexpression: this.newlpathqexpression } ); break proc; } } else { this.errcoll.collect(null,"neither newlpath nor newlpathqexpression for MFILE number "+this.fnum,this.query); break proc; } } sql update CCMFILES set MFILELPATH=::nlpath:: where MFILEID=::mfileid:: named_params nlpath,mfileid post_qexpression this.lpmoveinfo = "OLDLOGPATH="+this.mfilelpath+", NEWLOGPATH="+this.nlpath; # # 08: end of loop body, rinse and repeat # endmove: pre_qexpression this.ppush([this.phead("FILEPROCESSED"), "NUM="+this.fnum+", MFILEID="+this.mfileid, (this.ppmoveinfo?this.ppmoveinfo:""), (this.lpmoveinfo?this.lpmoveinfo:"")]); this.fnum++; goto nextfile # # 09: all files done # alldone: pre_qexpression this.ppush([this.phead("COMPLETE"),"FILESTOPROCESS="+this.fmax,"FILESPROCESSED="+this.fnum,""]); end