#
#	ITSV GmbH
#	CCDB - Command and Control Database
#	FILE:			objact_setLSWattribute
#	DESCRIPTION:	action sequence for object action setLSWattribute

#
# 0: determine SQL statement and key values
#
qexpression
	proc: {
		copycvars(this,"action,objtype,attribute,key,value");
		this.attmap = {
			LSWID:			{ mechtype:	'UPDATE_LSW',	colname:	'LSWID' },
			LSWHID:			{ mechtype:	'UPDATE_LSW',	colname:	'LSWHID' },
			HERSTELLER:		{ mechtype:	'UPDATE_LSW',	colname:	'LSWHID' },
			LSWATTRIBS:		{ mechtype:	'UPDATE_LSW',	colname:	'LSWATTRIBS' },
			ATTRIBUTE:		{ mechtype:	'UPDATE_LSW',	colname:	'LSWATTRIBS' }
			};
		this.attctl = this.attmap[this.attribute];
		if (!this.attctl) {
			this.errcoll.collect(null,"attribute "+this.attribute+" not defined in attribute control map for LSW",this.attmap);
			break proc;
		}
		if (this.attctl.mechtype=='UPDATE_LSW') {
			this.att_readsql   = "select LSWID,LSWHID,LSWATTRIBS from SVCLRWLSW where LSWID=::key::";
			this.att_updatesql = "update SVCLRWLSW set "+this.attctl.colname+"=::value:: where LSWID=::key::";
			this.att_namedparams = "key,value";
		} else if (this.attctl.mechtype=='UPDATE_ATT') {
			this.attparts = this.attribute.split("_");
			if (this.attparts.length!=2) {
				this.errcoll.collect(null,"attribute name "+this.attribute+" is not well formed for attribute setting mechanism "+this.attcol.mechtype);
				break proc;
			}
			this.archtyp = this.attparts[0];
			this.attname = this.attparts[1];
			this.att_readsql   = "select ATTARCHTYP,ATTPARTYPE,ATTPARKURZ,ATTNAME,ATTVALUE,ATTDESC from CCATT where ATTARCHTYP=::archtyp:: and ATTPARTYPE='SVCLRWLSW' and ATTPARKURZ=::key:: and ATTNAME=::attname::";
			this.att_updatesql = "update CCATT set ATTVALUE=::value:: where ATTARCHTYP=::archtyp:: and ATTPARTYPE='SVCLRWLSW' and ATTPARKURZ=::key:: and ATTNAME=::attname::";
			this.att_namedparams = "key,value,archtyp,attname";
		} else {
			this.errcoll.collect(null,"undefined LSW attribute change mechanism \""+this.attctl.mechtype+"\" for LSW attribute "+this.attribute,this.attmap);
			break proc;
		}
		ppush(this,['setLSWattribute_start','key='+this.key,'attribute='+this.attribute,'value="'+this.value]);
	}

#
# 1: read the original values
#
sql_varname		att_readsql
named_params	@@att_namedparams@@
result_varname	oldres
post_qexpression
	proc: {
		this.change_detected = false;
		if (this.oldres.getRowCount()!=1) {
			this.errcoll.collect(null,"old value read statement delivered wrong count of rows for LSWID-key=\""+this.key+"\"",{ named_params: this[att_namedparams], oldres: this.oldres });
			break proc;
		}
		let vcn = this.oldres.findColumn(this.attctl.colname);
		if (vcn<0) {
			this.errcoll.collect(null,"could not find column \""+this.attctl.colname+"\" in reading old value",{attctl: this.attctl, oldres: this.oldres});
			break proc;
		}
		this.old_value = this.oldres.getCellValue(0,vcn);
		if (this.value!=this.old_value) this.change_detected = true;
	}
	
#
# 2: if no change detected, terminate
#
if (!this.change_detected)
goto done

#
# 3: check if LSWID has changed and other tables need be updated
#
if (this.attribute!='LSWID')
goto not_LSWID

#
# 4: LSWID has changed, check if the new LSWID is already in use
#
sql 			select LSWID, LSWHID,LSWATTRIBS from SVCLRWLSW where LSWID=::old_value::
named_params	old_value
result_varname	dcres

#
# 5: if new LSWID not already in use, simply perform update
#
if (this.dcres.getRowCount()<1)
goto LSWID_only_update

#
# 6: new LSWID already exists, delete the object with the OLD LSWID
#
sql				delete from SVCLRWLSW where LSWID=::old_value::
named_params	old_value

#
# 7: LSWID has changed, perform update
#
LSWID_only_update:
sql_varname		att_updatesql
named_params	@@att_namedparams@@
result_varname	updres

#
# 8: LSWID has changed, update SVCLRWSOID
#
sql				UPDATE SVCLRWSOID set LSWID=::value:: where LSWID=::key::
named_params	key,value
result_varname	updres
post_qexpression
	logger.debug("setLSWattribute.UDBG.KeyChanged.MUSTUPDSOID.key="+this.key+".attribute="+this.attribute+".value="+this.value+".UPDATED_SVCLRWSOID.RES: "+
				aux.objTxt({sql: this.populated_sql, named_params: this.named_params, updres: this.updres}));

#
# 9: LSWID has changed, update CCMVBRFAUSW
#
sql				UPDATE CCMVBRFAUSW set LSWID=::value:: where LSWID=::key::
named_params	key,value
result_varname	updres
post_qexpression
	ppush(this,'result',this.updres);

#
# 10: LSWID change complete
#
goto done

#
# 11: no change to LSWID, perform the appropriate SQL
#
not_LSWID:
sql_varname		att_updatesql
named_params	@@att_namedparams@@
result_varname	updres
post_qexpression
	ppush(this,"att_not_LSWID_updated");
	ppush(this,{ type: 'objTxt', object: {updres: this.updres, named_params: this.named_params}});

#
# 12: finally, set chain query to re-display the complete LSWEDITOR or the query we were triggered from ("return_to")
#
done:
qexpression 
	logger.debug("setLSWattribute: protocol: "+aux.objTxt(this.protocol));
	if (this.query.return_to) {
		this.query.chain_query = {dataname: this.query.return_to};
		if (this.query.return_key && this.query.return_value) {
			this.query.chain_query[this.query.return_key] = this.query.return_value;
		}
		if (this.query.return_fragment) {
			this.query.chain_query.fragment = this.query.return_fragment;
		}
	} else {
		this.query.chain_query = {dataname: "LSWEDITOR"};
	}