#
#			ITSV GmbH
#	CCDB - Command and Control DAtabase
#
#	FILE:			dquerymfile_updatemfilelpath.txt
#	DESCRIPTION:	DQUERY definition for DQUERY updatemfilelpath
#					this DQUERY updates attributes for an MFILE logical path
#
@querytitle			MFILE logischer-Pfad Änderungen
@querydescription	diese Abfrage erlaubt Änderungen an logischen Pfaden für managed files im Regelwerk
					Jeder logische Pfad hat einen Eintrag im RW MFILELPATHS
					Diese Abfrage Erzeugt oder ändert einen solchen Eintrag
@group				DATAFILE
@attributenames		action:string:{{actionoptions}},lpath:string:{{lpathoptions}},parent:string:{{parentoptions}},title:string:{{titleoptions}},desc:string:{{descoptions}}
@actionoptions		{	type: {		structure:	"scalar.enum",	vlist:	"update:Ändern:Ändern eines logischen Pfads,delete:Löschen:Löschen eines logischen Pfads" },
						labeltext:	"Aktion",		typedesc:	"Aktion die durchgeführt werden soll: update - Ändern eines logischen Pfads, delete - Löschen eines logischen Pfads" }
@lpathoptions		{	labeltext:	"Logischer Pfad", typedesc: "vollständiger logischer Pfad, der bearbeitet werden soll" }
@parentoptions		{	labeltext:	"Parent Pfad", typedesc:	"Übergeordneter Pfad",	is_optional:	"true" }
@titleoptions		{	labeltext:	"Titel",		typedesc:	"Angezeigter Titel des zu bearbeitenden Pfades",	is_optional: "true" }
@descoptions		{	labeltext:	"Beschreibung",	typetext:	"Informationstext, der beschreibt, welche Daten unter diesem Pfad verwaltet werden und ähnliches", is_optional: "true" }
@querytype			function
@function			seqtrans.seqtrans

~query.tsteps

#
# <<TSTEPNUM:0>>: determine action to be executed
#
pre_qexpression
	proc: {
		this.lpath = this.query.lpath;
		this.parent = this.query.parent;
		this.title = this.query.title;
		this.desc = this.query.desc;
		this.action = this.query.action;
		if (!this.labels["action_"+this.action]) {
			this.errcoll.collect(null,"no handling method for action=\""+this.action+"\", seqtrans-label action_"+this.action+" does not exist.");
			break proc;
		}
	}
goto action_@@action@@

#
# <<TSTEPNUM:1>>: action: UPDATE - check if MFILELPATH entry already exists
#
action_update:
sql				select RWTYP, RWPARENT, RWNAME, RWVALUE, RWDESC from CCRW where RWTYP='MFILELPATH' and RWNAME=::lpath::
named_params	lpath
result_varname	readres
post_qexpression
	proc: {
		let numrows = this.readres.getRowCount();
		if (numrows>1) {
			this.errcoll.collect(null,"more than one MFILELPATH entry for LPATH='"+this.query.lpath+"' already existent",this.readres);
			break proc;
		} else if (numrows==1) {
			this.mode = "UPDATE";
		} else if (numrows==0) {
			this.mode = "CREATE";
		} else {
			this.errcoll.collect(null,"Illegal row count reading RW entry for MFILELPATH with LPATH='"+this.lpath+"'", this.readres);
			break proc;
		}
	}
then_goto	mfilelpathupdate_@@mode@@

#
# <<TSTEPNUM:2>>: update mode CREATE
#
mfilelpathupdate_CREATE:
sql				insert into CCRW(RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC) values('MFILELPATH',::parent::,::lpath::,::title::,::desc::)
named_params	parent,lpath,title,desc
result_varname	createres
post_qexpression
	proc: {
		if (this.createres && this.createres.resulttype && this.createres.resulttype=="object" && this.createres.resultobject && this.createres.resultobject.affectedRows) {
			if (this.createres.resultobject.affectedRows>1) {
				this.errcoll.collect(null,this.mode+" mfilelpath \""+this.lpath+"\" changed more than one row",this.createres);
				break proc;
			} else if (this.createres.resultobject.affectedRows==1) {
				this.result = new aux.Result({	resulttype:	"plain",	
												body: "Managed File logischer-Pfad <code>"+this.lpath+"</code> erfolgreich angelegt",
												structure_has_changed: true});
			} else if (this.createres.resultobject.affectedRows==0) {
				this.result = new aux.Result({ resulttype:	"plain",	body: "Managed File logischer-Pfad <code>"+this.lpath+"</code> konnte nicht angelegt werden." });
			} else {
				this.result = new aux.Result({	resulttype:	'object',
												title:	"Fehler beim Ändern von Managed File logischem Pfad <code>"+this.lpath+"</code>, Anzahl geänderter Zeilen < 0",
												resultobject:	{	operation: 'updatemfilelpath', action: this.action, mode: this.mode, lpath: this.lpath, parent: this.parent, 
																	title: this.title, desc: this.desc, db_response: this.createres }
											});
			}
		} else {
			this.errcoll.collect(null, "database result of "+this.query.dataname+" action "+this.action+" mode "+this.mode+" is not well formed", this.createres);
			break proc;
		}
	}

