1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.web.struts.form; |
17 | |
|
18 | |
import java.lang.reflect.Constructor; |
19 | |
import java.security.GeneralSecurityException; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Enumeration; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.List; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.servlet.http.HttpServletRequest; |
27 | |
|
28 | |
import org.apache.commons.lang.StringUtils; |
29 | |
import org.kuali.rice.core.api.encryption.EncryptionService; |
30 | |
import org.kuali.rice.core.api.services.CoreApiServiceLocator; |
31 | |
import org.kuali.rice.kns.authorization.AuthorizationConstants; |
32 | |
import org.kuali.rice.kns.bo.Exporter; |
33 | |
import org.kuali.rice.kns.datadictionary.BusinessObjectEntry; |
34 | |
import org.kuali.rice.kns.datadictionary.exception.UnknownBusinessClassAttributeException; |
35 | |
import org.kuali.rice.kns.inquiry.Inquirable; |
36 | |
import org.kuali.rice.kns.service.*; |
37 | |
import org.kuali.rice.kns.util.KNSConstants; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class InquiryForm extends KualiForm { |
43 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InquiryForm.class); |
44 | |
|
45 | |
private static final long serialVersionUID = 1L; |
46 | |
private String fieldConversions; |
47 | |
private List sections; |
48 | |
private String businessObjectClassName; |
49 | |
private Map editingMode; |
50 | |
private String formKey; |
51 | |
private boolean canExport; |
52 | |
|
53 | |
@Override |
54 | |
public void addRequiredNonEditableProperties(){ |
55 | 0 | super.addRequiredNonEditableProperties(); |
56 | 0 | registerRequiredNonEditableProperty(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE); |
57 | 0 | registerRequiredNonEditableProperty(KNSConstants.DISPATCH_REQUEST_PARAMETER); |
58 | 0 | registerRequiredNonEditableProperty(KNSConstants.DOC_FORM_KEY); |
59 | 0 | registerRequiredNonEditableProperty(KNSConstants.FORM_KEY); |
60 | 0 | registerRequiredNonEditableProperty(KNSConstants.FIELDS_CONVERSION_PARAMETER); |
61 | 0 | registerRequiredNonEditableProperty(KNSConstants.BACK_LOCATION); |
62 | 0 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private Map<String, String> inquiryPrimaryKeys; |
69 | |
|
70 | |
private Map<String, String> inquiryDecryptedPrimaryKeys; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private Map<String, Boolean> inactiveRecordDisplay; |
76 | |
|
77 | |
private Inquirable inquirable; |
78 | |
|
79 | |
public InquiryForm() { |
80 | 0 | super(); |
81 | 0 | this.editingMode = new HashMap(); |
82 | 0 | this.editingMode.put(AuthorizationConstants.EditMode.VIEW_ONLY, "TRUE"); |
83 | 0 | this.inactiveRecordDisplay = null; |
84 | 0 | } |
85 | |
|
86 | |
@Override |
87 | |
public void populate(HttpServletRequest request) { |
88 | |
|
89 | |
|
90 | 0 | this.inquirable = null; |
91 | 0 | super.populate(request); |
92 | 0 | if (request.getParameter("returnLocation") != null) { |
93 | 0 | setBackLocation(request.getParameter("returnLocation")); |
94 | |
} |
95 | 0 | if (request.getParameter(KNSConstants.DOC_FORM_KEY) != null) { |
96 | 0 | setFormKey(request.getParameter(KNSConstants.DOC_FORM_KEY)); |
97 | |
} |
98 | |
|
99 | 0 | if(!KNSConstants.DOWNLOAD_BO_ATTACHMENT_METHOD.equals(getMethodToCall())){ |
100 | 0 | inquirable = getInquirable(getBusinessObjectClassName()); |
101 | |
|
102 | |
|
103 | 0 | boolean passedFromPreviousInquiry = !KNSConstants.START_METHOD.equals(getMethodToCall()) && !KNSConstants.CONTINUE_WITH_INQUIRY_METHOD_TO_CALL.equals(getMethodToCall()) && !KNSConstants.DOWNLOAD_CUSTOM_BO_ATTACHMENT_METHOD.equals(getMethodToCall()); |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | 0 | this.inquiryPrimaryKeys = new HashMap<String, String>(); |
110 | 0 | this.inquiryDecryptedPrimaryKeys = new HashMap<String, String>(); |
111 | |
|
112 | 0 | populatePKFieldValues(request, getBusinessObjectClassName(), passedFromPreviousInquiry); |
113 | |
|
114 | 0 | populateInactiveRecordsInIntoInquirable(inquirable, request); |
115 | 0 | populateExportCapabilities(request, getBusinessObjectClassName()); |
116 | |
} |
117 | 0 | } |
118 | |
|
119 | |
protected Inquirable getInquirable(String boClassName) { |
120 | |
try { |
121 | 0 | Class customInquirableClass = null; |
122 | |
|
123 | |
try { |
124 | 0 | customInquirableClass = KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(boClassName).getInquiryDefinition().getInquirableClass(); |
125 | |
} |
126 | 0 | catch (Exception e) { |
127 | 0 | LOG.error("Unable to correlate business object class with maintenance document entry"); |
128 | 0 | } |
129 | |
|
130 | 0 | Inquirable kualiInquirable = KNSServiceLocatorWeb.getKualiInquirable(); |
131 | |
|
132 | 0 | if (customInquirableClass != null) { |
133 | 0 | Class[] defaultConstructor = new Class[] {}; |
134 | 0 | Constructor cons = customInquirableClass.getConstructor(defaultConstructor); |
135 | 0 | kualiInquirable = (Inquirable) cons.newInstance(); |
136 | |
} |
137 | |
|
138 | 0 | kualiInquirable.setBusinessObjectClass(Class.forName(boClassName)); |
139 | |
|
140 | 0 | return kualiInquirable; |
141 | |
} |
142 | 0 | catch (Exception e) { |
143 | 0 | LOG.error("Error attempting to retrieve inquirable.", e); |
144 | 0 | throw new RuntimeException("Error attempting to retrieve inquirable."); |
145 | |
} |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
private List<List<String>> getAltkeys(Class<?> clazz) { |
155 | 0 | final KualiModuleService kualiModuleService = KNSServiceLocatorWeb.getKualiModuleService(); |
156 | 0 | final ModuleService moduleService = kualiModuleService.getResponsibleModuleService(clazz); |
157 | |
|
158 | 0 | List<List<String>> altKeys = null; |
159 | 0 | if (moduleService != null) { |
160 | 0 | altKeys = moduleService.listAlternatePrimaryKeyFieldNames(clazz); |
161 | |
} |
162 | |
|
163 | 0 | return altKeys != null ? altKeys : new ArrayList<List<String>>(); |
164 | |
} |
165 | |
|
166 | |
protected void populatePKFieldValues(HttpServletRequest request, String boClassName, boolean passedFromPreviousInquiry) { |
167 | |
try { |
168 | 0 | EncryptionService encryptionService = CoreApiServiceLocator.getEncryptionService(); |
169 | 0 | DataDictionaryService dataDictionaryService = KNSServiceLocatorWeb.getDataDictionaryService(); |
170 | 0 | BusinessObjectAuthorizationService businessObjectAuthorizationService = KNSServiceLocatorWeb.getBusinessObjectAuthorizationService(); |
171 | 0 | BusinessObjectMetaDataService businessObjectMetaDataService = KNSServiceLocatorWeb.getBusinessObjectMetaDataService(); |
172 | |
|
173 | 0 | Class businessObjectClass = Class.forName(boClassName); |
174 | |
|
175 | |
|
176 | 0 | List<String> boPKeys = businessObjectMetaDataService.listPrimaryKeyFieldNames(businessObjectClass); |
177 | 0 | final List<List<String>> altKeys = this.getAltkeys(businessObjectClass); |
178 | |
|
179 | 0 | altKeys.add(boPKeys); |
180 | 0 | boolean bFound = false; |
181 | 0 | for(List<String> boKeys : altKeys ){ |
182 | 0 | if(bFound) |
183 | 0 | break; |
184 | 0 | int keyCount = boKeys.size(); |
185 | 0 | int foundCount = 0; |
186 | 0 | for (String boKey : boKeys) { |
187 | 0 | String pkParamName = boKey; |
188 | 0 | if (passedFromPreviousInquiry) { |
189 | 0 | pkParamName = KNSConstants.INQUIRY_PK_VALUE_PASSED_FROM_PREVIOUS_REQUEST_PREFIX + pkParamName; |
190 | |
} |
191 | |
|
192 | 0 | if (request.getParameter(pkParamName) != null) { |
193 | 0 | foundCount++; |
194 | 0 | String parameter = request.getParameter(pkParamName); |
195 | |
|
196 | 0 | Boolean forceUppercase = Boolean.FALSE; |
197 | |
try { |
198 | 0 | forceUppercase = dataDictionaryService.getAttributeForceUppercase(businessObjectClass, boKey); |
199 | 0 | } catch (UnknownBusinessClassAttributeException ex) { |
200 | |
|
201 | |
|
202 | |
|
203 | 0 | LOG.warn("BO class " + businessObjectClassName + " property " + boKey + " should probably have a DD definition.", ex); |
204 | 0 | } |
205 | 0 | String parameterCopy = parameter; |
206 | 0 | if (forceUppercase) { |
207 | 0 | parameter = parameter.toUpperCase(); |
208 | |
} |
209 | |
|
210 | 0 | inquiryPrimaryKeys.put(boKey, parameter); |
211 | 0 | if (businessObjectAuthorizationService.attributeValueNeedsToBeEncryptedOnFormsAndLinks(businessObjectClass, boKey)) { |
212 | |
try { |
213 | 0 | inquiryDecryptedPrimaryKeys.put(boKey, encryptionService.decrypt(parameterCopy)); |
214 | 0 | } catch (GeneralSecurityException e) { |
215 | 0 | LOG.error("BO class " + businessObjectClassName + " property " + boKey + " should have been encrypted, but there was a problem decrypting it."); |
216 | 0 | throw e; |
217 | 0 | } |
218 | |
} |
219 | |
else { |
220 | 0 | inquiryDecryptedPrimaryKeys.put(boKey, parameter); |
221 | |
} |
222 | |
} |
223 | 0 | } |
224 | 0 | if (foundCount == keyCount) { |
225 | 0 | bFound = true; |
226 | |
} |
227 | 0 | } |
228 | 0 | if(!bFound){ |
229 | 0 | LOG.error("All keys not given to lookup for bo class name " + businessObjectClass.getName()); |
230 | 0 | throw new RuntimeException("All keys not given to lookup for bo class name " + businessObjectClass.getName()); |
231 | |
} |
232 | |
} |
233 | 0 | catch (ClassNotFoundException e) { |
234 | 0 | LOG.error("Can't instantiate class: " + boClassName, e); |
235 | 0 | throw new RuntimeException("Can't instantiate class: " + boClassName); |
236 | |
} |
237 | 0 | catch (GeneralSecurityException e) { |
238 | 0 | LOG.error("Can't decrypt value", e); |
239 | 0 | throw new RuntimeException("Can't decrypt value"); |
240 | 0 | } |
241 | 0 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
protected void populateExportCapabilities(HttpServletRequest request, String boClassName) { |
248 | 0 | setCanExport(false); |
249 | 0 | BusinessObjectEntry businessObjectEntry = KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(boClassName); |
250 | 0 | Class<? extends Exporter> exporterClass = businessObjectEntry.getExporterClass(); |
251 | 0 | if (exporterClass != null) { |
252 | |
try { |
253 | 0 | Exporter exporter = exporterClass.newInstance(); |
254 | 0 | if (exporter.getSupportedFormats(businessObjectEntry.getBusinessObjectClass()).contains(KNSConstants.XML_FORMAT)) { |
255 | 0 | setCanExport(true); |
256 | |
} |
257 | 0 | } catch (Exception e) { |
258 | 0 | LOG.error("Failed to locate or create exporter class: " + exporterClass, e); |
259 | 0 | throw new RuntimeException("Failed to locate or create exporter class: " + exporterClass); |
260 | 0 | } |
261 | |
} |
262 | 0 | } |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
public String getFieldConversions() { |
269 | 0 | return fieldConversions; |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public void setFieldConversions(String fieldConversions) { |
277 | 0 | this.fieldConversions = fieldConversions; |
278 | 0 | } |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
public List getSections() { |
285 | 0 | return sections; |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
public void setSections(List sections) { |
293 | 0 | this.sections = sections; |
294 | 0 | } |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
public String getBusinessObjectClassName() { |
300 | 0 | return businessObjectClassName; |
301 | |
} |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
public void setBusinessObjectClassName(String businessObjectClassName) { |
307 | 0 | this.businessObjectClassName = businessObjectClassName; |
308 | 0 | } |
309 | |
|
310 | |
public Map getEditingMode() { |
311 | 0 | return editingMode; |
312 | |
} |
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
public Map<String, String> getInquiryPrimaryKeys() { |
321 | 0 | return this.inquiryPrimaryKeys; |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
public Map<String, String> retrieveInquiryDecryptedPrimaryKeys() { |
332 | 0 | return this.inquiryDecryptedPrimaryKeys; |
333 | |
} |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
public void setInquiryPrimaryKeys(Map<String, String> inquiryPrimaryKeys) { |
341 | 0 | this.inquiryPrimaryKeys = inquiryPrimaryKeys; |
342 | 0 | } |
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
public Map<String, Boolean> getInactiveRecordDisplay() { |
350 | 0 | return getInquirable().getInactiveRecordDisplay(); |
351 | |
} |
352 | |
|
353 | |
public Inquirable getInquirable() { |
354 | 0 | return inquirable; |
355 | |
} |
356 | |
|
357 | |
protected void populateInactiveRecordsInIntoInquirable(Inquirable inquirable, HttpServletRequest request) { |
358 | 0 | for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) { |
359 | 0 | String paramName = (String) e.nextElement(); |
360 | 0 | if (paramName.startsWith(KNSConstants.INACTIVE_RECORD_DISPLAY_PARAM_PREFIX)) { |
361 | 0 | String collectionName = StringUtils.substringAfter(paramName, KNSConstants.INACTIVE_RECORD_DISPLAY_PARAM_PREFIX); |
362 | 0 | Boolean showInactive = Boolean.parseBoolean(request.getParameter(paramName)); |
363 | 0 | inquirable.setShowInactiveRecords(collectionName, showInactive); |
364 | |
} |
365 | 0 | } |
366 | 0 | } |
367 | |
|
368 | |
public String getFormKey() { |
369 | 0 | return this.formKey; |
370 | |
} |
371 | |
|
372 | |
public void setFormKey(String formKey) { |
373 | 0 | this.formKey = formKey; |
374 | 0 | } |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
public boolean isCanExport() { |
380 | 0 | return this.canExport; |
381 | |
} |
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
public void setCanExport(boolean canExport) { |
387 | 0 | this.canExport = canExport; |
388 | 0 | } |
389 | |
|
390 | |
|
391 | |
} |