001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.edl.impl.components;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.kuali.rice.edl.impl.EDLContext;
022import org.kuali.rice.edl.impl.EDLModelComponent;
023import org.kuali.rice.edl.impl.EDLXmlUtils;
024import org.kuali.rice.edl.impl.RequestParser;
025import org.w3c.dom.Document;
026import org.w3c.dom.Element;
027import org.w3c.dom.Node;
028import org.w3c.dom.NodeList;
029
030
031/**
032 * Versions the data element if necessary by checking 'currentVersion' param on request.  If this request is
033 * a doc handler request this will configure the dom so the next request will cause the data to be incremented.
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038public class VersioningPreprocessor implements EDLModelComponent {
039
040    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VersioningPreprocessor.class);
041
042        public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
043
044                RequestParser requestParser = edlContext.getRequestParser();
045
046                boolean incrementVersion = edlContext.getUserAction().isIncrementVersionAction();
047                boolean replaceVersion = edlContext.getUserAction().isReplaceVersionAction();
048
049                Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
050                Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false);
051                Element currentVersion = findCurrentVersion(dom);
052
053
054                if (currentVersion == null) {
055                        Integer currentVersionCount = new Integer(0);
056                        currentVersion = EDLXmlUtils.getVersionFromData(dataElement, currentVersionCount);
057                } else if (incrementVersion)  {
058                        currentVersion.getAttributeNode("current").setNodeValue("false");
059                        int currentVersionCount = new Integer(currentVersion.getAttribute("version")).intValue() + 1;
060                        EDLXmlUtils.getVersionFromData(dataElement, new Integer(currentVersionCount));
061                } else if (replaceVersion) {
062                    NodeList children = currentVersion.getChildNodes();
063                    // important to store these in the last for removal later because we can't safely removeChild in the first for-loop below
064                    List<Node> childrenToRemove = new ArrayList<Node>();
065                    for (int index = 0; index < children.getLength(); index++) {
066                        childrenToRemove.add(children.item(index));
067                    }
068                    for (Node childToRemove : childrenToRemove) {
069                        currentVersion.removeChild(childToRemove);
070                    }
071                }
072                requestParser.setAttribute("currentVersion", currentVersion.getAttribute("currentVersion"));
073        }
074
075        public static Element findCurrentVersion(Document dom) {
076                Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
077                Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false);
078                NodeList versionElements = dataElement.getElementsByTagName(EDLXmlUtils.VERSION_E);
079                for (int i = 0; i < versionElements.getLength(); i++) {
080                        Element version = (Element) versionElements.item(i);
081                        Boolean currentVersion = new Boolean(version.getAttribute("current"));
082                        if (currentVersion.booleanValue()) {
083                                return version;
084                        }
085                }
086                return null;
087
088        }
089
090}