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 java.sql.Timestamp;
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.Date;
022    import java.util.HashMap;
023    import java.util.LinkedHashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.joda.time.DateTime;
028    import org.joda.time.DateTimeZone;
029    import org.kuali.hr.time.assignment.Assignment;
030    import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
031    import org.kuali.hr.time.clocklog.ClockLog;
032    import org.kuali.hr.time.collection.rule.TimeCollectionRule;
033    import org.kuali.hr.time.service.base.TkServiceLocator;
034    import org.kuali.hr.time.timeblock.TimeBlock;
035    import org.kuali.hr.time.timesheet.web.TimesheetActionForm;
036    import org.kuali.hr.time.util.TKUser;
037    import org.kuali.hr.time.util.TKUtils;
038    import org.kuali.hr.time.util.TkConstants;
039    
040    public class ClockActionForm extends TimesheetActionForm {
041    
042        /**
043         *
044         */
045        private static final long serialVersionUID = -3843074202863670372L;
046    
047        private String currentServerTime;
048        private String currentClockAction;
049        private String lastClockAction;
050        // do we still need nextClockAction?
051        private String nextClockAction;
052        private Timestamp lastClockTimestamp;
053        private Date lastClockTimeWithZone;
054    
055        private String lastClockHours;
056        private ClockLog clockLog;
057        private TimeBlock timeBlock;
058        private boolean clockButtonEnabled;
059        private boolean showLunchButton;
060        private boolean showDistributeButton;
061        private boolean showMissedPunchButton;
062        private Map<String, List<TimeBlock>> timeBlocksMap;
063        private List<String> assignDescriptionsList;
064    
065        private List<String> distributeAssignList;
066        private LinkedHashMap<String, String> desList;
067    
068        private String editTimeBlockId;
069        private TimeBlock currentTimeBlock;
070        private String currentAssignmentDescription;
071        private String currentAssignmentKey;
072        private String tbId;
073        private String tsDocId;
074    
075        private String newAssignDesCol;
076        private String newBDCol;
077        private String newBTCol;
078        private String newEDCol;
079        private String newETCol;
080        private String newHrsCol;
081            private String errorMessage;
082    
083    
084     // this is for the ajax call
085            private String outputString;
086            
087        public String getTargetUserTimezone() {
088            return TkServiceLocator.getTimezoneService().getUserTimezone(TKUser.getCurrentTargetPersonId());
089        }
090    
091        public Date getLastClockTimeWithZone() {
092            return lastClockTimeWithZone;
093        }
094    
095        public void setLastClockTimeWithZone(Date lastClockTimeWithZone) {
096            this.lastClockTimeWithZone = lastClockTimeWithZone;
097        }
098    
099        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    }