1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.lookup; |
17 | |
|
18 | |
import java.lang.reflect.InvocationTargetException; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Collection; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import java.util.Set; |
25 | |
|
26 | |
import org.apache.commons.beanutils.PropertyUtils; |
27 | |
import org.apache.commons.lang.StringUtils; |
28 | |
import org.kuali.rice.kns.bo.BusinessObject; |
29 | |
import org.kuali.rice.kns.bo.SelectedObjectIds; |
30 | |
import org.kuali.rice.kns.datadictionary.AttributeDefinition; |
31 | |
import org.kuali.rice.kns.datadictionary.BusinessObjectEntry; |
32 | |
import org.kuali.rice.kns.exception.AuthorizationException; |
33 | |
import org.kuali.rice.kns.service.DataDictionaryService; |
34 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
35 | |
import org.kuali.rice.kns.web.format.Formatter; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class DataDictionaryLookupResultsSupportStrategy implements |
44 | |
LookupResultsSupportStrategyService { |
45 | |
|
46 | |
private DataDictionaryService dataDictionaryService; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public String getLookupIdForBusinessObject(BusinessObject businessObject) { |
53 | 0 | final List<String> pkFieldNames = getPrimaryKeyFieldsForBusinessObject(businessObject.getClass()); |
54 | 0 | return convertPKFieldMapToLookupId(pkFieldNames, businessObject); |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public boolean qualifiesForStrategy(Class<? extends BusinessObject> boClass) { |
62 | 0 | if (getLookupableForBusinessObject(boClass) == null) { |
63 | 0 | return false; |
64 | |
} |
65 | 0 | final List<String> pkFields = getPrimaryKeyFieldsForBusinessObject(boClass); |
66 | 0 | return pkFields != null && pkFields.size() > 0; |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public <T extends BusinessObject> Collection<T> retrieveSelectedResultBOs(Class<T> boClass, Set<String> lookupIds) throws Exception { |
74 | |
|
75 | 0 | List<T> retrievedBusinessObjects = new ArrayList<T>(); |
76 | 0 | final Lookupable lookupable = getLookupableForBusinessObject(boClass); |
77 | 0 | for (String lookupId : lookupIds) { |
78 | 0 | final Map<String, String> lookupKeys = convertLookupIdToPKFieldMap(lookupId, boClass); |
79 | 0 | List<BusinessObject> bos = lookupable.getSearchResults(lookupKeys); |
80 | |
|
81 | |
|
82 | 0 | for (BusinessObject bo : bos) { |
83 | 0 | retrievedBusinessObjects.add((T)bo); |
84 | |
} |
85 | 0 | } |
86 | |
|
87 | 0 | return retrievedBusinessObjects; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
protected Lookupable getLookupableForBusinessObject(Class<? extends BusinessObject> businessObjectClass) { |
97 | 0 | final BusinessObjectEntry boEntry = getBusinessObjectEntry(businessObjectClass); |
98 | 0 | if (boEntry == null) { |
99 | 0 | return null; |
100 | |
} |
101 | |
|
102 | 0 | final String lookupableId = boEntry.getLookupDefinition().getLookupableID(); |
103 | 0 | return KNSServiceLocator.getLookupable(lookupableId); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
protected List<String> getPrimaryKeyFieldsForBusinessObject(Class<? extends BusinessObject> businessObjectClass) { |
113 | 0 | final BusinessObjectEntry boEntry = getBusinessObjectEntry(businessObjectClass); |
114 | 0 | if (boEntry == null) { |
115 | 0 | return null; |
116 | |
} |
117 | 0 | return boEntry.getPrimaryKeys(); |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
protected Map<String, String> convertLookupIdToPKFieldMap(String lookupId, Class<? extends BusinessObject> businessObjectClass) { |
128 | 0 | Map<String, String> pkFields = new HashMap<String, String>(); |
129 | 0 | if (!StringUtils.isBlank(lookupId)) { |
130 | 0 | final String[] pkValues = lookupId.split("\\|"); |
131 | 0 | for (String pkValue : pkValues) { |
132 | 0 | if (!StringUtils.isBlank(pkValue)) { |
133 | 0 | final String[] pkPieces = pkValue.split("-"); |
134 | 0 | if (!StringUtils.isBlank(pkPieces[0]) && !StringUtils.isBlank(pkPieces[1])) { |
135 | 0 | pkFields.put(pkPieces[0], pkPieces[1]); |
136 | |
} |
137 | |
} |
138 | |
} |
139 | |
} |
140 | 0 | return pkFields; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
protected String convertPKFieldMapToLookupId(List<String> pkFieldNames, BusinessObject businessObject) { |
150 | 0 | StringBuilder lookupId = new StringBuilder(); |
151 | 0 | for (String pkFieldName : pkFieldNames) { |
152 | |
try { |
153 | 0 | final Object value = PropertyUtils.getProperty(businessObject, pkFieldName); |
154 | |
|
155 | 0 | if (value != null) { |
156 | 0 | lookupId.append(pkFieldName); |
157 | 0 | lookupId.append("-"); |
158 | 0 | final Formatter formatter = retrieveBestFormatter(pkFieldName, businessObject.getClass()); |
159 | 0 | final String formattedValue = (formatter != null) ? formatter.format(value).toString() : value.toString(); |
160 | |
|
161 | 0 | lookupId.append(formattedValue); |
162 | |
} |
163 | 0 | lookupId.append("|"); |
164 | 0 | } catch (IllegalAccessException iae) { |
165 | 0 | throw new RuntimeException("Could not retrieve pk field value "+pkFieldName+" from business object "+businessObject.getClass().getName(), iae); |
166 | 0 | } catch (InvocationTargetException ite) { |
167 | 0 | throw new RuntimeException("Could not retrieve pk field value "+pkFieldName+" from business object "+businessObject.getClass().getName(), ite); |
168 | 0 | } catch (NoSuchMethodException nsme) { |
169 | 0 | throw new RuntimeException("Could not retrieve pk field value "+pkFieldName+" from business object "+businessObject.getClass().getName(), nsme); |
170 | 0 | } |
171 | |
} |
172 | 0 | return lookupId.substring(0, lookupId.length() - 1); |
173 | |
} |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
protected Formatter retrieveBestFormatter(String propertyName, Class<? extends BusinessObject> boClass) { |
184 | 0 | Formatter formatter = null; |
185 | |
|
186 | |
try { |
187 | 0 | Class<? extends Formatter> formatterClass = null; |
188 | |
|
189 | 0 | final BusinessObjectEntry boEntry = getBusinessObjectEntry(boClass); |
190 | 0 | if (boEntry != null) { |
191 | 0 | final AttributeDefinition attributeDefinition = boEntry.getAttributeDefinition(propertyName); |
192 | 0 | if (attributeDefinition != null && attributeDefinition.hasFormatterClass()) { |
193 | 0 | formatterClass = (Class<? extends Formatter>)Class.forName(attributeDefinition.getFormatterClass()); |
194 | |
} |
195 | |
} |
196 | 0 | if (formatterClass == null) { |
197 | 0 | final java.lang.reflect.Field propertyField = boClass.getDeclaredField(propertyName); |
198 | 0 | if (propertyField != null) { |
199 | 0 | formatterClass = Formatter.findFormatter(propertyField.getType()); |
200 | |
} |
201 | |
} |
202 | |
|
203 | 0 | if (formatterClass != null) { |
204 | 0 | formatter = formatterClass.newInstance(); |
205 | |
} |
206 | 0 | } catch (SecurityException se) { |
207 | 0 | throw new RuntimeException("Could not retrieve good formatter", se); |
208 | 0 | } catch (ClassNotFoundException cnfe) { |
209 | 0 | throw new RuntimeException("Could not retrieve good formatter", cnfe); |
210 | 0 | } catch (NoSuchFieldException nsfe) { |
211 | 0 | throw new RuntimeException("Could not retrieve good formatter", nsfe); |
212 | 0 | } catch (IllegalAccessException iae) { |
213 | 0 | throw new RuntimeException("Could not retrieve good formatter", iae); |
214 | 0 | } catch (InstantiationException ie) { |
215 | 0 | throw new RuntimeException("Could not retrieve good formatter", ie); |
216 | 0 | } |
217 | |
|
218 | 0 | return formatter; |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
protected BusinessObjectEntry getBusinessObjectEntry(Class<? extends BusinessObject> boClass) { |
228 | 0 | return getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(boClass.getName()); |
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public DataDictionaryService getDataDictionaryService() { |
235 | 0 | return this.dataDictionaryService; |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
242 | 0 | this.dataDictionaryService = dataDictionaryService; |
243 | 0 | } |
244 | |
} |