Saturday, 22 August 2015

Workflows


Writing custom workflow process:

@Component(service = WorkflowProcess.class, property = { "process.label=XYZ Process Step ",
"Constants.SERVICE_DESCRIPTION=A workflow process to peform xyz task" })
public class XYZWorkflow implements WorkflowProcess {

@Override
public void execute(WorkItem item, WorkflowSession wfsession, MetaDataMap args)
throws WorkflowException {

try {
  final WorkflowData workflowData = item.getWorkflowData(); // gain access to the payload data
  final String assetPath = WorkflowUtils.getAssetPathFromPayload(workflowData);
  if (StringUtils.isBlank(assetPath)) {
LOG.error("Error in getting asset path from payload. Cannot process workflow.");
return;
}

      final ResourceResolver resourceResolver = wfsession.adaptTo(ResourceResolver.class);
  
      final Map<String, Object> processArguments = WorkflowUtils.getProcessArguments(args);
      final Resource assetResource = resourceResolver.getResource(assetPath);

      final String workflow =
          WorkflowUtils.getParameter(processArguments, "xyzParam");
}
...........................................................................................................................................................
  public static String getAssetPathFromPayload(final WorkflowData workflowData) {
    if (workflowData == null || workflowData.getPayload() == null) {
      return null;
    }
    String assetPath = workflowData.getPayload().toString();
    if (assetPath.contains(CONTENT_PATH_SEPARATOR + JcrConstants.JCR_CONTENT
        + CONTENT_PATH_SEPARATOR)) {
      assetPath = assetPath.substring(0, assetPath.lastIndexOf(CONTENT_PATH_SEPARATOR
          + JcrConstants.JCR_CONTENT + CONTENT_PATH_SEPARATOR));
    }
    return assetPath;

  }
................................................................................................................................................................
  public static Map<String, Object> getProcessArguments(final MetaDataMap args) {
    String[] proccesArgsVals = ArrayUtils.EMPTY_STRING_ARRAY;
    if (args.containsKey("PROCESS_ARGS")) {
      final String processArgsString = args.get("PROCESS_ARGS", "");
      if (StringUtils.isNotEmpty(processArgsString)) {
        proccesArgsVals = StringUtils.split(processArgsString, "\n");
      }
    }
    return extractProcessArguments(proccesArgsVals);
  }
  private static Map<String, Object> extractProcessArguments(final String[] proccesArgsVals) {
    final Map<String, Object> params = new HashMap<>();
    if (ArrayUtils.isEmpty(proccesArgsVals)) {
      LOG.debug("Cannot extract workflow metadata. Hence returning empty.");
      return params;
    }
    for (final String processArgs : proccesArgsVals) {
      final String[] processArg = StringUtils.split(processArgs, "=");
      if (ArrayUtils.isNotEmpty(processArg) && processArg.length == 2) {
        params.put(StringUtils.trim(processArg[0]), StringUtils.trim(processArg[1]));
      }
    }
    return params;
  }
....................................................................................................................................................................
  public static String getParameter(final Map<String, Object> processArguments, final String key) {
    if (processArguments.containsKey(key)) {
      return processArguments.get(key).toString();
    }
    return StringUtils.EMPTY;

  }
......................................................................................................................................................................
Writing ECMA Script Sample:

/etc/workflow/scripts/myproj/abc.ecma

function check(){
var path = workflowData.getPayload().toString();
var session = workflowSession.getSession();
var node = session.getNode(path+"/jcr:content");
    log.info("tet-" + node.hasProperty("skipApprovalStep"));
return !node.hasProperty("skipApprovalStep");
}

No comments:

Post a Comment