Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdkhanhkhtn/d7d14b5198940c645ac6fe786b4376b1 to your computer and use it in GitHub Desktop.
Save hdkhanhkhtn/d7d14b5198940c645ac6fe786b4376b1 to your computer and use it in GitHub Desktop.
Script file
/**
* Learn Salesforce Commerce Cloud
*
* @author HDK
* Import inventory from the WebDav Directory
*
* @input WorkingFolder : String Local folder relatively to IMPEX/src.
* @input FilePattern : String Input File pattern to search in local folder relatively to IMPEX/ (default is "^Inventory_(\d){14}\.xml$").
* @input ImportMode : String The import mode (DELETE|MERGE|REPLACE|UPDATE)
* @input ImportFailedStatus : String Import status object after import failed.
* @input AfterProcessAction : String When file is uploaded, delete or keep it? ("Delete" / "Keep on server").
*/
importPackage( dw.system );
importPackage( dw.io );
/**
* Execute for Pipeline
*/
function execute( args : PipelineDictionary ) : Number {
var workingFolder = args.WorkingFolder;
var filePattern = args.FilePattern;
var importMode = args.ImportMode;
var afterProcessAction = args.AfterProcessAction;
var sourceFolder : File = new File(File.IMPEX + File.SEPARATOR + 'src' + File.SEPARATOR + workingFolder);
if (!sourceFolder.exists()) {
sourceFolder.mkdirs();
}
var listFiles : List = sourceFolder.listFiles();
var regExp : RegExp = new RegExp(filePattern);
for each (var _file : File in listFiles) {
if(regExp.test(_file.name)){
var importResult = new Pipelet('ImportInventoryLists').execute({
ImportFile: workingFolder + File.SEPARATOR + _file.name,
ImportMode: importMode
});
if (importResult.ErrorCode != 0) {
// Import failed
Logger.error("Import failded file {0}, message: {1}", _file.name, importResult.ErrorMsg);
return PIPELET_ERROR;
} else {
// Import success
//
afterProcessActionHandler(afterProcessAction, workingFolder, _file);
}
}
}
return PIPELET_NEXT;
}
function afterProcessActionHandler( afterProcessAction : String, workingFolder : String, fileImport : File ) {
try {
if ( afterProcessAction == "DELETE_FILE" ) {
fileImport.remove();
} else if ( afterProcessAction == "ARCHIVE_FILE" ) {
afterProcessActionArchiveHandler(workingFolder, fileImport);
}
return true;
} catch (e) {
Logger.error("Error occurred processing afterProcessAction: " + e.message);
return false;
}
}
function afterProcessActionArchiveHandler( workingFolder : String, fileImport : File ) {
try {
if ( workingFolder.charAt(0) == File.SEPARATOR ) {
workingFolder = workingFolder.substring(1);
}
var archiveFolder : File = new File(File.IMPEX + File.SEPARATOR + 'src' + File.SEPARATOR + workingFolder + File.SEPARATOR + 'archive' + File.SEPARATOR);
if (!archiveFolder.exists()) {
archiveFolder.mkdirs();
}
var fileImportArchivePath : File = new File(archiveFolder.getFullPath() + fileImport.getName());
fileImport.renameTo(fileImportArchivePath);
return true;
} catch (e) {
Logger.error("Error occurred processing afterProcessActionArchiveHandler: " + e.message);
return false;
}
}
/**
* Execute for Scriptmodule
*/
function importInventory( args : PipelineDictionary ) : Status {
var importFailedStatus = args.ImportFailedStatus;
if ( execute(args) == PIPELET_ERROR ) {
if (importFailedStatus == 'WARN') {
return new Status(Status.OK, 'WARN');
} else {
return new Status(Status.ERROR);
}
}
return new Status(Status.OK);
}
/** Exported functions **/
module.exports = {
execute: execute,
importInventory: importInventory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment