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 distributed under the License
012: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
013: * or implied. See the License for the specific language governing permissions and limitations under
014: * the License.
015: *
016: */
017:
018: /* $Id: WorkflowInvoker.java 473861 2006-11-12 03:51:14Z gregor $ */
019:
020: package org.apache.lenya.cms.task;
021:
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.logger.ConsoleLogger;
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.lenya.ac.Identity;
027: import org.apache.lenya.ac.Machine;
028: import org.apache.lenya.ac.Role;
029: import org.apache.lenya.ac.User;
030: import org.apache.lenya.cms.publication.Document;
031: import org.apache.lenya.cms.publication.DocumentFactory;
032: import org.apache.lenya.cms.publication.DocumentUtil;
033: import org.apache.lenya.cms.publication.Publication;
034: import org.apache.lenya.cms.repository.RepositoryUtil;
035: import org.apache.lenya.cms.repository.Session;
036: import org.apache.lenya.cms.workflow.WorkflowUtil;
037: import org.apache.lenya.util.NamespaceMap;
038: import org.apache.log4j.Logger;
039:
040: /**
041: * The workflow invoker
042: * @deprecated Use the usecase framework instead.
043: */
044: public class WorkflowInvoker extends ParameterWrapper {
045:
046: private static Logger log = Logger.getLogger(WorkflowInvoker.class);
047: private ServiceManager manager;
048:
049: /**
050: * <code>ROLES</code> The roles
051: */
052: public static final String ROLES = "roles";
053: /**
054: * <code>USER_ID</code> The user id
055: */
056: public static final String USER_ID = "user-id";
057: /**
058: * <code>MACHINE</code> The machine
059: */
060: public static final String MACHINE = "machine";
061: /**
062: * <code>EVENT</code> The event
063: */
064: public static final String EVENT = "event";
065: /**
066: * <code>PREFIX</code> The workflow namespace prefix
067: */
068: public static final String PREFIX = "workflow";
069: /**
070: * <code>EVENT_REQUEST_PARAMETER</code> The workflow event request parameter
071: */
072: public static final String EVENT_REQUEST_PARAMETER = "workflow.event";
073: /**
074: * <code>LENYA_EVENT_REQUEST_PARAMETER</code> The Lenya event request parameter
075: */
076: public static final String LENYA_EVENT_REQUEST_PARAMETER = "lenya.event";
077:
078: /**
079: * Ctor.
080: *
081: * @param eventName The event name.
082: * @param identity The identity.
083: * @param roles The roles.
084: * @return A namespace map containing the parameters.
085: */
086: public static NamespaceMap extractParameters(String eventName,
087: Identity identity, Role[] roles) {
088: NamespaceMap parameters = new NamespaceMap(PREFIX);
089: log.debug("Extractign workflow invoker parameters.");
090: log.debug(" Event: [" + eventName + "]");
091: parameters.put(EVENT, eventName);
092: setRoles(parameters, roles);
093: setIdentity(parameters, identity);
094: return parameters;
095: }
096:
097: /**
098: * Ctor.
099: *
100: * @param parameters A map containing the prefixed parameters.
101: * @param manager The service manager.
102: */
103: public WorkflowInvoker(Map parameters, ServiceManager manager) {
104: super (parameters);
105: this .manager = manager;
106: }
107:
108: /**
109: * Returns the role names.
110: *
111: * @return A string array.
112: */
113: protected String[] getRoleIDs() {
114: String rolesString = get(ROLES);
115: String[] roleIDs = rolesString.split(",");
116: return roleIDs;
117: }
118:
119: /**
120: * Sets the roles.
121: *
122: * @param parameters A workflow invoker namespace map.
123: * @param roles A role array.
124: */
125: public static void setRoles(NamespaceMap parameters, Role[] roles) {
126:
127: StringBuffer buf = new StringBuffer();
128: for (int i = 0; i < roles.length; i++) {
129: if (i > 0) {
130: buf.append(",");
131: }
132: buf.append(roles[i].getId());
133: }
134: String roleString = buf.toString();
135: parameters.put(ROLES, roleString);
136: }
137:
138: /**
139: * Sets the identity.
140: *
141: * @param parameters A workflow invoker namespace map.
142: * @param identity An identity.
143: */
144: public static void setIdentity(NamespaceMap parameters,
145: Identity identity) {
146:
147: String userId = "";
148: User user = identity.getUser();
149: if (user != null) {
150: userId = user.getId();
151: }
152: parameters.put(USER_ID, userId);
153:
154: String machineIp = "";
155: Machine machine = identity.getMachine();
156: if (machine != null) {
157: machineIp = machine.getIp();
158: }
159: parameters.put(MACHINE, machineIp);
160: }
161:
162: /**
163: * Returns the workflow event name.
164: * @return A string.
165: */
166: public String getEventName() {
167: return get(EVENT);
168: }
169:
170: /**
171: * Returns the user ID.
172: * @return A string.
173: */
174: public String getUserId() {
175: return get(USER_ID);
176: }
177:
178: /**
179: * Returns the machine IP address.
180: * @return A string.
181: */
182: public String getMachineIp() {
183: return get(MACHINE);
184: }
185:
186: private Document document;
187:
188: /**
189: * Initializes the workflow invoker.
190: *
191: * @param publication The publication.
192: * @param webappUrl The webapp URL.
193: * @throws ExecutionException when something went wrong.
194: */
195: public void setup(Publication publication, String webappUrl)
196: throws ExecutionException {
197: String eventName = getEventName();
198: if (eventName == null) {
199: log.debug("No workflow event.");
200: } else {
201: log.debug("Workflow event: [" + eventName + "]");
202: // check for workflow instance first (task can initialize the workflow history)
203: try {
204: Session session = RepositoryUtil.createSession(
205: this .manager, null);
206: DocumentFactory map = DocumentUtil
207: .createDocumentIdentityMap(this .manager,
208: session);
209: this .document = map.getFromURL(webappUrl);
210: } catch (Exception e) {
211: throw new ExecutionException(e);
212: }
213: }
214: }
215:
216: /**
217: * Invokes the transition.
218: * @throws ExecutionException when something went wrong.
219: */
220: public void invokeTransition() throws ExecutionException {
221:
222: try {
223: Session session = RepositoryUtil.createSession(
224: this .manager, null);
225: WorkflowUtil.invoke(this .manager, session,
226: new ConsoleLogger(), this .document, getEventName());
227: } catch (Exception e) {
228: throw new ExecutionException(e);
229: }
230: }
231:
232: /**
233: * @see org.apache.lenya.cms.task.ParameterWrapper#getPrefix()
234: */
235: public String getPrefix() {
236: return PREFIX;
237: }
238:
239: /**
240: * @see org.apache.lenya.cms.task.ParameterWrapper#getRequiredKeys()
241: */
242: protected String[] getRequiredKeys() {
243: String[] keys = {};
244: return keys;
245: }
246:
247: }
|