Innov-AI
Innovator in Reunion Island
Services     Supporters     Contact     About
ETL_LOCAL_REST_MENTDB_EXCEL.mql | MQL Samples | MentDB Weak Server

if (not (group exist "0001_folder1_folder2_folder3")) {
	group add "0001_folder1_folder2_folder3";
};

script merge "folder1.folder2.folder3.step_1_source_connect.exe" true 5
  (param
  )
  "Connect to the source"
{

	# CONFIGURATION ;
	-> "[CONF_NAME_OF_THE_FLOW]" "name_of_the_flow";

	# INITIALIZATION ;
	-> "[FLOW_PID]" [PID];
	
	# HANDLE ;
	try {
	
		# Flow initialization;
		stack flow_init [FLOW_PID] [CONF_NAME_OF_THE_FLOW] "{}";
		stack flow_step [FLOW_PID] 1 "source_connect...";

		# Local connection: don't use step 1;
		log write "Source: Local connection" OK null null;
		
		#Step 1 is valid;
		stack flow_step [FLOW_PID] 1 "source_connect_ok";
		
		include "folder1.folder2.folder3.step_2_extract.exe";
	
	} {

		#Step 1 is not valid;
		stack flow_step [FLOW_PID] 1 "source_connect_KO";
		
		# Give the error to the stack and stop the process;
		exception (1) ([global_err]);
	
	} "[global_err]";
	
} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_1_source_connect.exe" "0001_folder1_folder2_folder3")) {
	group grant script "folder1.folder2.folder3.step_1_source_connect.exe" "0001_folder1_folder2_folder3";
};

script merge "folder1.folder2.folder3.step_2_extract.exe" true 1
  (param
  )
  "Extract files from the source"
{
	
	# HANDLE ;
	try {

		# Mark the flow as step 2;
		stack flow_step [FLOW_PID] 2 "extract...";

		# Create the local directory PID;
		file mkdir (concat "home/" [FLOW_PID]);
		
		#Step 2 is valid;
		stack flow_step [FLOW_PID] 2 "extract_ok";

		stack (date now) "folder1.folder2.folder3.step_3_transform.exe" "[FLOW_PID]" [FLOW_PID] "[filename]" "";
	
	} {

		#Step 2 is not valid;
		stack flow_step [FLOW_PID] 2 "extract_KO";
		
		# Give the error to the stack and stop the process;
		exception (1) ([global_err]);
	
	} "[global_err]";

} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_2_extract.exe" "0001_folder1_folder2_folder3")) {
	group grant script "folder1.folder2.folder3.step_2_extract.exe" "0001_folder1_folder2_folder3";
};

script merge "folder1.folder2.folder3.step_3_transform.exe" true 1
  (param
  	(var "[FLOW_PID]" {true} "The flow PID" is_null:true is_empty:true "1")
  	(var "[filename]" {true} "The filename" is_null:true is_empty:true "file.xxx")
  )
  "Transform the file"
{

	# CONFIGURATION ;
	-> "[CONF_DESTINATION_NEW_FILENAME]" (concat "new_filename_" (date systimestamp_min) ".xls");
	-> "[CONF_DESTINATION_SHEETNAME]" "sheet1";
	
	# HANDLE ;
	try {

		# Mark the flow as step 3;
		stack flow_step [FLOW_PID] 3 "transform...";

		excel load empty "flow";
		excel sheet add "flow" [CONF_DESTINATION_SHEETNAME];

		log write (concat "Build the file 'home/" [FLOW_PID] "/" [CONF_DESTINATION_NEW_FILENAME] "' from REST API...") OK null null;
		
		excel cell set "flow" [CONF_DESTINATION_SHEETNAME] 0 0 "ColNameA" STR;
		excel cell set "flow" [CONF_DESTINATION_SHEETNAME] 0 1 "ColNameB" STR;
		excel cell set "flow" [CONF_DESTINATION_SHEETNAME] 0 2 "ColNameC" STR;

		json load "header" "{}";
		json iobject "header" / "x-user" "admin" STR;
		json iobject "header" / "x-password" "pwd" STR;
		json load "flow_source" (rest https get "https://localhost:9999/api/addition" "v1=1&v2=6" (json doc "header") "[]");
		
		# BEGIN METHOD 1;
		-> "[T_A]" "";
		-> "[T_B]" "";
		-> "[T_C]" "";
		json parse_obj "flow_source" "/" "[key]" "[val]" {

			-> (concat "[T_" [key] "]") [val];

		};

		excel cell set "flow" [CONF_DESTINATION_SHEETNAME] 1 0 [T_A] STR;
		excel cell set "flow" [CONF_DESTINATION_SHEETNAME] 1 1 [T_B] STR;
		excel cell set "flow" [CONF_DESTINATION_SHEETNAME] 1 2 [T_C] STR;
		# END METHOD 1;
		
		# BEGIN METHOD 2;
		-> "[index]" 1;
		json parse_array "flow_source" "/" "row" {

			-> "[T_A]" (json select "row" /A);
			-> "[T_B]" (json select "row" /B);
			-> "[T_C]" (json select "row" /C);

			excel cell set "flow" [CONF_DESTINATION_SHEETNAME] [index] 0 [T_A] STR;
			excel cell set "flow" [CONF_DESTINATION_SHEETNAME] [index] 1 [T_B] STR;
			excel cell set "flow" [CONF_DESTINATION_SHEETNAME] [index] 2 [T_C] STR;
			
			++ "[index]";
		
		};
		# END METHOD 2;

		excel save "flow" (concat "home/" [FLOW_PID] "/" [CONF_DESTINATION_NEW_FILENAME]);
		excel close "flow";

		log write (concat "Builded.") OK null null;

		#Step 3 is valid;
		stack flow_step [FLOW_PID] 3 "transform_ok";
		
		stack (date now) "folder1.folder2.folder3.step_4_destination_connect.exe" "[FLOW_PID]" [FLOW_PID] "[filename]" [CONF_DESTINATION_NEW_FILENAME];
	
	} {

		try {excel close "flow";} {} "[err]";

		#Step 3 is not valid;
		stack flow_step [FLOW_PID] 3 "transform_KO";
		
		# Give the error to the stack and stop the process;
		exception (1) ([global_err]);
	
	} "[global_err]";

} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_3_transform.exe" "0001_folder1_folder2_folder3")) {
	group grant script "folder1.folder2.folder3.step_3_transform.exe" "0001_folder1_folder2_folder3";
};

