1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
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
49
50
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 }