KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > task > PublicationTask


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: PublicationTask.java 153090 2005-02-09 17:21:59Z edith $ */
19
20 package org.apache.lenya.cms.publication.task;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
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 /**
49  * Abstract super class for publication-based tasks.
50  */

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     /**
58      * Returns the publication used by this task.
59      * @return A publication.
60      * @throws ExecutionException when an error occurs.
61      */

62     protected Publication getPublication() throws ExecutionException {
63         if (publication == null) {
64             try {
65                 String JavaDoc publicationId = getParameters().getParameter(Task.PARAMETER_PUBLICATION_ID);
66                 String JavaDoc servletContextPath =
67                     getParameters().getParameter(Task.PARAMETER_SERVLET_CONTEXT);
68                 publication = PublicationFactory.getPublication(publicationId, servletContextPath);
69             } catch (Exception JavaDoc e) {
70                 throw new ExecutionException(e);
71             }
72         }
73         return publication;
74     }
75
76     /**
77      * Copies the resources of a document to another document.
78      * @param sourceDocument The source document.
79      * @param destinationDocument The destination document.
80      * @throws PublicationException when something went wrong.
81      * @throws ExecutionException when something went wrong.
82      * @throws IOException when something went wrong.
83      */

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 JavaDoc resourcesList = new ArrayList JavaDoc(Arrays.asList(sourceManager.getResources()));
95         resourcesList.addAll(Arrays.asList(sourceManager.getMetaFiles()));
96         File JavaDoc[] resources = (File JavaDoc[]) resourcesList.toArray(new File JavaDoc[resourcesList.size()]);
97         File JavaDoc destinationDirectory = destinationManager.getPath();
98
99         for (int i = 0; i < resources.length; i++) {
100             File JavaDoc destinationResource = new File JavaDoc(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 JavaDoc e) {
113                 throw new ExecutionException(e);
114             }
115         }
116     }
117
118     public static final String JavaDoc PARAMETER_WORKFLOW_EVENT = "workflow-event";
119     public static final String JavaDoc PARAMETER_USER_ID = "user-id";
120     public static final String JavaDoc PARAMETER_IP_ADDRESS = "ip-address";
121     public static final String JavaDoc PARAMETER_ROLE_IDS = "role-ids";
122     public static final String JavaDoc ROLE_SEPARATOR_REGEXP = ",";
123
124     /**
125      * Checks if the workflow event can be invoked on a document.
126      * @param document The document.
127      * @return A boolean value.
128      * @throws ExecutionException when something went wrong.
129      */

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 JavaDoc e) {
157                 throw new ExecutionException(e);
158             }
159         }
160         return canFire;
161     }
162
163     /**
164      * Returns the workflow situation.
165      * @return A situation.
166      * @throws ParameterException when something went wrong.
167      */

168     protected Situation getSituation() throws ParameterException {
169         WorkflowFactory workflowFactory = WorkflowFactory.newInstance();
170         String JavaDoc userId = getParameters().getParameter(PARAMETER_USER_ID);
171         String JavaDoc machineIp = getParameters().getParameter(PARAMETER_IP_ADDRESS);
172         Situation situation = workflowFactory.buildSituation(getRoleIDs(), userId, machineIp);
173         return situation;
174     }
175
176     /**
177      * Invokes the workflow on a document.
178      * @param document The document.
179      * @throws ParameterException when something went wrong.
180      * @throws WorkflowException when something went wrong.
181      */

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 JavaDoc userId = getParameters().getParameter(PARAMETER_USER_ID);
192                 String JavaDoc 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 JavaDoc 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     /**
225      * Returns the executable event for the provided {@link #PARAMETER_WORKFLOW_EVENT} parameter.
226      * @param instance The workflow instance.
227      * @param situation The situation.
228      * @return An event.
229      * @throws WorkflowException when something went wrong.
230      * @throws ParameterException when the {@link #PARAMETER_WORKFLOW_EVENT} parameter could not be resolved.
231      */

