#
#	ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:			dquerymfile_loadCuCCstatMULTI.txt
# 	DESCRIPTION:	dquery definition for loadCuCCstatMULTI
#
@group			SVCLSLSTATSTAT
@querytitle 	Alle noch nicht geladenen CuCC-Statistik-Dateien laden
@description	lädt alle managed files mit CuCC-Statistiken mit Hilfe des Query loadCuCCCallStat, die noch nicht status="loaded" haben
@attributenames	sql_select:string:{{sql_selectoptions}}
@sql_select		SELECT MFILEID,MFILETYPE,MFILENAME,MFILEPATH,MFILELPATH,MFILEATTRIBS FROM CCMFILES 
				 WHERE MFILETYPE IN ('cucccallstat','cucccallcat','cuccemailstat')
				   AND MFILEATTRIBS not like '%"state":"loaded"%' 
				   AND MFILEATTRIBS not like '%"state":"inhibited"%'
@sql_selectoptions	{"labeltext":"Auswahl-SQL-Statement","typedesc":"SQL-Statement für Abfrage in CCMFILES um die zu ladenden Dateien auszuwählen"}
@querytype		function
@function		seqtrans.seqtrans

~query.tsteps

#
# <<TSTEPNUM:0>>: 
#
pre_qexpression
	this.recordinfo = { recnum: this.recnum, mfile: this.cdata.MFILEID };
if	(this.init_done)
goto	record_proper

#
# <<TSTEPNUM:1>>: get MFILE info
#
qexpression
	proc: {
		if (!this.init_done) {
			this.startstime = aux.nowstring();
			this.filesprocessed = 0;
			this.filestoprocess = this.inputresult.getRowCount();
			this.protocol = new Array();
			this.ppush([this.phead("started"),"TIME="+aux.nowsvtime(),"FILES_TO_LOAD="+this.filestoprocess,""]);
		}
	}

#
# 2: init done
#
qexpression
	this.init_done = true;
	
#
# <<TSTEPNUM:3>>: load one file
#
record_proper:
pre_qexpression
	switch (this.cdata.MFILETYPE) {
		case 'cucccallstat':
			this.cdata.filedatatype = 'Call';
			break;
		case 'cuccemailstat':
			this.cdata.filedatatype = 'Email';
			break;
		case 'cucccallcat':
			this.cdata.filedatatype = 'Cat';
			break;
		default:
			this.errcoll.collect(null,"Illegal MFILETYPE \""+this.cdata.MFILETYPE+"\"",this.cdata);
	}
query.dataname		loadCuCCCallStat
query.mfileid		@@MFILEID@@
query.filedatatype	@@filedatatype@@
post_qexpression
	this.filesprocessed++;
	let finfo;
	if (this.stepresult.resulttype=='plain') {
		finfo = this.stepresult.body;
	} else {
		finfo = '<pre>'+aux.objTxt(this.stepresult)+'</pre>';
	}
	this.ppush([this.phead("file_processed"),"MFILEID="+this.cdata.MFILEID,"MFILENAME="+this.cdata.MFILENAME,{ rawhtml: finfo }]);

#
# <<TSTEPNUM:4>>: check if this was the last record
#
if (!this.is_last_record)
goto record_done

#
# <<TSTEPNUM:5>>: finalize protocol, seqtrans will create a visible result from the protocol
#
qexpression
	this.finishtime = aux.nowstring().substring(0,14);
	this.ppush([this.phead("finished"),"TIME="+aux.nowsvtime(),"FILES_PROCESSED="+this.filesprocessed,""]);
	this.query.title = " "+this.filesprocessed+" Dateien mit CuCC-Statistiken geladen";
	this.query.toptext = "<ul><li>Vorgang begonnen: "+this.startstime+"</li><li>Vorgang beendet "+this.finishtime+"</li></ul>";
	this.query.result_filename = "LOADRESULT_"+this.finishtime;

#
# <<TSTEPNUM:6>>: end of record processing
#
record_done:
end

