1 17 18 19 20 package org.apache.lenya.cms.workflow; 21 22 import java.io.File ; 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 ; 35 36 public class CMSHistory extends History { 37 public static final String HISTORY_PATH = "content/workflow/history"; 38 39 44 protected CMSHistory(Document document) { 45 setDocument(document); 46 } 47 48 private Document document; 49 50 public static final String IDENTITY_ELEMENT = "identity"; 51 public static final String USER_ELEMENT = "user"; 52 public static final String MACHINE_ELEMENT = "machine"; 53 public static final String ID_ATTRIBUTE = "id"; 54 public static final String NAME_ATTRIBUTE = "name"; 55 public static final String IP_ATTRIBUTE = "ip-address"; 56 57 60 protected Element createVersionElement(NamespaceHelper helper, Situation situation) { 61 Element element = super.createVersionElement(helper, situation); 62 63 CMSSituation cmsSituation = (CMSSituation) situation; 64 65 Element identityElement = helper.createElement(IDENTITY_ELEMENT); 66 element.appendChild(identityElement); 67 68 String userId = cmsSituation.getUserId(); 69 if (userId != null) { 70 identityElement.appendChild(generateUserElement(helper, userId)); 71 } 72 73 String machineIp = cmsSituation.getMachineIp(); 74 if (machineIp != null) { 75 identityElement.appendChild(generateMachineElement(helper, machineIp)); 76 } 77 78 return element; 79 } 80 81 87 protected Element generateUserElement(NamespaceHelper helper, String userId) { 88 Element userElement = null; 89 userElement = helper.createElement(USER_ELEMENT); 90 userElement.setAttribute(ID_ATTRIBUTE, userId); 91 return userElement; 92 } 93 94 100 protected Element generateMachineElement(NamespaceHelper helper, String machineIp) { 101 Element machineElement = null; 102 machineElement = helper.createElement(MACHINE_ELEMENT); 103 machineElement.setAttribute(IP_ATTRIBUTE, machineIp); 104 return machineElement; 105 } 106 107 112 public String getHistoryPath(Document document) { 113 DocumentIdToPathMapper pathMapper = document.getPublication().getPathMapper(); 114 String documentPath = pathMapper.getPath(document.getId(), document.getLanguage()); 115 116 String area = document.getArea(); 117 if (!area.equals(Publication.ARCHIVE_AREA) && !area.equals(Publication.TRASH_AREA)) { 118 area = Publication.AUTHORING_AREA; 119 } 120 121 String path = HISTORY_PATH + "/" + area + "/" + documentPath; 122 path = path.replace('/', File.separatorChar); 123 return path; 124 } 125 126 129 protected File getHistoryFile() { 130 return getHistoryFile(getDocument()); 131 } 132 133 138 protected File getHistoryFile(Document document) { 139 File historyFile = 140 new File (document.getPublication().getDirectory(), getHistoryPath(document)); 141 return historyFile; 142 } 143 144 147 protected WorkflowInstanceImpl createInstance() throws WorkflowException { 148 return new WorkflowDocument(getDocument()); 149 } 150 151 156 public Document getDocument() { 157 return document; 158 } 159 160 165 public void setDocument(Document document) { 166 this.document = document; 167 } 168 169 176 protected void initialize(Document newDocument, Situation situation) throws WorkflowException { 177 String workflowId = getWorkflowId(); 178 CMSHistory newHistory = new CMSHistory(newDocument); 179 newHistory.initialize(workflowId, situation); 180 } 181 182 187 protected void move(Document newDocument) throws WorkflowException { 188 assert newDocument != null; 189 move(getHistoryFile(newDocument)); 190 setDocument(newDocument); 191 } 192 193 196 protected Version restoreVersion(NamespaceHelper helper, Element element) 197 throws WorkflowException { 198 Version version = super.restoreVersion(helper, element); 199 CMSVersion cmsVersion = new CMSVersion(version.getEvent(), version.getState()); 200 201 Element identityElement = helper.getFirstChild(element, IDENTITY_ELEMENT); 202 Element userElement = helper.getFirstChild(identityElement, USER_ELEMENT); 203 if (userElement != null) { 204 String userId = userElement.getAttribute(ID_ATTRIBUTE); 205 cmsVersion.setUserId(userId); 206 } 207 208 return cmsVersion; 209 } 210 211 215 public String getHistoryPath() { 216 return getHistoryPath(getDocument()); 217 } 218 219 224 public void delete() throws WorkflowException { 225 super.delete(); 226 227 File stopDirectory = new File (getDocument().getPublication().getDirectory(), HISTORY_PATH); 228 FileUtil.deleteParentDirs(getHistoryFile(), stopDirectory); 229 } 230 } 231 | Popular Tags |