Coverage Report - org.kuali.rice.kew.util.Utilities
 
Classes in this File Line Coverage Branch Coverage Complexity
Utilities
0%
0/54
0%
0/18
4.273
Utilities$PrioritySorter
0%
0/9
0%
0/8
4.273
Utilities$RouteLogActionRequestSorter
0%
0/13
0%
0/14
4.273
 
 1  
 /*
 2  
  * Copyright 2006-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.kew.util;
 17  
 
 18  
 import org.apache.commons.lang.text.StrSubstitutor;
 19  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 20  
 import org.kuali.rice.core.api.util.KeyValue;
 21  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 22  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 23  
 import org.kuali.rice.kim.util.KimConstants;
 24  
 
 25  
 import java.net.InetAddress;
 26  
 import java.net.UnknownHostException;
 27  
 import java.util.Calendar;
 28  
 import java.util.Collections;
 29  
 import java.util.Comparator;
 30  
 import java.util.Date;
 31  
 import java.util.HashMap;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  
 
 36  
 /**
 37  
  * Various static utility methods.
 38  
  *
 39  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 40  
  */
 41  
 public final class Utilities {
 42  
     /**
 43  
      * Commons-Lang StrSubstitor which substitutes variables specified like ${name} in strings,
 44  
      * using a lookup implementation that pulls variables from the core config
 45  
      */
 46  0
     private static final StrSubstitutor SUBSTITUTOR = new StrSubstitutor(new ConfigStringLookup());
 47  
     
 48  0
     private Utilities() {
 49  0
             throw new UnsupportedOperationException("do not call");
 50  
     }
 51  
 
 52  
     /**
 53  
      * Performs variable substitution on the specified string, replacing variables specified like ${name}
 54  
      * with the value of the corresponding config parameter obtained from the current context Config object.
 55  
      * This version of the method also takes an application id to qualify the parameter.
 56  
      * @param applicationId the application id to use for qualifying the parameter
 57  
      * @param string the string on which to perform variable substitution
 58  
      * @return a string with any variables substituted with configuration parameter values
 59  
      */
 60  
     public static String substituteConfigParameters(String applicationId, String string) {
 61  0
             StrSubstitutor sub = new StrSubstitutor(new ConfigStringLookup(applicationId));
 62  0
         return sub.replace(string);
 63  
     }
 64  
         
 65  
     /**
 66  
      * Performs variable substitution on the specified string, replacing variables specified like ${name}
 67  
      * with the value of the corresponding config parameter obtained from the current context Config object
 68  
      * @param string the string on which to perform variable substitution
 69  
      * @return a string with any variables substituted with configuration parameter values
 70  
      */
 71  
     public static String substituteConfigParameters(String string) {
 72  0
         return SUBSTITUTOR.replace(string);
 73  
     }
 74  
 
 75  
     public static String parseGroupNamespaceCode(String namespaceAndNameCombo) {
 76  0
         if (namespaceAndNameCombo == null) {
 77  0
             return null;
 78  
         }
 79  0
         String[] groupData = namespaceAndNameCombo.split(KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER);
 80  0
         if (groupData.length == 1) {
 81  0
             return KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE;
 82  0
         } else if (groupData.length == 2) {
 83  0
             return groupData[0].trim();
 84  
         } else {
 85  0
             return null;
 86  
         }
 87  
     }
 88  
 
 89  
     public static String parseGroupName(String namespaceAndNameCombo) {
 90  0
         if (namespaceAndNameCombo == null) {
 91  0
             return null;
 92  
         }
 93  0
         String[] groupData = namespaceAndNameCombo.split(KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER);
 94  0
         if (groupData.length == 1) {
 95  0
             return groupData[0].trim();
 96  0
         } else if (groupData.length == 2) {
 97  0
             return groupData[1].trim();
 98  
         } else {
 99  0
             return null;
 100  
         }
 101  
     }
 102  
 
 103  
     /**
 104  
      *
 105  
      *        Consider moving out of this class if this bugs
 106  
      */
 107  0
     public static class PrioritySorter implements Comparator<ActionRequestValue> {
 108  
         @Override
 109  
                 public int compare(ActionRequestValue ar1, ActionRequestValue ar2) {
 110  0
             int value = ar1.getPriority().compareTo(ar2.getPriority());
 111  0
             if (value == 0) {
 112  0
                 value = ActionRequestValue.compareActionCode(ar1.getActionRequested(), ar2.getActionRequested(), true);
 113  0
                 if (value == 0) {
 114  0
                     if ( (ar1.getActionRequestId() != null) && (ar2.getActionRequestId() != null) ) {
 115  0
                         value = ar1.getActionRequestId().compareTo(ar2.getActionRequestId());
 116  
                     } else {
 117  
                         // if even one action request id is null at this point return that the two are equal
 118  0
                         value = 0;
 119  
                     }
 120  
                 }
 121  
             }
 122  0
             return value;
 123  
         }
 124  
     }
 125  
 
 126  
     /**
 127  
      *
 128  
      *        Consider moving out of this class if this bugs
 129  
      */
 130  0
     public static class RouteLogActionRequestSorter extends PrioritySorter implements Comparator<ActionRequestValue> {
 131  
         @Override
 132  
                 public int compare(ActionRequestValue ar1, ActionRequestValue ar2) {
 133  0
             if (! ar1.getChildrenRequests().isEmpty()) {
 134  0
                 Collections.sort(ar1.getChildrenRequests(), this);
 135  
             }
 136  0
             if (! ar2.getChildrenRequests().isEmpty()) {
 137  0
                 Collections.sort(ar2.getChildrenRequests(), this);
 138  
             }
 139  
 
 140  0
             int routeLevelCompareVal = ar1.getRouteLevel().compareTo(ar2.getRouteLevel());
 141  0
             if (routeLevelCompareVal != 0) {
 142  0
                 return routeLevelCompareVal;
 143  
             }
 144  
 
 145  0
             if (ar1.isActive() && ar2.isPending()) {
 146  0
                 return -1;
 147  0
             } else if (ar2.isActive() && ar1.isPending()) {
 148  0
                 return 1;
 149  
             }
 150  
 
 151  0
             return super.compare(ar1, ar2);
 152  
         }
 153  
     }
 154  
 
 155  
     public static boolean checkDateRanges(String fromDate, String toDate) {
 156  
         try {
 157  0
             Date parsedDate = CoreApiServiceLocator.getDateTimeService().convertToDate(fromDate.trim());
 158  0
             Calendar fromCalendar = Calendar.getInstance();
 159  0
             fromCalendar.setLenient(false);
 160  0
             fromCalendar.setTime(parsedDate);
 161  0
             fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
 162  0
             fromCalendar.set(Calendar.MINUTE, 0);
 163  0
             fromCalendar.set(Calendar.SECOND, 0);
 164  0
             fromCalendar.set(Calendar.MILLISECOND, 0);
 165  0
             parsedDate = CoreApiServiceLocator.getDateTimeService().convertToDate(toDate.trim());
 166  0
             Calendar toCalendar = Calendar.getInstance();
 167  0
             toCalendar.setLenient(false);
 168  0
             toCalendar.setTime(parsedDate);
 169  0
             toCalendar.set(Calendar.HOUR_OF_DAY, 0);
 170  0
             toCalendar.set(Calendar.MINUTE, 0);
 171  0
             toCalendar.set(Calendar.SECOND, 0);
 172  0
             toCalendar.set(Calendar.MILLISECOND, 0);
 173  0
             if (fromCalendar.after(toCalendar)) {
 174  0
                 return false;
 175  
             }
 176  0
             return true;
 177  0
         } catch (Exception ex) {
 178  0
             return false;
 179  
         }
 180  
     }
 181  
 
 182  
     public static String getIpNumber() {
 183  
         try {
 184  0
             return InetAddress.getLocalHost().getHostAddress();
 185  0
         } catch (UnknownHostException e) {
 186  0
             throw new WorkflowRuntimeException("Error retrieving ip number.", e);
 187  
         }
 188  
     }
 189  
 
 190  
     /**
 191  
      * Helper method that takes a List of {@link KeyValue} and presents it as a Map
 192  
      * @param collection collection of {@link KeyValue}
 193  
      * @return a Map<String, String> representing the keys and values in the KeyValue collection
 194  
      */
 195  
     public static <T  extends KeyValue> Map<String, String> getKeyValueCollectionAsMap(List<T> collection) {
 196  0
         Map<String, String> map = new HashMap<String, String>(collection.size());
 197  0
         for (KeyValue kv: collection) {
 198  0
             map.put(kv.getKey(), kv.getValue());
 199  
         }
 200  0
         return map;
 201  
     }
 202  
 
 203  
     /**
 204  
      * Helper method that takes a List of {@link KeyValue} and presents it as a Map containing
 205  
      * KeyValue values
 206  
      * @param <T> the key type
 207  
      * @param collection collection of {@link KeyValue}
 208  
      * @return a Map<T,Z> where keys of the KeyValues in the collection are mapped to their respective KeyValue object
 209  
      */
 210  
     public static <T  extends KeyValue> Map<String, T> getKeyValueCollectionAsLookupTable(List<T> collection) {
 211  0
         Map<String, T> map = new HashMap<String, T>(collection.size());
 212  0
         for (T kv: collection) {
 213  0
             map.put(kv.getKey(), kv);
 214  
         }
 215  0
         return map;
 216  
     }
 217  
 }