01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.edl.components;
18:
19: import org.w3c.dom.Document;
20: import org.w3c.dom.Element;
21: import org.w3c.dom.NodeList;
22:
23: import edu.iu.uis.eden.edl.EDLContext;
24: import edu.iu.uis.eden.edl.EDLModelComponent;
25: import edu.iu.uis.eden.edl.EDLXmlUtils;
26: import edu.iu.uis.eden.edl.RequestParser;
27:
28: /**
29: * Versions the data element if necessary by checking 'currentVersion' param on request. If this request is
30: * a doc handler request this will configure the dom so the next request will cause the data to be incremented.
31: *
32: * @author rkirkend
33: *
34: */
35: public class VersioningPreprocessor implements EDLModelComponent {
36:
37: public void updateDOM(Document dom, Element configElement,
38: EDLContext edlContext) {
39: RequestParser requestParser = edlContext.getRequestParser();
40:
41: Element edlContentElement = EDLXmlUtils.getEDLContent(dom,
42: false);
43: Element dataElement = EDLXmlUtils.getDataFromEDLDocument(
44: edlContentElement, false);
45:
46: Element currentVersion = findCurrentVersion(dom);
47: if (currentVersion == null) {
48: Integer currentVersionCount = new Integer(0);
49: currentVersion = EDLXmlUtils.getVersionFromData(
50: dataElement, currentVersionCount);
51: } else if (requestParser.getParameterValue("incrementVersion") != null) {
52: currentVersion.getAttributeNode("current").setNodeValue(
53: "false");
54:
55: int currentVersionCount = new Integer(currentVersion
56: .getAttribute("version")).intValue() + 1;
57: EDLXmlUtils.getVersionFromData(dataElement, new Integer(
58: currentVersionCount));
59: }
60:
61: requestParser.setAttribute("currentVersion", currentVersion
62: .getAttribute("currentVersion"));
63:
64: //this assumes we're coming from a docHandler
65: if (requestParser.getParameterValue("command") != null) {
66: // this needs set so that it will be made a request param to be read
67: // in above
68: EDLXmlUtils.createTextElementOnParent(currentVersion,
69: "incrementVersion", "true");
70: }
71:
72: }
73:
74: public static Element findCurrentVersion(Document dom) {
75: Element edlContentElement = EDLXmlUtils.getEDLContent(dom,
76: false);
77: Element dataElement = EDLXmlUtils.getDataFromEDLDocument(
78: edlContentElement, false);
79: NodeList versionElements = dataElement
80: .getElementsByTagName(EDLXmlUtils.VERSION_E);
81: for (int i = 0; i < versionElements.getLength(); i++) {
82: Element version = (Element) versionElements.item(i);
83: Boolean currentVersion = new Boolean(version
84: .getAttribute("current"));
85: if (currentVersion.booleanValue()) {
86: return version;
87: }
88: }
89: return null;
90: }
91:
92: }
|