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:
019: package org.apache.lenya.cms.cocoon.components.modules.input;
020:
021: import java.text.DateFormat;
022: import java.text.SimpleDateFormat;
023: import java.util.Arrays;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import org.apache.avalon.framework.configuration.Configuration;
028: import org.apache.avalon.framework.configuration.ConfigurationException;
029: import org.apache.cocoon.environment.ObjectModelHelper;
030: import org.apache.lenya.cms.publication.Document;
031: import org.apache.lenya.cms.publication.PageEnvelope;
032: import org.apache.lenya.cms.repository.RepositoryUtil;
033: import org.apache.lenya.cms.repository.Session;
034: import org.apache.lenya.cms.workflow.WorkflowUtil;
035: import org.apache.lenya.workflow.Version;
036: import org.apache.lenya.workflow.Workflow;
037: import org.apache.lenya.workflow.WorkflowManager;
038: import org.apache.lenya.workflow.Workflowable;
039:
040: /**
041: * Module for workflow access.
042: *
043: * @version $Id: WorkflowModule.java 565823 2007-08-14 16:25:29Z rfrovarp $
044: */
045: public class WorkflowModule extends AbstractPageEnvelopeModule {
046:
047: /**
048: * <code>STATE</code> The state
049: */
050: public static final String STATE = "state";
051: /**
052: * <code>VARIABLE_PREFIX</code> The variable prefix
053: */
054: public static final String VARIABLE_PREFIX = "variable.";
055:
056: /**
057: * The prefix to get the last user who invoked a certain event.
058: */
059: public static final String LAST_USER_PREFIX = "lastUser.";
060:
061: /**
062: * The prefix to get the last date at which a certain event was invoked.
063: * @see #DATE_FORMAT
064: */
065: public static final String LAST_DATE_PREFIX = "lastDate.";
066:
067: private final DateFormat DATE_FORMAT = new SimpleDateFormat(
068: "yyyy-MM-dd HH:mm:ss");
069: private final Object lock = new Object();
070: static final String[] PARAMETER_NAMES = { STATE };
071:
072: /**
073: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
074: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
075: */
076: public Object getAttribute(String name, Configuration modeConf,
077: Map objectModel) throws ConfigurationException {
078:
079: Object value = null;
080: WorkflowManager wfManager = null;
081:
082: try {
083: PageEnvelope envelope = getEnvelope(objectModel, name);
084: Document document = envelope.getDocument();
085: if (document != null && document.exists()) {
086: wfManager = (WorkflowManager) this .manager
087: .lookup(WorkflowManager.ROLE);
088: Session session = RepositoryUtil.getSession(
089: this .manager, ObjectModelHelper
090: .getRequest(objectModel));
091: Workflowable workflowable = WorkflowUtil
092: .getWorkflowable(this .manager, session,
093: getLogger(), document);
094: if (wfManager.hasWorkflow(workflowable)) {
095:
096: Version latestVersion = workflowable
097: .getLatestVersion();
098:
099: if (name.equals(STATE)) {
100: if (latestVersion == null) {
101: Workflow workflow = wfManager
102: .getWorkflowSchema(workflowable);
103: value = workflow.getInitialState();
104: } else {
105: value = latestVersion.getState();
106: }
107: } else if (name.startsWith(VARIABLE_PREFIX)) {
108: String variableName = name
109: .substring(VARIABLE_PREFIX.length());
110: Workflow workflow = wfManager
111: .getWorkflowSchema(workflowable);
112: String[] variableNames = workflow
113: .getVariableNames();
114: if (Arrays.asList(variableNames).contains(
115: variableName)) {
116: if (latestVersion == null) {
117: value = Boolean.valueOf(workflow
118: .getInitialValue(variableName));
119: } else {
120: value = Boolean.valueOf(latestVersion
121: .getValue(variableName));
122: }
123: }
124: } else if (name.startsWith(LAST_USER_PREFIX)) {
125: String event = name.substring(LAST_USER_PREFIX
126: .length());
127: Version latestEventVersion = getLatestVersion(
128: workflowable, event);
129: if (latestEventVersion != null) {
130: value = latestEventVersion.getUserId();
131: }
132: } else if (name.startsWith(LAST_DATE_PREFIX)) {
133: String event = name.substring(LAST_DATE_PREFIX
134: .length());
135: Version latestEventVersion = getLatestVersion(
136: workflowable, event);
137: if (latestEventVersion != null) {
138: synchronized (lock) {
139: value = this .DATE_FORMAT
140: .format(latestEventVersion
141: .getDate());
142: }
143: }
144: } else {
145: throw new ConfigurationException(
146: "The attribute [" + name
147: + "] is not supported!");
148: }
149: }
150: }
151: } catch (ConfigurationException e) {
152: throw e;
153: } catch (Exception e) {
154: throw new ConfigurationException(
155: "Resolving attribute failed: ", e);
156: } finally {
157: if (wfManager != null) {
158: this .manager.release(wfManager);
159: }
160: }
161: return value;
162: }
163:
164: protected Version getLatestVersion(Workflowable workflowable,
165: String event) {
166: Version latestEventVersion = null;
167: Version versions[] = workflowable.getVersions();
168: int i = versions.length - 1;
169: while (i > -1 && !versions[i].getEvent().equals(event)) {
170: i--;
171: }
172: if (i > -1) {
173: latestEventVersion = versions[i];
174: }
175: return latestEventVersion;
176: }
177:
178: /**
179: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
180: * java.util.Map)
181: */
182: public Iterator getAttributeNames(Configuration modeConf,
183: Map objectModel) throws ConfigurationException {
184: return Arrays.asList(PARAMETER_NAMES).iterator();
185: }
186:
187: /**
188: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
189: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
190: */
191: public Object[] getAttributeValues(String name,
192: Configuration modeConf, Map objectModel)
193: throws ConfigurationException {
194: Object[] objects = { getAttribute(name, modeConf, objectModel) };
195:
196: return objects;
197: }
198:
199: }
|