Page 1 of 2

Select a file from the beginning of a flow

Posted: Fri Jul 17, 2020 1:16 pm
by bkromer
Hello,

I have this flow:
Bildschirmfoto 2020-07-17 um 13.07.23.png
Bildschirmfoto 2020-07-17 um 13.07.23.png (41.24 KiB) Viewed 14227 times
In the Flow I get a PDF-File from Email convert it to text search with regex in the text.

When I work with the File i always use File.read(job.getPath(), 'UTF-8'); wich gives me the incoming file wich is the txt-File.
Now I also need the PDF from the beginning, how can I select this?

I want to upload it to an API with theHTTP.setAttachedFile(job.getPath() , filename); ...

Thanks in Advance for your Help! :)

Re: Select a file from the beginning of a flow

Posted: Fri Jul 17, 2020 1:31 pm
by jan_suhr
You have to branch out before the text conversion so you get a copy of the PDF. Then you can upload that one.

The PDF you convert to text is sent to null after the conversion.

Re: Select a file from the beginning of a flow

Posted: Fri Jul 17, 2020 1:36 pm
by bkromer
okay, I have this PDF ARCHIV 48h trashcan in there, where all the incoming PDF are archived for 48 hours.
Is there a way to select the belonging PDF-file in the script, some job.get... function or similar?

Re: Select a file from the beginning of a flow

Posted: Fri Jul 17, 2020 2:36 pm
by jan_suhr
Branch of to one folder before the pdfToolbox element and then from that folder back to the folder before the script, then both the PDf and the TXT will be going in to the script. In the script you have to pick up both the PDF and the TXT file.

Re: Select a file from the beginning of a flow

