#
#			ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:			dquerymfile_textFragmentEditor.txt
#	DESCRIPTION:	DQUERY definition for DQUERY textFragmentEditor
#					this DQUERY implements the backend service for the Text fragment editor frontend
#
@querytitle			TextFragmentEditor Backend
@querydescription	REST-Services für das Frontend des TextFragment-Editors
@group				ADMINTEXTFRAG
@hidden				TRUE
@attributenames		action:string,type:string:{{typeoptions}},ident:string:{{identoptions}},version:string:{{versionoptions}},
					parent:string:{{parentoptions}},child_index:string:{{childindexoptions}},frag_index:string:{{fragindexoptions}},
					textData:string:{{textdataoptions}},
@typeoptions		{"is_optional":"true"}
@identoptions		{"is_optional":"true"}
@textdataoptions	{"is_optional":"true"}
@versionoptions		{"is_optional":"true"}
@parentoptions		{"is_optional":"true"}
@childindexoptions	{"is_optional":"true"}
@fragindexoptions	{"is_optional":"true"}
@querytype			function
@function			seqtrans.seqtrans
@format				none
#
# attention: this is a special case of seqtrans, the inputresult having only ONE dummy entry
#

~query.tsteps

#
# <<TSTEPNUM:0>>: prepare action data and branch to the desired sub-action
#
pre_qexpression
	proc: {
		if (this.query.action)		this.action			= this.query.action;
		if (this.query.ident)		this.ident			= this.query.ident;
		if (this.query.version)		this.version		= this.query.version;
		if (this.query.type)		this.type			= this.query.type;
		if (this.query.child_index)	this.child_index	= this.query.child_index;
		if (this.query.frag_index)	this.frag_index		= this.query.frag_index;
		if (this.query.data)		this.data			= this.query.data;
		this.actionlabel = "action_" + this.query.action;
		if (!this.labels[this.actionlabel]) {
			this.errcoll.collect(null,"Cannot process action \""+this.action+"\", label \""+this.actionlabel+"\" does not exist",this.query);
			break proc;
		}
	}
goto	@@actionlabel@@

#
# <<TSTEPNUM:1>>: action - "save"
#
action_save:
sql		update CCTEXTFRAG set type=::type::,version=::version::+1,data=::data:: where ident=::ident:: and child_index=::child_index:: and frag_index=::frag_index:: and version=::version::
named_params		type,ident,child_index,frag_index,version,data
result_varname		updres
post_qexpression
	proc: {
		if (this.updres && this.updres.resultobject && this.updres.resultobject.affectedRows==1) {
		} else {
			this.errcoll.collect(null,"textFragment update should affect exactly one row but has not",this.updres);
			break proc;
		}
	}
then_goto	action_load

#
# <<TSTEPNUM:2>>: save - reload after save
#
sql		select 

#
# <<TSTEPNUM:3>>: save - end
#
end

#
# <<TSTEPNUM:4>>: action - "load"
#
action_load:
sql				select type,ident,parent,child_index,frag_index,version,data 
				  from CCTEXTFRAG 
				 where ident=::ident:: and child_index=::child_index and frag_index=::frag_index 
				   and version=(select max(version) from CCTEXTFRAG where ident=::ident:: and child_index=::child_index and frag_index=::frag_index)
named_params	ident
result_varname	loadres
post_qexpression
	this.result = new aux.Result({resulttype: "object", resultobject: this.loadres.getRowObject(0)});

#
# <<TSTEPNUM:5>>: load - end
#
end

#
# <<TSTEPNUM:6>>: action - "saveAs" - check if ID already exists
#
action_saveAs:
sql				select * from CCTEXTFRAG where ident=::ident::
named_params	ident
result_varname	sckres
post_qexpression
	proc: {
		if (this.sckres.getRowCount()>0) {
			this.errcoll.collect(null,"Text fragment with id \""+this.ident+"\" already exists",this.sckres);
			break proc;
		}
	}

#
# <<TSTEPNUM:7>>: saveAs - store/create
#
sql				insert into CCTEXTFRAG(type,ident,version,parent,child_index,frag_index,data) values(::type::,::ident::,::version::,::parent::,::child_index::,::frag_index::,::data::)
named_params	type,ident,version,parent,child_index,frag_index,data
post_qexpression
	this.result = new aux.Result("plain");
	this.result.body = "new text fragment with ID \""+this.ident+"\" created";

#
# <<TSTEPNUM:8>>: saveAs - end
#
end