#
# <<TSTEPNUM:3>>: end of CREATE path
#
end

#
# <<TSTEPNUM:4>>: update mode UPDATE
#
mfilelpathupdate_UPDATE:
sql				update CCRW set RWPARENT=::parent::,RWVALUE=::title::,RWDESC=::desc:: where RWTYP='MFILELPATH' and RWNAME=::lpath::
named_params	parent,lpath,title,desc
result_varname	updres
post_qexpression
	proc: {
		if (this.updres && this.updres.resulttype && this.updres.resulttype=="object" && this.updres.resultobject && this.updres.resultobject.affectedRows) {
			if (this.updres.resultobject.affectedRows>1) {
				this.errcoll.collect(null,"update mfilelpath \""+this.lpath+"\" changed more than one row",this.updres);
				break proc;
			} else if (this.updres.resultobject.affectedRows==1) {
				this.result = new aux.Result({ resulttype:	"plain",	body: "Managed File logischer-Pfad <code>"+this.lpath+"</code> erfolgreich geändert" });
			} else if (this.updres.resultobject.affectedRows==0) {
				this.result = new aux.Result({ resulttype:	"plain",	body: "Managed File logischer-Pfad <code>"+this.lpath+"</code> konnte nicht geändert werden." });
			} else {
				this.result = new aux.Result({	resulttype:	'object',
												title:	"Fehler beim Ändern von Managed File logischem Pfad <code>"+this.lpath+"</code>, Anzahl geänderter Zeilen < 0",
												resultobject:	{	operation: 'updatemfilelpath', action: this.action, mode: this.mode, lpath: this.lpath, parent: this.parent, 
																	title: this.title, desc: this.desc, db_response: this.updres }
											});
			}
		} else {
			this.errcoll.collect(null, "database result of "+this.query.dataname+" action "+this.action+" mode "+this.mode+" is not well formed", { updres: this.updres });
			break proc;
		}
	}

#
# <<TSTEPNUM:5>>: end of UPDATE path
#
end

#
# <<TSTEPNUM:6>>: action DELETE 
#
action_delete:
sql				delete from CCRW where RWTYP='MFILELPATH' and RWNAME=::lpath::
named_params	lpath
result_varname	delres
post_qexpression
	proc: {
		if (this.delres && this.delres.resulttype && this.delres.resulttype=='object' && this.delres.resultobject && this.delres.resultobject.affectedRows) {
			if (this.delres.resultobject.affectedRows>1) {
				this.errcoll.collect(null,"delete mfilelpath \""+this.lpath+"\" deleted more than one row",this.delres);
				break proc;
			} else if (this.delres.resultobject.affectedRows==1) {
				this.result = new aux.Result({	resulttype:	"plain",	
												body: "Managed File logischer-Pfad <code>"+this.lpath+"</code> gelöscht",
												structure_has_changed: true});
			} else if (this.delres.resultobject.affectedRows==0) {
				this.result = new aux.Result({ resulttype:	"plain",	body: "Managed File logischer-Pfad <code>"+this.lpath+"</code> konnte nicht gelöscht werden." });
			} else {
				this.result = new aux.Result({	resulttype:	'object',
												title:	"Fehler beim Löschen von Managed File logischem Pfad <code>"+this.lpath+"</code>",
												resultobject:	{	operation: 'updatemfilelpath', action: this.action, mode: this.mode, lpath: this.lpath, parent: this.parent, 
																	title: this.title, desc: this.desc, db_response: this.delres }
											});
			}
		} else {
			this.errcoll.collect(null, "database result of "+this.query.dataname+" action "+this.action+" is not well formed", this.delres);
			break proc;
		}
	}

#
# <<TSTEPNUM:7>>: end of DELETE action
#
end 