001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.workflow;
019:
020: import org.apache.avalon.framework.logger.Logger;
021: import org.apache.avalon.framework.service.ServiceException;
022: import org.apache.avalon.framework.service.ServiceManager;
023: import org.apache.lenya.cms.publication.Document;
024: import org.apache.lenya.cms.publication.util.DocumentSet;
025: import org.apache.lenya.cms.repository.Session;
026: import org.apache.lenya.workflow.Workflow;
027: import org.apache.lenya.workflow.WorkflowException;
028: import org.apache.lenya.workflow.WorkflowManager;
029: import org.apache.lenya.workflow.Workflowable;
030:
031: /**
032: * Utility class for workflow tasks.
033: *
034: * @version $Id:$
035: */
036: public class WorkflowUtil {
037:
038: /**
039: * Invokes a workflow event on a document. This is the same as
040: * <code>invoke(Document, String, true)</code>.
041: * @param manager The service manager.
042: * @param session The repository session.
043: * @param logger The logger.
044: * @param document The document.
045: * @param event The name of the event.
046: * @throws WorkflowException if the event could not be invoked in the current situation.
047: */
048: public static void invoke(ServiceManager manager, Session session,
049: Logger logger, Document document, String event)
050: throws WorkflowException {
051: WorkflowManager wfManager = null;
052: try {
053: wfManager = (WorkflowManager) manager
054: .lookup(WorkflowManager.ROLE);
055: Workflowable workflowable = getWorkflowable(manager,
056: session, logger, document);
057: wfManager.invoke(workflowable, event);
058: } catch (ServiceException e) {
059: throw new WorkflowException(e);
060: } finally {
061: if (wfManager != null) {
062: manager.release(wfManager);
063: }
064: }
065:
066: }
067:
068: /**
069: * Invokes a workflow event on a document.
070: * @param manager The service manager.
071: * @param session The repository session.
072: * @param logger The logger.
073: * @param document The document.
074: * @param event The name of the event.
075: * @param force If this is set to <code>true</code>, the execution is forced, which means an
076: * exception is thrown if the workflowable in the set does not support the event. If
077: * set to <code>false</code>, non-supporting documents are ignored.
078: * @throws WorkflowException if the event could not be invoked in the current situation.
079: */
080: public static void invoke(ServiceManager manager, Session session,
081: Logger logger, Document document, String event,
082: boolean force) throws WorkflowException {
083: WorkflowManager wfManager = null;
084: try {
085: wfManager = (WorkflowManager) manager
086: .lookup(WorkflowManager.ROLE);
087: Workflowable workflowable = getWorkflowable(manager,
088: session, logger, document);
089: wfManager.invoke(workflowable, event, force);
090: } catch (ServiceException e) {
091: throw new WorkflowException(e);
092: } finally {
093: if (wfManager != null) {
094: manager.release(wfManager);
095: }
096: }
097:
098: }
099:
100: /**
101: * Invokes a workflow event on a document set.
102: * @param manager The service manager.
103: * @param session The repository session.
104: * @param logger The logger.
105: * @param documentSet The document.
106: * @param event The event.
107: * @param force If this is set to <code>true</code>, the execution is forced, which means an
108: * exception is thrown if a document in the set does not support the event. If set to
109: * <code>false</code>, non-supporting documents are ignored.
110: * @throws WorkflowException if <code>force</code> is set to <code>true</code> and a
111: * document does not support the workflow event.
112: */
113: public static void invoke(ServiceManager manager, Session session,
114: Logger logger, DocumentSet documentSet, String event,
115: boolean force) throws WorkflowException {
116: WorkflowManager wfManager = null;
117: try {
118: wfManager = (WorkflowManager) manager
119: .lookup(WorkflowManager.ROLE);
120:
121: Document[] documents = documentSet.getDocuments();
122: for (int i = 0; i < documents.length; i++) {
123: Workflowable workflowable = new DocumentWorkflowable(
124: manager, session, documents[i], logger);
125: wfManager.invoke(workflowable, event, force);
126: }
127:
128: } catch (ServiceException e) {
129: throw new WorkflowException(e);
130: } finally {
131: if (wfManager != null) {
132: manager.release(wfManager);
133: }
134: }
135:
136: }
137:
138: /**
139: * Checks if an event can be invoked on a document.
140: * @param manager The service manager.
141: * @param session The repository session.
142: * @param logger The logger.
143: * @param document The document.
144: * @param event The event.
145: * @return A boolean value.
146: * @throws WorkflowException
147: */
148: public static boolean canInvoke(ServiceManager manager,
149: Session session, Logger logger, Document document,
150: String event) throws WorkflowException {
151: WorkflowManager wfManager = null;
152: try {
153: wfManager = (WorkflowManager) manager
154: .lookup(WorkflowManager.ROLE);
155: Workflowable workflowable = new DocumentWorkflowable(
156: manager, session, document, logger);
157: return wfManager.canInvoke(workflowable, event);
158: } catch (ServiceException e) {
159: throw new WorkflowException(e);
160: } finally {
161: if (wfManager != null) {
162: manager.release(wfManager);
163: }
164: }
165:
166: }
167:
168: /**
169: * Checks if an event can be invoked on all documents in a set.
170: * @param manager The service manager.
171: * @param session The repository session.
172: * @param logger The logger.
173: * @param documents The documents.
174: * @param event The event.
175: * @return if an error occurs.
176: * @throws WorkflowException
177: */
178: public static boolean canInvoke(ServiceManager manager,
179: Session session, Logger logger, DocumentSet documents,
180: String event) throws WorkflowException {
181: WorkflowManager wfManager = null;
182: try {
183: wfManager = (WorkflowManager) manager
184: .lookup(WorkflowManager.ROLE);
185:
186: boolean canInvoke = true;
187: Document[] documentArray = documents.getDocuments();
188: for (int i = 0; i < documentArray.length; i++) {
189: Workflowable workflowable = new DocumentWorkflowable(
190: manager, session, documentArray[i], logger);
191: canInvoke = canInvoke
192: && wfManager.canInvoke(workflowable, event);
193: }
194: return canInvoke;
195:
196: } catch (ServiceException e) {
197: throw new WorkflowException(e);
198: } finally {
199: if (wfManager != null) {
200: manager.release(wfManager);
201: }
202: }
203: }
204:
205: /**
206: * Returns if a document has a workflow.
207: * @param manager The service manager.
208: * @param session The repository session.
209: * @param logger The logger.
210: * @param document The document.
211: * @return A boolean value.
212: * @throws WorkflowException if an error occurs.
213: */
214: public static boolean hasWorkflow(ServiceManager manager,
215: Session session, Logger logger, Document document)
216: throws WorkflowException {
217: WorkflowManager wfManager = null;
218: try {
219: wfManager = (WorkflowManager) manager
220: .lookup(WorkflowManager.ROLE);
221: Workflowable workflowable = new DocumentWorkflowable(
222: manager, session, document, logger);
223: return wfManager.hasWorkflow(workflowable);
224: } catch (ServiceException e) {
225: throw new WorkflowException(e);
226: } finally {
227: if (wfManager != null) {
228: manager.release(wfManager);
229: }
230: }
231: }
232:
233: /**
234: * Returns the workflow schema of a document.
235: * @param manager The service manager.
236: * @param session The repository session.
237: * @param logger The logger.
238: * @param document The document.
239: * @return A workflow schema.
240: * @throws WorkflowException if an error occurs.
241: */
242: public static Workflow getWorkflowSchema(ServiceManager manager,
243: Session session, Logger logger, Document document)
244: throws WorkflowException {
245: WorkflowManager wfManager = null;
246: try {
247: wfManager = (WorkflowManager) manager
248: .lookup(WorkflowManager.ROLE);
249: Workflowable workflowable = getWorkflowable(manager,
250: session, logger, document);
251: if (wfManager.hasWorkflow(workflowable)) {
252: return wfManager.getWorkflowSchema(workflowable);
253: } else {
254: throw new WorkflowException("The document [" + document
255: + "] has no workflow!");
256: }
257: } catch (ServiceException e) {
258: throw new WorkflowException(e);
259: } finally {
260: if (wfManager != null) {
261: manager.release(wfManager);
262: }
263: }
264: }
265:
266: /**
267: * Returns a workflowable for a document.
268: * @param manager The service manager.
269: * @param session The session.
270: * @param logger The logger.
271: * @param document The document.
272: * @return A workflowable.
273: */
274: public static Workflowable getWorkflowable(ServiceManager manager,
275: Session session, Logger logger, Document document) {
276: Workflowable workflowable = new DocumentWorkflowable(manager,
277: session, document, logger);
278: return workflowable;
279: }
280:
281: }
|