KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > workflow > WorkflowFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: WorkflowFactory.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.workflow;
21
22 import java.io.File JavaDoc;
23
24 import org.apache.lenya.ac.Identity;
25 import org.apache.lenya.ac.Machine;
26 import org.apache.lenya.ac.Role;
27 import org.apache.lenya.ac.User;
28 import org.apache.lenya.cms.publication.Document;
29 import org.apache.lenya.cms.publication.DocumentException;
30 import org.apache.lenya.cms.publication.LanguageVersions;
31 import org.apache.lenya.cms.publication.Publication;
32 import org.apache.lenya.workflow.Situation;
33 import org.apache.lenya.workflow.SynchronizedWorkflowInstances;
34 import org.apache.lenya.workflow.Workflow;
35 import org.apache.lenya.workflow.WorkflowException;
36 import org.apache.lenya.workflow.WorkflowInstance;
37 import org.apache.lenya.workflow.impl.History;
38 import org.apache.lenya.workflow.impl.WorkflowBuilder;
39
40 /**
41  * Workflow factory.
42  */

43 public class WorkflowFactory {
44     public static final String JavaDoc WORKFLOW_DIRECTORY =
45         "config/workflow".replace('/', File.separatorChar);
46
47     /** Creates a new instance of WorkflowFactory */
48     protected WorkflowFactory() {
49     }
50
51     /**
52      * Returns an instance of the workflow factory.
53      * @return A workflow factory.
54      */

55     public static WorkflowFactory newInstance() {
56         return new WorkflowFactory();
57     }
58
59     /**
60      * Creates a new workflow instance.
61      * @param document The document to create the instance for.
62      * @return A workflow instance.
63      * @throws WorkflowException when something went wrong.
64      */

65     public WorkflowInstance buildInstance(Document document) throws WorkflowException {
66         assert document != null;
67         return getHistory(document).getInstance();
68     }
69
70     /**
71      * Creates a new synchronized workflow instances object..
72      * @param document The document to create the instances for.
73      * @return A synchronized workflow instances object.
74      * @throws WorkflowException when something went wrong.
75      */

76     public SynchronizedWorkflowInstances buildSynchronizedInstance(Document document) throws WorkflowException {
77         assert document != null;
78         LanguageVersions versions;
79         try {
80             versions = new LanguageVersions(document);
81         } catch (DocumentException e) {
82             throw new WorkflowException(e);
83         }
84         return new WorkflowDocumentSet(versions, document);
85     }
86
87     /**
88      * Moves the history of a document.
89      * @param oldDocument The document to move the instance for.
90      * @param newDocument The new document.
91      * @throws WorkflowException when something went wrong.
92      */

93     public static void moveHistory(Document oldDocument, Document newDocument) throws WorkflowException {
94         assert oldDocument != null;
95         new CMSHistory(oldDocument).move(newDocument);
96     }
97
98     /**
99      * Deletes the history of a document.
100      * @param document The document to delete the instance for.
101      * @throws WorkflowException when something went wrong.
102      */

103     public static void deleteHistory(Document document) throws WorkflowException {
104         assert document != null;
105         getHistory(document).delete();
106     }
107
108     /**
109      * Checks if a workflow is assigned to the document.
110      * This is done by looking for the workflow history file.
111      * @param document The document to check.
112      * @return <code>true</code> if the document has a workflow, <code>false</code> otherwise.
113      */

114     public boolean hasWorkflow(Document document) {
115         return getHistory(document).isInitialized();
116     }
117
118     /**
119      * Builds a workflow for a given publication.
120      * @param publication The publication.
121      * @param workflowFileName The workflow definition filename.
122      * @return A workflow object.
123      * @throws WorkflowException when something went wrong.
124      */

125     protected static Workflow buildWorkflow(Publication publication, String JavaDoc workflowFileName)
126         throws WorkflowException {
127         assert publication != null;
128         assert(workflowFileName != null) && !"".equals(workflowFileName);
129
130         File JavaDoc workflowDirectory = new File JavaDoc(publication.getDirectory(), WORKFLOW_DIRECTORY);
131         File JavaDoc workflowFile = new File JavaDoc(workflowDirectory, workflowFileName);
132         Workflow workflow = WorkflowBuilder.buildWorkflow(workflowFile);
133
134         return workflow;
135     }
136
137     /**
138      * Creates a situation for a set of roles and an identity.
139      * @param roles The roles.
140      * @param identity The identity.
141      * @return A workflow situation.
142      * @throws WorkflowException when something went wrong.
143      */

144     public Situation buildSituation(Role[] roles, Identity identity) throws WorkflowException {
145         if (identity == null) {
146             throw new WorkflowException("Session does not contain identity!");
147         }
148         String JavaDoc userId = null;
149         User user = identity.getUser();
150         if (user != null) {
151             userId = user.getId();
152         }
153         
154         String JavaDoc machineIp = null;
155         Machine machine = identity.getMachine();
156         if (machine != null) {
157             machineIp = machine.getIp();
158         }
159         
160         String JavaDoc[] roleIds = new String JavaDoc[roles.length];
161         for (int i = 0; i < roles.length; i++) {
162             roleIds[i] = roles[i].getId();
163         }
164         
165         return buildSituation(roleIds, userId, machineIp);
166     }
167
168     /**
169      * Builds a situation from a role name set, a user ID and a machine IP address.
170      * @param roleIds The role IDs.
171      * @param userId The user ID.
172      * @param machineIp The machine IP address.
173      * @return A situation.
174      */

175     public Situation buildSituation(String JavaDoc[] roleIds, String JavaDoc userId, String JavaDoc machineIp) {
176         return new CMSSituation(roleIds, userId, machineIp);
177     }
178
179     /**
180      * Initializes the history of a document.
181      * @param document The document object.
182      * @param workflowId The ID of the workflow.
183      * @param situation The current situation.
184      * @throws WorkflowException When something goes wrong.
185      */

186     public static void initHistory(Document document, String JavaDoc workflowId, Situation situation) throws WorkflowException {
187         new CMSHistory(document).initialize(workflowId, situation);
188     }
189     
190     /**
191      * Returns the workflow history of a document.
192      * @param document A document.
193      * @return A workflow history.
194      */

195     public static History getHistory(Document document) {
196         return new CMSHistory(document);
197     }
198
199     /**
200      * Initializes the workflow history of a document that is a copy of
201      * another document.
202      * @param sourceDocument The original document.
203      * @param destinationDocument The document to initialize the history for.
204      * @throws WorkflowException When something goes wrong.
205      */

206     public static void initHistory(Document sourceDocument, Document destinationDocument, Situation situation)
207         throws WorkflowException {
208         CMSHistory history = new CMSHistory(sourceDocument);
209         history.initialize(destinationDocument, situation);
210     }
211
212 }
213
Popular Tags