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.clocklog;
017
018 import java.util.LinkedList;
019 import java.util.List;
020 import java.util.Map;
021 import java.util.Set;
022
023 import org.apache.commons.lang.StringUtils;
024 import org.kuali.hr.time.missedpunch.MissedPunchDocument;
025 import org.kuali.hr.time.service.base.TkServiceLocator;
026 import org.kuali.hr.time.util.TKContext;
027 import org.kuali.hr.time.util.TKUser;
028 import org.kuali.hr.time.util.TKUtils;
029 import org.kuali.hr.time.util.TkConstants;
030 import org.kuali.rice.core.api.util.ConcreteKeyValue;
031 import org.kuali.rice.core.api.util.KeyValue;
032 import org.kuali.rice.kns.web.struts.form.KualiForm;
033 import org.kuali.rice.kns.web.struts.form.KualiTransactionalDocumentFormBase;
034 import org.kuali.rice.krad.keyvalues.KeyValuesBase;
035
036 public class TkClockActionValuesFinder extends KeyValuesBase {
037
038 @Override
039 public List<KeyValue> getKeyValues() {
040 List<KeyValue> keyLabels = new LinkedList<KeyValue>();
041 // get the missed punch doc Id if this is an existing document
042 String mpDocId = (String)TKContext.getHttpServletRequest().getParameter(TkConstants.DOCUMENT_ID_REQUEST_NAME);
043 if(StringUtils.isBlank(mpDocId)) {
044 mpDocId = (String)TKContext.getHttpServletRequest().getAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME);
045 if(StringUtils.isBlank(mpDocId)){
046 KualiForm kualiForm = (KualiForm)TKContext.getHttpServletRequest().getAttribute("KualiForm");
047 if(kualiForm instanceof KualiTransactionalDocumentFormBase){
048 mpDocId = ((KualiTransactionalDocumentFormBase)kualiForm).getDocId();
049 }
050 }
051 if(StringUtils.isBlank(mpDocId)){
052 mpDocId = (String)TKContext.getHttpServletRequest().getParameter("docId");
053 }
054 }
055 // if the user is working on an existing missed punch doc, only return available actions based on the initial clock action
056 if(!StringUtils.isEmpty(mpDocId)) {
057 MissedPunchDocument mp = TkServiceLocator.getMissedPunchService().getMissedPunchByRouteHeader(mpDocId);
058 if(mp != null) {
059 String clockAction = mp.getClockAction();
060 if(!StringUtils.isEmpty(clockAction)) {
061 Set<String> availableActions = TkConstants.CLOCK_AVAILABLE_ACTION_MAP.get(clockAction);
062 for (String entry : availableActions) {
063 keyLabels.add(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)));
064 }
065 }
066 }
067 } else {
068 String targetPerson = TKUser.getCurrentTargetPerson().getPrincipalId();
069 ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(targetPerson);
070 Set<String> validEntries = lastClock != null ?
071 TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(lastClock.getClockAction()) :
072 TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_OUT); // Force CLOCK_IN as next valid action.
073 for (String entry : validEntries) {
074 keyLabels.add(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)));
075 }
076 //#KPME-2178 nullity checked.
077 if(lastClock != null){
078 String dept = TkServiceLocator.getJobService().getJob(targetPerson, lastClock.getJobNumber(), TKUtils.getCurrentDate()).getDept();
079 Long workArea = lastClock.getWorkArea();
080 Long jobNumber = lastClock.getJobNumber();
081
082 if (!TkServiceLocator.getSystemLunchRuleService().getSystemLunchRule(TKUtils.getCurrentDate()).getShowLunchButton()) {
083 keyLabels.remove(new ConcreteKeyValue("LO", "Lunch Out"));
084 keyLabels.remove(new ConcreteKeyValue("LI", "Lunch In"));
085 } else {
086 if (TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule(dept, workArea, targetPerson, jobNumber,TKUtils.getCurrentDate()) != null) {
087 keyLabels.remove(new ConcreteKeyValue("LO", "Lunch Out"));
088 keyLabels.remove(new ConcreteKeyValue("LI", "Lunch In"));
089 }
090 }
091 }
092
093 }
094
095 if(keyLabels.isEmpty()) { // default is returning all options
096 for (Map.Entry entry : TkConstants.CLOCK_ACTION_STRINGS.entrySet()) {
097 keyLabels.add(new ConcreteKeyValue((String)entry.getKey(), (String)entry.getValue()));
098 }
099 }
100 return keyLabels;
101 }
102
103 }