01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.lenya.cms.workflow.usecases;
19:
20: import org.apache.avalon.framework.logger.Logger;
21: import org.apache.avalon.framework.service.ServiceManager;
22: import org.apache.lenya.cms.metadata.MetaDataException;
23: import org.apache.lenya.cms.metadata.dublincore.DublinCoreHelper;
24: import org.apache.lenya.cms.publication.Document;
25: import org.apache.lenya.cms.usecase.AbstractUsecase;
26: import org.apache.lenya.cms.workflow.WorkflowUtil;
27: import org.apache.lenya.workflow.WorkflowException;
28:
29: /**
30: * Helper class for workflow related usecases.
31: */
32: public class UsecaseWorkflowHelper {
33:
34: /**
35: * The error message that an event can not be invoked on a document. It
36: * takes two parameters: the event name and the document title.
37: */
38: protected static final String ERROR_CANNOT_INVOKE_EVENT = "error-workflow-document";
39:
40: /**
41: * Adds an error message to a usecase that an event cannot be invoked on a
42: * document.
43: * @param usecase The usecase.
44: * @param event The event.
45: * @param doc The document.
46: */
47: protected static final void addWorkflowError(
48: AbstractUsecase usecase, String event, Document doc) {
49: try {
50: String title = DublinCoreHelper.getTitle(doc, true);
51: if (title == null) {
52: title = "";
53: }
54: usecase.addErrorMessage(ERROR_CANNOT_INVOKE_EVENT,
55: new String[] { event, title });
56: } catch (MetaDataException e) {
57: throw new RuntimeException(e);
58: }
59: }
60:
61: /**
62: * Adds an error message if the event can not be invoked.
63: * @param manager The service manager.
64: * @param usecase The usecase.
65: * @param event The event.
66: * @param doc The document.
67: * @param logger The logger.
68: */
69: public static final void checkWorkflow(ServiceManager manager,
70: AbstractUsecase usecase, String event, Document doc,
71: Logger logger) {
72: try {
73: if (!WorkflowUtil.canInvoke(manager, usecase.getSession(),
74: logger, doc, event)) {
75: UsecaseWorkflowHelper.addWorkflowError(usecase, event,
76: doc);
77: }
78: } catch (WorkflowException e) {
79: throw new RuntimeException(e);
80: }
81:
82: }
83:
84: }
|