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