#
#				ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:				dquerymfile_ANLTMFILE.txt
#	DESCRIPTION:		DQUERY definition for DQUERY ANLTMFILE
#						the query analyzes a Table-structured managed file
#						in order to be able to create a table in the database
#						into which the file can be loaded
#
@querytitle				Analysiere Managed Datei mit Tabellenstruktur
@querydescription		Analysiert ein Managed File mit Tabellenstruktur (CSV, XLS,XLSX), damit eine Tabelle in der Datenbank angelegt werden kann
						in die die Datei geladen werden kann
@group					DATAFILE
@attributenames			amfileid:mfileid:{{amfileidoptions}},structname:string:{{structnameoptions}}
@amfileidoptions		{"labeltext":"Managed File","typedesc":"Managed Datei, die eine Tabellen-Datenstruktur beinhaltet, die analysiert werden soll"}
@structnameoptions		{"labeltext":"Strukturname","typedesc":"Eindeutiger Name der analysierten Struktur, bestimmt auch den Namen der Managed Datei, in der das Analyeergebnis abgelegt wird"}
@querytype				function
@function				seqtrans.seqtrans

~query.tsteps

#
# <<TSTEPNUM:0>>: check if structure description MFILE already exists
#
pre_qexpression
	logger.debug("ANLTMFILE.inputresult: "+aux.objTxt(this.inputresult));
	this.amfileid = this.query.amfileid;
	this.structname = this.query.structname;
	this.structmfileid = "struct_"+this.structname;
	this.input_options = {};
	this.repfunc = function(linetext,repres) {
		let rp = new Array();
		let fldarr = this.load_file.splitCSVline(linetext,{fieldsep: ";", quotechar: "\""});
		/* let fldarr = load_file.splitCSVline(linetext,{ fieldsep: ";" }); */
		if (aux.res_is_error(fldarr)) {
			rp.push("Error splitting input line by semicolons WITH quoting: "+aux.objTxt(fldarr));
			fldarr = linetext.split(";");
			rp.push("stopgap by simply String.split()ting it along \";\"");
		}
		rp.push("Trying to repair fields: "+aux.objTxt(fldarr));
		if (repres) repres.info = rp.join("\n");
		return linetext;
	}.bind(this);
	aux.default_param(this.input_options,"headersInFirstLine","Y");
	aux.default_param(this.input_options,"fieldsep",";");
	aux.default_param(this.input_options,"quotechar","\"");
	this.get_mfile_options = { rep_function: this.repfunc};
sql				select * from CCMFILES where MFILEID=::structmfileid::
named_params	structmfileid
result_varname	smfres
post_qexpression
	if (this.smfres.getRowCount()>0) {
		this.errcoll.collect(null,"Structure descriptor managed file "+this.structmfileid+" already exists",this.smfres);
	}

#
# <<TSTEPNUM:1>>: get data from MFILE
#
aexpression
	mfile.get_mfile_data(
		this.amfileid,
		function(err,res) {
			if (err) {
				this.errcoll.collect(err,"Error getting MFILE info for ID=\""+this.amfileid+"\" in ANLTMFILE",res);
				this();
				return;
			}
			this.amfileinfo = res;
			load_file.readArrayTableFromFile(
				this.amfileinfo,
				this.input_options,
				function(err,res) {
					if (err) {
						this.errcoll.collect(err,"Error reading data array table from managed file",{result: res, mfileinfo: this.amfileinfo});
						this();
						return;
					}
					this.ppush([this.phead("arraytableloaded"),"ROWS="+res.getRowCount(),"",""]);
					this.amfres = res;
					this();
				}.bind(this),
				function(pstat) {
					seqtrans_progress_status(this,"read_input_mfile_array",pstat);
				}.bind(this)
			);
		}.bind(this),
		function(pstat) {
			seqtrans_progress_status(this,"get_input_mfile_data",pstat);
		}.bind(this),
		this.get_mfile_options);
post_qexpression
	logger.debug("ANLTMFILE.post_get: "+aux.objTxt({amfres: this.amfres}));
	

#
# <<TSTEPNUM:2>>: analyze the input data and save to managed file
#
aexpression
	this.desc = aux.analyzeDBresult(this.amfres);
	logger.debug("ANLTMFILE.analyzed: "+aux.objTxt(this.desc));
	this.jsondesc = JSON.stringify(this.desc);
	this.ppush([this.phead("ANALYZED"),"","",this.desc]);
	mfile.saveToMfile(this.req.session,
						"struct",
						this.jsondesc,
						{	mfileid: this.structmfileid,
							extension: "json"
						},
						function(err,res) {
							logger.debug("ANLTMFILE.saveCallback: "+aux.objTxt({ err: err, res: res}));
							if (err) {
								this.errcoll.collect(err,"Error creating structure definition managed file",res);
								this(err,null);
								return;
							}
							this.mfcres = res;
							this.ppush([this.phead("STRUCTSAVED"),{rawhtml: "STRUCTURE_SAVED.MFILEID=<a href=\"/dquery?dataname=SHOWFILE&mfileid="+this.structmfileid+"\">"+this.structmfileid+"</a>"},"",res]);
							this(null,res);
						}.bind(this)
					);
post_qexpression
	logger.debug("ANLTMFILE.post_save");

#
# <<TSTEPNUM:3>>: end
#
pre_qexpression
	logger.debug("ANLTMFILE.end");
end
