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