PhenixID

Script Execution

Version: 1.0.0

Category: Process

Extended Category: Session Attribute, Session Object, Transformation

Action Package: Standard Actions

Description

Performs execution of custom scripts (Javascript) on the list of session objects

Parameter

Description

Example

Script to execute:

The script to execute

if(context.sessionObjects().length !== 0) {
throw new Error("Expected no session objects");
}
var sessionObject = context.sessionObjects().create("foo-object");
var sessionAttributeBinary = sessionObject.attributes().create();
sessionAttributeBinary.setName("binary-data");
const byteArray = new Uint8Array(256);
for (let i = 0; i < 256; i++) {
byteArray[i] = i;
}
sessionAttributeBinary.setBinaryValue(byteArray);
sessionObject.attributes().add(sessionAttributeBinary);
var sessionAttributeMultiValueString =sessionObject.attributes().create();
sessionAttributeMultiValueString.setName("multi-value-string");
sessionAttributeMultiValueString.setStringValues(["foo", "bar"]);
sessionObject.attributes().add(sessionAttributeMultiValueString);
context.sessionObjects().push(sessionObject);

Script to execute

The script (Javascript) to execute.

  • The script is executed once per invocation of the action
  • A context object can be accessed via the field ‘context’

context.sessionObjects()

Returns an objects that provides access to the session objects

  • .create(name) – creates a new session object – does not add it
  • .get(index)
  • .size()
  • .set(index, sessionObject)
  • .add(sessionObject)
  • .add(index, sessionObject)
  • .remove(index)
  • .clear()
  • .indexOf(sessionObject)
  • .lastIndexOf(sessionObject)

Returned from .create() and .get() above is a session object:

  • .attributes() – provides access to the session attributes for the session object
  • .copy() – copies the entire object, but does not add it to the list of session objects
  • .get-/setName()

Returned from .attributes() is an object which provides access to the session attributes:

  • .create() – creates a new session attribute – the caller needs to set the name and then add it explicitly
  • .add(attribute) – adds a new session attribute – if there already are attributes with the same name, all attributes are kept
  • .set(attribute) – adds a new session attribute – if tehre already are attributes with the same name, only the new attributes is kept, the other attributes with the same name will be removed
  • .remove(attribute)
  • .getAll() – returns all session attributes. Beware, there might be multiple attributes with the same name
  • .getAllByName(name) – returns all attributes with the provided name
  • .getSingleByName(name) – returns a single attribute with the provided name, or null (if not attribute with the provided name is found). In case there are multiple attributes with the provided name, an error is thrown

From the methods above, a single session attribute provides these methods:

  • .get/setName()
  • .isBinary() – whether the attribute seems to contain binary data
  • .get/setEdited()
  • .get/setBinaryValue() – null is no value. When getting, if there are multiple values, an error is thrown
  • .get/setBinaryValues() – a list of byte arrays. An empty array equals to no values. Must not be null.
  • .get/setStringValue() – null is no value. When getting, if there are multiple values, an error is thrown
  • .get/setStringValues() – a list of strings. An empty array equals to no values. Must not be null.
  • .copy() – copies the attribute, but does not add it to the session object

context.requestBy()

Returns the initiator of the request, ex. from PIM

context.globalParameters()

Returns a regular Javascript object where all global parameters can be accessed as regular properties, both set and get

context.fileSystem()

Returns an object that provides access to filesystem operations

  • .absolutePath(path) – makes the path absolute for the filesystem
  • .combinePaths(basePath, additional_paths …) – combines multiple path elements into one path (string)
  • .copy(sourcePath, targetPath)
  • .createDirectory(path)
  • .delete(path)
  • .exists(path)
  • .listDirectories(path) – returns the directory names (not the full path) inside the path
  • .listFiles(path) – returns the file names (not the full path) inside the path
  • .move(sourcePath, targetPath)
  • .normalizePath(path) – normalizes a path, ex “foo/bar/..” might get normalized to just “foo” since “..” will “undo” the subdirectory “bar”
  • .readBinaryFile(path)
  • .readTextFile(path[, charset])
  • .writeBinaryFile(path, byteArray)
  • .writeTextFile(path, string[, charset])

Examples

These are just some examples of what’s possible to achieve

Write to log (debug level)

console.log("foo");
console.log("bar");
console.log("foo\nbar\rfoo\r\nbar");

Write to log (warning level)

console.error("foo");
console.error("bar");
console.error("foo\nbar\rfoo\r\nbar");

Reading binary session attributes

