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.Collection;
020 import java.util.Iterator;
021 import java.util.List;
022
023 import org.kuali.kfs.sys.context.SpringContext;
024 import org.kuali.rice.kns.bo.KualiCodeBase;
025 import org.kuali.rice.kns.lookup.keyvalues.KeyValuesBase;
026 import org.kuali.rice.kns.service.KeyValuesService;
027 import org.kuali.rice.core.util.KeyLabelPair;
028
029 /**
030 * This class is the base class for all the ValueFinders for any class extending KualiSystemCode. Subclasses should extend this, but
031 * do nothing. Just extending this class will be sufficient to work.
032 */
033 public abstract class KualiSystemCodeValuesFinder extends KeyValuesBase {
034
035 /**
036 * Calls getValuesClass() to generate a list of key/value pairs from the {@link KualiCodeBase}'s code as the key and the code
037 * and description as the value
038 *
039 * @see org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder#getKeyValues()
040 * @return list of key/value pairs for displaying on the client side
041 */
042 public List getKeyValues() {
043
044 // get all the KualiCodeService objects that are associated with this class
045 Collection businessObjects = SpringContext.getBean(KeyValuesService.class).findAll(this.getValuesClass());
046 List keyLabels = new ArrayList();
047
048 // add a blank pair for the first/default key/value pair
049 keyLabels.add(new KeyLabelPair("", ""));
050
051 // build the list of code/name combos
052 for (Iterator iter = businessObjects.iterator(); iter.hasNext();) {
053 KualiCodeBase businessObject = (KualiCodeBase) iter.next();
054 keyLabels.add(new KeyLabelPair(businessObject.getCode(), businessObject.getCodeAndDescription()));
055 }
056
057 return keyLabels;
058 }
059
060 /**
061 * This method must be implemented by the base class, should return the Class of the object being looked up
062 *
063 * @return class of object being looked up
064 */
065 protected abstract Class getValuesClass();
066
067 }