1 17 18 19 20 package org.apache.lenya.cms.cocoon.components.modules.input; 21 22 import java.util.Arrays ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.cocoon.components.modules.input.AbstractInputModule; 29 import org.apache.lenya.cms.publication.Document; 30 import org.apache.lenya.cms.publication.PageEnvelope; 31 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 32 import org.apache.lenya.cms.workflow.CMSHistory; 33 import org.apache.lenya.cms.workflow.WorkflowFactory; 34 import org.apache.lenya.workflow.WorkflowInstance; 35 36 public class WorkflowModule extends AbstractInputModule { 37 38 public static final String STATE = "state"; 39 public static final String VARIABLE_PREFIX = "variable."; 40 public static final String HISTORY_PATH = "history-path"; 41 42 static final String [] PARAMETER_NAMES = { STATE, HISTORY_PATH }; 43 44 47 public Object getAttribute(String name, Configuration modeConf, Map objectModel) 48 throws ConfigurationException { 49 50 Object value = null; 51 52 53 try { 54 PageEnvelope envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 55 Document document = envelope.getDocument(); 56 57 WorkflowFactory factory = WorkflowFactory.newInstance(); 58 if (factory.hasWorkflow(document)) { 59 WorkflowInstance instance = factory.buildInstance(document); 60 if (name.equals(STATE)) { 61 value = instance.getCurrentState().toString(); 62 } 63 else if (name.startsWith(VARIABLE_PREFIX)) { 64 String variableName = name.substring(VARIABLE_PREFIX.length()); 65 String [] variableNames = instance.getWorkflow().getVariableNames(); 66 if (Arrays.asList(variableNames).contains(variableName)) { 67 value = Boolean.valueOf(instance.getValue(variableName)); 68 } 69 } 70 else if (name.equals(HISTORY_PATH)) { 71 value = ((CMSHistory) WorkflowFactory.getHistory(document)).getHistoryPath(); 72 } 73 else { 74 throw new ConfigurationException("The attribute [" + name + "] is not supported!"); 75 } 76 } 77 } catch (ConfigurationException e) { 78 throw e; 79 } catch (Exception e) { 80 throw new ConfigurationException("Resolving attribute failed: ", e); 81 } 82 return value; 83 } 84 85 88 public Iterator getAttributeNames(Configuration modeConf, Map objectModel) 89 throws ConfigurationException { 90 return Arrays.asList(PARAMETER_NAMES).iterator(); 91 } 92 93 96 public Object [] getAttributeValues(String name, Configuration modeConf, Map objectModel) 97 throws ConfigurationException { 98 Object [] objects = { getAttribute(name, modeConf, objectModel)}; 99 100 return objects; 101 } 102 103 } 104 | Popular Tags |