232     protected Event getExecutableEvent(SynchronizedWorkflowInstances instance, Situation situation)
233         throws WorkflowException, ParameterException {
234
235         String JavaDoc 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     /**
263      * Returns the workflow event name.
264      * @return A string.
265      * @throws ParameterException when the parameter does not exist.
266      */

267     protected String JavaDoc getEventName() throws ParameterException {
268         String JavaDoc workflowEvent = getParameters().getParameter(PARAMETER_WORKFLOW_EVENT);
269         return workflowEvent;
270     }
271
272     /**
273      * Returns the role IDs.
274      * @return An array of strings.
275      */

276     protected String JavaDoc[] getRoleIDs() throws ParameterException {
277         String JavaDoc rolesString = getParameters().getParameter(PARAMETER_ROLE_IDS);
278         String JavaDoc[] roles = rolesString.split(ROLE_SEPARATOR_REGEXP);
279         return roles;
280     }
281
282     private RevisionController revisionController;
283
284     /**
285      * Returns the revision controller.
286      * @return A revision controller.
287      * @throws ExecutionException
288      * @throws IOException when something went wrong.
289      */

290     protected RevisionController getRevisionController() throws RevisionControlException, ExecutionException {
291         if (revisionController == null) {
292             File JavaDoc publicationDir = publication.getDirectory();
293             RCEnvironment rcEnvironment;
294             try {
295                 File JavaDoc servletContext = publication.getServletContext();
296                 rcEnvironment = RCEnvironment.getInstance(servletContext.getCanonicalPath());
297             } catch (IOException JavaDoc e) {
298                 throw new RevisionControlException(e);
299             }
300
301             File JavaDoc rcmlDirectory = new File JavaDoc(publicationDir, rcEnvironment.getRCMLDirectory());
302             File JavaDoc backupDirectory = new File JavaDoc(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     /**
314      * Returns the revision file path of a document.
315      * @param document The document.
316      * @return A string.
317      * @throws IOException when something went wrong.
318      */

319     protected String JavaDoc getRevisionFilePath(Document document) throws IOException JavaDoc {
320         String JavaDoc path = document.getFile().getCanonicalPath()
321         .substring(publication.getDirectory().getCanonicalPath().length());
322         return path;
323     }
324
325
326     /**
327      * Checks if the document can be checked out.
328      * @param document The document
329      * @return A boolean value.
330      * @throws ExecutionException when something went wrong.
331      * @throws IOException when something went wrong.
332      * @throws Exception when something went wrong.
333      */

334     protected boolean canCheckOut(Document document) throws ExecutionException, IOException JavaDoc, Exception JavaDoc{
335         String JavaDoc userId = getParameters().getParameter(PARAMETER_USER_ID);
336         String JavaDoc path = getRevisionFilePath(document);
337         return getRevisionController().canCheckOut(path, userId);
338     }
339     
340     /**Checks in a document and creates a revision, if backup is true.
341      * @param document
342      * @param backup A boolean Value
343      * @throws FileReservedCheckInException when something went wrong.
344      * @throws Exception when something went wrong.
345      */

346     protected void reservedCheckIn(Document document, boolean backup)
347             throws FileReservedCheckInException, Exception JavaDoc {
348         String JavaDoc userId = getParameters().getParameter(PARAMETER_USER_ID);
349         String JavaDoc path = getRevisionFilePath(document);
350         getRevisionController().reservedCheckIn(path, userId, backup);
351     }
352
353     /**
354      * Checks out a document.
355      * @param document The document
356      * @throws IOException when something went wrong.
357      * @throws Exception when something went wrong.
358      */

359     protected void reservedCheckOut(Document document) throws IOException JavaDoc, Exception JavaDoc {
360         String JavaDoc userId = getParameters().getParameter(PARAMETER_USER_ID);
361         String JavaDoc path = getRevisionFilePath(document);
362         getRevisionController().reservedCheckOut(path, userId);
363     }
364 }
365
Popular Tags