#
#	ITSV GmbH
#	CCDB - Command and Control Database 
#
#	FILE:			dquerymfile_regMVBRFAUSWLSWH
#	DESCRIPTION:	dquery definition for regMVBRFAUSWLSWH
#
# 
@group				SVCLMVBRFAUSWDATALOAD
@querytitle 		registriere noch unbekannte LSWH aus SVCLRWLSW in SVCLRWLSWH
@querydescription	registriert zu jeder LSWHID aus SVCLRWLSW, die noch nicht in SVCLRWLSWH aufzufinden ist, einen LSW-Eintrag in SVCLRWLSWH
@querytype			function
@function			seqtrans.seqtrans
@sql_select			select distinct LSWHID from SVCLRWLSW

~query.tsteps
#
# 0: 
#
if	(this.init_done)
goto	record_proper

#
# 1: initialize
#
qexpression
	proc: {
		if (!this.init_done) {
			this.qname = this.query.dataname;
			logger.debug(this.fname+"."+this.qname+".UDBG.t_init.query: "+aux.objTxt(this.query)+"...INPUTRESULT: "+aux.objTxt(this.inputresult));
			this.lswhstoprocess = this.inputresult.getRowCount();
			this.protocol = new Array();
			this.protocol.push([this.fname+"("+this.qname+")_started","LSWHS_TO_PROCESS="+this.lswhstoprocess,"",""]);
			this.lswhsprocessed = 0;
			this.newlswhsregistered = 0;
			this.knownlswhsfound = 0;
			this.init_done = true;
		}
	}

#
# 2: read SVCLRWLSW to find if LSWHID already known
#
record_proper:
sql				select * from SVCLRWLSWH where LSWHID=::LSWHID::
named_params	LSWHID
result_varname	chklswhres
post_qexpression
	/* logger.debug(this.fname+"."+this.qname+".UDBG.chklswres: "+aux.objTxt(this.chklswhres)); */

#
# 3: if already known, skip creation
#
pre_qexpression
	logger.debug(this.fname+"."+this.qname+".UDBG.rows_found="+this.chklswhres.getRowCount());
if		(this.chklswhres.getRowCount()>0)
goto	lswh_already_there

#
# 4: register new LSWH, use ID from SVCLRWLSW and store in SVCLRWLSWH
#
pre_qexpression
	this.cdata.LSWHBEZ = "Name_"+this.cdata.LSWHID;
	this.cdata.LSWHATTRIBS = "{}";
sql				INSERT INTO SVCLRWLSWH(LSWHID,LSWHBEZ,LSWHATTRIBS) values(::LSWHID::,::LSWHBEZ::,::LSWHATTRIBS::)
named_params	LSWHID,LSWHBEZ,LSWHATTRIBS
post_qexpression
	this.newlswhsregistered++;
	this.protocol.push([this.fname+"("+this.qname+")_NEW_LSWH_CREATED","LSWH_ID="+this.cdata.LSWHID,"LSWHBEZ="+this.cdata.LSWHBEZ,""]);

#
# 5: LSWH successfully created
#
goto lswh_there

#
# 6: LSWH already there
#
lswh_already_there:
qexpression
	this.knownlswhsfound++;
	
#
# 5: LSWH is there (either already there or created now), nothing more to be done, check if last record
#
lswh_there:
pre_qexpression
	this.lswhsprocessed++;
if (!this.is_last_record)
goto record_done

#
# 6: create result from protocol
#
qexpression
	this.protocol.push([this.fname+"("+this.qname+")_finished","LSWHS_PROCESSED="+this.lswhsprocessed,"KNOWN_LSWHS_ENCOUNTERED="+this.knownlswhsfound,"NEW_LSWHs_REGISTERED="+this.newlswhsregistered]);
	this.result = new aux.Result({resulttype: 'dbresult', metaData: [{name: 'Info'},{name: 'parameter 1'},{name: 'parameter 2'},{name: 'parameter 3'}], rows: this.protocol});
	
#
# 7: end of record processing
#
record_done:
end

	