A workflow pre-processor is a Java class that gets invoked before the workflow start event is fired.

You can have it invoked before the workflow instantiation form is displayed or when it gets submitted but before the workflow starts.

This is normally quite useful to do some cleanup operation or to attach files to the workflow definition before it concretely gets instantiated.

In workflow modeler, select the workflow properties and scroll rigth down on the properties pane. The last two propewrties are Commands’s preProcessor and postProcessor properties. PreProcessor is for classes to be invoked before the instantiation form gets displayed, postProcessor for when the form is first submitted but before the workflow starts.

Now that we know a little bit more about those processors, let’s create a class that will attach some files (the index.page) to our workflow model before it gets an instantiation form, so we’ll use a pre processor command. Our class will need to implement InProcessJavaCommand to quality as a pre processor.

package com.acme.workflow;

import com.interwoven.modeler.workflow.WFMWorkflow;
import com.interwoven.modeler.workflow.commands.InProcessJavaCommand;
import java.util.Map;

public class AcmePreProcessor implements InProcessJavaCommand {

 public WFMWorkflow execute(WFMWorkflow workflow, Map<String, String> params) {
     return(workflow);
 }

}

This is a starting point. The workflow is the workflow model of our future Teamsite job (remember, it’s not been instantiated yet) and the params contains your configurable variables as a Map of key/value pairs. It is a normal map, so you can create or modify configurable variables before instantiation using get/put methods. Pretty handy.

At the end, we return the workflow as we have modified it. Also note the class names. These are not CSSDK classes, they’re Workflow Modeler classes.

Below is the final class doing the work of finding .page files and whenever we find one, attach the index.page of the same folder to the workflow. This is handy when publishing .pages in static delivery environment when they need refreshing. Note that in preProcessors, the paths to files are starting from the content store (/default/main/…).

package com.acme.workflow;

import com.interwoven.modeler.workflow.WFMFile;
import com.interwoven.modeler.workflow.WFMWorkflow;
import com.interwoven.modeler.workflow.commands.InProcessJavaCommand;
import com.interwoven.modeler.workflow.exceptions.WFMException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AcmePreProcessor implements InProcessJavaCommand {

public WFMWorkflow execute(WFMWorkflow workflow, Map<String, String> params) {

        Pattern pagePattern = Pattern.compile("(.*/sites/.*/).*\\.page$");

        String[] dependentFiles = params.get("preProcessorDependentFiles").split(",");

        for (String dependentFile : dependentFiles) {
            WFMFile[] attachedFiles = workflow.getFiles();
            for (WFMFile wFMFile : attachedFiles) {
                System.out.println("preprocessor: " + wFMFile.getFilePath());
                Matcher pageMatcher = pagePattern.matcher(wFMFile.getFilePath()); // we check this is a SitePublisher page by its name
                if (pageMatcher.find()) {
                    String pageFolderPath = pageMatcher.group(1); // we get the first back reference of the regular expression
                    String pagePathToAttach = pageFolderPath + dependentFile.trim();
                    WFMFile pageFileToAttach = new WFMFile(pagePathToAttach, "page attached by the preProcessor");

                    // check that this file is not already in the array of attached files
                    boolean pageAlreadyAttached = false; // assume that it's not until proven otherwise
                    WFMFile[] uptodateAttachedFiles = workflow.getFiles();
                    for (int i = 0; i < uptodateAttachedFiles.length; i++) {
                        if (uptodateAttachedFiles[i].getFilePath().equals(pageFileToAttach.getFilePath())) {
                            pageAlreadyAttached = true;
                        }
                    }
                    // also check that the file actually does exist
                    File pageFile = new File(pagePathToAttach);

                    // if it's not already attached, attach it
                    if (!pageAlreadyAttached && pageFile.exists()) {
                        try {
                            System.out.println("attadching file " + pagePathToAttach);
                            workflow.addFile(pageFileToAttach);
                        } catch (WFMException ex) {
                            Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "unable to attach the workflow file", ex);
                        }
                    } else {
                        System.out.println("The file is alrady attached to the workflow");
                    }
                } else {
                    System.out.println("The file " + wFMFile.getFilePath() + " is not a page");
                }
            }
        }
        return (workflow);
    }
}

Compile the class in the customer toolkit on the Teamsite server. After the server is back up, we can now tell the workflow to use our class.

The parameter preProcessorDependentFiles is a comma separated list of dependent files to attach whenever a page is found. In the workflow modeler, modify the preProcessor property to be “inprocess:com.acme.workflow.AcmePreProcessor:preProcessorDependentFiles=index.page” and publish the workflow model. Instantiate the workflow by selecting a SitePublisher page and the index.page should automagically be attached to the workflow with the comment “page attached by the preProcessor”.

Pattern pagePattern = Pattern.compile(“(.*/sites/(.*/)(.*)\\.page$”);

WFMFile[] attachedFiles = workflow.getFiles();
for (WFMFile wFMFile : attachedFiles) {
System.out.println(“portal news submit preprocessor: ” + wFMFile.getFilePath());
Matcher pageMatcher = pagePattern.matcher(wFMFile.getFilePath()); // we check this is a SitePublisher page by its name
if (pageMatcher.find()){
String pageFolderPath=pageMatcher.group(1); // we get the first back reference of the regular expression
String pageName=pageMatcher.group(2); // we get the second back reference of the regular expression
String pagePathToAttach=pageFolderPath + “index.page”;
WFMFile pageFileToAttach=new WFMFile(pagePathToAttach, “page attached by the preProcessor”);

// check that this file is not already in the array of attached files
boolean pageAlreadyAttached=false; // assume that it’s not until proven otherwise
for (int i=0; i<attachedFiles.length; i++){
if (attachedFiles[i].equals(pageFileToAttach)){
pageAlreadyAttached=true;
}
}
// if it’s not already attached, attach it
if (!pageAlreadyAttached){
try {
workflow.addFile(pageFileToAttach);
} catch (WFMException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, “unable to attach the workflow file”, ex);
}
}
} else {
System.out.println(“The file ” + wFMFile.getFilePath() + ” is not a page”);
}
}

return(workflow);
}

}

Advertisements
Comments
  1. howell says:

    I was also created a workflow postprocessor to set some value after the job instantiation, but have some problem for the WFMException handling. My code is like below:

    public class PostProcessor implements InProcessJavaCommand {
    private static Logger log = new Logger(LogFactory.getLog(PostProcessor.class));

    public WFMWorkflow execute(WFMWorkflow workflow, Map arg1){

    String jobDescription = “”;
    try {
    jobDescription = workflow.getVariable(“JobDescription”);
    workflow.setDescription(jobDescription);

    } catch (WFMException e) {
    //log.createLogError(“Error in postprocessor”, e);
    }

    return workflow;

    }
    }

    It tell me “Incompatible types” from my java IDE, any advise?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s