View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.time.detail.web;
17  
18  import java.sql.Date;
19  import java.text.SimpleDateFormat;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.LinkedHashMap;
25  import java.util.LinkedList;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.commons.collections.CollectionUtils;
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.commons.lang.time.DateUtils;
33  import org.joda.time.DateTime;
34  import org.joda.time.format.ISODateTimeFormat;
35  import org.json.simple.JSONValue;
36  import org.kuali.hr.time.accrual.AccrualCategory;
37  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
38  import org.kuali.hr.time.calendar.CalendarEntries;
39  import org.kuali.hr.time.earncode.EarnCode;
40  import org.kuali.hr.time.roles.TkUserRoles;
41  import org.kuali.hr.time.service.base.TkServiceLocator;
42  import org.kuali.hr.time.timeblock.TimeBlock;
43  import org.kuali.hr.time.timeblock.TimeHourDetail;
44  import org.kuali.hr.time.util.TKUser;
45  import org.kuali.hr.time.util.TKUtils;
46  import org.kuali.hr.time.util.TkConstants;
47  import org.kuali.hr.time.workarea.WorkArea;
48  import org.kuali.rice.krad.util.GlobalVariables;
49  
50  public class ActionFormUtils {
51  
52      public static void validateHourLimit(TimeDetailActionFormBase tdaf) throws Exception {
53          List<String> warningMessages = TkServiceLocator.getTimeOffAccrualService().validateAccrualHoursLimit(tdaf.getTimesheetDocument());
54          addUniqueWarningsToForm(tdaf, warningMessages);
55      }
56  
57      public static void addWarningTextFromEarnGroup(TimeDetailActionFormBase tdaf) throws Exception {
58          List<String> warningMessages = TkServiceLocator.getEarnGroupService().warningTextFromEarnGroupsOfDocument(tdaf.getTimesheetDocument());
59          addUniqueWarningsToForm(tdaf, warningMessages);
60      }
61  
62      public static void addUnapprovedIPWarningFromClockLog(TimeDetailActionFormBase tdaf) {
63      	List<String> warningMessages = new ArrayList<String>();
64      	Set<String> aSet = new HashSet<String>();
65      	if(tdaf.getTimesheetDocument() != null) {
66  	    	List<TimeBlock> tbList = tdaf.getTimesheetDocument().getTimeBlocks();
67  	    	if(CollectionUtils.isNotEmpty(tbList)) {
68  		    	 aSet.addAll(TkServiceLocator.getClockLogService().getUnapprovedIPWarning(tbList));
69  		        
70  	    	}
71      	}
72      	warningMessages.addAll(aSet);
73      	addUniqueWarningsToForm(tdaf, warningMessages);
74      }
75      
76      public static void addUniqueWarningsToForm(TimeDetailActionFormBase tdaf, List<String> warningMessages) {
77          if (!warningMessages.isEmpty()) {
78              Set<String> aSet = new HashSet<String>();
79              aSet.addAll(warningMessages);
80              aSet.addAll(tdaf.getWarnings());
81              List<String> aList = new ArrayList<String>();
82              aList.addAll(aSet);
83              tdaf.setWarnings(aList);
84          }
85      }
86  
87  //    public static String getTimeBlockJSONMap(List<TimeBlock> blocks) {
88  //        List<Map<String, Object>> jsonList = getTimeBlocksJson(blocks, null);
89  //        Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>();
90  //        for (Map<String, Object> tbm : jsonList) {
91  //            String id = (String) tbm.get("id");
92  //            jsonMappedList.put(id, tbm);
93  //        }
94  //        return JSONValue.toJSONString(jsonMappedList);
95  //    }
96  
97      public static Map<String, String> buildAssignmentStyleClassMap(List<TimeBlock> timeBlocks) {
98          Map<String, String> aMap = new HashMap<String, String>();
99          List<String> assignmentKeys = new ArrayList<String>();
100 
101         for (TimeBlock tb : timeBlocks) {
102             if (!assignmentKeys.contains(tb.getAssignmentKey())) {
103                 assignmentKeys.add(tb.getAssignmentKey());
104             }
105         }
106 
107         Collections.sort(assignmentKeys);
108 
109         for (int i = 0; i < assignmentKeys.size(); i++) {
110             // pick a color from a five color palette
111             aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 5));
112         }
113 
114         return aMap;
115     }
116 
117     /**
118      * This method will build the JSON data structure needed for calendar
119      * manipulation and processing on the client side. Start and End times here
120      * are based on the pre-timezone adjusted times startDisplayTime, and
121      * endDisplayTime.
122      *
123      * @param timeBlocks
124      * @return
125      */
126     public static String getTimeBlocksJson(List<TimeBlock> timeBlocks) {
127 
128         if (timeBlocks == null || timeBlocks.size() == 0) {
129             return "";
130         }
131 
132         List<Map<String, Object>> timeBlockList = new LinkedList<Map<String, Object>>();
133         String timezone = TkServiceLocator.getTimezoneService().getUserTimezone();
134 
135         for (TimeBlock timeBlock : timeBlocks) {
136             Map<String, Object> timeBlockMap = new LinkedHashMap<String, Object>();
137 
138             WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(timeBlock.getWorkArea(), new java.sql.Date(timeBlock.getEndTimestamp().getTime()));
139             String workAreaDesc = workArea.getDescription();
140 
141             // Roles
142             Boolean isAnyApprover = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isAnyApproverActive();
143             timeBlockMap.put("isApprover", isAnyApprover);
144             timeBlockMap.put("isSynchronousUser", timeBlock.getClockLogCreated());
145 
146             // Permissions
147             timeBlockMap.put("canEditTb", TkServiceLocator.getPermissionsService().canEditTimeBlock(timeBlock));
148             timeBlockMap.put("canEditTBOvt", TkServiceLocator.getPermissionsService().canEditOvertimeEarnCode(timeBlock));
149             timeBlockMap.put("canAddTB", TkServiceLocator.getPermissionsService().canAddTimeBlock());
150 
151             if (TkServiceLocator.getPermissionsService().canEditTimeBlockAllFields(timeBlock)) {
152                 timeBlockMap.put("canEditTBAll", true);
153                 timeBlockMap.put("canEditTBAssgOnly", false);
154             } else {
155                 timeBlockMap.put("canEditTBAll", false);
156                 timeBlockMap.put("canEditTBAssgOnly", true);
157             }
158 
159             //    tracking any kind of 'mutating' state with this object, it's just a one off modification under a specific circumstance.
160             DateTime start = timeBlock.getBeginTimeDisplay();
161             DateTime end = timeBlock.getEndTimeDisplay();
162 
163             /**
164              * This is the timeblock backward pushing logic.
165              * the purpose of this is to accommodate the virtual day mode where the start/end period time is not from 12a to 12a.
166              * A timeblock will be pushed back if the timeblock is still within the previous interval
167              */
168             if (timeBlock.isPushBackward()) {
169                 start = start.minusDays(1);
170                 end = end.minusDays(1);
171             }
172 
173             timeBlockMap.put("documentId", timeBlock.getDocumentId());
174             timeBlockMap.put("title", workAreaDesc);
175             timeBlockMap.put("earnCode", timeBlock.getEarnCode());
176             timeBlockMap.put("earnCodeDesc", TkServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), TKUtils.getCurrentDate()).getDescription());
177             //TODO: need to cache this or pre-load it when the app boots up
178             // EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime()));
179             timeBlockMap.put("earnCodeType", timeBlock.getEarnCodeType());
180 
181             // TODO: Cleanup the start / end time related properties. We certainly don't need all of them.
182             // The ones which are used by the javascript are startDate, endDate, startTime, endTime, startTimeHourMinute, and endTimeHourMinute
183             timeBlockMap.put("start", start.toString(ISODateTimeFormat.dateTimeNoMillis()));
184             timeBlockMap.put("end", end.toString(ISODateTimeFormat.dateTimeNoMillis()));
185             timeBlockMap.put("startDate", start.toString(TkConstants.DT_BASIC_DATE_FORMAT));
186             timeBlockMap.put("endDate", end.toString(TkConstants.DT_BASIC_DATE_FORMAT));
187             timeBlockMap.put("startNoTz", start.toString(ISODateTimeFormat.dateHourMinuteSecond()));
188             timeBlockMap.put("endNoTz", end.toString(ISODateTimeFormat.dateHourMinuteSecond()));
189             // start / endTimeHourMinute fields are for only for the display purpose
190             timeBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
191             timeBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
192             // start / endTime are the actual fields used by the adding / editing timeblocks
193             timeBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
194             timeBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
195             timeBlockMap.put("id", timeBlock.getTkTimeBlockId() == null ? null : timeBlock.getTkTimeBlockId().toString());
196             timeBlockMap.put("hours", timeBlock.getHours());
197             timeBlockMap.put("amount", timeBlock.getAmount());
198             timeBlockMap.put("timezone", timezone);
199             timeBlockMap.put("assignment", new AssignmentDescriptionKey(timeBlock.getJobNumber(), timeBlock.getWorkArea(), timeBlock.getTask()).toAssignmentKeyString());
200             timeBlockMap.put("tkTimeBlockId", timeBlock.getTkTimeBlockId() != null ? timeBlock.getTkTimeBlockId() : "");
201             timeBlockMap.put("lunchDeleted", timeBlock.isLunchDeleted());
202 
203             List<Map<String, Object>> timeHourDetailList = new LinkedList<Map<String, Object>>();
204             for (TimeHourDetail timeHourDetail : timeBlock.getTimeHourDetails()) {
205                 Map<String, Object> timeHourDetailMap = new LinkedHashMap<String, Object>();
206                 timeHourDetailMap.put("earnCode", timeHourDetail.getEarnCode());
207                 timeHourDetailMap.put("hours", timeHourDetail.getHours());
208                 timeHourDetailMap.put("amount", timeHourDetail.getAmount());
209 
210                 // if there is a lunch hour deduction, add a flag to the timeBlockMap
211                 if (StringUtils.equals(timeHourDetail.getEarnCode(), "LUN")) {
212                     timeBlockMap.put("lunchDeduction", true);
213                 }
214 
215                 timeHourDetailList.add(timeHourDetailMap);
216             }
217             timeBlockMap.put("timeHourDetails", JSONValue.toJSONString(timeHourDetailList));
218 
219             timeBlockList.add(timeBlockMap);
220         }
221 
222 //        Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>();
223 //        for (Map<String, Object> tbm : timeBlockList) {
224 //            String id = (String) tbm.get("id");
225 //            jsonMappedList.put(id, tbm);
226 //        }
227         return JSONValue.toJSONString(timeBlockList);
228     }
229 
230     public static Map<String, String> getPayPeriodsMap(List<CalendarEntries> payPeriods) {
231     	// use linked map to keep the order of the pay periods
232     	Map<String, String> pMap = Collections.synchronizedMap(new LinkedHashMap<String, String>());
233     	if (payPeriods == null || payPeriods.isEmpty()) {
234             return pMap;
235         }
236     	payPeriods.removeAll(Collections.singletonList(null));
237     	Collections.sort(payPeriods);  // sort the pay period list by getBeginPeriodDate
238     	Collections.reverse(payPeriods); 	// newest on top
239     	SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
240         for (CalendarEntries pce : payPeriods) {
241         	if(pce != null && pce.getHrCalendarEntriesId()!= null && pce.getBeginPeriodDate() != null && pce.getEndPeriodDate() != null) {
242         		//pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format(pce.getEndPeriodDate()));
243                 //getting one millisecond of the endperioddate to match the actual pay period. i.e. pay period end at the 11:59:59:59...PM of that day
244                 pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));
245         	}
246         }
247         
248     	return pMap;
249     }
250     
251     // detect if the passed-in calendar entry is the current one
252     public static boolean getOnCurrentPeriodFlag(CalendarEntries pce) {
253     	Date currentDate = TKUtils.getTimelessDate(null);
254     	String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
255         CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal,  currentDate);
256 
257         if(pce != null && calendarEntry != null && calendarEntry.equals(pce)) {
258     		return true;
259     	}
260     	return false;
261     }
262     
263     public static String getUnitOfTimeForEarnCode(EarnCode earnCode) {
264     	AccrualCategory acObj = null;
265     	if(earnCode.getAccrualCategory() != null) {
266     		acObj = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(earnCode.getAccrualCategory(), TKUtils.getCurrentDate());
267     	}
268     	String unitTime = (acObj!= null ? acObj.getUnitOfTime() : earnCode.getRecordMethod()) ;
269     	return unitTime;
270     }
271     
272 }
273