1 17 18 19 20 package org.apache.lenya.cms.publication.task; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.List ; 27 28 import org.apache.avalon.excalibur.io.FileUtil; 29 import org.apache.avalon.framework.parameters.ParameterException; 30 import org.apache.lenya.cms.publication.Document; 31 import org.apache.lenya.cms.publication.Publication; 32 import org.apache.lenya.cms.publication.PublicationFactory; 33 import org.apache.lenya.cms.publication.ResourcesManager; 34 import org.apache.lenya.cms.rc.FileReservedCheckInException; 35 import org.apache.lenya.cms.rc.RCEnvironment; 36 import org.apache.lenya.cms.rc.RevisionControlException; 37 import org.apache.lenya.cms.rc.RevisionController; 38 import org.apache.lenya.cms.task.AbstractTask; 39 import org.apache.lenya.cms.task.ExecutionException; 40 import org.apache.lenya.cms.task.Task; 41 import org.apache.lenya.cms.workflow.WorkflowFactory; 42 import org.apache.lenya.workflow.Event; 43 import org.apache.lenya.workflow.Situation; 44 import org.apache.lenya.workflow.SynchronizedWorkflowInstances; 45 import org.apache.lenya.workflow.WorkflowException; 46 import org.apache.log4j.Category; 47 48 51 public abstract class PublicationTask extends AbstractTask { 52 53 private static final Category log = Category.getInstance(PublicationTask.class); 54 55 private Publication publication; 56 57 62 protected Publication getPublication() throws ExecutionException { 63 if (publication == null) { 64 try { 65 String publicationId = getParameters().getParameter(Task.PARAMETER_PUBLICATION_ID); 66 String servletContextPath = 67 getParameters().getParameter(Task.PARAMETER_SERVLET_CONTEXT); 68 publication = PublicationFactory.getPublication(publicationId, servletContextPath); 69 } catch (Exception e) { 70 throw new ExecutionException(e); 71 } 72 } 73 return publication; 74 } 75 76 84 protected void copyResources(Document sourceDocument, Document destinationDocument) 85 throws ExecutionException { 86 87 if (log.isDebugEnabled()) { 88 log.debug("Copying resources"); 89 } 90 91 ResourcesManager sourceManager = new ResourcesManager(sourceDocument); 92 ResourcesManager destinationManager = new ResourcesManager(destinationDocument); 93 94 List resourcesList = new ArrayList (Arrays.asList(sourceManager.getResources())); 95 resourcesList.addAll(Arrays.asList(sourceManager.getMetaFiles())); 96 File [] resources = (File []) resourcesList.toArray(new File [resourcesList.size()]); 97 File destinationDirectory = destinationManager.getPath(); 98 99 for (int i = 0; i < resources.length; i++) { 100 File destinationResource = new File (destinationDirectory, resources[i].getName()); 101 102 if (log.isDebugEnabled()) { 103 log.debug( 104 "Copy file [" 105 + resources[i].getAbsolutePath() 106 + "] to [" 107 + destinationResource.getAbsolutePath() 108 + "]"); 109 } 110 try { 111 FileUtil.copyFile(resources[i], destinationResource); 112 } catch (IOException e) { 113 throw new ExecutionException(e); 114 } 115 } 116 } 117 118 public static final String PARAMETER_WORKFLOW_EVENT = "workflow-event"; 119 public static final String PARAMETER_USER_ID = "user-id"; 120 public static final String PARAMETER_IP_ADDRESS = "ip-address"; 121 public static final String PARAMETER_ROLE_IDS = "role-ids"; 122 public static final String ROLE_SEPARATOR_REGEXP = ","; 123 124 130 protected boolean canWorkflowFire(Document document) throws ExecutionException { 131 132 if (log.isDebugEnabled()) { 133 log.debug("Checking workflow of document [" + document + "]."); 134 } 135 136 boolean canFire = true; 137 138 WorkflowFactory factory = WorkflowFactory.newInstance(); 139 if (factory.hasWorkflow(document)) { 140 try { 141 Situation situation = getSituation(); 142 143 SynchronizedWorkflowInstances instance; 144 try { 145 instance = factory.buildSynchronizedInstance(document); 146 } catch (WorkflowException e) { 147 throw new ExecutionException(e); 148 } 149 Event event = getExecutableEvent(instance, situation); 150 151 if (event == null) { 152 canFire = false; 153 } 154 155 } 156 catch (Exception e) { 157 throw new ExecutionException(e); 158 } 159 } 160 return canFire; 161 } 162 163 168 protected Situation getSituation() throws ParameterException { 169 WorkflowFactory workflowFactory = WorkflowFactory.newInstance(); 170 String userId = getParameters().getParameter(PARAMETER_USER_ID); 171 String machineIp = getParameters().getParameter(PARAMETER_IP_ADDRESS); 172 Situation situation = workflowFactory.buildSituation(getRoleIDs(), userId, machineIp); 173 return situation; 174 } 175 176 182 protected void triggerWorkflow(Document document) throws ExecutionException { 183 184 if (log.isDebugEnabled()) { 185 log.debug("Trying to execute workflow on document [" + document.getId() + "]."); 186 } 187 188 WorkflowFactory factory = WorkflowFactory.newInstance(); 189 if (factory.hasWorkflow(document)) { 190 try { 191 String userId = getParameters().getParameter(PARAMETER_USER_ID); 192 String machineIp = getParameters().getParameter(PARAMETER_IP_ADDRESS); 193 194 SynchronizedWorkflowInstances instance; 195 try { 196 instance = factory.buildSynchronizedInstance(document); 197 } catch (WorkflowException e) { 198 throw new ExecutionException(e); 199 } 200 Situation situation = factory.buildSituation(getRoleIDs(), userId, machineIp); 201 202 Event event = getExecutableEvent(instance, situation); 203 204 assert event != null; 205 206 if (log.isDebugEnabled()) { 207 log.debug("Invoking event [" + event.getName() + "]"); 208 } 209 instance.invoke(situation, event); 210 if (log.isDebugEnabled()) { 211 log.debug("Invoking transition completed."); 212 } 213 } catch (Exception e) { 214 throw new ExecutionException(e); 215 } 216 } else { 217 if (log.isDebugEnabled()) { 218 log.debug("No workflow associated with document."); 219 } 220 } 221 222 } 223 224 232 protected Event getExecutableEvent(SynchronizedWorkflowInstances instance, Situation situation) 233 throws WorkflowException, ParameterException { 234 235 String workflowEvent = getEventName(); 236 237 Event event = null; 238 Event[] events = instance.getExecutableEvents(situation); 239 240 if (log.isDebugEnabled()) { 241 log.debug("Workflow event name: [" + workflowEvent + "]"); 242 log.debug("Resolved executable events."); 243 } 244 245 for (int i = 0; i < events.length; i++) { 246 if (events[i].getName().equals(workflowEvent)) { 247 event = events[i]; 248 } 249 } 250 251 if (log.isDebugEnabled()) { 252 log.debug("Executable event found: [" + event + "]"); 253 } 254 255 if (event == null) { 256 log.error("Event [" + workflowEvent + "] cannot be invoked!"); 257 } 258 259 return event; 260 } 261 262 267 protected String getEventName() throws ParameterException { 268 String workflowEvent = getParameters().getParameter(PARAMETER_WORKFLOW_EVENT); 269 return workflowEvent; 270 } 271 272 276 protected String [] getRoleIDs() throws ParameterException { 277 String rolesString = getParameters().getParameter(PARAMETER_ROLE_IDS); 278 String [] roles = rolesString.split(ROLE_SEPARATOR_REGEXP); 279 return roles; 280 } 281 282 private RevisionController revisionController; 283 284 290 protected RevisionController getRevisionController() throws RevisionControlException, ExecutionException { 291 if (revisionController == null) { 292 File publicationDir = publication.getDirectory(); 293 RCEnvironment rcEnvironment; 294 try { 295 File servletContext = publication.getServletContext(); 296 rcEnvironment = RCEnvironment.getInstance(servletContext.getCanonicalPath()); 297 } catch (IOException e) { 298 throw new RevisionControlException(e); 299 } 300 301 File rcmlDirectory = new File (publicationDir, rcEnvironment.getRCMLDirectory()); 302 File backupDirectory = new File (publicationDir, rcEnvironment.getBackupDirectory()); 303 304 revisionController = 305 new RevisionController( 306 rcmlDirectory.getAbsolutePath(), 307 backupDirectory.getAbsolutePath(), 308 publicationDir.getAbsolutePath()); 309 } 310 return revisionController; 311 } 312 313 319 protected String getRevisionFilePath(Document document) throws IOException { 320 String path = document.getFile().getCanonicalPath() 321 .substring(publication.getDirectory().getCanonicalPath().length()); 322 return path; 323 } 324 325 326 334 protected boolean canCheckOut(Document document) throws ExecutionException, IOException , Exception { 335 String userId = getParameters().getParameter(PARAMETER_USER_ID); 336 String path = getRevisionFilePath(document); 337 return getRevisionController().canCheckOut(path, userId); 338 } 339 340 346 protected void reservedCheckIn(Document document, boolean backup) 347 throws FileReservedCheckInException, Exception { 348 String userId = getParameters().getParameter(PARAMETER_USER_ID); 349 String path = getRevisionFilePath(document); 350 getRevisionController().reservedCheckIn(path, userId, backup); 351 } 352 353 359 protected void reservedCheckOut(Document document) throws IOException , Exception { 360 String userId = getParameters().getParameter(PARAMETER_USER_ID); 361 String path = getRevisionFilePath(document); 362 getRevisionController().reservedCheckOut(path, userId); 363 } 364 } 365 | Popular Tags |