WorkflowConfigurationHandler.java 3.08 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Property
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemfd.watchfolder.workflow;

import com.adobe.aemfd.watchfolder.config.WatchFolderConfiguration;
import com.adobe.aemfd.watchfolder.spi.ConfigurationHandler;
import com.adobe.aemfd.watchfolder.util.JcrUtil;
import com.adobe.aemfd.watchfolder.workflow.VariableInfo;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Property;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(immediate=1)
@Service(value={WorkflowConfigurationHandler.class})
public class WorkflowConfigurationHandler
implements ConfigurationHandler {
    private static final String WF_VAR_PREFIX = "workflow.var.";
    private static final String WF_VAR_MAP = "workflow.variables";
    private static final Logger logger = LoggerFactory.getLogger(WorkflowConfigurationHandler.class);

    @Override
    public boolean enhanceConfiguration(WatchFolderConfiguration config, Property p) throws Exception {
        String pName = p.getName();
        if (!pName.startsWith("workflow.var.")) {
            return false;
        }
        try {
            Object varValue;
            HashMap<String, VariableInfo> vars = (HashMap<String, VariableInfo>)config.getCustomData("workflow.variables");
            if (vars == null) {
                vars = new HashMap<String, VariableInfo>();
                config.setCustomData("workflow.variables", vars);
            }
            String varName = pName.substring("workflow.var.".length());
            VariableInfo vi = new VariableInfo();
            vi.name = varName;
            vi.propertyType = p.getType();
            vi.isMultiple = p.isMultiple();
            vi.value = varValue = JcrUtil.getPropertyValue(p, null, false);
            vars.put(varName, vi);
            logger.info("Set workflow variable " + varName + " to value " + JcrUtil.toString(varValue));
        }
        catch (Exception e) {
            logger.warn("Error handling workflow variable " + pName + ", error = '" + e + "', skipping...");
        }
        return true;
    }

    Map<String, VariableInfo> getVariableDefinitions(WatchFolderConfiguration config) {
        Map result = (Map)config.getCustomData("workflow.variables");
        return result == null ? Collections.emptyMap() : result;
    }

    VariableInfo getVariableDefinition(WatchFolderConfiguration config, String varName) throws Exception {
        Map<String, VariableInfo> vars = this.getVariableDefinitions(config);
        VariableInfo vi = vars.get(varName);
        if (vi == null) {
            throw new Exception("No workflow variable with name " + varName + " defined for watch-folder " + config.getId());
        }
        return vi;
    }
}