View Javadoc

1   /**
2    * Copyright 2004-2014 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.service.mobile;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.joda.time.DateTime;
25  import org.joda.time.LocalDate;
26  import org.kuali.kpme.core.assignment.Assignment;
27  import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
28  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
29  import org.kuali.kpme.core.service.HrServiceLocator;
30  import org.kuali.kpme.core.util.HrContext;
31  import org.kuali.kpme.core.util.TKUtils;
32  import org.kuali.kpme.tklm.common.TkConstants;
33  import org.kuali.kpme.tklm.time.clocklog.ClockLog;
34  import org.kuali.kpme.tklm.time.rules.timecollection.TimeCollectionRule;
35  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
36  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
37  import org.kuali.rice.kew.api.exception.WorkflowException;
38  
39  import com.google.gson.Gson;
40  
41  public class TkMobileServiceImpl implements TkMobileService {
42  
43  	@Override
44  	public String getClockEntryInfo(String principalId) {
45  		ClockEntryInfo clockEntryInfo = new ClockEntryInfo();
46  		ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(principalId);
47  		if(lastClockLog != null){
48  			clockEntryInfo.setLastClockLogDescription(getLastClockLogDescription(principalId));
49  		}
50  		List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(principalId, LocalDate.now());
51  
52  		for(Assignment assignment : assignments){
53  			if(assignment.getJob() != null) {
54  				TimeCollectionRule tcr = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(assignment.getDept(), assignment.getWorkArea(), assignment.getJob().getHrPayType(), LocalDate.now());
55  				if(tcr == null || tcr.isClockUserFl()){
56  					String key = new AssignmentDescriptionKey(assignment).toAssignmentKeyString();
57  					String desc = assignment.getAssignmentDescription();
58  					clockEntryInfo.getAssignKeyToAssignmentDescriptions().put(key, desc);
59  				}
60  			}
61  		}
62  		List<String> clockActions = getClockActions(principalId);
63  		clockEntryInfo.setClockActions(clockActions);
64  		return new Gson().toJson(clockEntryInfo);
65  	}
66  
67  	@Override
68  	public Map<String,List<String>> addClockAction(String principalId, String assignmentKey, String clockAction, String ipAddress) {
69  		HashMap<String,List<String>> errorWarningMap = new HashMap<String,List<String>>();
70  
71  		Assignment assignment = HrServiceLocator.getAssignmentService().getAssignmentForTargetPrincipal(AssignmentDescriptionKey.get(assignmentKey), LocalDate.now());
72          // Set person on the context
73          // This is primary for getting the assignment, since we get the assignment by using the target principal id on the context
74          HrContext.setTargetPrincipalId(principalId);
75  
76          CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates(principalId, new LocalDate().toDateTimeAtStartOfDay());
77          TimesheetDocument td;
78  		try {
79  			td = TkServiceLocator.getTimesheetService().openTimesheetDocument(principalId, calendarEntry);
80  		} catch (WorkflowException e) {
81  			throw new RuntimeException("Could not open timesheet");
82  		}
83          
84          // processClockLog is the correct method to use. It creates and persists a clock log and a time block if necessary.
85          // buildClockLog just creates a clock log object.
86          TkServiceLocator.getClockLogService().processClockLog(new DateTime(), assignment, td.getCalendarEntry(), ipAddress,
87                  LocalDate.now(), td, getCurrentClockAction(), true, principalId);
88  
89          // TODO: not sure what we want to return for the errorWarningMap
90  
91  		return errorWarningMap;
92  	}
93  	
94  	private String getLastClockLogDescription(String principalId){
95  		ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(principalId);
96  		if(lastClockLog != null){
97  			String lastClockDescription;
98  			if(StringUtils.equals(lastClockLog.getClockAction(), "CI")){
99  				lastClockDescription = "Clocked in since : ";
100 			} else if(StringUtils.equals(lastClockLog.getClockAction(), "CO")){
101 				lastClockDescription = "Clocked out since : ";
102 			} else if(StringUtils.equals(lastClockLog.getClockAction(), "LI")){
103 				lastClockDescription = "Returned from lunch since :";
104 			} else {
105 				lastClockDescription = "At lunch since :";
106 			}
107 			//TODO convert for timezone
108 			
109 			lastClockDescription += TKUtils.formatDateTimeLong(lastClockLog.getClockDateTime());
110 			return lastClockDescription;
111 		}
112 		return "";
113 	}
114 	
115 	private List<String> getClockActions(String principalId){
116 		ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(principalId);
117 		List<String> clockActions = new ArrayList<String>();
118 		if(lastClockLog != null){
119 			if(StringUtils.equals(lastClockLog.getClockAction(), "CI")){
120 				clockActions.add("Clock Out");
121 				clockActions.add("Lunch Out");
122 			} else if(StringUtils.equals(lastClockLog.getClockAction(), "CO")){
123 				clockActions.add("Clock In");
124 			} else if(StringUtils.equals(lastClockLog.getClockAction(), "LI")){
125 				clockActions.add("Clock Out");
126 			} else {
127 				clockActions.add("Lunch In");
128 			}
129 		}
130 		return clockActions;
131 	}
132 
133     private String getCurrentClockAction() {
134         ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(HrContext.getTargetPrincipalId());
135         String currentClockAction = "";
136         if(lastClockLog != null){
137 			if(StringUtils.equals(lastClockLog.getClockAction(), TkConstants.CLOCK_IN)){
138 				currentClockAction = TkConstants.CLOCK_OUT;
139 			} else if(StringUtils.equals(lastClockLog.getClockAction(), TkConstants.CLOCK_OUT)){
140 				currentClockAction = TkConstants.CLOCK_IN;
141 			} else if(StringUtils.equals(lastClockLog.getClockAction(), TkConstants.LUNCH_IN)){
142 				currentClockAction = TkConstants.LUNCH_OUT;
143 			} else {
144 				currentClockAction = TkConstants.LUNCH_IN;
145 			}
146 		}
147 		return currentClockAction;
148     }
149 
150 }