Posted: Fri Jul 17, 2020 2:47 pm
by bkromer
Hmmm... :|
But I run my script for every file which means It would handle the PDF and TXT file separately. So I will end up with 2 new entries in my database.

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var pdf =  File.read( job.getPath(), "UTF-8" );// PDF Text einlesen
		function getSuppliers(){
		var theHTTP = new HTTP( HTTP.SSL );
		theHTTP.authScheme = HTTP.ProprietaryAuth;
		theHTTP.addHeader( "Authorization", "Bearer asdasdasdasd");
		theHTTP.url = "https://api.ninoxdb.de/v1/teams/asdadsasd/databases/asdasd/tables/ASDDDrecords?perPage=9999"; 
		theHTTP.get();
		while (!theHTTP.waitForFinished(3)) {
			job.log(1, 'API CALL IN PROGRESS!');
		}
	
		if (theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200) {
			job.log(1, 'getSuppliers - API CALL successfull RESPONSE: ' +theHTTP.statusCode ); // theHTTP.getServerResponse().toString('UTF-8')
			job.log(1,"FULL RESPONSE GET SUPPLIERS: "+theHTTP.getServerResponse().toString('UTF-8'))
			supplierJson = JSON.parse(theHTTP.getServerResponse().toString('UTF-8'));
			
			// job.log( 1, JSON.stringify( jsonReturn[0]['fields']['Bestellungen'] ));
			
			// job.log(1,jsonReturn[0]['fields']['Name oder Firma']);
			
		} else {
			job.log(
				3,
				'API CALL Failed:  ' +
					theHTTP.getServerResponse().toString('UTF-8') +
					'  Status Code: ' +
					theHTTP.statusCode.toString('UTF-8')
			);
		}
		return supplierJson
	}	
	function getCompany( supplierJson, fileAsText ){
		var newSupplierJson = [];
		for ( var j=0; j< supplierJson.length; j++ ){
				if ( "UST ID" in supplierJson[j]['fields'] ){
					newSupplierJson.push(supplierJson[j]);
				}
		}
		//File.write("C:\\Users\\Administrator.BECHTELDRUCK\\Nextcloud\\Documents\\coding\\SwitchScripts\\RECHNUNGSEINGANG\\RECHNUNGEN AUSLESEN TEST\\TXT LOWER ARCHIV\\"+job.getName(), pdfLower, 'UTF-8'); // AUCH ALS LOWER TEXT ABSPEICHERN!
		// job.log(1,"länge json: "+supplierJson.length);
		supplierId = 0;
		 for (var i=0; i< newSupplierJson.length; i++){
			var supplierUSTID = newSupplierJson[i]['fields']['UST ID'];
			var regex = "("+supplierUSTID+")";
			var pattern = new RegExp( regex );
			pattern.global = true; 
			pattern.ignoreCase = true;
			//job.log(1,i);
			var matched = pdf.match(pattern);
				if (matched){
					job.log(1,"Pattern found: "+supplierUSTID+" Name: "+ newSupplierJson[i]['fields']['Name oder Firma'] );
					job.setPrivateData("pdSupplierUSTID", supplierUSTID );	
					job.log(1,"matches: "+matched );
					job.log(1, JSON.stringify( newSupplierJson[i] ) );
					supplierId = newSupplierJson[i]['id'];
				}
			}
			job.sendToSingle( job.getPath() );
		return supplierId	
};
	function upload2Ninox(supplierId){
	name = job.getName();
	prefix = job.getUniqueNamePrefix();
	filename = name.replace(prefix,"");
	var pdf =  File.read( job.getPath(), "UTF-8" );// PDF Text einlesen
	var encodedText = HTTP.encodeURI( pdf );
	var nummer = "asd";
	var date="2020-07-16";
	job.log(1,date);
	var doktyp = "Rechnung";
	
	var theHTTP = new HTTP (HTTP.SSL);
	theHTTP.authScheme = HTTP.ProprietaryAuth;
	theHTTP.authorization = "Bearer asdasd";
	theHTTP.url = "https://api.ninoxdb.de/v1/teams/asdasd/databases/gasasd/tables/ASD/records";
	var json = new ByteArray(
		'{"fields":{"Lieferanten":"' +
		supplierId+
		'","Nummer":"' +
		nummer +
		'","Datum":"' +
		date +
		'","Dokumenttyp":"' +
		doktyp +
		'","pdf als text":"' +
		 encodedText +
		'"}}',
	'UTF-8'
	);
	theHTTP.setPostData(json, 'application/json');
	theHTTP.post();
	while( !theHTTP.waitForFinished( 3 ) )
	  {
	       job.log( 1, "API CALL IN PROGRESS!" );
	  } 
		
	if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
	  {
		job.log(1," upload2Ninox - API CALL successfull RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
		// UPLOAD VIA API TO NINOX DATABASE
		try{
		var response = JSON.parse(theHTTP.getServerResponse().toString( "UTF-8" ));
		var ninoxId =response.id;
		job.setPrivateData("pdNinoxId", ninoxId );	
		var theHTTP = new HTTP( HTTP.SSL );
		theHTTP.authScheme = HTTP.ProprietaryAuth;
		theHTTP.authorization = "Bearer asdasd";
		theHTTP.setAttachedFile(     job.getPath() , filename );     
		theHTTP.url = "https://api.ninoxdb.de/v1/teams/uasdasdQ/databases/gasdadv/tables/ASD/records/"+id+"/files"; 
		theHTTP.enableMime = true;
		theHTTP.post();
		while( !theHTTP.waitForFinished( 3 ) )
		  {
		       job.log( 1, "API CALL IN PROGRESS!" );
		  } 
			
		if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
		  {
				
		       
				job.log(1,"API CALL successfull RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
		  }
		else
		{
			job.log(3,"API CALL Failed:  "+theHTTP.getServerResponse().toString( "UTF-8" )+ "  Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
		}
		}
		catch(theError) {
			job.log(3,'json parse error: ' +theError);
		}
		
		  }
		else
		{
			job.log(3,"API CALL Failed:  "+theHTTP.getServerResponse().toString( "UTF-8" )+ "  Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
		}
	}
	// Ausführung
	getSuppliers();	
	getCompany(supplierJson,pdf);
	upload2Ninox(supplierId);
}

Re: Select a file from the beginning of a flow

Posted: Fri Jul 17, 2020 3:05 pm
by jan_suhr
Well can't you let the first file create the record and the second update that record?

Re: Select a file from the beginning of a flow

Posted: Sat Nov 14, 2020 9:18 am
by bkromer
jan_suhr wrote: Fri Jul 17, 2020 2:36 pm Branch of to one folder before the pdfToolbox element and then from that folder back to the folder before the script, then both the PDf and the TXT will be going in to the script. In the script you have to pick up both the PDF and the TXT file.
How can I select the textfile and the pdf file in the same script and handle them differently?
Can I refer to the textfile when its in a different folder?

Re: Select a file from the beginning of a flow

Posted: Sat Nov 14, 2020 9:38 am
by jan_suhr
One way is to embed the text file or the PDF as an Opaque dataset in the other file and in the script access that dataset. It will then be saved as a temp file that you can read from the script.

Re: Select a file from the beginning of a flow

Posted: Sun Nov 15, 2020 9:47 am
by bkromer
jan_suhr wrote: Sat Nov 14, 2020 9:38 am One way is to embed the text file or the PDF as an Opaque dataset in the other file and in the script access that dataset. It will then be saved as a temp file that you can read from the script.
For Example I have this flow where I want to opaque pickup a *.json file in a *.pdf file but it stucks before the pickup flow element.
Why is that? :?:
The attachment 2020-11-15 09_42_18-Enfocus Switch - Switch Server (localhost) - euchnerSwitchFlux.png is no longer available
2020-11-15 09_42_18-Enfocus Switch - Switch Server (localhost) - euchnerSwitchFlux.png
2020-11-15 09_42_18-Enfocus Switch - Switch Server (localhost) - euchnerSwitchFlux.png (16 KiB) Viewed 13264 times

Re: Select a file from the beginning of a flow

Posted: Sun Nov 15, 2020 9:48 am
by bkromer
2020-11-15 09_46_43-Enfocus Switch - Switch Server (localhost) - euchnerSwitchFlux.png
2020-11-15 09_46_43-Enfocus Switch - Switch Server (localhost) - euchnerSwitchFlux.png (11.49 KiB) Viewed 13264 times

Re: Select a file from the beginning of a flow

Posted: Sun Nov 15, 2020 11:05 am
by jan_suhr
Do both files have the same name?

Re: Select a file from the beginning of a flow

Posted: Sun Nov 15, 2020 1:26 pm
by bkromer
jan_suhr wrote: Sun Nov 15, 2020 11:05 am Do both files have the same name?
No its 470_payload.json and 127398.pdf as u can see on the screenshot.

Re: Select a file from the beginning of a flow

Posted: Sun Nov 15, 2020 1:52 pm
by jan_suhr
Sorry I didn't see that.

But that is your problem. For the Pickup tools to work with the setting "Metadaten neben Asset" both files have to have the same name.

So you have to find a way to rename the payload file.

Re: Select a file from the beginning of a flow

Posted: Mon Nov 16, 2020 9:40 am
by bkromer
jan_suhr wrote: Sun Nov 15, 2020 1:52 pm ...
So you have to find a way to rename the payload file.
Is it not enough to rename one of the files? Image
They still have their unique filename for a second.

Re: Select a file from the beginning of a flow

Posted: Mon Nov 16, 2020 9:59 am
by jan_suhr
You renamed the payload file and also changed the extension. The filename before the extension must be alike but different extensions. The filetype you want to embed as an opaque datatype i specified in the properties.

You you need two files as this: myfilename.pdf and myfilename.json