if(context.sessionObjects().length !== 1) {
    throw new Error("Expected one session object");
}
var sessionObject = context.sessionObjects()[0];
if(sessionObject === null) {
    throw new Error("Session object is null");
}
if(sessionObject.getName() !== "foo") {
    throw new Error("Session object not named 'foo'");
}
var sessionAttributeBars = sessionObject.attributes().getAllByName("bar");
if(sessionAttributeBars === null) {
    throw new Error("Could not find the attributes 'bar'");
}
if(sessionAttributeBars.length !== 1) {
    throw new Error("Could not find the attribute 'bar'");
}
const sessionAttributeBar = sessionAttributeBars[0];
var sessionAttributeBinaryDatas = sessionObject.attributes().getAllByName("binary-data");
if(sessionAttributeBinaryDatas === null) {
    throw new Error("Could not find the attributes 'binary-data'");
}
if(sessionAttributeBinaryDatas.length !== 1) {
    throw new Error("Could not find the attribute 'binary-data'");
}
const sessionAttributeBinaryData = sessionAttributeBinaryDatas[0];
if(sessionAttributeBar.getStringValue() !== "barson") {
    throw new Error("The attribute 'bar' did not have the value 'barson' - actual value: " + sessionAttributeBar.getValue());
}
var binaryData = sessionAttributeBinaryData.getBinaryValue();
if(binaryData === null) {
    throw new Error("The attribute binary-data has a null value");
}
if(binaryData.length !== 256) {
    throw new Error("The binary data should be 256 bytes");
}
for (var i=0; i < binaryData.length; i++) {
    var value = binaryData[i];
    if(value !== i) {
        throw new Error("The binary data with index " + i + " should be " + i + ", but was " + value);
    }
}
context.sessionObjects().splice(0, 1);

Creating binary and text objects

if(context.sessionObjects().length !== 0) {
    throw new Error("Expected no session objects");
}
var sessionObject = context.sessionObjects().create("foo-object");
var sessionAttributeBinary = sessionObject.attributes().create();
sessionAttributeBinary.setName("binary-data");
const byteArray = new Uint8Array(256);
for (let i = 0; i < 256; i++) {
  byteArray[i] = i;
}
sessionAttributeBinary.setBinaryValue(byteArray);
sessionObject.attributes().add(sessionAttributeBinary);
var sessionAttributeMultiValueString =sessionObject.attributes().create();
sessionAttributeMultiValueString.setName("multi-value-string");
sessionAttributeMultiValueString.setStringValues(["foo", "bar"]);
sessionObject.attributes().add(sessionAttributeMultiValueString);
context.sessionObjects().push(sessionObject);

Reading and writing global parameters

if(Object.keys(context.globalParameters()).length !== 0) {
    throw new Error("There should be 0 globalparameters; actual: " + Object.keys(context.globalParameters()).length);
}
context.globalParameters()["foo"] = "bar";
console.log("X: " + context.globalParameters()["foo"]);
console.log("X: " + context.globalParameters()["moo"]);
for(var x in context.globalParameters()) {
    console.log("Y: " + x);
}
if(Object.keys(context.globalParameters()).length !== 1) {
    throw new Error("There should be 1 globalparameters; actual: " + Object.keys(context.globalParameters()).length);
}
if(context.globalParameters()["foo"] !== "bar") {
    throw new Error("There should be a global parameter foo with the value bar");
}
if(context.globalParameters()["moo"]) {
    throw new Error("There should not be a global parameter moo with the value bar");
}
delete context.globalParameters()["foo"];
if(context.globalParameters()["foo"]) {
    throw new Error("There should not be a global parameter foo with the value bar");
}
if(Object.keys(context.globalParameters()).length !== 0) {
    throw new Error("There should be 0 globalparameters; actual: " + Object.keys(context.globalParameters()).length);
}

Working file filesystem paths

if(!context.fileSystem()) {
    throw new Error("No fileSystem()");
}
const fs = context.fileSystem();
const tempPath = fs.combinePaths("target", "temp");
console.log("absolute path: " + fs.absolutePath(tempPath));
if(!fs.exists(tempPath)) {
    throw new Error("target/temp does not exist");
}
fs.delete(tempPath);
if(fs.exists(tempPath)) {
    throw new Error("target/temp does exist");
}
const subPath = fs.combinePaths(tempPath, "A", "B");
fs.createDirectory(subPath);
if(!fs.exists(tempPath)) {
    throw new Error(tempPath + " does not exist");
}
if(!fs.exists(subPath)) {
    throw new Error(subPath + " does not exist");
}

Normalizing filesystem paths

if(!context.fileSystem()) {
    throw new Error("No fileSystem()");
}
const fs = context.fileSystem();
const rootPath = "root";
const subPath = "sub";
const innerSubPath = "innerSub";
const combinedSubPath = fs.combinePaths(rootPath, subPath);
const combinedInnerSubPath = fs.combinePaths(rootPath, subPath, innerSubPath);
if(!combinedInnerSubPath.startsWith(combinedSubPath)) {
    throw new Error("combinedInnerSubPath does not start with combinedSubPath");
}
const combinedInnerSubAndUpPath = fs.combinePaths(fs.combinePaths(rootPath), subPath, innerSubPath, "..");
if(!combinedInnerSubAndUpPath.startsWith(combinedSubPath)) {
    throw new Error("combinedInnerSubAndUpPath does not start with combinedSubPath");
}
const combinedInnerSubAndUpNormalizedPath = fs.normalizePath(combinedInnerSubAndUpPath);
if(combinedInnerSubAndUpNormalizedPath !== combinedSubPath) {
    throw new Error("combinedInnerSubAndUpNormalizedPath does not equals to combinedSubPath");
}

