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