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