User Tools

Site Tools


Extract Data From Products Used

Summary

Below is an example demonstrating how you can remotely execute the List Products Used script, and optionally the Zip Compress Each script, by passing in an ECMA object to control the operation of the script.

API Areas of Interest

Example

DB_List_Products_Used_Extract.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// void : A function that gets the path of the current script
	function getScriptPath()
	{
		// Get the filename of the running script
		var sFileName = getScriptFileName();
		// Create a file info object
		var oFileInfo = new DzFileInfo( sFileName );
 
		// Declare a working variable
		var sPath;
 
		// If the version of the application provides the method
		if( typeof( oFileInfo.canonicalPath ) == "function" ){
			// Get the canonical path from the file
			sPath = oFileInfo.canonicalPath(); //requires 4.9.3.29 or newer
		// If the method we prefer is not available
		} else {
			// Use the fallback to get the (absolute) path
			sPath = oFileInfo.path();
		}
 
		// Clean up; do not leak memory
		oFileInfo.deleteLater();
 
		// Return the path
		return sPath;
	};
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// void : A function that executes a script and passes arguments to it
	function executeScriptWithArgs( sPath, aScriptArgs )
	{
		// Declare working variables
		var sTitle, sMessage;
 
		// Define common strings
		var sButton = text( "&OK" );
 
		// Create a script object
		var oScript = new DzScript();
 
		// Create a file info object
		var oFileInfo = new DzFileInfo( sPath );
		// Get the file extension
		var sExtension = oFileInfo.extension();
 
		// If the path does not have a file extension, attempt to find the
		// script with a supported extension; doing it this way, we can debug
		// with an ascii file and distribute a binary (encrypted) file with
		// the same name... without having to update the contents of the script
		// or manually handle the file extensions; requires 3.0.1.5 or newer
		var sScriptPath = sExtension.isEmpty() ?
				oScript.getScriptFile( sPath ) : sPath;
 
		// Clean up; do not leak memory
		oFileInfo.deleteLater();
 
		// If a script is found
		if( !sScriptPath.isEmpty() ){
			// If the script loads
			if( oScript.loadFromFile( sScriptPath ) ){
				// Execute the script; pass in an array of arguments;
				// passing in arguments requires 2.2.2.17 or newer
				oScript.execute( aScriptArgs );
			// If the script doesn't load
			} else {
				// Define text variables for the message
				sTitle = text( "Read Error" );
				sMessage = text( "The '%1' file could not be loaded." ).arg( sScriptPath );
				// Inform the user
				MessageBox.information( sMessage, sTitle, sButton );
			}
		// If a script is not found
		} else {
			// Define text variables for the message
			sTitle = text( "File Not Found" );
			sMessage = text( "A '%1.ds(a|b|e)' file could not be found." ).arg( sPath );
			// Inform the user
			MessageBox.information( sMessage, sTitle, sButton );
		}
	};
 
	/*********************************************************************/
	// Array<String> : A function that extracts a member from the List Products Used JSON data 
	function extractDataFromProductsUsed( sTargetPath, sMember, bRemoveTarget )
	{
		// Declare working variables
		var sTitle;
		var sMessage;
 
		// Define common strings
		var sButton = text( "&OK" );
 
		// Create a new file object for the target file
		var oFile = new DzFile( sTargetPath );
		// If the file does not exist
		if( !oFile.exists() ){
			// Define text variables for the message
			sTitle = text( "File Not Found" );
			sMessage = text( "The '%1' file could not be found." ).arg( sTargetPath );
			// Inform the user
			MessageBox.information( sMessage, sTitle, sButton );
 
			// We are done...
			return [];
		}
 
		// If the file could not be opened for reading
		if( !oFile.open( DzFile.ReadOnly ) ){
			// Define text variables for the message
			sTitle = text( "File Open Failed" );
			sMessage = text( "The '%1' file could not be opened." ).arg( sTargetPath );
			// Inform the user
			MessageBox.information( sMessage, sTitle, sButton );
 
			// We are done...
			return [];
		}
 
		// Read the lines of the file
		var aLines = oFile.readLines();
 
		// Close the file
		oFile.close();
 
		// If we are cleaning up temporary files
		if( bRemoveTarget ){
			// If we cannot remove the file
			if( !oFile.remove() ){
				// Define text variables for the message
				sTitle = text( "File Remove Failed" );
				sMessage = text( "The '%1' file could not be deleted." ).arg( sTargetPath );
				// Inform the user
				MessageBox.information( sMessage, sTitle, sButton );
			}
		}
 
		// Clean up; do not leak memory
		oFile.deleteLater();
 
		// Declare working variables
		var oProduct;
		var vMember;
 
		// Convert file lines into an ECMA Object
		var aProducts = JSON.parse( aLines.join( "\n" ) );
 
		// Create an object to capture the data we want
		var oValues = {};
 
		// Iterate over the products
		for( var i = 0, n = aProducts.length; i < n; i += 1 ){
			// Get the current product
			oProduct = aProducts[ i ];
			// If the product has the member we want to extract
			if( oProduct.hasOwnProperty( sMember ) ){
				// Get the value of the member
				vMember = oProduct[ sMember ];
				// If the value is an Array; e.g., "files"
				if( typeof( vMember ) == "object"
				&& Array.isArray( vMember ) ){
					// Iterate over members of the array
					for( var j = 0, m = vMember.length; j < m; j += 1 ){
						// Capture the data we want as a key;
						// keys are unique;
						// value does not matter
						oValues[ vMember[ j ] ] = true;
					}
				// If the value is an object
				} else if( typeof( vMember ) == "object" ){
					// Next!!
					continue;
				// Otherwise, e.g. String
				} else {
					// Capture the data we want as a key;
					// keys are unique;
					// value does not matter
					oValues[ vMember ] = true;
				}
			}
		}
 
		// Return the keys
		return Object.keys( oValues );
	};
 
	/*********************************************************************/
	// Define the path of the script we will call; without the file extension
	var sScriptPath = String( "%1/DB_List_Products_Used" ).arg( getScriptPath() );
 
	// Define the target path of the json file
	var sTargetPath = "C:/Temp/ProductsUsed.json";
 
	// Define the arguments to pass
	var oScriptArgs = {
			"target_path" : sTargetPath,
			"unify_products": true,
			"selected_only": false,
			"open_results": false,
			"run_silent": true
		};
 
	// Execute the script with our argument
	executeScriptWithArgs( sScriptPath, [ oScriptArgs ] );
 
	// Define the name of the member we want to extract
	var sMember = "files"; // token
 
	// Define whether to collect if "files"
	var bCollectIfFiles = false;
 
	// Let the user know we are busy
	setBusyCursor();
 
	// Extract data from the product used
	var aValues = extractDataFromProductsUsed( sTargetPath, sMember, true );
	var nValues = aValues.length;
 
	// Let the user know we are done
	clearBusyCursor();
 
	// Declare working variables
	var oContentMgr;
	var sRelPath;
	var aAbsPaths;
 
	// If the data we want to extract are the files
	if( sMember == "files"
	&& nValues > 0
	&& bCollectIfFiles ){
		// Get the content manager
		oContentMgr = App.getContentMgr();
		// If we do not have a content manager
		if( !oContentMgr ){
			// We are done...
			return;
		}
 
		// Let the user know we are busy
		setBusyCursor();
 
		// Pre-size
		aAbsPaths = new Array( nValues );
 
		// Iterate over the files
		for( var i = 0; i < nValues; i += 1 ){
			// Get the 'current' file
			sRelPath = aValues[ i ];
			// Get the absolute path
			aAbsPaths[ i ] = oContentMgr.getAbsolutePath( sRelPath, false );
		}
 
		// Let the user know we are done
		clearBusyCursor();
 
		// Remove empties
		aAbsPaths = aAbsPaths.filter( Boolean );
		// If we do not have equally sized lists
		if( aAbsPaths.length != nValues ){
			// We are done...
			return;
		}
 
		// Define the path of the script we will call; without the file extension
		sScriptPath = String( "%1/File_Zip_Compress_Each_Remote" ).arg( getScriptPath() );
 
		// Execute the script with our argument
		executeScriptWithArgs( sScriptPath,
			[ {
				"target_path": "C:/Temp/ProductsUsed.zip",
				"source_paths": aAbsPaths,
				"relative_paths": aValues
				//, "run_silent": true
			} ] );
	// Otherwise
	} else {
 
		// Let the user know we are busy
		setBusyCursor();
 
		// Iterate over the values
		for( var i = 0; i < nValues; i += 1 ){
			// Provide feedback
			print( aValues[ i ] );
		}
 
		// Let the user know we are done
		clearBusyCursor();
	}
 
// Finalize the function and invoke
})();