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