KickJava   Java API By Example, From Geeks To Geeks.

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


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: CMSHistory.java 43175 2004-08-02 12:29:25Z andreas $ */
19
20 package org.apache.lenya.cms.workflow;
21
22 import java.io.File JavaDoc;
23
24 import org.apache.lenya.cms.publication.Document;
25 import org.apache.lenya.cms.publication.DocumentIdToPathMapper;
26 import org.apache.lenya.cms.publication.Publication;
27 import org.apache.lenya.util.FileUtil;
28 import org.apache.lenya.workflow.Situation;
29 import org.apache.lenya.workflow.WorkflowException;
30 import org.apache.lenya.workflow.impl.History;
31 import org.apache.lenya.workflow.impl.Version;
32 import org.apache.lenya.workflow.impl.WorkflowInstanceImpl;
33 import org.apache.lenya.xml.NamespaceHelper;
34 import org.w3c.dom.Element JavaDoc;
35
36 public class CMSHistory extends History {
37     public static final String JavaDoc HISTORY_PATH = "content/workflow/history";
38
39     /**
40      * Creates a new CMSHistory object.
41      *
42      * @param document the document to which the CMSHistory is attached
43      */

44     protected CMSHistory(Document document) {
45         setDocument(document);
46     }
47
48     private Document document;
49
50     public static final String JavaDoc IDENTITY_ELEMENT = "identity";
51     public static final String JavaDoc USER_ELEMENT = "user";
52     public static final String JavaDoc MACHINE_ELEMENT = "machine";
53     public static final String JavaDoc ID_ATTRIBUTE = "id";
54     public static final String JavaDoc NAME_ATTRIBUTE = "name";
55     public static final String JavaDoc IP_ATTRIBUTE = "ip-address";
56
57     /**
58      * @see org.apache.lenya.workflow.impl.History#createVersionElement(org.apache.lenya.xml.NamespaceHelper, org.apache.lenya.workflow.Situation)
59      */

60     protected Element JavaDoc createVersionElement(NamespaceHelper helper, Situation situation) {
61         Element JavaDoc element = super.createVersionElement(helper, situation);
62
63         CMSSituation cmsSituation = (CMSSituation) situation;
64
65         Element JavaDoc identityElement = helper.createElement(IDENTITY_ELEMENT);
66         element.appendChild(identityElement);
67
68         String JavaDoc userId = cmsSituation.getUserId();
69         if (userId != null) {
70             identityElement.appendChild(generateUserElement(helper, userId));
71         }
72
73         String JavaDoc machineIp = cmsSituation.getMachineIp();
74         if (machineIp != null) {
75             identityElement.appendChild(generateMachineElement(helper, machineIp));
76         }
77
78         return element;
79     }
80
81     /**
82      * Creates an XML element describing the user.
83      * @param helper The namespace helper of the document.
84      * @param userId The user ID.
85      * @return An XML element.
86      */

87     protected Element JavaDoc generateUserElement(NamespaceHelper helper, String JavaDoc userId) {
88         Element JavaDoc userElement = null;
89         userElement = helper.createElement(USER_ELEMENT);
90         userElement.setAttribute(ID_ATTRIBUTE, userId);
91         return userElement;
92     }
93
94     /**
95      * Creates an XML element describing the machine.
96      * @param helper The namespace helper of the document.
97      * @param machineIp The machine IP address.
98      * @return An XML element.
99      */

100     protected Element JavaDoc generateMachineElement(NamespaceHelper helper, String JavaDoc machineIp) {
101         Element JavaDoc machineElement = null;
102         machineElement = helper.createElement(MACHINE_ELEMENT);
103         machineElement.setAttribute(IP_ATTRIBUTE, machineIp);
104         return machineElement;
105     }
106
107     /**
108      * Returns the path of the history file inside the publication directory.
109      * @param document A CMS document.
110      * @return A string.
111      */

112     public String JavaDoc getHistoryPath(Document document) {
113         DocumentIdToPathMapper pathMapper = document.getPublication().getPathMapper();
114         String JavaDoc documentPath = pathMapper.getPath(document.getId(), document.getLanguage());
115
116         String JavaDoc area = document.getArea();
117         if (!area.equals(Publication.ARCHIVE_AREA) && !area.equals(Publication.TRASH_AREA)) {
118             area = Publication.AUTHORING_AREA;
119         }
120
121         String JavaDoc path = HISTORY_PATH + "/" + area + "/" + documentPath;
122         path = path.replace('/', File.separatorChar);
123         return path;
124     }
125
126     /**
127      * @see org.apache.lenya.workflow.impl.History#getHistoryFile()
128      */

129     protected File JavaDoc getHistoryFile() {
130         return getHistoryFile(getDocument());
131     }
132
133     /**
134      * Returns the history file for a certain document.
135      * @param document The document.
136      * @return A file.
137      */

138     protected File JavaDoc getHistoryFile(Document document) {
139         File JavaDoc historyFile =
140             new File JavaDoc(document.getPublication().getDirectory(), getHistoryPath(document));
141         return historyFile;
142     }
143
144     /**
145      * @see org.apache.lenya.workflow.impl.History#createInstance()
146      */

147     protected WorkflowInstanceImpl createInstance() throws WorkflowException {
148         return new WorkflowDocument(getDocument());
149     }
150
151     /**
152      * Get the document
153      *
154      * @return the Document
155      */

156     public Document getDocument() {
157         return document;
158     }
159
160     /**
161      * Set the document
162      *
163      * @param document the document
164      */

165     public void setDocument(Document document) {
166         this.document = document;
167     }
168
169     /**
170      * Initializes the workflow history of another document using the same
171      * workflow schema like this history.
172      * @param newDocument The document to initialize the history for.
173      * @param situation The current situation.
174      * @throws WorkflowException when something went wrong.
175      */

176     protected void initialize(Document newDocument, Situation situation) throws WorkflowException {
177         String JavaDoc workflowId = getWorkflowId();
178         CMSHistory newHistory = new CMSHistory(newDocument);
179         newHistory.initialize(workflowId, situation);
180     }
181
182     /**
183      * Moves this history to a new document.
184      * @param newDocument The new document.
185      * @throws WorkflowException when something went wrong.
186      */

187     protected void move(Document newDocument) throws WorkflowException {
188         assert newDocument != null;
189         move(getHistoryFile(newDocument));
190         setDocument(newDocument);
191     }
192
193     /**
194      * @see org.apache.lenya.workflow.impl.History#restoreVersion(NamespaceHelper, org.w3c.dom.Element)
195      */

196     protected Version restoreVersion(NamespaceHelper helper, Element JavaDoc element)
197         throws WorkflowException {
198         Version version = super.restoreVersion(helper, element);
199         CMSVersion cmsVersion = new CMSVersion(version.getEvent(), version.getState());
200
201         Element JavaDoc identityElement = helper.getFirstChild(element, IDENTITY_ELEMENT);
202         Element JavaDoc userElement = helper.getFirstChild(identityElement, USER_ELEMENT);
203         if (userElement != null) {
204             String JavaDoc userId = userElement.getAttribute(ID_ATTRIBUTE);
205             cmsVersion.setUserId(userId);
206         }
207
208         return cmsVersion;
209     }
210
211     /**
212      * Returns the history path of this history.
213      * @return A string.
214      */

215     public String JavaDoc getHistoryPath() {
216         return getHistoryPath(getDocument());
217     }
218
219     /**
220      * Additionally to deleting the workflow history, the parent directories
221      * are deleted up to the workflow history directory.
222      * @see org.apache.lenya.workflow.impl.History#delete()
223      */

224     public void delete() throws WorkflowException {
225         super.delete();
226         
227         File JavaDoc stopDirectory = new File JavaDoc(getDocument().getPublication().getDirectory(), HISTORY_PATH);
228         FileUtil.deleteParentDirs(getHistoryFile(), stopDirectory);
229     }
230 }
231
Popular Tags