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-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.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 org.kuali.rice.edl.impl.components;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.kuali.rice.edl.impl.EDLContext;
 23  
 import org.kuali.rice.edl.impl.EDLModelComponent;
 24  
 import org.kuali.rice.edl.impl.EDLXmlUtils;
 25  
 import org.kuali.rice.edl.impl.RequestParser;
 26  
 import org.w3c.dom.Document;
 27  
 import org.w3c.dom.Element;
 28  
 import org.w3c.dom.Node;
 29  
 import org.w3c.dom.NodeList;
 30  
 
 31  
 
 32  
 /**
 33  
  * Versions the data element if necessary by checking 'currentVersion' param on request.  If this request is
 34  
  * a doc handler request this will configure the dom so the next request will cause the data to be incremented.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  *
 38  
  */
 39  0
 public class VersioningPreprocessor implements EDLModelComponent {
 40  
 
 41  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VersioningPreprocessor.class);
 42  
 
 43  
         public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
 44  
 
 45  0
                 RequestParser requestParser = edlContext.getRequestParser();
 46  
 
 47  0
                 boolean incrementVersion = edlContext.getUserAction().isIncrementVersionAction();
 48  0
                 boolean replaceVersion = edlContext.getUserAction().isReplaceVersionAction();
 49  
 
 50  0
                 Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
 51  0
                 Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false);
 52  0
                 Element currentVersion = findCurrentVersion(dom);
 53  
 
 54  
 
 55  0
                 if (currentVersion == null) {
 56  0
                         Integer currentVersionCount = new Integer(0);
 57  0
                         currentVersion = EDLXmlUtils.getVersionFromData(dataElement, currentVersionCount);
 58  0
                 } else if (incrementVersion)  {
 59  0
                         currentVersion.getAttributeNode("current").setNodeValue("false");
 60  0
                         int currentVersionCount = new Integer(currentVersion.getAttribute("version")).intValue() + 1;
 61  0
                         EDLXmlUtils.getVersionFromData(dataElement, new Integer(currentVersionCount));
 62  0
                 } else if (replaceVersion) {
 63  0
                     NodeList children = currentVersion.getChildNodes();
 64  
                     // important to store these in the last for removal later because we can't safely removeChild in the first for-loop below
 65  0
                     List<Node> childrenToRemove = new ArrayList<Node>();
 66  0
                     for (int index = 0; index < children.getLength(); index++) {
 67  0
                         childrenToRemove.add(children.item(index));
 68  
                     }
 69  0
                     for (Node childToRemove : childrenToRemove) {
 70  0
                         currentVersion.removeChild(childToRemove);
 71  
                     }
 72  
                 }
 73  0
                 requestParser.setAttribute("currentVersion", currentVersion.getAttribute("currentVersion"));
 74  0
         }
 75  
 
 76  
         public static Element findCurrentVersion(Document dom) {
 77  0
                 Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
 78  0
                 Element dataElement = EDLXmlUtils.getDataFromEDLDocument(edlContentElement, false);
 79  0
                 NodeList versionElements = dataElement.getElementsByTagName(EDLXmlUtils.VERSION_E);
 80  0
                 for (int i = 0; i < versionElements.getLength(); i++) {
 81  0
                         Element version = (Element) versionElements.item(i);
 82  0
                         Boolean currentVersion = new Boolean(version.getAttribute("current"));
 83  0
                         if (currentVersion.booleanValue()) {
 84  0
                                 return version;
 85  
                         }
 86  
                 }
 87  0
                 return null;
 88  
         }
 89  
 
 90  
 }