1 16 package nl.hippo.cms.workflow; 17 18 import com.opensymphony.workflow.Workflow; 19 import com.opensymphony.workflow.loader.ActionDescriptor; 20 import com.opensymphony.workflow.loader.WorkflowDescriptor; 21 import com.thoughtworks.xstream.XStream; 22 import com.thoughtworks.xstream.io.xml.DomDriver; 23 import com.thoughtworks.xstream.io.xml.DomReader; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Vector ; 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 import nl.hippo.cms.workflow.queries.Action; 36 import nl.hippo.cms.workflow.queries.And; 37 import nl.hippo.cms.workflow.queries.Authorization; 38 import nl.hippo.cms.workflow.queries.CurrentStepsAction; 39 import nl.hippo.cms.workflow.queries.CurrentStepsCaller; 40 import nl.hippo.cms.workflow.queries.CurrentStepsFinishDate; 41 import nl.hippo.cms.workflow.queries.CurrentStepsOwner; 42 import nl.hippo.cms.workflow.queries.CurrentStepsStartDate; 43 import nl.hippo.cms.workflow.queries.CurrentStepsStatus; 44 import nl.hippo.cms.workflow.queries.CurrentStepsStep; 45 import nl.hippo.cms.workflow.queries.EntryName; 46 import nl.hippo.cms.workflow.queries.EntryState; 47 import nl.hippo.cms.workflow.queries.Equal; 48 import nl.hippo.cms.workflow.queries.GreaterThan; 49 import nl.hippo.cms.workflow.queries.GreaterThanOrEqual; 50 import nl.hippo.cms.workflow.queries.HistoryStepsAction; 51 import nl.hippo.cms.workflow.queries.HistoryStepsCaller; 52 import nl.hippo.cms.workflow.queries.HistoryStepsFinishDate; 53 import nl.hippo.cms.workflow.queries.HistoryStepsOwner; 54 import nl.hippo.cms.workflow.queries.HistoryStepsStartDate; 55 import nl.hippo.cms.workflow.queries.HistoryStepsStatus; 56 import nl.hippo.cms.workflow.queries.HistoryStepsStep; 57 import nl.hippo.cms.workflow.queries.Internationalization; 58 import nl.hippo.cms.workflow.queries.LessThan; 59 import nl.hippo.cms.workflow.queries.LessThanOrEqual; 60 import nl.hippo.cms.workflow.queries.NotEqual; 61 import nl.hippo.cms.workflow.queries.Or; 62 import nl.hippo.cms.workflow.queries.Query; 63 import nl.hippo.cocoon.webdav.WebDAVHelper; 64 import nl.hippo.servermanager.ComponentLoadException; 65 import nl.hippo.servermanager.ProjectWorkflowRepository; 66 import org.apache.commons.httpclient.HttpState; 67 import org.w3c.dom.Document ; 68 import org.xml.sax.SAXException ; 69 70 75 public class WorkflowHelper { 76 77 private static final String PROPERTIES_CACHE_KEY = "propertiesCache"; 78 79 public static ActionDescriptor[] getAvailableActions(Workflow workflow, String idAsString, Map parameters) { 80 return getAvailableActions(workflow, Long.parseLong(idAsString), parameters); 81 } 82 83 public static ActionDescriptor[] getAvailableActions(Workflow workflow, long id, Map parameters) { 84 final String name = workflow.getWorkflowName(id); 85 if (name == null) { 86 return null; 87 } 88 final WorkflowDescriptor descriptor = workflow.getWorkflowDescriptor(name); 89 90 final int[] ids = workflow.getAvailableActions(id, parameters); 91 final ActionDescriptor[] actions = new ActionDescriptor[ids.length]; 92 for (int i = 0; i < ids.length; i++) { 93 actions[i] = descriptor.getAction(ids[i]); 94 } 95 96 return actions; 97 } 98 99 public static ActionDescriptor[] getInitialActions(Workflow workflow, String name) { 100 final WorkflowDescriptor descriptor = workflow.getWorkflowDescriptor(name); 101 final List actions = descriptor.getInitialActions(); 102 return (ActionDescriptor[]) actions.toArray(new ActionDescriptor[actions.size()]); 103 } 104 105 public static List readQueries(InputStream inputStream) throws SAXException , IOException , ParserConfigurationException 106 { 107 List result; 108 109 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 110 Document d = db.parse(inputStream); 111 Object deserializedObject = createXStreamForQueries().unmarshal(new DomReader(d)); 112 if (deserializedObject instanceof List ) 113 { 114 result = (List ) deserializedObject; 115 } 116 else 117 { 118 result = null; 119 } 120 121 return result; 122 } 123 124 private static XStream createXStreamForQueries() 125 { 126 DomDriver dd = new DomDriver(); 127 XStream xs = new XStream(dd); 128 129 xs.setMode(XStream.NO_REFERENCES); 130 131 xs.alias("query", Query.class); 132 xs.alias("internationalization", Internationalization.class); 133 xs.alias("authorization", Authorization.class); 134 xs.alias("action", Action.class); 135 136 xs.alias("and", And.class); 137 xs.alias("equal", Equal.class); 138 xs.alias("greater-than", GreaterThan.class); 139 xs.alias("greater-than-or-equal", GreaterThanOrEqual.class); 140 xs.alias("less-than", LessThan.class); 141 xs.alias("less-than-or-equal", LessThanOrEqual.class); 142 xs.alias("not-equal", NotEqual.class); 143 xs.alias("or", Or.class); 144 145 xs.alias("current-steps-action", CurrentStepsAction.class); 146 xs.alias("current-steps-caller", CurrentStepsCaller.class); 147 xs.alias("current-steps-finish-date", CurrentStepsFinishDate.class); 148 xs.alias("current-steps-owner", CurrentStepsOwner.class); 149 xs.alias("current-steps-start-date", CurrentStepsStartDate.class); 150 xs.alias("current-steps-status", CurrentStepsStatus.class); 151 xs.alias("current-steps-step", CurrentStepsStep.class); 152 153 xs.alias("entry-name", EntryName.class); 154 xs.alias("entry-state", EntryState.class); 155 156 xs.alias("history-steps-action", HistoryStepsAction.class); 157 xs.alias("history-steps-caller", HistoryStepsCaller.class); 158 xs.alias("history-steps-finish-date", HistoryStepsFinishDate.class); 159 xs.alias("history-steps-owner", HistoryStepsOwner.class); 160 xs.alias("history-steps-start-date", HistoryStepsStartDate.class); 161 xs.alias("history-steps-status", HistoryStepsStatus.class); 162 xs.alias("history-steps-step", HistoryStepsStep.class); 163 164 return xs; 165 } 166 167 public static void movePropertiesToCache(String location, Enumeration propertiesToFind, Map cache, HttpState httpState) throws IOException { 168 Vector propertiesNotFoundYet = new Vector (); 169 while (propertiesToFind.hasMoreElements()) { 170 Object propertyToFind = propertiesToFind.nextElement(); 171 if (!cache.containsKey(propertyToFind)) { 172 propertiesNotFoundYet.add(propertyToFind); 173 } 174 } 175 if (propertiesNotFoundYet.size() > 0) { 176 Map propertyValues = WebDAVHelper.propfind(location, propertiesNotFoundYet.elements(), httpState); 177 for (Iterator propertyValuesIterator = propertyValues.keySet().iterator(); propertyValuesIterator.hasNext();) 178 { 179 Object key = propertyValuesIterator.next(); 180 cache.put(key, propertyValues.get(key)); 181 } 182 } 183 } 184 185 public static Map getPropertiesCache(final Map transientVars) 186 { 187 Map result = (Map ) transientVars.get(PROPERTIES_CACHE_KEY); 188 if (result == null) 189 { 190 result = new HashMap (); 191 transientVars.put(PROPERTIES_CACHE_KEY, result); 192 } 193 return result; 194 } 195 196 public static WorkflowActionAuthorizer getActionsAuthorizer(ProjectWorkflowRepository workflow, long workflowId) throws ClassNotFoundException , InstantiationException , IllegalAccessException 197 { 198 WorkflowActionAuthorizer result; 199 String authorizerClassName = null; 200 201 String workflowName = workflow.getWorkflowName(workflowId); 202 WorkflowDescriptor workflowDescriptor = workflow.getWorkflowDescriptor(workflowName); 203 if (workflowDescriptor != null) 204 { 205 Map metaAttributes = workflowDescriptor.getMetaAttributes(); 206 authorizerClassName = (String ) metaAttributes.get("authorizerClass"); 207 } 208 209 if (authorizerClassName == null || authorizerClassName.equals("")) 210 { 211 authorizerClassName = NonFilteringAuthorizer.class.getName(); 212 } 213 214 try 215 { 216 result = (WorkflowActionAuthorizer) workflow.loadComponent(workflowId, authorizerClassName); 217 } 218 catch (ComponentLoadException e) 219 { 220 throw new InstantiationException ("Workflow component could not create instance of authorizer class"); 221 } 222 223 return result; 224 } 225 } 226 | Popular Tags |