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 org.apache.commons.beanutils.PropertyUtils;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.kuali.rice.core.api.util.ConcreteKeyValue;
22 import org.kuali.rice.core.api.util.KeyValue;
23 import org.kuali.rice.krad.bo.PersistableBusinessObject;
24 import org.kuali.rice.krad.service.KRADServiceLocator;
25 import org.kuali.rice.krad.service.KeyValuesService;
26 import org.springframework.transaction.annotation.Transactional;
27
28 import java.lang.reflect.InvocationTargetException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.List;
32
33
34
35
36
37
38 @Transactional
39 public class PersistableBusinessObjectValuesFinder <T extends PersistableBusinessObject> extends KeyValuesBase {
40
41 private static final Log LOG = LogFactory.getLog(PersistableBusinessObjectValuesFinder.class);
42
43 private Class<T> businessObjectClass;
44 private String keyAttributeName;
45 private String labelAttributeName;
46 private boolean includeKeyInDescription = false;
47 private boolean includeBlankRow = false;
48
49
50
51
52
53
54
55
56 @Override
57 public List<KeyValue> getKeyValues() {
58 List<KeyValue> labels = new ArrayList<KeyValue>();
59
60 try {
61 KeyValuesService boService = KRADServiceLocator.getKeyValuesService();
62 Collection<T> objects = boService.findAll(businessObjectClass);
63 if(includeBlankRow) {
64 labels.add(new ConcreteKeyValue("", ""));
65 }
66 for (T object : objects) {
67 Object key = PropertyUtils.getProperty(object, keyAttributeName);
68 String label = (String)PropertyUtils.getProperty(object, labelAttributeName);
69 if (includeKeyInDescription) {
70 label = key + " - " + label;
71 }
72 labels.add(new ConcreteKeyValue(key.toString(), label));
73 }
74 } catch (IllegalAccessException e) {
75 LOG.debug(e.getMessage(), e);
76 LOG.error(e.getMessage());
77 throw new RuntimeException("IllegalAccessException occurred while trying to build keyValues List. dataObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e);
78 } catch (InvocationTargetException e) {
79 LOG.debug(e.getMessage(), e);
80 LOG.error(e.getMessage());
81 throw new RuntimeException("InvocationTargetException occurred while trying to build keyValues List. dataObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e);
82 } catch (NoSuchMethodException e) {
83 LOG.debug(e.getMessage(), e);
84 LOG.error(e.getMessage());
85 throw new RuntimeException("NoSuchMethodException occurred while trying to build keyValues List. dataObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e);
86 }
87
88 return labels;
89 }
90
91
92
93
94 public Class<T> getBusinessObjectClass() {
95 return this.businessObjectClass;
96 }
97
98
99
100
101 public void setBusinessObjectClass(Class<T> businessObjectClass) {
102 this.businessObjectClass = businessObjectClass;
103 }
104
105
106
107
108 public boolean isIncludeKeyInDescription() {
109 return this.includeKeyInDescription;
110 }
111
112
113
114
115 public void setIncludeKeyInDescription(boolean includeKeyInDescription) {
116 this.includeKeyInDescription = includeKeyInDescription;
117 }
118
119
120
121
122 public String getKeyAttributeName() {
123 return this.keyAttributeName;
124 }
125
126
127
128
129 public void setKeyAttributeName(String keyAttributeName) {
130 this.keyAttributeName = keyAttributeName;
131 }
132
133
134
135
136 public String getLabelAttributeName() {
137 return this.labelAttributeName;
138 }
139
140
141
142
143 public void setLabelAttributeName(String labelAttributeName) {
144 this.labelAttributeName = labelAttributeName;
145 }
146
147
148
149
150 public boolean isIncludeBlankRow() {
151 return this.includeBlankRow;
152 }
153
154
155
156
157 public void setIncludeBlankRow(boolean includeBlankRow) {
158 this.includeBlankRow = includeBlankRow;
159 }
160
161 }