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.SimpleDateFormat;
020 import java.util.ArrayList;
021 import java.util.Collections;
022 import java.util.HashMap;
023 import java.util.HashSet;
024 import java.util.LinkedHashMap;
025 import java.util.LinkedList;
026 import java.util.List;
027 import java.util.Map;
028 import java.util.Set;
029
030 import org.apache.commons.collections.CollectionUtils;
031 import org.apache.commons.lang.StringUtils;
032 import org.apache.commons.lang.time.DateUtils;
033 import org.joda.time.DateTime;
034 import org.joda.time.format.ISODateTimeFormat;
035 import org.json.simple.JSONValue;
036 import org.kuali.hr.time.accrual.AccrualCategory;
037 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
038 import org.kuali.hr.time.calendar.CalendarEntries;
039 import org.kuali.hr.time.earncode.EarnCode;
040 import org.kuali.hr.time.roles.TkUserRoles;
041 import org.kuali.hr.time.service.base.TkServiceLocator;
042 import org.kuali.hr.time.timeblock.TimeBlock;
043 import org.kuali.hr.time.timeblock.TimeHourDetail;
044 import org.kuali.hr.time.util.TKUser;
045 import org.kuali.hr.time.util.TKUtils;
046 import org.kuali.hr.time.util.TkConstants;
047 import org.kuali.hr.time.workarea.WorkArea;
048 import org.kuali.rice.krad.util.GlobalVariables;
049
050 public class ActionFormUtils {
051
052 public static void validateHourLimit(TimeDetailActionFormBase tdaf) throws Exception {
053 List<String> warningMessages = TkServiceLocator.getTimeOffAccrualService().validateAccrualHoursLimit(tdaf.getTimesheetDocument());
054 addUniqueWarningsToForm(tdaf, warningMessages);
055 }
056
057 public static void addWarningTextFromEarnGroup(TimeDetailActionFormBase tdaf) throws Exception {
058 List<String> warningMessages = TkServiceLocator.getEarnGroupService().warningTextFromEarnGroupsOfDocument(tdaf.getTimesheetDocument());
059 addUniqueWarningsToForm(tdaf, warningMessages);
060 }
061
062 public static void addUnapprovedIPWarningFromClockLog(TimeDetailActionFormBase tdaf) {
063 List<String> warningMessages = new ArrayList<String>();
064 Set<String> aSet = new HashSet<String>();
065 if(tdaf.getTimesheetDocument() != null) {
066 List<TimeBlock> tbList = tdaf.getTimesheetDocument().getTimeBlocks();
067 if(CollectionUtils.isNotEmpty(tbList)) {
068 aSet.addAll(TkServiceLocator.getClockLogService().getUnapprovedIPWarning(tbList));
069
070 }
071 }
072 warningMessages.addAll(aSet);
073 addUniqueWarningsToForm(tdaf, warningMessages);
074 }
075
076 public static void addUniqueWarningsToForm(TimeDetailActionFormBase tdaf, List<String> warningMessages) {
077 if (!warningMessages.isEmpty()) {
078 Set<String> aSet = new HashSet<String>();
079 aSet.addAll(warningMessages);
080 aSet.addAll(tdaf.getWarnings());
081 List<String> aList = new ArrayList<String>();
082 aList.addAll(aSet);
083 tdaf.setWarnings(aList);
084 }
085 }
086
087 // public static String getTimeBlockJSONMap(List<TimeBlock> blocks) {
088 // List<Map<String, Object>> jsonList = getTimeBlocksJson(blocks, null);
089 // Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>();
090 // for (Map<String, Object> tbm : jsonList) {
091 // String id = (String) tbm.get("id");
092 // jsonMappedList.put(id, tbm);
093 // }
094 // return JSONValue.toJSONString(jsonMappedList);
095 // }
096
097 public static Map<String, String> buildAssignmentStyleClassMap(List<TimeBlock> timeBlocks) {
098 Map<String, String> aMap = new HashMap<String, String>();
099 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