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