View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.core.api.util;
17  
18  
19  import org.apache.commons.collections.MapUtils;
20  import org.apache.commons.lang.ObjectUtils;
21  import org.apache.commons.lang.StringUtils;
22  import org.joda.time.LocalDate;
23  import org.kuali.kpme.core.api.assignment.Assignment;
24  
25  import java.util.*;
26  
27  public class KpmeUtils {
28      public static String formatAssignmentKey(String groupKeyCode, Long jobNumber, Long workArea, Long task) {
29          String assignmentKey = StringUtils.EMPTY;
30  
31          String jobNumberString = ObjectUtils.toString(jobNumber, "0");
32          String workAreaString = ObjectUtils.toString(workArea, "0");
33          String taskString = ObjectUtils.toString(task, "0");
34  
35          if (!jobNumberString.equals("0") || !workAreaString.equals("0") || !taskString.equals("0")) {
36              assignmentKey = StringUtils.join(new String[] {groupKeyCode, jobNumberString, workAreaString, taskString}, HrApiConstants.ASSIGNMENT_KEY_DELIMITER);
37          }
38  
39          return assignmentKey;
40      }
41  
42      public static List<Assignment> getUniqueAssignments(Map<LocalDate, List<Assignment>> history) {
43          if (MapUtils.isEmpty(history)) {
44              return Collections.emptyList();
45          }
46          Set<Assignment> allAssignments = new HashSet<Assignment>();
47          for (List<Assignment> assignList : history.values()) {
48              allAssignments.addAll(assignList);
49          }
50          return new ArrayList<Assignment>(allAssignments);
51      }
52  
53      public static <T extends Comparable<? super T>> int nullSafeCompare(T s1, T s2) {
54          if(s1 == null && s2 != null) { return -1;}
55          if(s1 != null && s2 == null) { return 1;}
56          if(s1 == null) { return 0;}
57          return s1.compareTo(s2);
58      }
59  }