001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.kpme.pm.util;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.collections.CollectionUtils;
022import org.kuali.kpme.core.bo.HrBusinessObject;
023import org.kuali.kpme.pm.positionflag.PositionFlag;
024import org.kuali.kpme.pm.service.base.PmServiceLocator;
025import org.kuali.rice.core.api.util.ConcreteKeyValue;
026import org.kuali.rice.core.api.util.KeyValue;
027import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
028import org.kuali.rice.krad.uif.view.ViewModel;
029import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
030
031public class FlagCategoryKeyValueFinder extends UifKeyValuesFinderBase {
032        
033        private static final long serialVersionUID = 1L;
034        
035        @Override
036        public List<KeyValue> getKeyValues() {            
037                List<KeyValue> keyValues = new ArrayList<KeyValue>();
038                List<String> categories = PmServiceLocator.getPositionFlagService().getAllActiveFlagCategories();
039                keyValues.add(new ConcreteKeyValue("", "Select category to see flags"));
040                if(CollectionUtils.isNotEmpty(categories)) {
041                        for(String aCategory : categories) {
042                                keyValues.add(new ConcreteKeyValue(aCategory, aCategory));
043                        }
044                }         
045                return keyValues;
046        }
047        
048        @Override
049        public List<KeyValue> getKeyValues(ViewModel model) {
050                List<KeyValue> options = new ArrayList<KeyValue>();
051                MaintenanceDocumentForm docForm = (MaintenanceDocumentForm) model; 
052                HrBusinessObject anHrObject = (HrBusinessObject) docForm.getDocument().getNewMaintainableObject().getDataObject();
053                if(anHrObject.getEffectiveDate() != null) {
054                        List<PositionFlag> flagList = PmServiceLocator.getPositionFlagService().getAllActivePositionFlags(null, null, anHrObject.getEffectiveLocalDate());
055                        options.add(new ConcreteKeyValue("", "Select category to see flags"));
056                        if(CollectionUtils.isNotEmpty(flagList)) {
057                                for(PositionFlag aFlag : flagList) {
058                                        options.add(new ConcreteKeyValue((String) aFlag.getCategory(), (String) aFlag.getCategory()));
059                                }
060                        }         
061                } else {
062                        options = this.getKeyValues();
063                }
064                
065        return options;
066    }
067
068}