001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.time.detail.web; 017 018 import java.sql.Date; 019 import java.text.DateFormat; 020 import java.text.SimpleDateFormat; 021 import java.util.ArrayList; 022 import java.util.Collections; 023 import java.util.HashMap; 024 import java.util.HashSet; 025 import java.util.LinkedHashMap; 026 import java.util.LinkedList; 027 import java.util.List; 028 import java.util.Map; 029 import java.util.Set; 030 031 import org.apache.commons.collections.CollectionUtils; 032 import org.apache.commons.lang.StringUtils; 033 import org.apache.commons.lang.time.DateUtils; 034 import org.joda.time.DateTime; 035 import org.joda.time.format.ISODateTimeFormat; 036 import org.json.simple.JSONValue; 037 import org.kuali.hr.lm.accrual.AccrualCategory; 038 import org.kuali.hr.lm.leaveblock.LeaveBlock; 039 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil; 040 import org.kuali.hr.lm.leaveplan.LeavePlan; 041 import org.kuali.hr.time.assignment.AssignmentDescriptionKey; 042 import org.kuali.hr.time.calendar.CalendarEntries; 043 import org.kuali.hr.time.earncode.EarnCode; 044 import org.kuali.hr.time.earncodegroup.EarnCodeGroup; 045 import org.kuali.hr.time.principal.PrincipalHRAttributes; 046 import org.kuali.hr.time.roles.TkUserRoles; 047 import org.kuali.hr.time.service.base.TkServiceLocator; 048 import org.kuali.hr.time.timeblock.TimeBlock; 049 import org.kuali.hr.time.timeblock.TimeHourDetail; 050 import org.kuali.hr.time.util.TKUser; 051 import org.kuali.hr.time.util.TKUtils; 052 import org.kuali.hr.time.util.TkConstants; 053 import org.kuali.hr.time.workarea.WorkArea; 054 import org.kuali.rice.krad.util.GlobalVariables; 055 056 public class ActionFormUtils { 057 058 public static void validateHourLimit(TimeDetailActionFormBase tdaf) throws Exception { 059 List<String> warningMessages = TkServiceLocator.getTimeOffAccrualService().validateAccrualHoursLimit(tdaf.getTimesheetDocument()); 060 addUniqueWarningsToForm(tdaf, warningMessages); 061 } 062 063 public static void addWarningTextFromEarnGroup(TimeDetailActionFormBase tdaf) throws Exception { 064 List<String> warningMessages = TkServiceLocator.getEarnCodeGroupService().warningTextFromEarnCodeGroupsOfDocument(tdaf.getTimesheetDocument()); 065 addUniqueWarningsToForm(tdaf, warningMessages); 066 } 067 068 public static void addUnapprovedIPWarningFromClockLog(TimeDetailActionFormBase tdaf) { 069 List<String> warningMessages = new ArrayList<String>(); 070 Set<String> aSet = new HashSet<String>(); 071 if(tdaf.getTimesheetDocument() != null) { 072 List<TimeBlock> tbList = tdaf.getTimesheetDocument().getTimeBlocks(); 073 if(CollectionUtils.isNotEmpty(tbList)) { 074 aSet.addAll(TkServiceLocator.getClockLogService().getUnapprovedIPWarning(tbList)); 075 076 } 077 } 078 warningMessages.addAll(aSet); 079 addUniqueWarningsToForm(tdaf, warningMessages); 080 } 081 082 public static void addUniqueWarningsToForm(TimeDetailActionFormBase tdaf, List<String> warningMessages) { 083 if (!warningMessages.isEmpty()) { 084 Set<String> aSet = new HashSet<String>(); 085 aSet.addAll(warningMessages); 086 aSet.addAll(tdaf.getWarningMessages()); //Only warnings. TODO: Do we need actions and info messages here? 087 088 List<String> aList = new ArrayList<String>(); 089 aList.addAll(aSet); 090 tdaf.setWarningMessages(aList); 091 } 092 } 093 094 // public static String getTimeBlockJSONMap(List<TimeBlock> blocks) { 095 // List<Map<String, Object>> jsonList = getTimeBlocksJson(blocks, null); 096 // Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>(); 097 // for (Map<String, Object> tbm : jsonList) { 098 // String id = (String) tbm.get("id"); 099 // jsonMappedList.put(id, tbm); 100 // } 101 // return JSONValue.toJSONString(jsonMappedList); 102 // } 103 104 public static Map<String, String> buildAssignmentStyleClassMap(List<TimeBlock> timeBlocks, List<LeaveBlock> leaveBlocks) { 105 Map<String, String> aMap = new HashMap<String, String>(); 106 List<String> assignmentKeys = new ArrayList<String>(); 107 108 for (TimeBlock tb : timeBlocks) { 109 if (!assignmentKeys.contains(tb.getAssignmentKey())) { 110 assignmentKeys.add(tb.getAssignmentKey()); 111 } 112 } 113 for(LeaveBlock lb : leaveBlocks) { 114 if (!assignmentKeys.contains(lb.getAssignmentKey())) { 115 assignmentKeys.add(lb.getAssignmentKey()); 116 } 117 } 118 Collections.sort(assignmentKeys); 119 120 for (int i = 0; i < assignmentKeys.size(); i++) { 121 // pick a color from a five color palette 122 aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 5)); 123 } 124 125 return aMap; 126 } 127 128 public static Map<String, String> buildAssignmentStyleClassMap(List<TimeBlock> timeBlocks) { 129 Map<String, String> aMap = new HashMap<String, String>(); 130 List<String> assignmentKeys = new ArrayList<String>(); 131 132 for (TimeBlock tb : timeBlocks) { 133 if (!assignmentKeys.contains(tb.getAssignmentKey())) { 134 assignmentKeys.add(tb.getAssignmentKey()); 135 } 136 } 137 138 Collections.sort(assignmentKeys); 139 140 for (int i = 0; i < assignmentKeys.size(); i++) { 141 // pick a color from a five color palette 142 aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 5)); 143 } 144 145 return aMap; 146 } 147 148 /** 149 * This method will build the JSON data structure needed for calendar 150 * manipulation and processing on the client side. Start and End times here 151 * are based on the pre-timezone adjusted times startDisplayTime, and 152 * endDisplayTime. 153 * 154 * @param timeBlocks 155 * @return 156 */ 157 public static String getTimeBlocksJson(List<TimeBlock> timeBlocks) { 158 159 if (timeBlocks == null || timeBlocks.size() == 0) { 160 return ""; 161 } 162 163 List<Map<String, Object>> timeBlockList = new LinkedList<Map<String, Object>>(); 164 String timezone = TkServiceLocator.getTimezoneService().getUserTimezone(); 165 166 for (TimeBlock timeBlock : timeBlocks) { 167 Map<String, Object> timeBlockMap = new LinkedHashMap<String, Object>(); 168 169 WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(timeBlock.getWorkArea(), new java.sql.Date(timeBlock.getEndTimestamp().getTime())); 170 String workAreaDesc = workArea.getDescription(); 171 172 // Roles 173 Boolean isAnyApprover = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isAnyApproverActive(); 174 timeBlockMap.put("isApprover", isAnyApprover); 175 timeBlockMap.put("isSynchronousUser", timeBlock.getClockLogCreated()); 176 177 // Permissions 178 timeBlockMap.put("canEditTb", TkServiceLocator.getPermissionsService().canEditTimeBlock(timeBlock)); 179 timeBlockMap.put("canEditTBOvt", TkServiceLocator.getPermissionsService().canEditOvertimeEarnCode(timeBlock)); 180 timeBlockMap.put("canAddTB", TkServiceLocator.getPermissionsService().canAddTimeBlock()); 181 182 if (TkServiceLocator.getPermissionsService().canEditTimeBlockAllFields(timeBlock)) { 183 timeBlockMap.put("canEditTBAll", true); 184 timeBlockMap.put("canEditTBAssgOnly", false); 185 } else { 186 timeBlockMap.put("canEditTBAll", false); 187 timeBlockMap.put("canEditTBAssgOnly", true); 188 } 189 190 // tracking any kind of 'mutating' state with this object, it's just a one off modification under a specific circumstance. 191 DateTime start = timeBlock.getBeginTimeDisplay(); 192 DateTime end = timeBlock.getEndTimeDisplay(); 193 194 /** 195 * This is the timeblock backward pushing logic. 196 * the purpose of this is to accommodate the virtual day mode where the start/end period time is not from 12a to 12a. 197 * A timeblock will be pushed back if the timeblock is still within the previous interval 198 */ 199 if (timeBlock.isPushBackward()) { 200 start = start.minusDays(1); 201 end = end.minusDays(1); 202 } 203 204 timeBlockMap.put("documentId", timeBlock.getDocumentId()); 205 timeBlockMap.put("title", workAreaDesc); 206 timeBlockMap.put("earnCode", timeBlock.getEarnCode()); 207 timeBlockMap.put("earnCodeDesc", TkServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), TKUtils.getCurrentDate()).getDescription()); 208 //TODO: need to cache this or pre-load it when the app boots up 209 // EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime())); 210 timeBlockMap.put("earnCodeType", timeBlock.getEarnCodeType()); 211 212 // TODO: Cleanup the start / end time related properties. We certainly don't need all of them. 213 // The ones which are used by the javascript are startDate, endDate, startTime, endTime, startTimeHourMinute, and endTimeHourMinute 214 timeBlockMap.put("start", start.toString(ISODateTimeFormat.dateTimeNoMillis())); 215 timeBlockMap.put("end", end.toString(ISODateTimeFormat.dateTimeNoMillis())); 216 timeBlockMap.put("startDate", start.toString(TkConstants.DT_BASIC_DATE_FORMAT)); 217 timeBlockMap.put("endDate", end.toString(TkConstants.DT_BASIC_DATE_FORMAT)); 218 timeBlockMap.put("startNoTz", start.toString(ISODateTimeFormat.dateHourMinuteSecond())); 219 timeBlockMap.put("endNoTz", end.toString(ISODateTimeFormat.dateHourMinuteSecond())); 220 // start / endTimeHourMinute fields are for only for the display purpose 221 timeBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT)); 222 timeBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT)); 223 // start / endTime are the actual fields used by the adding / editing timeblocks 224 timeBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT)); 225 timeBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT)); 226 timeBlockMap.put("id", timeBlock.getTkTimeBlockId() == null ? null : timeBlock.getTkTimeBlockId().toString()); 227 timeBlockMap.put("hours", timeBlock.getHours()); 228 timeBlockMap.put("amount", timeBlock.getAmount()); 229 timeBlockMap.put("timezone", timezone); 230 timeBlockMap.put("assignment", new AssignmentDescriptionKey(timeBlock.getJobNumber(), timeBlock.getWorkArea(), timeBlock.getTask()).toAssignmentKeyString()); 231 timeBlockMap.put("tkTimeBlockId", timeBlock.getTkTimeBlockId() != null ? timeBlock.getTkTimeBlockId() : ""); 232 timeBlockMap.put("lunchDeleted", timeBlock.isLunchDeleted()); 233 234 List<Map<String, Object>> timeHourDetailList = new LinkedList<Map<String, Object>>(); 235 for (TimeHourDetail timeHourDetail : timeBlock.getTimeHourDetails()) { 236 Map<String, Object> timeHourDetailMap = new LinkedHashMap<String, Object>(); 237 timeHourDetailMap.put("earnCode", timeHourDetail.getEarnCode()); 238 timeHourDetailMap.put("hours", timeHourDetail.getHours()); 239 timeHourDetailMap.put("amount", timeHourDetail.getAmount()); 240 241 // if there is a lunch hour deduction, add a flag to the timeBlockMap 242 if (StringUtils.equals(timeHourDetail.getEarnCode(), "LUN")) { 243 timeBlockMap.put("lunchDeduction", true); 244 } 245 246 timeHourDetailList.add(timeHourDetailMap); 247 } 248 timeBlockMap.put("timeHourDetails", JSONValue.toJSONString(timeHourDetailList)); 249 250 timeBlockList.add(timeBlockMap); 251 } 252 253 // Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>(); 254 // for (Map<String, Object> tbm : timeBlockList) { 255 // String id = (String) tbm.get("id"); 256 // jsonMappedList.put(id, tbm); 257 // } 258 return JSONValue.toJSONString(timeBlockList); 259 } 260 261 262 /** 263 * This method will build the leave blocks JSON data structure needed for calendar 264 * manipulation and processing on the client side. 265 * 266 * @param leaveBlocks 267 * @return 268 */ 269 public static String getLeaveBlocksJson(List<LeaveBlock> leaveBlocks) { 270 if (CollectionUtils.isEmpty(leaveBlocks)) { 271 return ""; 272 } 273 List<Map<String, Object>> leaveBlockList = new LinkedList<Map<String, Object>>(); 274 for (LeaveBlock leaveBlock : leaveBlocks) { 275 Map<String, Object> leaveBlockMap = new LinkedHashMap<String, Object>(); 276 leaveBlockMap.put("title", leaveBlock.getAssignmentTitle()); 277 leaveBlockMap.put("assignment", leaveBlock.getAssignmentKey()); 278 leaveBlockMap.put("earnCode", leaveBlock.getEarnCode()); 279 leaveBlockMap.put("lmLeaveBlockId", leaveBlock.getLmLeaveBlockId()); 280 leaveBlockMap.put("leaveAmount", leaveBlock.getLeaveAmount().toString()); 281 DateTime leaveDate = new DateTime(leaveBlock.getLeaveDate()); 282 leaveBlockMap.put("leaveDate", leaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT)); 283 leaveBlockMap.put("id", leaveBlock.getLmLeaveBlockId()); 284 leaveBlockMap.put("canTransfer", TkServiceLocator.getPermissionsService().canTransferSSTOUsage(leaveBlock)); 285 286 287 leaveBlockList.add(leaveBlockMap); 288 } 289 return JSONValue.toJSONString(leaveBlockList); 290 } 291 292 public static Map<String, String> getPayPeriodsMap(List<CalendarEntries> payPeriods) { 293 // use linked map to keep the order of the pay periods 294 Map<String, String> pMap = Collections.synchronizedMap(new LinkedHashMap<String, String>()); 295 if (payPeriods == null || payPeriods.isEmpty()) { 296 return pMap; 297 } 298 payPeriods.removeAll(Collections.singletonList(null)); 299 Collections.sort(payPeriods); // sort the pay period list by getBeginPeriodDate 300 Collections.reverse(payPeriods); // newest on top 301 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); 302 for (CalendarEntries pce : payPeriods) { 303 if(pce != null && pce.getHrCalendarEntriesId()!= null && pce.getBeginPeriodDate() != null && pce.getEndPeriodDate() != null) { 304 //pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format(pce.getEndPeriodDate())); 305 //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 306 pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1)))); 307 } 308 } 309 310 return pMap; 311 } 312 313 // detect if the passed-in calendar entry is the current one 314 public static boolean getOnCurrentPeriodFlag(CalendarEntries pce) { 315 Date currentDate = TKUtils.getTimelessDate(null); 316 String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId(); 317 CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal, currentDate); 318 319 if(pce != null && calendarEntry != null && calendarEntry.equals(pce)) { 320 return true; 321 } 322 return false; 323 } 324 325 public static String getUnitOfTimeForEarnCode(EarnCode earnCode) { 326 AccrualCategory acObj = null; 327 if(earnCode.getAccrualCategory() != null) { 328 acObj = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(earnCode.getAccrualCategory(), TKUtils.getCurrentDate()); 329 } 330 String unitTime = (acObj!= null ? acObj.getUnitOfTime() : earnCode.getRecordMethod()) ; 331 return unitTime; 332 } 333 334 public static int getPlanningMonthsForEmployee(String principalid) { 335 int plannningMonths = 0; 336 PrincipalHRAttributes principalHRAttributes = TkServiceLocator 337 .getPrincipalHRAttributeService().getPrincipalCalendar( 338 principalid, TKUtils.getCurrentDate()); 339 if (principalHRAttributes != null 340 && principalHRAttributes.getLeavePlan() != null) { 341 LeavePlan lp = TkServiceLocator.getLeavePlanService() 342 .getLeavePlan(principalHRAttributes.getLeavePlan(), 343 TKUtils.getCurrentDate()); 344 if (lp != null && lp.getPlanningMonths() != null) { 345 plannningMonths = Integer.parseInt(lp.getPlanningMonths()); 346 } 347 } 348 return plannningMonths; 349 } 350 351 public static List<CalendarEntries> getAllCalendarEntriesForYear(List<CalendarEntries> entries, String year) { 352 List<CalendarEntries> results = new ArrayList<CalendarEntries>(); 353 DateFormat df = new SimpleDateFormat("yyyy"); 354 355 for(CalendarEntries ce : entries) { 356 if(df.format(ce.getBeginPeriodDate()).equals(year)) { 357 results.add(ce); 358 } 359 } 360 return results; 361 } 362 363 } 364