1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.edl.impl.components;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.rice.edl.impl.EDLContext;
22 import org.kuali.rice.edl.impl.EDLModelComponent;
23 import org.kuali.rice.edl.impl.EDLXmlUtils;
24 import org.kuali.rice.edl.impl.RequestParser;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.Node;
28 import org.w3c.dom.NodeList;
29
30
31
32
33
34
35
36
37
38 public class VersioningPreprocessor implements EDLModelComponent {
39
40 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VersioningPreprocessor.class);
41
42 public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
43
44 RequestParser requestParser = edlContext.getRequestParser();
45
46 boolean incrementVersion = edlContext.getUserAction().isIncrementVersionAction();
47 boolean replaceVersion = edlContext.getUserAction().isReplaceVersionAction();
48
49 Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
50 Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false);
51 Element currentVersion = findCurrentVersion(dom);
52
53
54 if (currentVersion == null) {
55 Integer currentVersionCount = new Integer(0);
56 currentVersion = EDLXmlUtils.getVersionFromData(dataElement, currentVersionCount);
57 } else if (incrementVersion) {
58 currentVersion.getAttributeNode("current").setNodeValue("false");
59 int currentVersionCount = new Integer(currentVersion.getAttribute("version")).intValue() + 1;
60 EDLXmlUtils.getVersionFromData(dataElement, new Integer(currentVersionCount));
61 } else if (replaceVersion) {
62 NodeList children = currentVersion.getChildNodes();
63
64 List<Node> childrenToRemove = new ArrayList<Node>();
65 for (int index = 0; index < children.getLength(); index++) {
66 childrenToRemove.add(children.item(index));
67 }
68 for (Node childToRemove : childrenToRemove) {
69 currentVersion.removeChild(childToRemove);
70 }
71 }
72 requestParser.setAttribute("currentVersion", currentVersion.getAttribute("currentVersion"));
73 }
74
75 public static Element findCurrentVersion(Document dom) {
76 Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
77 Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false);
78 NodeList versionElements = dataElement.getElementsByTagName(EDLXmlUtils.VERSION_E);
79 for (int i = 0; i < versionElements.getLength(); i++) {
80 Element version = (Element) versionElements.item(i);
81 Boolean currentVersion = new Boolean(version.getAttribute("current"));
82 if (currentVersion.booleanValue()) {
83 return version;
84 }
85 }
86 return null;
87
88 }
89
90 }