Coverage Report - org.kuali.rice.edl.impl.components.VersioningPreprocessor
 
Classes in this File Line Coverage Branch Coverage Complexity
VersioningPreprocessor
0%
0/33
0%
0/14
5
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * Versions the data element if necessary by checking 'currentVersion' param on request.  If this request is
 33  
  * a doc handler request this will configure the dom so the next request will cause the data to be incremented.
 34  
  *
 35  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 36  
  *
 37  
  */
 38  0
 public class VersioningPreprocessor implements EDLModelComponent {
 39  
 
 40  0
     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  0
                 RequestParser requestParser = edlContext.getRequestParser();
 45  
 
 46  0
                 boolean incrementVersion = edlContext.getUserAction().isIncrementVersionAction();
 47  0
                 boolean replaceVersion = edlContext.getUserAction().isReplaceVersionAction();
 48  
 
 49  0
                 Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
 50  0
                 Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false);
 51  0
                 Element currentVersion = findCurrentVersion(dom);
 52  
 
 53  
 
 54  0
                 if (currentVersion == null) {
 55  0
                         Integer currentVersionCount = new Integer(0);
 56  0
                         currentVersion = EDLXmlUtils.getVersionFromData(dataElement, currentVersionCount);
 57  0
                 } else if (incrementVersion)  {
 58  0
                         currentVersion.getAttributeNode("current").setNodeValue("false");
 59  0
                         int currentVersionCount = new Integer(currentVersion.getAttribute("version")).intValue() + 1;
 60  0
                         EDLXmlUtils.getVersionFromData(dataElement, new Integer(currentVersionCount));
 61  0
                 } else if (replaceVersion) {
 62  0
                     NodeList children = currentVersion.getChildNodes();
 63  
                     // important to store these in the last for removal later because we can't safely removeChild in the first for-loop below
 64  0
                     List<Node> childrenToRemove = new ArrayList<Node>();
 65  0
                     for (int index = 0; index < children.getLength(); index++) {
 66  0
                         childrenToRemove.add(children.item(index));
 67  
                     }
 68  0
                     for (Node childToRemove : childrenToRemove) {
 69  0
                         currentVersion.removeChild(childToRemove);
 70  
                     }
 71  
                 }
 72  0
                 requestParser.setAttribute("currentVersion", currentVersion.getAttribute("currentVersion"));
 73  0
         }
 74  
 
 75  
         public static Element findCurrentVersion(Document dom) {
 76  0
                 Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
 77  0
                 Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false);
 78  0
                 NodeList versionElements = dataElement.getElementsByTagName(EDLXmlUtils.VERSION_E);
 79  0
                 for (int i = 0; i < versionElements.getLength(); i++) {
 80  0
                         Element version = (Element) versionElements.item(i);
 81  0
                         Boolean currentVersion = new Boolean(version.getAttribute("current"));
 82  0
                         if (currentVersion.booleanValue()) {
 83  0
                                 return version;
 84  
                         }
 85  
                 }
 86  0
                 return null;
 87  
         }
 88  
 
 89  
 }