Coverage Report - org.kuali.rice.kns.web.EditablePropertiesHistoryHolder
 
Classes in this File Line Coverage Branch Coverage Complexity
EditablePropertiesHistoryHolder
0%
0/32
0%
0/8
1.6
 
 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.kns.web;
 17  
 
 18  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 19  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 20  
 
 21  
 import java.util.HashMap;
 22  
 import java.util.HashSet;
 23  
 import java.util.LinkedList;
 24  
 import java.util.Map;
 25  
 import java.util.Queue;
 26  
 import java.util.Set;
 27  
 import java.util.UUID;
 28  
 
 29  
 
 30  
 /**
 31  
  * A class which will hold a Map of editable properties, dropping editable properties when too many
 32  
  * are filled in. 
 33  
  * 
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  *
 36  
  */
 37  
 @Deprecated
 38  
 public class EditablePropertiesHistoryHolder implements java.io.Serializable {
 39  
         private Map<String, Set<String>> editablePropertiesMap;
 40  0
         private Integer maxLength = null;
 41  
         private Queue<String> historyOrder;
 42  
         private static final String EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME = "kns.editable.properties.history.size";
 43  
         private transient ConfigurationService configurationService;
 44  
         
 45  
         /**
 46  
          * Constructs the EditablePropertiesHistoryHolder
 47  
          *
 48  
          */
 49  0
         public EditablePropertiesHistoryHolder() {
 50  0
                 editablePropertiesMap = new HashMap<String, Set<String>>();
 51  0
                 historyOrder = new LinkedList<String>();
 52  0
         }
 53  
         
 54  
         /**
 55  
          * @return the maximum length of the history that this will hold
 56  
          */
 57  
         public int getMaxHistoryLength() {
 58  0
                 if (maxLength == null) {
 59  0
                         final String historyLengthAsString = getConfigurationService().getPropertyValueAsString(
 60  
                     EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME);
 61  0
                         if (historyLengthAsString == null) {
 62  0
                                 maxLength = new Integer(20);
 63  
                         } else {
 64  
                                 try {
 65  0
                                         maxLength = new Integer(historyLengthAsString);
 66  0
                                 } catch (NumberFormatException nfe) {
 67  0
                                         throw new RuntimeException("Cannot convert property "+EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME+" with value "+historyLengthAsString+" to integer", nfe);
 68  0
                                 }
 69  
                         }
 70  
                 }
 71  0
                 return maxLength.intValue();
 72  
         }
 73  
         
 74  
         /**
 75  
          * Adds a Set of editable property names to the history, keyed with the given guid String.  If the editable properties exceeds the buffer size,
 76  
          * the earliest editable properties will be bumped
 77  
          * @param editableProperties the Set of editable property names to save in the history
 78  
          * @return a String to act as a key (or guid) to the editable properties
 79  
          */
 80  
         public String addEditablePropertiesToHistory(Set<String> editableProperties) {
 81  0
                 String guid = generateNewGuid();
 82  
                 
 83  0
                 if (getHistoryOrder().size() > getMaxHistoryLength()) {
 84  0
                         final String guidForRemoval = getHistoryOrder().remove();
 85  0
                         getEditablePropertiesMap().remove(guidForRemoval);
 86  
                 }
 87  0
                 getHistoryOrder().add(guid);
 88  0
                 getEditablePropertiesMap().put(guid, editableProperties);
 89  
                 
 90  0
                 return guid;
 91  
         }
 92  
         
 93  
         /**
 94  
          * 
 95  
          * @return a newly generated Guid to act as a key to an editable properties Set
 96  
          */
 97  
         public String generateNewGuid() {
 98  0
                 final String guid = UUID.randomUUID().toString();
 99  0
                 return guid;
 100  
         }
 101  
         
 102  
         /**
 103  
          * Returns the editable properties registered with the current guid
 104  
          * @param guid the guid to find editable properties for
 105  
          * @return a Set<String> of editable properties
 106  
          */
 107  
         public Set<String> getEditableProperties(String guid) {
 108  0
                 return getEditablePropertiesMap().get(guid);
 109  
         }
 110  
         
 111  
         /**
 112  
          * Clears out the editable properties associated with the given guid
 113  
          * @param guid the guid to clear out editable properties for
 114  
          */
 115  
         public void clearEditableProperties(String guid) {
 116  0
                 getEditablePropertiesMap().put(guid, createNewEditablePropertiesEntry());
 117  0
         }
 118  
         
 119  
         /**
 120  
          * @return the order of the entries as they chronologically were created
 121  
          */
 122  
         protected Queue<String> getHistoryOrder() {
 123  0
                 return historyOrder;
 124  
         }
 125  
         
 126  
         /**
 127  
          * @return the Map which associates editable property guids with Sets of editable property names
 128  
          */
 129  
         protected Map<String, Set<String>> getEditablePropertiesMap() {
 130  0
                 return editablePropertiesMap;
 131  
         }
 132  
         
 133  
         /**
 134  
          * @return a new Entry to hold the names of editable properties
 135  
          */
 136  
         protected Set<String> createNewEditablePropertiesEntry() {
 137  0
                 return new HashSet<String>();
 138  
         }
 139  
         
 140  
         /**
 141  
          * @return an implementation of the ConfigurationService
 142  
          */
 143  
         protected ConfigurationService getConfigurationService() {
 144  0
                 if (configurationService == null) {
 145  0
                         configurationService = KRADServiceLocator.getKualiConfigurationService();
 146  
                 }
 147  0
                 return configurationService;
 148  
         }
 149  
 }