1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.mobile.service;
17
18 import java.sql.Date;
19 import java.sql.Timestamp;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.kuali.hr.time.assignment.Assignment;
26 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
27 import org.kuali.hr.time.calendar.CalendarEntries;
28 import org.kuali.hr.time.clocklog.ClockLog;
29 import org.kuali.hr.time.service.base.TkServiceLocator;
30 import org.kuali.hr.time.timesheet.TimesheetDocument;
31 import org.kuali.hr.time.util.TKContext;
32 import org.kuali.hr.time.util.TKUser;
33 import org.kuali.hr.time.util.TKUtils;
34 import org.kuali.hr.time.util.TkConstants;
35 import org.kuali.rice.kew.api.exception.WorkflowException;
36 import org.kuali.rice.kim.api.identity.Person;
37 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
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 = TkServiceLocator.getAssignmentService().getAssignments(principalId, TKUtils.getCurrentDate());
51
52 for(Assignment assignment : assignments){
53 if(assignment.isSynchronous()){
54 String key = new AssignmentDescriptionKey(assignment).toAssignmentKeyString();
55 String desc = assignment.getAssignmentDescription();
56 clockEntryInfo.getAssignKeyToAssignmentDescriptions().put(key, desc);
57 }
58 }
59 List<String> clockActions = getClockActions(principalId);
60 clockEntryInfo.setClockActions(clockActions);
61 return new Gson().toJson(clockEntryInfo);
62 }
63
64 @Override
65 public HashMap<String,List<String>> addClockAction(String principalId, String assignmentKey, String clockAction) {
66 HashMap<String,List<String>> errorWarningMap = new HashMap<String,List<String>>();
67
68
69
70 Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
71 TKUser.setTargetPerson(person);
72
73 Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(new AssignmentDescriptionKey(assignmentKey), TKUtils.getCurrentDate());
74 Date currentDate = TKUtils.getCurrentDate();
75 CalendarEntries calendarEntries = TkServiceLocator.getCalendarService().getCurrentCalendarDates(principalId, currentDate);
76 TimesheetDocument td;
77 try {
78 td = TkServiceLocator.getTimesheetService().openTimesheetDocument(principalId, calendarEntries);
79 } catch (WorkflowException e) {
80 throw new RuntimeException("Could not open timesheet");
81 }
82
83 String ip = TKUtils.getIPAddressFromRequest(TKContext.getHttpServletRequest());
84 Timestamp currentTs = new Timestamp(System.currentTimeMillis());
85
86
87
88 TkServiceLocator.getClockLogService().processClockLog(currentTs, assignment, td.getPayCalendarEntry(), ip,
89 new java.sql.Date(currentTs.getTime()), td, getCurrentClockAction(), principalId);
90
91
92
93 return errorWarningMap;
94 }
95
96 private String getLastClockLogDescription(String principalId){
97 ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(principalId);
98 if(lastClockLog != null){
99 String lastClockDescription;
100 if(StringUtils.equals(lastClockLog.getClockAction(), "CI")){
101 lastClockDescription = "Clocked in since : ";
102 } else if(StringUtils.equals(lastClockLog.getClockAction(), "CO")){
103 lastClockDescription = "Clocked out since : ";
104 } else if(StringUtils.equals(lastClockLog.getClockAction(), "LI")){
105 lastClockDescription = "Returned from lunch since :";
106 } else {
107 lastClockDescription = "At lunch since :";
108 }
109
110
111 lastClockDescription += TKUtils.formatDateTime(lastClockLog.getClockTimestamp());
112 return lastClockDescription;
113 }
114 return "";
115 }
116
117 private List<String> getClockActions(String principalId){
118 ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(principalId);
119 List<String> clockActions = new ArrayList<String>();
120 if(lastClockLog != null){
121 if(StringUtils.equals(lastClockLog.getClockAction(), "CI")){
122 clockActions.add("Clock Out");
123 clockActions.add("Lunch Out");
124 } else if(StringUtils.equals(lastClockLog.getClockAction(), "CO")){
125 clockActions.add("Clock In");
126 } else if(StringUtils.equals(lastClockLog.getClockAction(), "LI")){
127 clockActions.add("Clock Out");
128 } else {
129 clockActions.add("Lunch In");
130 }
131 }
132 return clockActions;
133 }
134
135 private String getCurrentClockAction() {
136 ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(TKContext.getTargetPrincipalId());
137 String currentClockAction = "";
138 if(lastClockLog != null){
139 if(StringUtils.equals(lastClockLog.getClockAction(), TkConstants.CLOCK_IN)){
140 currentClockAction = TkConstants.CLOCK_OUT;
141 } else if(StringUtils.equals(lastClockLog.getClockAction(), TkConstants.CLOCK_OUT)){
142 currentClockAction = TkConstants.CLOCK_IN;
143 } else if(StringUtils.equals(lastClockLog.getClockAction(), TkConstants.LUNCH_IN)){
144 currentClockAction = TkConstants.LUNCH_OUT;
145 } else {
146 currentClockAction = TkConstants.LUNCH_IN;
147 }
148 }
149 return currentClockAction;
150 }
151
152 }