1 17 18 19 20 package org.apache.lenya.cms.workflow; 21 22 import java.io.File ; 23 24 import org.apache.lenya.ac.Identity; 25 import org.apache.lenya.ac.Machine; 26 import org.apache.lenya.ac.Role; 27 import org.apache.lenya.ac.User; 28 import org.apache.lenya.cms.publication.Document; 29 import org.apache.lenya.cms.publication.DocumentException; 30 import org.apache.lenya.cms.publication.LanguageVersions; 31 import org.apache.lenya.cms.publication.Publication; 32 import org.apache.lenya.workflow.Situation; 33 import org.apache.lenya.workflow.SynchronizedWorkflowInstances; 34 import org.apache.lenya.workflow.Workflow; 35 import org.apache.lenya.workflow.WorkflowException; 36 import org.apache.lenya.workflow.WorkflowInstance; 37 import org.apache.lenya.workflow.impl.History; 38 import org.apache.lenya.workflow.impl.WorkflowBuilder; 39 40 43 public class WorkflowFactory { 44 public static final String WORKFLOW_DIRECTORY = 45 "config/workflow".replace('/', File.separatorChar); 46 47 48 protected WorkflowFactory() { 49 } 50 51 55 public static WorkflowFactory newInstance() { 56 return new WorkflowFactory(); 57 } 58 59 65 public WorkflowInstance buildInstance(Document document) throws WorkflowException { 66 assert document != null; 67 return getHistory(document).getInstance(); 68 } 69 70 76 public SynchronizedWorkflowInstances buildSynchronizedInstance(Document document) throws WorkflowException { 77 assert document != null; 78 LanguageVersions versions; 79 try { 80 versions = new LanguageVersions(document); 81 } catch (DocumentException e) { 82 throw new WorkflowException(e); 83 } 84 return new WorkflowDocumentSet(versions, document); 85 } 86 87 93 public static void moveHistory(Document oldDocument, Document newDocument) throws WorkflowException { 94 assert oldDocument != null; 95 new CMSHistory(oldDocument).move(newDocument); 96 } 97 98 103 public static void deleteHistory(Document document) throws WorkflowException { 104 assert document != null; 105 getHistory(document).delete(); 106 } 107 108 114 public boolean hasWorkflow(Document document) { 115 return getHistory(document).isInitialized(); 116 } 117 118 125 protected static Workflow buildWorkflow(Publication publication, String workflowFileName) 126 throws WorkflowException { 127 assert publication != null; 128 assert(workflowFileName != null) && !"".equals(workflowFileName); 129 130 File workflowDirectory = new File (publication.getDirectory(), WORKFLOW_DIRECTORY); 131 File workflowFile = new File (workflowDirectory, workflowFileName); 132 Workflow workflow = WorkflowBuilder.buildWorkflow(workflowFile); 133 134 return workflow; 135 } 136 137 144 public Situation buildSituation(Role[] roles, Identity identity) throws WorkflowException { 145 if (identity == null) { 146 throw new WorkflowException("Session does not contain identity!"); 147 } 148 String userId = null; 149 User user = identity.getUser(); 150 if (user != null) { 151 userId = user.getId(); 152 } 153 154 String machineIp = null; 155 Machine machine = identity.getMachine(); 156 if (machine != null) { 157 machineIp = machine.getIp(); 158 } 159 160 String [] roleIds = new String [roles.length]; 161 for (int i = 0; i < roles.length; i++) { 162 roleIds[i] = roles[i].getId(); 163 } 164 165 return buildSituation(roleIds, userId, machineIp); 166 } 167 168 175 public Situation buildSituation(String [] roleIds, String userId, String machineIp) { 176 return new CMSSituation(roleIds, userId, machineIp); 177 } 178 179 186 public static void initHistory(Document document, String workflowId, Situation situation) throws WorkflowException { 187 new CMSHistory(document).initialize(workflowId, situation); 188 } 189 190 195 public static History getHistory(Document document) { 196 return new CMSHistory(document); 197 } 198 199 206 public static void initHistory(Document sourceDocument, Document destinationDocument, Situation situation) 207 throws WorkflowException { 208 CMSHistory history = new CMSHistory(sourceDocument); 209 history.initialize(destinationDocument, situation); 210 } 211 212 } 213 | Popular Tags |