001 /**
002 * Copyright 2004-2012 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.TkConstants;
029 import org.kuali.rice.core.api.util.ConcreteKeyValue;
030 import org.kuali.rice.core.api.util.KeyValue;
031 import org.kuali.rice.kns.web.struts.form.KualiForm;
032 import org.kuali.rice.kns.web.struts.form.KualiTransactionalDocumentFormBase;
033 import org.kuali.rice.krad.keyvalues.KeyValuesBase;
034
035 public class TkClockActionValuesFinder extends KeyValuesBase {
036
037 @Override
038 public List getKeyValues() {
039 List<KeyValue> keyLabels = new LinkedList<KeyValue>();
040 // get the missed punch doc Id if this is an existing document
041 String mpDocId = (String)TKContext.getHttpServletRequest().getParameter(TkConstants.DOCUMENT_ID_REQUEST_NAME);
042 if(StringUtils.isBlank(mpDocId)) {
043 mpDocId = (String)TKContext.getHttpServletRequest().getAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME);
044 if(StringUtils.isBlank(mpDocId)){
045 KualiForm kualiForm = (KualiForm)TKContext.getHttpServletRequest().getAttribute("KualiForm");
046 if(kualiForm instanceof KualiTransactionalDocumentFormBase){
047 mpDocId = ((KualiTransactionalDocumentFormBase)kualiForm).getDocId();
048 }
049 }
050 if(StringUtils.isBlank(mpDocId)){
051 mpDocId = (String)TKContext.getHttpServletRequest().getParameter("docId");
052 }
053 }
054 // if the user is working on an existing missed punch doc, only return available actions based on the initial clock action
055 if(!StringUtils.isEmpty(mpDocId)) {
056 MissedPunchDocument mp = TkServiceLocator.getMissedPunchService().getMissedPunchByRouteHeader(mpDocId);
057 if(mp != null) {
058 String clockAction = mp.getClockAction();
059 if(!StringUtils.isEmpty(clockAction)) {
060 Set<String> availableActions = TkConstants.CLOCK_AVAILABLE_ACTION_MAP.get(clockAction);
061 for (String entry : availableActions) {
062 keyLabels.add(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)));
063 }
064 }
065 }
066 } else {
067 ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(TKUser.getCurrentTargetPerson().getPrincipalId());
068 Set<String> validEntries = lastClock != null ?
069 TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(lastClock.getClockAction()) :
070 TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_OUT); // Force CLOCK_IN as next valid action.
071 for (String entry : validEntries) {
072 keyLabels.add(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)));
073 }
074 }
075
076 if(keyLabels.isEmpty()) { // default is returning all options
077 for (Map.Entry entry : TkConstants.CLOCK_ACTION_STRINGS.entrySet()) {
078 keyLabels.add(new ConcreteKeyValue((String)entry.getKey(), (String)entry.getValue()));
079 }
080 }
081 return keyLabels;
082 }
083
084 }