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.clock.web;
017    
018    import org.joda.time.DateTime;
019    import org.joda.time.DateTimeZone;
020    import org.kuali.hr.time.assignment.Assignment;
021    import org.kuali.hr.time.clocklog.ClockLog;
022    import org.kuali.hr.time.collection.rule.TimeCollectionRule;
023    import org.kuali.hr.time.service.base.TkServiceLocator;
024    import org.kuali.hr.time.timeblock.TimeBlock;
025    import org.kuali.hr.time.timesheet.TimesheetDocument;
026    import org.kuali.hr.time.timesheet.web.TimesheetActionForm;
027    import org.kuali.hr.time.util.TKUser;
028    import org.kuali.hr.time.util.TKUtils;
029    import org.kuali.hr.time.util.TkConstants;
030    
031    import java.sql.Timestamp;
032    import java.util.*;
033    
034    public class ClockActionForm extends TimesheetActionForm {
035    
036        /**
037         *
038         */
039        private static final long serialVersionUID = -3843074202863670372L;
040    
041        private String currentServerTime;
042        private String currentClockAction;
043        private String lastClockAction;
044        // do we still need nextClockAction?
045        private String nextClockAction;
046        private Timestamp lastClockTimestamp;
047        private Date lastClockTimeWithZone;
048    
049        private String lastClockHours;
050        private ClockLog clockLog;
051        private TimeBlock timeBlock;
052        private boolean showLunchButton;
053        private boolean showDistributeButton;
054        private boolean showMissedPunchButton;
055        private Map<String, List<TimeBlock>> timeBlocksMap;
056        private List<String> assignDescriptionsList;
057    
058        private List<String> distributeAssignList;
059        private LinkedHashMap<String, String> desList;
060    
061        private String editTimeBlockId;
062        private TimeBlock currentTimeBlock;
063        private String currentAssignmentDescription;
064        private String currentAssignmentKey;
065        private String tbId;
066        private String tsDocId;
067    
068        private String newAssignDesCol;
069        private String newBDCol;
070        private String newBTCol;
071        private String newEDCol;
072        private String newETCol;
073        private String newHrsCol;
074            private String errorMessage;
075    
076    
077     // this is for the ajax call
078            private String outputString;
079            
080        public String getTargetUserTimezone() {
081            return TkServiceLocator.getTimezoneService().getUserTimezone(TKUser.getCurrentTargetPerson().getPrincipalId());
082        }
083    
084        public Date getLastClockTimeWithZone() {
085            return lastClockTimeWithZone;
086        }
087    
088        public void setLastClockTimeWithZone(Date lastClockTimeWithZone) {
089            this.lastClockTimeWithZone = lastClockTimeWithZone;
090        }
091    
092        public String getErrorMessage() {
093                    return errorMessage;
094            }
095    
096            public void setErrorMessage(String errorMessage) {
097                    this.errorMessage = errorMessage;
098            }
099    
100            public String getOutputString() {
101                    return outputString;
102            }
103    
104            public void setOutputString(String outputString) {
105                    this.outputString = outputString;
106            }
107        /** This map is used to determine whether or not lunch buttons will render
108         * for the selected assignment. The key of this map should be the same key
109         * as what is selected in the assignment drop down selection. */
110        private Map<String,Boolean> assignmentLunchMap;
111    
112        public Map<String, Boolean> getAssignmentLunchMap() {
113            return assignmentLunchMap;
114        }
115    
116        public void setAssignmentLunchMap(Map<String,Boolean> assignmentLunchMap) {
117            this.assignmentLunchMap = assignmentLunchMap;
118        }
119    
120        public String getCurrentServerTime() {
121                    return currentServerTime;
122            }
123    
124            public void setCurrentServerTime(String currentServerTime) {
125                    this.currentServerTime = currentServerTime;
126            }
127            
128            public String getUserSystemOffsetServerTime(){
129                    DateTime dt = new DateTime(TKUtils.getCurrentDate().getTime()+getUserTimezoneOffset());
130                    return String.valueOf(dt.getMillis());
131            }
132            
133            public Long getUserTimezoneOffset(){
134                    DateTimeZone dtz = TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback();
135                    return TkServiceLocator.getTimezoneService().getTimezoneOffsetFromServerTime(dtz);
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 Timestamp getLastClockTimestamp() {
155                    return lastClockTimestamp;
156            }
157    
158            public void setLastClockTimestamp(Timestamp 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                    }
198                    return currentTimeBlock;
199            }
200    
201            public void setCurrentTimeBlock(TimeBlock currentTimeBlock) {
202                    this.currentTimeBlock = currentTimeBlock;
203            }
204    
205        public String getCurrentAssignmentDescription() {
206            if(currentAssignmentDescription == null && this.getCurrentTimeBlock() != null) {
207                    Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(this.getTimesheetDocument(), this.getCurrentTimeBlock().getAssignmentKey());
208                    if(assignment != null) {
209                            this.setCurrentAssignmentDescription(assignment.getAssignmentDescription());
210                    }
211            }
212                    return currentAssignmentDescription;
213            }
214    
215            public void setCurrentAssignmentDescription(String currentAssignmentDescription) {
216                    this.currentAssignmentDescription = currentAssignmentDescription;
217            }
218    
219            /**
220         * Accounts for presence of Department Lunch Rule and System Lunch Rule.
221         *
222         * This method is dependent on assignmentLunchMap being populated with
223         * proper keys/values.
224         *
225         * @return true if lunch buttons should be displayed, false otherwise.
226         */
227        public boolean isShowLunchButton() {
228            if (showLunchButton) {
229                if (this.assignmentLunchMap != null) {
230                    Boolean val = this.assignmentLunchMap.get(this.getSelectedAssignment());
231                    return (showLunchButton && val != null && !val);
232                } else {
233                    return showLunchButton;
234                }
235            } else {
236                return false;
237            }
238        }
239    
240        /**
241         * @param showLunchButton true if system lunch rule is set.
242         */
243        public void setShowLunchButton(boolean showLunchButton) {
244            this.showLunchButton = showLunchButton;
245        }
246    
247        /**
248         *
249         * This method is dependent on hrsDistributionF flag of TimeCollectionRule
250         *
251         * @return true if Distribute TimeBlock button should be displayed, false otherwise.
252         */
253        public boolean isShowDistributeButton() {
254            return showDistributeButton;
255        }
256    
257        public void setShowDistrubuteButton(boolean showDistrubuteButton) {
258            this.showDistributeButton = showDistrubuteButton;
259        }
260    
261            public void setSelectedAssignment(String selectedAssignment) {
262                    super.setSelectedAssignment(selectedAssignment);
263                    this.isShowDistributeButton();
264            }
265    
266            public List<String> getAssignDescriptionsList() {
267                    if(assignDescriptionsList == null && this.getTimeBlocksMap() != null) {
268                            List<String> list1= new ArrayList<String>();
269                             for(String aString : getTimeBlocksMap().keySet()) {
270                                     list1.add(aString);
271                             }
272                             this.setAssignDescriptionsList(list1);
273                    }
274                    return assignDescriptionsList;
275            }
276    
277            public void setAssignDescriptionsList(List<String> assignDescriptionsList) {
278                    this.assignDescriptionsList = assignDescriptionsList;
279            }
280    
281            public String getEditTimeBlockId() {
282                    return editTimeBlockId;
283            }
284    
285            public void setEditTimeBlockId(String editTimeBlockId) {
286                    this.editTimeBlockId = editTimeBlockId;
287            }
288    
289            public void findTimeBlocksToDistribute() {
290                     String pId = this.getPrincipalId();
291                     if(pId != null) {
292                             TimesheetDocument td = this.getTimesheetDocument();
293                             if(td != null && !td.getDocumentHeader().getDocumentStatus().equals(TkConstants.ROUTE_STATUS.FINAL)) {
294                                     List<TimeBlock> tbList = new ArrayList<TimeBlock>();
295                                     if(td != null) {
296                                             for(TimeBlock tbTemp : td.getTimeBlocks()) {
297                                                     if(tbTemp.getClockLogCreated()) {
298                                                             tbList.add(tbTemp);
299                                                     }
300                                             }
301                                     }
302                                     List<Assignment> assignmentList = TkServiceLocator.getAssignmentService().getAssignments(pId, null);
303                                     List<String> aList = new ArrayList<String>();
304                                     Map<String, List<TimeBlock>> tbMap = new HashMap<String, List<TimeBlock>>();
305                                     Map<String, String> map2 = new HashMap<String, String>();
306                                     LinkedHashMap<String, String> desMap = new LinkedHashMap<String, String>();  // for populating assignment dropdown list when click Edit button
307    
308                                     for(Assignment assignment : assignmentList) {
309                                            TimeCollectionRule rule = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(assignment.getJob().getDept(), assignment.getWorkArea(), assignment.getEffectiveDate());
310                                            if(rule != null && rule.isHrsDistributionF() && rule.isClockUserFl()) {
311                                                    aList.add(assignment.getAssignmentDescription()+ "=" + assignment.getTkAssignmentId().toString());
312                                                    desMap.put(assignment.getTkAssignmentId().toString(), assignment.getAssignmentDescription());
313    
314                                                    for(TimeBlock tb: tbList){
315                                                            if(assignment.getWorkArea().equals(tb.getWorkArea())) {
316                                                                    List<TimeBlock> tempList = tbMap.get(assignment.getAssignmentDescription());
317                                                                    if(tempList == null) {
318                                                                            tempList = new ArrayList<TimeBlock>();
319                                                                    }
320                                                                    tempList.add(tb);
321                                                                    Collections.sort(tempList);
322                                                                    tbMap.put(assignment.getAssignmentDescription(), tempList);
323                                                                    map2.put(assignment.getAssignmentDescription(),assignment.getTkAssignmentId().toString() );
324                                                            }
325                                                    }
326                                            }
327                                     }
328    
329                                     this.setTimeBlocksMap(tbMap);
330                                     this.setDesList(desMap);
331    
332                                     List<String> list1= new ArrayList<String>();
333                                     for(String aString : tbMap.keySet()) {
334                                             list1.add(aString);
335                                     }
336    
337                                     //remove duplicate
338                                     HashSet h = new HashSet(aList);
339                                     aList.clear();
340                                     aList.addAll(h);
341                                     Collections.sort(aList);
342                                     this.setAssignDescriptionsList(list1);
343                                     this.setDistributeAssignList(aList);
344                             }
345                     }
346    
347            }
348    
349            public String getCurrentAssignmentKey() {
350                    return currentAssignmentKey;
351            }
352    
353            public void setCurrentAssignmentKey(String currentAssignmentKey) {
354                    this.currentAssignmentKey = currentAssignmentKey;
355            }
356    
357            public String getTbId() {
358                    return tbId;
359            }
360    
361            public void setTbId(String tbId) {
362                    this.tbId = tbId;
363            }
364            public String getNewAssignDesCol() {
365                    return newAssignDesCol;
366            }
367    
368            public void setNewAssignDesCol(String newAssignDesCol) {
369                    this.newAssignDesCol = newAssignDesCol;
370            }
371    
372            public String getNewBDCol() {
373                    return newBDCol;
374            }
375    
376            public void setNewBDCol(String newBDCol) {
377                    this.newBDCol = newBDCol;
378            }
379    
380            public String getNewBTCol() {
381                    return newBTCol;
382            }
383    
384            public void setNewBTCol(String newBTCol) {
385                    this.newBTCol = newBTCol;
386            }
387    
388            public String getNewEDCol() {
389                    return newEDCol;
390            }
391    
392            public void setNewEDCol(String newEDCol) {
393                    this.newEDCol = newEDCol;
394            }
395    
396            public String getNewETCol() {
397                    return newETCol;
398            }
399    
400            public void setNewETCol(String newETCol) {
401                    this.newETCol = newETCol;
402            }
403    
404            public String getNewHrsCol() {
405                    return newHrsCol;
406            }
407    
408            public void setNewHrsCol(String newHrsCol) {
409                    this.newHrsCol = newHrsCol;
410            }
411    
412            public Map<String, List<TimeBlock>> getTimeBlocksMap() {
413                    if(timeBlocksMap == null ) {
414                            this.findTimeBlocksToDistribute();
415                    }
416                    return timeBlocksMap;
417            }
418    
419            public void setTimeBlocksMap(Map<String, List<TimeBlock>> timeBlocksMap) {
420                    this.timeBlocksMap = timeBlocksMap;
421            }
422    
423            public List<String> getDistributeAssignList() {
424                    return distributeAssignList;
425            }
426    
427            public void setDistributeAssignList(List<String> distributeAssignList) {
428                    this.distributeAssignList = distributeAssignList;
429            }
430    
431            public LinkedHashMap<String, String> getDesList() {
432                    return desList;
433            }
434    
435            public void setDesList(LinkedHashMap<String, String> desList) {
436                    this.desList = desList;
437            }
438    
439            public String getTsDocId() {
440                    return tsDocId;
441            }
442    
443            public void setTsDocId(String tsDocId) {
444                    this.tsDocId = tsDocId;
445            }
446    
447            public boolean isShowMissedPunchButton() {
448                    return showMissedPunchButton;
449            }
450    
451            public void setShowMissedPunchButton(boolean showMissedPunchButton) {
452                    this.showMissedPunchButton = showMissedPunchButton;
453            }
454            
455    }