Global library

A shared workflow library that provides common utility classes available to any workflow in the system. Deploy it once per OpenKM instance; other workflows load its nodes via a Library node and invoke its methods directly in their scripts. It contains two library nodes: library_global_base (general utilities) and library_global_document (document operations).

Overview

Download Global library.okmflow 

Diagram

 

Library nodes

library_global_base

Defines the BaseLibrary class. Bind this node to the name BaseLibrary in your Library node configuration. Provides logging, context access, OpenKM web services, JSON serialisation and duration checking.

MethodReturn valueDescription

logInfo(String component, String message)

void

Writes an INFO entry to the workflow log file via FileLogger.

component: Label that identifies the caller (e.g. "MyWorkflow").

message: Text to log.

logError(String component, String message, Exception e)

void

Writes an ERROR entry including the full stack trace via StackTraceUtils. The exception parameter is optional (null is accepted).

component: Label that identifies the caller.

message: Error description.

e: Exception to include in the log entry. Pass null if not applicable.

getWebservices()

OKMWebservices

Returns the shared OKMWebservices instance via WebservicesHelper.getInstance(). Use it to interact with the OpenKM repository (documents, folders, metadata, etc.).

getContextValue(Map<String,Object> context, String key)

Object

Returns the value stored under key in the workflow context map.

context: The workflow context map (the context variable in scripts).

key: Context variable name.

setContextValue(Map<String,Object> context, String key, Object value)

void

Stores value under key in the workflow context map.

context: The workflow context map.

key: Context variable name.

value: Value to store.

getInitiatorId(Map<String,Object> context)

String

Returns the user ID of the person who started the workflow process (reads the initiator Actor from context).

context: The workflow context map.

getProcessInstanceId(Map<String,Object> context)

long

Returns the numeric ID of the current process instance (reads the processInstance from context).

context: The workflow context map.

convertToJson(Object obj)

String

Serialises obj to a JSON string using Jackson ObjectMapper. Throws JsonProcessingException on failure.

obj: Any Java/Groovy object to serialise.

convertFromJson(String jsonString, Class targetClass)

Object

Deserialises a JSON string into an instance of targetClass. Unknown properties in the JSON are silently ignored (FAIL_ON_UNKNOWN_PROPERTIES = false). Throws an exception on invalid JSON or class mismatch.

jsonString: JSON string to deserialise.

targetClass: Target Java class (e.g. MyDTO.class).

hasDurationElapsed(String durationString, LocalDateTime startTime)

boolean

Returns true if the time elapsed since startTime is greater than or equal to the ISO 8601 duration in durationString. Commonly used in Scheduled nodes to poll whether enough time has passed.

durationString: ISO 8601 duration string (e.g. "PT4M" for 4 minutes, "P1D" for 1 day).

startTime: Reference LocalDateTime stored previously in the context.

library_global_document

Provides document-specific operations. Bind this node to the name docLib in your Library node configuration. Depends on BaseLibrary ? the Library node for library_global_base must be configured in the same workflow.

MethodReturn valueDescription

rename(Map<String,Object> context, String newName)

void

Renames the workflow document following the convention prefix-newName.ext. Reads the document UUID from the context key "uuid", resolves the current file name, extracts the prefix (the part before the first -), then calls ws.document.rename() with the new composed name.

context: The workflow context map. Must contain the key "uuid" with the document UUID.

newName: The new suffix to place after the prefix separator (e.g. "approved" produces DOC-001-approved.pdf).

How to use

Add one Library node per library node you want to consume. In each node, configure:

  • Library workflow name: Global library
  • Library node name: the node to load (library_global_base or library_global_document)
  • Binding variable: the name used to call its methods in scripts (e.g. BaseLibrary, docLib)

Example ? Action node script that uses both library nodes:

// Library node 1 configured as: Global library / library_global_base ? BaseLibrary
// Library node 2 configured as: Global library / library_global_document ? docLib

BaseLibrary.logInfo("MyWorkflow", "Starting document processing")

long piId = BaseLibrary.getProcessInstanceId(context)
String initiator = BaseLibrary.getInitiatorId(context)
BaseLibrary.logInfo("MyWorkflow", "Process ${piId} started by ${initiator}")

// Rename document: prefix-approved.pdf
docLib.rename(context, "approved")

Example ? Scheduled node using hasDurationElapsed to poll every cycle:

import java.time.LocalDateTime

// On first pass, store the start time
if (context.get("taskStarted") == null) {
    context.put("taskStarted", LocalDateTime.now())
}

LocalDateTime taskStarted = context.get("taskStarted")

// Move to "timeout" transition after 4 minutes
if (BaseLibrary.hasDurationElapsed("PT4M", taskStarted)) {
    return "timeout"
}
return "wait"