#
#	ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:				dquerymfile_unloadMVBRFAUSW.txt
#	DESCRIPTION:		DQUERY definition for uloadMVBRFAUSW
#
#
@querytitle				entlade ein oder mehrere Dateien aus CCMVBRFAUSW
@querydescription		entlädt alle Daten aus Dateien, die der <whereclause> in SVCLMAFILES entsprechen, aus CCMVBRFAUSW
@group					SVCLMVBRFAUSWDATALOAD
@attributenames			whereclause:string:{{whereclauseoptions}},mfileid:mfileid:{{mfileidoptions}}
@whereclauseoptions		{"labeltext":"Auswahlkriterien","typedesc":"WHERE-Clause zur Auswahl aus Daten in der Tabelle SVCLMAFILES, Felder sind: MFILEID,TRAEGER,FTIMESTAMP","is_optional":"true"}
@mfileidoptions			{"labeltext":"Managed File","typedesc":"Managed File ID des managed file, das entladen werden soll","is_optional":"true"}
@pre_query
	if (!that.query.whereclause && !that.query.mfileid) {
		aux.error_callback(that.callback,null,"one query attribute of <mfileid> or <whereclause> must be given",that.query);
	}
	that.sql = "SELECT maf.TRAEGER,maf.FTIMESTAMP,mf.MFILEID,mf.MFILENAME,mf.MFILEPATH,mf.MFILEATTRIBS FROM SVCLMAFILES maf JOIN CCMFILES mf on mf.MFILEID=maf.MFILEID";
	that.wcact = false;
	if (that.query.mfileid) {
		if (!this.wact) that.sql += " WHERE "; else that.sql += " AND ";
		that.sql += "(maf.MFILEID='"+that.query.mfileid+"')";
		that.wact = true;
	}
	if (that.query.whereclause) {
		if (!this.wact) that.sql += " WHERE "; else that.sql += " AND ";
		that.sql += "("+that.query.whereclause+")";
		that.wact = true;
	}
	that.query.sql_select = that.sql;
@querytype				function
@function				seqtrans.seqtrans

~query.tsteps

#
# 0: initialize
#
qexpression
	proc: {
		copycvars(this,"TRAEGER,FTIMESTAMP,MFILEID,MFILENAME,MFILEPATH,MFILEATTRIBS");
		if (!this.init_done) {
			this.ppfx = this.fname+"("+this.query.dataname+")";
			this.protocol = new Array();
			this.filesprocessed = 0;
			this.init_done = true;
		}
	}

#
# 1: remove data from CCMVBRFAUSW
#
sql				delete from CCMVBRFAUSW where TRAEGER=::TRAEGER:: and FTIMESTAMP=::FTIMESTAMP::
named_params	TRAEGER,FTIMESTAMP
result_varname	delres
post_qexpression
	this.protocol.push({ type: 'objTxt', object: this.delres });

#
# 2: mark MFILE as "to_load"
#
pre_qexpression
	this.mfattributes = aux.txtObjDecode(this.MFILEATTRIBS);
	this.mfattributes.loadstate = "to_load";
	this.mfattributes.unloadtime = aux.nowstring();
	this.cdata.MFNEWATTRIBS = JSON.stringify(this.mfattributes);
sql				update CCMFILES set MFILEATTRIBS=::MFNEWATTRIBS:: WHERE MFILEID=::MFILEID::
named_params	MFILEID,MFNEWATTRIBS

#
# 3: remove file entry from SVCLMAFILES
#
sql				DELETE FROM SVCLMAFILES WHERE MFILEID=::MFILEID::
named_params	MFILEID

#
# 4: log this file
#
qexpression	
	this.protocol.push({type: 'row', row: [this.ppfx+"_FILE_UNLOADED","MFILEID="+this.MFILEID,"MFILENAME="+this.MFILENAME,"TRAEGER/FTIMESTAMP="+this.TRAEGER+"/"+this.FTIMESTAMP]});
	this.filesprocessed++;

#
# 5: record done, check last record
#
if 		(!this.is_last_record)
goto	done

#
# 6: log final summary
#
qexpression
	this.protocol.push({type: 'row', row: [this.ppfx+"_FINISHED","FILES_PROCESSED="+this.filesprocessed,"",""]});
	this.protocol.push({type: 'html', htext: "<ul><li><a href=\"/dquery?dataname=toreconvMAFILESinfo\">Zu re-konvertierende Dateien anzeigen</a></li></ul>"});

#
# 7: last step
#
done:
end

