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.kpme.tklm.time.clock.web;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.Date;
22  import java.util.HashMap;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.joda.time.DateTime;
28  import org.joda.time.LocalDate;
29  import org.kuali.kpme.core.assignment.Assignment;
30  import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
31  import org.kuali.kpme.core.job.Job;
32  import org.kuali.kpme.core.service.HrServiceLocator;
33  import org.kuali.kpme.core.util.HrConstants;
34  import org.kuali.kpme.core.util.HrContext;
35  import org.kuali.kpme.core.util.TKUtils;
36  import org.kuali.kpme.core.workarea.WorkArea;
37  import org.kuali.kpme.tklm.time.clocklog.ClockLog;
38  import org.kuali.kpme.tklm.time.rules.timecollection.TimeCollectionRule;
39  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
40  import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
41  import org.kuali.kpme.tklm.time.timehourdetail.TimeHourDetail;
42  import org.kuali.kpme.tklm.time.timesheet.web.TimesheetActionForm;
43  
44  public class ClockActionForm extends TimesheetActionForm {
45  
46      private static final long serialVersionUID = -3843074202863670372L;
47  
48      private String currentClockAction;
49      private String lastClockAction;
50      // do we still need nextClockAction?
51      private String nextClockAction;
52      private Date lastClockTimestamp;
53      private Date lastClockTimeWithZone;
54  
55      private String lastClockHours;
56      private ClockLog clockLog;
57      private TimeBlock timeBlock;
58      private boolean clockButtonEnabled;
59  	private boolean showClockButton;
60      private boolean showLunchButton;
61      private boolean showDistributeButton;
62      private Map<String, List<TimeBlock>> timeBlocksMap;
63      private List<String> assignDescriptionsList;
64  
65      private List<String> distributeAssignList;
66      private LinkedHashMap<String, String> desList;
67  
68      private String editTimeBlockId;
69      private TimeBlock currentTimeBlock;
70      private String currentAssignmentDescription;
71      private String currentAssignmentKey;
72      private String tbId;
73      private String tsDocId;
74  
75      private String newAssignDesCol;
76      private String newBDCol;
77      private String newBTCol;
78      private String newEDCol;
79      private String newETCol;
80      private String newHrsCol;
81  	private String errorMessage;
82  
83  
84   // this is for the ajax call
85  	private String outputString;
86  	
87  	// for lunch deduction rule
88  	private BigDecimal lunchDeductionAmt = BigDecimal.ZERO;
89  	
90      public String getTargetUserTimezone() {
91          return HrServiceLocator.getTimezoneService().getUserTimezone(HrContext.getTargetPrincipalId());
92      }
93  
94      public Date getLastClockTimeWithZone() {
95          return lastClockTimeWithZone;
96      }
97  
98      public void setLastClockTimeWithZone(Date lastClockTimeWithZone) {
99          this.lastClockTimeWithZone = lastClockTimeWithZone;
100     }
101 
102     public String getErrorMessage() {
103 		return errorMessage;
104 	}
105 
106 	public void setErrorMessage(String errorMessage) {
107 		this.errorMessage = errorMessage;
108 	}
109 
110 	public String getOutputString() {
111 		return outputString;
112 	}
113 
114 	public void setOutputString(String outputString) {
115 		this.outputString = outputString;
116 	}
117     /** This map is used to determine whether or not lunch buttons will render
118      * for the selected assignment. The key of this map should be the same key
119      * as what is selected in the assignment drop down selection. */
120     private Map<String,Boolean> assignmentLunchMap;
121 
122     public Map<String, Boolean> getAssignmentLunchMap() {
123         return assignmentLunchMap;
124     }
125 
126     public void setAssignmentLunchMap(Map<String,Boolean> assignmentLunchMap) {
127         this.assignmentLunchMap = assignmentLunchMap;
128     }
129 
130     public long getCurrentServerTime() {
131 		return DateTime.now(TKUtils.getSystemDateTimeZone()).getMillis();
132 	}
133 	
134 	public long getCurrentUserUTCOffset() {
135 		return Long.valueOf(DateTime.now(HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback()).toString("Z")) / 100;
136 	}
137 
138     public String getCurrentClockAction() {
139         return currentClockAction;
140     }
141 
142     public void setCurrentClockAction(String currentClockAction) {
143         this.currentClockAction = currentClockAction;
144     }
145 
146 	public String getNextClockAction() {
147 		return nextClockAction;
148 	}
149 
150 	public void setNextClockAction(String nextClockAction) {
151 		this.nextClockAction = nextClockAction;
152 	}
153 
154 	public Date getLastClockTimestamp() {
155 		return lastClockTimestamp;
156 	}
157 
158 	public void setLastClockTimestamp(Date lastClockTimestamp) {
159 		this.lastClockTimestamp = lastClockTimestamp;
160 	}
161 
162 	public ClockLog getClockLog() {
163 		return clockLog;
164 	}
165 
166 	public void setClockLog(ClockLog clockLog) {
167 		this.clockLog = clockLog;
168 	}
169 
170 	public String getLastClockHours() {
171 		return lastClockHours;
172 	}
173 
174 	public void setLastClockHours(String lastClockHours) {
175 		this.lastClockHours = lastClockHours;
176 	}
177 
178 	public TimeBlock getTimeBlock() {
179 		return timeBlock;
180 	}
181 
182 	public void setTimeBlock(TimeBlock timeBlock) {
183 		this.timeBlock = timeBlock;
184 	}
185 
186 	public String getLastClockAction() {
187 		return lastClockAction;
188 	}
189 
190 	public void setLastClockAction(String lastClockAction) {
191 		this.lastClockAction = lastClockAction;
192 	}
193 
194 	public TimeBlock getCurrentTimeBlock() {
195 		if(currentTimeBlock == null && this.getEditTimeBlockId() != null) {
196 			this.setCurrentTimeBlock(TkServiceLocator.getTimeBlockService().getTimeBlock(this.getEditTimeBlockId()));
197 			if(currentTimeBlock != null) {
198 				for(TimeHourDetail thd : currentTimeBlock.getTimeHourDetails()) {
199 					System.out.println("time hour details is >>> "+thd);
200 					if(thd.getEarnCode().equalsIgnoreCase(HrConstants.LUNCH_EARN_CODE)) {
201 						System.out.println("Lunch earncode "+thd.getHours());
202 						this.setLunchDeductionAmt(thd.getHours());
203 					}
204 				}
205 			}
206 		}
207 		return currentTimeBlock;
208 	}
209 
210 	public void setCurrentTimeBlock(TimeBlock currentTimeBlock) {
211 		this.currentTimeBlock = currentTimeBlock;
212 	}
213 
214     public String getCurrentAssignmentDescription() {
215     	if(currentAssignmentDescription == null && this.getCurrentTimeBlock() != null) {
216     		AssignmentDescriptionKey adk = AssignmentDescriptionKey.get(this.getCurrentTimeBlock().getAssignmentKey());
217     		Assignment assignment = this.getTimesheetDocument().getAssignment(adk);
218     		if(assignment != null) {
219     			this.setCurrentAssignmentDescription(assignment.getAssignmentDescription());
220     		}
221     	}
222 		return currentAssignmentDescription;
223 	}
224 
225 	public void setCurrentAssignmentDescription(String currentAssignmentDescription) {
226 		this.currentAssignmentDescription = currentAssignmentDescription;
227 	}
228 
229 	public boolean isClockButtonEnabled() {
230 		return clockButtonEnabled;
231 	}
232 
233 	public void setClockButtonEnabled(boolean clockButtonEnabled) {
234 		this.clockButtonEnabled = clockButtonEnabled;
235 	}
236 
237 	/**
238      * Accounts for presence of Department Lunch Rule and System Lunch Rule.
239      *
240      * This method is dependent on assignmentLunchMap being populated with
241      * proper keys/values.
242      *
243      * @return true if lunch buttons should be displayed, false otherwise.
244      */
245     public boolean isShowLunchButton() {
246         if (showLunchButton) {
247             if (this.assignmentLunchMap != null) {
248                 Boolean val = this.assignmentLunchMap.get(this.getSelectedAssignment());
249                 return (showLunchButton && val != null && !val);
250             } else {
251                 return showLunchButton;
252             }
253         } else {
254             return false;
255         }
256     }
257 
258     /**
259      * @param showLunchButton true if system lunch rule is set.
260      */
261     public void setShowLunchButton(boolean showLunchButton) {
262         this.showLunchButton = showLunchButton;
263     }
264 
265     /**
266      *
267      * This method is dependent on hrsDistributionF flag of TimeCollectionRule
268      *
269      * @return true if Distribute TimeBlock button should be displayed, false otherwise.
270      */
271     public boolean isShowDistributeButton() {
272     	return showDistributeButton;
273     }
274 
275     public void setShowDistrubuteButton(boolean showDistrubuteButton) {
276         this.showDistributeButton = showDistrubuteButton;
277     }
278 
279 	public void setSelectedAssignment(String selectedAssignment) {
280 		super.setSelectedAssignment(selectedAssignment);
281 		this.isShowDistributeButton();
282 	}
283 
284 	public List<String> getAssignDescriptionsList() {
285 		if(assignDescriptionsList == null && this.getTimeBlocksMap() != null) {
286 			List<String> list1= new ArrayList<String>();
287 			 for(String aString : getTimeBlocksMap().keySet()) {
288 				 list1.add(aString);
289 			 }
290 			 this.setAssignDescriptionsList(list1);
291 		}
292 		return assignDescriptionsList;
293 	}
294 
295 	public void setAssignDescriptionsList(List<String> assignDescriptionsList) {
296 		this.assignDescriptionsList = assignDescriptionsList;
297 	}
298 
299 	public String getEditTimeBlockId() {
300 		return editTimeBlockId;
301 	}
302 
303 	public void setEditTimeBlockId(String editTimeBlockId) {
304 		this.editTimeBlockId = editTimeBlockId;
305 	}
306 
307 	public void findTimeBlocksToDistribute() {
308 		if (getTimesheetDocument() != null && !HrConstants.ROUTE_STATUS.FINAL.equals(getTimesheetDocument().getDocumentHeader().getDocumentStatus())) {
309 
310 			Map<String, List<TimeBlock>> timeBlocksMap = new HashMap<String, List<TimeBlock>>();
311 			for (TimeBlock timeBlock : getTimesheetDocument().getTimeBlocks()) {
312 				if (timeBlock.getClockLogCreated()) {
313 					Assignment assignment = HrServiceLocator.getAssignmentService().getAssignmentForTargetPrincipal(AssignmentDescriptionKey.get(timeBlock.getAssignmentKey()), LocalDate.fromDateFields(timeBlock.getBeginDate()));
314 					if (assignment != null) {
315 						WorkArea aWorkArea = HrServiceLocator.getWorkAreaService().getWorkArea(assignment.getWorkArea(), LocalDate.fromDateFields(timeBlock.getBeginDate()));
316 						if(assignment.getJob() != null) {
317 							TimeCollectionRule rule = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(assignment.getJob().getDept(), assignment.getWorkArea(), assignment.getJob().getHrPayType(), LocalDate.fromDateFields(timeBlock.getBeginDate()));
318 							if (rule != null && aWorkArea != null && aWorkArea.isHrsDistributionF() && rule.isClockUserFl()) {
319 								List<TimeBlock> timeBlockList = timeBlocksMap.get(assignment.getAssignmentDescription());
320 								if (timeBlockList == null) {
321 									timeBlockList = new ArrayList<TimeBlock>();
322 								}
323 								timeBlockList.add(timeBlock);
324 								Collections.sort(timeBlockList);
325 								timeBlocksMap.put(assignment.getAssignmentDescription(), timeBlockList);	
326 							}
327 						}
328 					}
329 				}
330 			}
331 			
332 			setTimeBlocksMap(timeBlocksMap);
333 			setAssignDescriptionsList(new ArrayList<String>(timeBlocksMap.keySet()));
334 		}
335 	}
336 	
337 	public void populateAssignmentsForSelectedTimeBlock(TimeBlock tb) {
338 		if(tb == null) {
339 			setDesList(new LinkedHashMap<String, String>());
340 			setDistributeAssignList(new ArrayList<String>());
341 			return;
342 		}
343 		if (getTimesheetDocument() != null && !HrConstants.ROUTE_STATUS.FINAL.equals(getTimesheetDocument().getDocumentHeader().getDocumentStatus())) {
344 	
345 			LinkedHashMap<String, String> desList = new LinkedHashMap<String, String>();
346 			List<String> distributeAssignList = new ArrayList<String>();
347 			for (Assignment assignment : getTimesheetDocument().getAssignments()) {
348 				WorkArea aWorkArea = HrServiceLocator.getWorkAreaService().getWorkArea(assignment.getWorkArea(), LocalDate.fromDateFields(tb.getBeginDate()));
349 				if(assignment.getJob() != null) {
350 					TimeCollectionRule rule = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(assignment.getJob().getDept(), assignment.getWorkArea(), assignment.getJob().getHrPayType(), LocalDate.fromDateFields(tb.getBeginDate()));
351 					if (rule != null && aWorkArea != null && aWorkArea.isHrsDistributionF() && rule.isClockUserFl()) {
352 						desList.put(assignment.getTkAssignmentId().toString(), assignment.getAssignmentDescription());
353 						distributeAssignList.add(assignment.getAssignmentDescription()+ "=" + assignment.getTkAssignmentId().toString());
354 					}
355 				}
356 			}
357 			setDesList(desList);
358 			setDistributeAssignList(distributeAssignList);
359 		}
360 	}
361 					
362 	public String getCurrentAssignmentKey() {
363 		return currentAssignmentKey;
364 	}
365 
366 	public void setCurrentAssignmentKey(String currentAssignmentKey) {
367 		this.currentAssignmentKey = currentAssignmentKey;
368 	}
369 
370 	public String getTbId() {
371 		return tbId;
372 	}
373 
374 	public void setTbId(String tbId) {
375 		this.tbId = tbId;
376 	}
377 	public String getNewAssignDesCol() {
378 		return newAssignDesCol;
379 	}
380 
381 	public void setNewAssignDesCol(String newAssignDesCol) {
382 		this.newAssignDesCol = newAssignDesCol;
383 	}
384 
385 	public String getNewBDCol() {
386 		return newBDCol;
387 	}
388 
389 	public void setNewBDCol(String newBDCol) {
390 		this.newBDCol = newBDCol;
391 	}
392 
393 	public String getNewBTCol() {
394 		return newBTCol;
395 	}
396 
397 	public void setNewBTCol(String newBTCol) {
398 		this.newBTCol = newBTCol;
399 	}
400 
401 	public String getNewEDCol() {
402 		return newEDCol;
403 	}
404 
405 	public void setNewEDCol(String newEDCol) {
406 		this.newEDCol = newEDCol;
407 	}
408 
409 	public String getNewETCol() {
410 		return newETCol;
411 	}
412 
413 	public void setNewETCol(String newETCol) {
414 		this.newETCol = newETCol;
415 	}
416 
417 	public String getNewHrsCol() {
418 		return newHrsCol;
419 	}
420 
421 	public void setNewHrsCol(String newHrsCol) {
422 		this.newHrsCol = newHrsCol;
423 	}
424 
425 	public Map<String, List<TimeBlock>> getTimeBlocksMap() {
426 		if(timeBlocksMap == null ) {
427 			this.findTimeBlocksToDistribute();
428 		}
429 		return timeBlocksMap;
430 	}
431 
432 	public void setTimeBlocksMap(Map<String, List<TimeBlock>> timeBlocksMap) {
433 		this.timeBlocksMap = timeBlocksMap;
434 	}
435 
436 	public List<String> getDistributeAssignList() {
437 		return distributeAssignList;
438 	}
439 
440 	public void setDistributeAssignList(List<String> distributeAssignList) {
441 		this.distributeAssignList = distributeAssignList;
442 	}
443 
444 	public LinkedHashMap<String, String> getDesList() {
445 		return desList;
446 	}
447 
448 	public void setDesList(LinkedHashMap<String, String> desList) {
449 		this.desList = desList;
450 	}
451 
452 	public String getTsDocId() {
453 		return tsDocId;
454 	}
455 
456 	public void setTsDocId(String tsDocId) {
457 		this.tsDocId = tsDocId;
458 	}
459 
460 	public BigDecimal getLunchDeductionAmt() {
461 		return lunchDeductionAmt;
462 	}
463 
464 	public void setLunchDeductionAmt(BigDecimal lunchDeductionAmt) {
465 		this.lunchDeductionAmt = lunchDeductionAmt;
466 	}
467 
468 	public void setShowClockButton(boolean showClockButton) {
469 		this.showClockButton = showClockButton;
470 	}
471 	
472 	public boolean getShowClockButton() {
473 		return showClockButton;
474 	}
475 	
476 }