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