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