1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.clocklog;
17
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.kuali.hr.time.missedpunch.MissedPunchDocument;
25 import org.kuali.hr.time.service.base.TkServiceLocator;
26 import org.kuali.hr.time.syslunch.rule.SystemLunchRule;
27 import org.kuali.hr.time.util.TKContext;
28 import org.kuali.hr.time.util.TKUser;
29 import org.kuali.hr.time.util.TKUtils;
30 import org.kuali.hr.time.util.TkConstants;
31 import org.kuali.rice.core.api.util.ConcreteKeyValue;
32 import org.kuali.rice.core.api.util.KeyValue;
33 import org.kuali.rice.kns.web.struts.form.KualiForm;
34 import org.kuali.rice.kns.web.struts.form.KualiTransactionalDocumentFormBase;
35 import org.kuali.rice.krad.keyvalues.KeyValuesBase;
36
37 public class TkClockActionValuesFinder extends KeyValuesBase {
38
39 @Override
40 public List<KeyValue> getKeyValues() {
41 List<KeyValue> keyLabels = new LinkedList<KeyValue>();
42
43 String mpDocId = (String)TKContext.getHttpServletRequest().getParameter(TkConstants.DOCUMENT_ID_REQUEST_NAME);
44 if(StringUtils.isBlank(mpDocId)) {
45 mpDocId = (String)TKContext.getHttpServletRequest().getAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME);
46 if(StringUtils.isBlank(mpDocId)){
47 KualiForm kualiForm = (KualiForm)TKContext.getHttpServletRequest().getAttribute("KualiForm");
48 if(kualiForm instanceof KualiTransactionalDocumentFormBase){
49 mpDocId = ((KualiTransactionalDocumentFormBase)kualiForm).getDocId();
50 }
51 }
52 if(StringUtils.isBlank(mpDocId)){
53 mpDocId = (String)TKContext.getHttpServletRequest().getParameter("docId");
54 }
55 }
56
57 if(!StringUtils.isEmpty(mpDocId)) {
58 MissedPunchDocument mp = TkServiceLocator.getMissedPunchService().getMissedPunchByRouteHeader(mpDocId);
59 if(mp != null) {
60 String clockAction = mp.getClockAction();
61 if(!StringUtils.isEmpty(clockAction)) {
62 Set<String> availableActions = TkConstants.CLOCK_AVAILABLE_ACTION_MAP.get(clockAction);
63 for (String entry : availableActions) {
64 keyLabels.add(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)));
65 }
66 }
67 }
68 } else {
69 String targetPerson = TKUser.getCurrentTargetPersonId();
70 ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(targetPerson);
71 Set<String> validEntries = lastClock != null ?
72 TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(lastClock.getClockAction()) :
73 TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_OUT);
74 for (String entry : validEntries) {
75 keyLabels.add(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)));
76 }
77
78 if(lastClock != null){
79 String dept = TkServiceLocator.getJobService().getJob(targetPerson, lastClock.getJobNumber(), TKUtils.getCurrentDate()).getDept();
80 Long workArea = lastClock.getWorkArea();
81 Long jobNumber = lastClock.getJobNumber();
82
83 SystemLunchRule slr = TkServiceLocator.getSystemLunchRuleService().getSystemLunchRule(TKUtils.getCurrentDate());
84 if (slr == null || !slr.getShowLunchButton()) {
85 keyLabels.remove(new ConcreteKeyValue("LO", "Lunch Out"));
86 keyLabels.remove(new ConcreteKeyValue("LI", "Lunch In"));
87 } else {
88 if (TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule(dept, workArea, targetPerson, jobNumber,TKUtils.getCurrentDate()) != null) {
89 keyLabels.remove(new ConcreteKeyValue("LO", "Lunch Out"));
90 keyLabels.remove(new ConcreteKeyValue("LI", "Lunch In"));
91 }
92 }
93 }
94
95 }
96
97 if(keyLabels.isEmpty()) {
98 for (Map.Entry entry : TkConstants.CLOCK_ACTION_STRINGS.entrySet()) {
99 keyLabels.add(new ConcreteKeyValue((String)entry.getKey(), (String)entry.getValue()));
100 }
101 }
102 return keyLabels;
103 }
104
105 }