001/** 002 * Copyright 2005-2015 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 public void updateDOM(Document dom, Element configElement, EDLContext edlContext) { 041 042 RequestParser requestParser = edlContext.getRequestParser(); 043 044 boolean incrementVersion = edlContext.getUserAction().isIncrementVersionAction(); 045 boolean replaceVersion = edlContext.getUserAction().isReplaceVersionAction(); 046 047 Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false); 048 Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false); 049 Element currentVersion = findCurrentVersion(dom); 050 051 052 if (currentVersion == null) { 053 Integer currentVersionCount = new Integer(0); 054 currentVersion = EDLXmlUtils.getVersionFromData(dataElement, currentVersionCount); 055 } else if (incrementVersion) { 056 currentVersion.getAttributeNode("current").setNodeValue("false"); 057 int currentVersionCount = new Integer(currentVersion.getAttribute("version")).intValue() + 1; 058 EDLXmlUtils.getVersionFromData(dataElement, new Integer(currentVersionCount)); 059 } else if (replaceVersion) { 060 NodeList children = currentVersion.getChildNodes(); 061 // important to store these in the last for removal later because we can't safely removeChild in the first for-loop below 062 List<Node> childrenToRemove = new ArrayList<Node>(); 063 for (int index = 0; index < children.getLength(); index++) { 064 childrenToRemove.add(children.item(index)); 065 } 066 for (Node childToRemove : childrenToRemove) { 067 currentVersion.removeChild(childToRemove); 068 } 069 } 070 requestParser.setAttribute("currentVersion", currentVersion.getAttribute("currentVersion")); 071 } 072 073 public static Element findCurrentVersion(Document dom) { 074 Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false); 075 Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false); 076 NodeList versionElements = dataElement.getElementsByTagName(EDLXmlUtils.VERSION_E); 077 for (int i = 0; i < versionElements.getLength(); i++) { 078 Element version = (Element) versionElements.item(i); 079 Boolean currentVersion = new Boolean(version.getAttribute("current")); 080 if (currentVersion.booleanValue()) { 081 return version; 082 } 083 } 084 return null; 085 086 } 087 088}