001    /*
002     * Copyright 2011 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.kfs.coa.businessobject.options;
017    
018    import java.util.ArrayList;
019    import java.util.Collections;
020    import java.util.List;
021    
022    import org.kuali.kfs.coa.businessobject.MandatoryTransferEliminationCode;
023    import org.kuali.kfs.sys.context.SpringContext;
024    import org.kuali.rice.kns.lookup.keyvalues.KeyValuesBase;
025    import org.kuali.rice.kns.service.KeyValuesService;
026    import org.kuali.rice.core.util.KeyLabelPair;
027    
028    /**
029     * This class creates a new finder for our forms view (creates a drop-down of {@link MandatoryTransferEliminationCode}s)
030     */
031    public class MandatoryTransferEliminationCodeValuesFinder extends KeyValuesBase {
032    
033        /**
034         * Creates a list of {@link MandatoryTransferEliminationCode}s using their code as their key, and their code "-" name as the
035         * display value
036         * 
037         * @see org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder#getKeyValues()
038         */
039        public List getKeyValues() {
040    
041            // get a list of all Mandatory Transfer Elimination Codes
042            List<MandatoryTransferEliminationCode> codes = (List<MandatoryTransferEliminationCode>) SpringContext.getBean(KeyValuesService.class).findAll(MandatoryTransferEliminationCode.class);
043            // copy the list of codes before sorting, since we can't modify the results from this method
044            if ( codes == null ) {
045                codes = new ArrayList<MandatoryTransferEliminationCode>(0);
046            } else {
047                codes = new ArrayList<MandatoryTransferEliminationCode>( codes );
048            }
049    
050            // sort using comparator.
051            Collections.sort(codes, new MandatoryTransferEliminationCodeComparator());
052    
053            // create a new list (code, descriptive-name)
054            List<KeyLabelPair> labels = new ArrayList<KeyLabelPair>();
055    
056            for (MandatoryTransferEliminationCode mteCode : codes) {
057                if(mteCode.isActive()) {
058                    labels.add(new KeyLabelPair(mteCode.getCode(), mteCode.getCodeAndDescription()));
059                }
060            }
061    
062            return labels;
063        }
064    
065    }