script merge "folder1.folder2.folder3.step_4_destination_connect.exe" true 5
  (param
  	(var "[FLOW_PID]" {true} "The flow PID" is_null:true is_empty:true "1")
  	(var "[filename]" {true} "The filename" is_null:true is_empty:true "file.xxx")
  )
  "Connect to the destination"
{

	# CONFIGURATION ;
	-> "[CONF_DESTINATION_CM]" "demo_cm";
	
	# HANDLE ;
	try {
	
		# Flow initialization;
		stack flow_step [FLOW_PID] 4 "destination_connect...";

		tunnel connect "destination_sess" {cm get [CONF_DESTINATION_CM];};
		
		#Step 4 is valid;
		stack flow_step [FLOW_PID] 4 "destination_connect_ok";
		
		include "folder1.folder2.folder3.step_5_load.exe";
	
	} {

		try {tunnel disconnect "destination_sess";} {} "[err]";

		#Step 4 is not valid;
		stack flow_step [FLOW_PID] 4 "destination_connect_KO";
		
		# Give the error to the stack and stop the process;
		exception (1) ([global_err]);
	
	} "[global_err]";
	
} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_4_destination_connect.exe" "0001_folder1_folder2_folder3")) {
	group grant script "folder1.folder2.folder3.step_4_destination_connect.exe" "0001_folder1_folder2_folder3";
};

script merge "folder1.folder2.folder3.step_5_load.exe" true 1
  (param
  )
  "Load file to the destination"
{

	# CONFIGURATION ;
	-> "[CONF_DIR_DESTINATION]" "tmp/destination";
	
	# HANDLE ;
	try {

		# Mark the flow as step 5;
		stack flow_step [FLOW_PID] 5 "load...";
		
		log write (concat "Send '" (concat "home/" [FLOW_PID] "/" [filename]) "' to the '" (concat [CONF_DIR_DESTINATION] "/" [filename]) "' ...") OK null null;

		# Copy file into the destination directory;
		#Open a reader;
		file reader_open "r1" (concat "home/" [FLOW_PID] "/" [filename]) BINARY null;

		tunnel execute "destination_sess" (concat 
			"-> \"[remote_destination]\" \"" (mql encode (concat [CONF_DIR_DESTINATION] "/" [filename])) "\";"
			(mql {
				file writer_open "w1" [remote_destination] true BINARY null;
			})
		);
	
		#Parse the file;
		while (is not null (-> "[bytes]" (file reader_get_bytes "r1" 402400));) {
	
			tunnel execute "destination_sess" (concat 
				"-> \"[bytes]\" \"" (mql encode [bytes]) "\";"
				(mql {
					file writer_add_bytes "w1" [bytes];
					file writer_flush "w1";
				})
			);
	
		};

		tunnel execute "destination_sess" (concat 
			(mql {
				#Force to write;
				file writer_flush "w1";
				file writer_close "w1";
			})
		);
	
		#Close the reader;
		file reader_close "r1";
		
		log write "Sended." OK null null;

		#Step 5 is valid;
		stack flow_step [FLOW_PID] 5 "load_ok";
		
		try {tunnel disconnect "destination_sess";} {} "[err]";
		
	} {

		#Close objects;
		try {file reader_close "r1";} {} "[sub_err]";
		try {tunnel execute "destination_sess" (concat 
			(mql {
				try {file writer_close "w1";} {} "[err]";
			})
		);} {} "[err]";
		try {tunnel disconnect "destination_sess";} {} "[err]";

		#Step 5 is not valid;
		stack flow_step [FLOW_PID] 5 "load_KO";
		
		# Give the error to the stack and stop the process;
		exception (1) ([global_err]);
	
	} "[global_err]";

} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_5_load.exe" "0001_folder1_folder2_folder3")) {
	group grant script "folder1.folder2.folder3.step_5_load.exe" "0001_folder1_folder2_folder3";
};
Services     Supporters     Contact     About     Legal notice © 2024