Listing directories

if(!context.fileSystem()) {
    throw new Error("No fileSystem()");
}
const fs = context.fileSystem();
const tempPath = fs.combinePaths("target", "temp");
const pathA = fs.combinePaths(tempPath, "A");
const pathB = fs.combinePaths(tempPath, "B");
fs.createDirectory(pathA);
if(!fs.exists(pathA)) {
    throw new Error("pathA does not exist");
}
if(fs.exists(pathB)) {
    throw new Error("pathB exist");
}
fs.copy(pathA, pathB);
if(!fs.exists(pathA)) {
    throw new Error("pathA does not exist");
}
if(!fs.exists(pathB)) {
    throw new Error("pathB does not exist");
}
const listedDirectories = fs.listDirectories(tempPath);
if(!listedDirectories) {
    throw new Error("listedDirectories is empty");
}
if(listedDirectories.length !== 2) {
    throw new Error("listedDirectories does not contain 2 entries");
}
if(!listedDirectories.includes("A")) {
    throw new Error("listedDirectories is does not include A");
}
if(!listedDirectories.includes("B")) {
    throw new Error("listedDirectories is does not include B");
}

Writing and reading binary files

if(!context.fileSystem()) {
    throw new Error("No fileSystem()");
}
const fs = context.fileSystem();
const tempPath = fs.combinePaths("target", "temp");
const filePath = fs.combinePaths(tempPath, "test.bin");
const testDir = fs.combinePaths(tempPath, "test-dir");
fs.createDirectory(testDir);
const byteArray = new Uint8Array(256);
for (let i = 0; i < 256; i++) {
  byteArray[i] = i;
}
fs.writeBinaryFile(filePath, byteArray);
const listedDirectories = fs.listDirectories(tempPath);
if(!listedDirectories) {
    throw new Error("listedDirectories is empty");
}
if(listedDirectories.length !== 1) {
    throw new Error("listedDirectories does not contain 1 entry");
}
if(listedDirectories[0] !== "test-dir") {
    throw new Error("listedDirectories does not contain test-dir");
}
const listedFiles = fs.listFiles(tempPath);
if(!listedFiles) {
    throw new Error("listedFiles is empty");
}
if(listedFiles.length !== 1) {
    throw new Error("listedFiles does not contain 1 entry");
}
if(listedFiles[0] !== "test.bin") {
    throw new Error("listedFiles does not contain test.bin");
}
const readBytes = fs.readBinaryFile(filePath);
if(!readBytes) {
    throw new Error("readBytes is null");
}
if(readBytes.length !== byteArray.length) {
    throw new Error("readBytes.length !== byteArray.length; " + readBytes.length + " vs " + byteArray.length);
}
for(var i=0; i < readBytes.length; i++) {
    if(readBytes[i] !== byteArray[i]) {
        throw new Error("readBytes[" + i + "] !== byteArray[" + i + "]; " + readBytes[i] + " vs " + byteArray[
    }
}
fs.delete(filePath);
fs.delete(testDir);
fs.delete(tempPath);

Reading and writing text files with encoding

if(!context.fileSystem()) {
    throw new Error("No fileSystem()");
}
const fs = context.fileSystem();
const tempPath = fs.combinePaths("target", "temp");
const filePath = fs.combinePaths(tempPath, "test.bin");
const testDir = fs.combinePaths(tempPath, "test-dir");
fs.createDirectory(testDir);
const fileContent = "teståäöÅÄÖ";
fs.writeTextFile(filePath, fileContent, "UTF-8");
const listedDirectories = fs.listDirectories(tempPath);
if(!listedDirectories) {
    throw new Error("listedDirectories is empty");
}
if(listedDirectories.length !== 1) {
    throw new Error("listedDirectories does not contain 1 entry");
}
if(listedDirectories[0] !== "test-dir") {
    throw new Error("listedDirectories does not contain test-dir");
}
const listedFiles = fs.listFiles(tempPath);
if(!listedFiles) {
    throw new Error("listedFiles is empty");
}
if(listedFiles.length !== 1) {
    throw new Error("listedFiles does not contain 1 entry");
}
if(listedFiles[0] !== "test.bin") {
    throw new Error("listedFiles does not contain test.bin");
}
const readFileContent = fs.readTextFile(filePath, "UTF-8");
if(!readFileContent) {
    throw new Error("readFileContent is null");
}
if(readFileContent !== fileContent) {
    throw new Error("readFileContent !== fileContent; " + readFileContent + " vs " + fileContent);
}
fs.delete(filePath);
fs.delete(testDir);
fs.delete(tempPath);

DISCLAIMER
Information provided in this document is for your information only. PhenixID makes no explicit or implied claims to the validity of this information. Any trademarks referenced in this document are the property of their respective owners.

The origin of this information may be internal or external to PhenixID. PhenixID makes all reasonable efforts to verify this information.

PhenixID - support.phenixid.se