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.usecases;
019:
020: import java.util.Arrays;
021: import java.util.SortedSet;
022: import java.util.TreeSet;
023:
024: import org.apache.avalon.framework.container.ContainerUtil;
025: import org.apache.avalon.framework.logger.AbstractLogEnabled;
026: import org.apache.avalon.framework.logger.Logger;
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.lenya.cms.publication.Document;
029: import org.apache.lenya.cms.publication.DocumentException;
030: import org.apache.lenya.cms.repository.Session;
031: import org.apache.lenya.cms.workflow.WorkflowUtil;
032: import org.apache.lenya.workflow.Workflow;
033: import org.apache.lenya.workflow.WorkflowException;
034: import org.apache.lenya.workflow.Workflowable;
035:
036: /**
037: * Wrap a workflowable for easy evaluation in JX template.
038: */
039: public class WorkflowableWrapper extends AbstractLogEnabled {
040:
041: private MultiWorkflow usecase;
042: private Workflowable workflowable;
043: private ServiceManager manager;
044: private Document document;
045: private Session session;
046:
047: /**
048: * Ctor.
049: * @param usecase The usecase.
050: * @param manager The service manager.
051: * @param session The session.
052: * @param document The document to wrap.
053: * @param logger The logger.
054: */
055: public WorkflowableWrapper(MultiWorkflow usecase,
056: ServiceManager manager, Session session, Document document,
057: Logger logger) {
058: this .usecase = usecase;
059: this .document = document;
060: this .manager = manager;
061: this .session = session;
062: ContainerUtil.enableLogging(this , logger);
063: }
064:
065: protected Workflowable getWorkflowable() {
066: if (this .workflowable == null) {
067: this .workflowable = WorkflowUtil.getWorkflowable(
068: this .manager, this .session, getLogger(),
069: this .document);
070: }
071: return this .workflowable;
072: }
073:
074: /**
075: * @return The state of the latest version.
076: * @throws WorkflowException
077: */
078: public String getState() throws WorkflowException {
079: String state;
080: if (getWorkflowable().getVersions().length > 0) {
081: state = getWorkflowable().getLatestVersion().getState();
082: } else {
083: Workflow workflow = getWorkflowSchema();
084: state = workflow.getInitialState();
085: }
086: return state;
087: }
088:
089: /**
090: * @return All states supported by the workflow schema.
091: * @throws WorkflowException
092: */
093: public String[] getStates() throws WorkflowException {
094: return getWorkflowSchema().getStates();
095: }
096:
097: protected Workflow getWorkflowSchema() throws WorkflowException {
098: Workflow workflow = WorkflowUtil.getWorkflowSchema(
099: this .manager, this .session, getLogger(), this .document);
100: return workflow;
101: }
102:
103: /**
104: * @return The path of the document.
105: * @throws DocumentException If the document is not referenced in the site
106: * structure.
107: */
108: public String getPath() throws DocumentException {
109: return this .document.getPath();
110: }
111:
112: /**
113: * @return The language of the document.
114: */
115: public String getLanguage() {
116: return this .document.getLanguage();
117: }
118:
119: /**
120: * @return The web application URL of the document.
121: */
122: public String getUrl() {
123: return this .document.getCanonicalWebappURL();
124: }
125:
126: /**
127: * @return All executable events in alphabetical order.
128: * @throws WorkflowException if an error occurs.
129: */
130: public String[] getUsecases() throws WorkflowException {
131: SortedSet usecases = new TreeSet();
132: String[] events = getWorkflowSchema().getEvents();
133: for (int i = 0; i < events.length; i++) {
134: if (WorkflowUtil.canInvoke(this .manager, this .session,
135: getLogger(), this .document, events[i])) {
136: String[] eventUsecases = this .usecase
137: .getUsecases(events[i]);
138: usecases.addAll(Arrays.asList(eventUsecases));
139: }
140: }
141:
142: return (String[]) usecases.toArray(new String[usecases.size()]);
143: }
144:
145: /**
146: * @return The languages of the document in alphabetical order.
147: * @throws DocumentException
148: */
149: public String[] getLanguages() throws DocumentException {
150: String[] languages = this.document.getLanguages();
151: Arrays.sort(languages);
152: return languages;
153: }
154: }
|