View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.keyvalues;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.beanutils.PropertyUtils;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.kuali.rice.core.api.util.ConcreteKeyValue;
27  import org.kuali.rice.core.api.util.KeyValue;
28  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29  
30  /**
31   * This class is a Generic ValuesFinder that builds the list of KeyValuePairs it returns
32   * in getKeyValues() based on a BO along with a keyAttributeName and labelAttributeName
33   * that are specified.
34   * 
35   * @param <T> business object type
36   */
37  public class PersistableBusinessObjectValuesFinder extends KeyValuesBase {
38      private static final Log LOG = LogFactory.getLog(PersistableBusinessObjectValuesFinder.class);
39      private static final long serialVersionUID = 1L;
40  
41      protected Class<?> businessObjectClass;
42      protected String keyAttributeName;
43      protected String labelAttributeName;
44      protected boolean includeKeyInDescription = false;
45      protected boolean includeBlankRow = false;
46  
47      /**
48       * Build the list of KeyValues using the key (keyAttributeName) and
49       * label (labelAttributeName) of the list of all business objects found
50       * for the BO class specified.
51       */
52      @Override
53  	public List<KeyValue> getKeyValues() {
54      	try {
55              @SuppressWarnings("deprecation")
56              Collection<?> objects = KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(businessObjectClass, Collections.<String, String>emptyMap());
57              List<KeyValue> labels = new ArrayList<KeyValue>(objects.size());
58              if(includeBlankRow) {
59              	labels.add(new ConcreteKeyValue("", ""));
60              }
61              for (Object object : objects) {
62              	Object key = PropertyUtils.getProperty(object, keyAttributeName);
63              	String label = (String)PropertyUtils.getProperty(object, labelAttributeName);
64              	if (includeKeyInDescription) {
65              	    label = key + " - " + label;
66              	}
67              	labels.add(new ConcreteKeyValue(key.toString(), label));
68      	    }
69              return labels;
70      	} catch (Exception e) {
71              LOG.error("Exception occurred while trying to build keyValues List: " + this, e);
72              throw new RuntimeException("Exception occurred while trying to build keyValues List: " + this, e);
73      	}
74      }
75  
76      public void setBusinessObjectClass(Class<?> businessObjectClass) {
77          this.businessObjectClass = businessObjectClass;
78      }
79  
80      public void setIncludeKeyInDescription(boolean includeKeyInDescription) {
81          this.includeKeyInDescription = includeKeyInDescription;
82      }
83  
84      public void setKeyAttributeName(String keyAttributeName) {
85          this.keyAttributeName = keyAttributeName;
86      }
87  
88      public void setLabelAttributeName(String labelAttributeName) {
89          this.labelAttributeName = labelAttributeName;
90      }
91  
92  	public void setIncludeBlankRow(boolean includeBlankRow) {
93  		this.includeBlankRow = includeBlankRow;
94  	}
95  
96      @Override
97      public String toString() {
98          StringBuilder builder = new StringBuilder();
99          builder.append("PersistableBusinessObjectValuesFinder [businessObjectClass=").append(this.businessObjectClass)
100                 .append(", keyAttributeName=").append(this.keyAttributeName).append(", labelAttributeName=")
101                 .append(this.labelAttributeName).append(", includeKeyInDescription=")
102                 .append(this.includeKeyInDescription).append(", includeBlankRow=").append(this.includeBlankRow)
103                 .append("]");
104         return builder.toString();
105     }
106 
107 }