#
#		ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:		dquermfile_exportXLSRecMissCat.txt
#	DESCRIPTION:	DQUERY definition for CCDB DQUERY "exportXLSRecMissCat"
#
@querytitle			Excel-Export aller aufgezeichneten fehlenden Anrufkategorien
@querydescription	Exportiert aufgezeichnete fehlende Anrufkategorien aus Text-Fragmenten (Tabelle CCTEXTFRAG) der Type 'MISSCALLCATCSV' in eine Excel-Datei
@group				SVCLSLSTATSTAT
@querytype			dbselect
@sql				select type,ident,data from CCTEXTFRAG where type='MISSCALLCATCSV'
@post_query
		let csvs = new Array();		
		let logs = new Array();		
		let cdata, ri, li, crecs, hdrline, fragid, flds;		
		logs.push("BLOCKS="+that.result.getRowCount());		
		for (ri=0; ri<that.result.getRowCount(); ri++) {
		  fragid = that.result.getCellValue(ri,1);
		  logs.push("FRAGID="+fragid);		
		  cdata = that.result.getCellValue(ri,2);		
		  logger.debug("CDATA(read): "+aux.objTxt(cdata));
		  cdata = cdata.replace(/\r\r/g,"\r");
		  logger.debug("CDATA(rr->r): "+aux.objTxt(cdata));
		  cdata = cdata.replace(/\r\n/g,"\n");		
		  logger.debug("CDATA(rn->n): "+aux.objTxt(cdata));
		  cdata = cdata.replace(/\n\r/g,"\n");		
		  logger.debug("CDATA(nr->n): "+aux.objTxt(cdata));
		  cdata = cdata.replace(/\r/g,"\n");		
		  logger.debug("CDATA(r->n): "+aux.objTxt(cdata));
		  crecs = cdata.split("\n");	
		  if (crecs[0].startsWith("RWTYP;RWPARENT")) hdrline = crecs.shift();		
		  for (li=0; li<crecs.length; li++) {
			flds = crecs[li].split(";");
			if (flds.length<5) {
				logger.debug("Record "+li+" of FRAGID "+fragid+" is not well formed: "+aux.objTxt({cdata: cdata, crec: crecs[li], flds: flds}));
			}
			csvs.push(flds);
		  }
		  logs.push("LINES="+crecs.length);
		}		
		hdrline = hdrline || "RWTYP;RWPARENT;RWNAME;RWVALUE;RWDESC";		
		var mdns = hdrline.split(";");		
		var md = new Array();		
		for (ri=0; ri<mdns.length; ri++) md.push({name: mdns[ri]});
		that.result = new aux.Result({resulttype: 'dbresult', rows: csvs, logs: logs, metaData: md});
		logger.debug("exportXLSRecMissCat.UDBG.result: "+aux.objTxt(that.result));
@title				Alle aufgezeichneten fehlenden Anrufkategorien
@format				xlsx
@contentDisposition	attachment; filename="MISSCALLCATCSV_CCRW.XLSX"
