1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.struts.action;
17
18 import org.apache.commons.beanutils.PropertyUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
21 import org.apache.struts.action.ActionForm;
22 import org.apache.struts.action.ActionForward;
23 import org.apache.struts.action.ActionMapping;
24 import org.kuali.rice.core.api.CoreApiServiceLocator;
25 import org.kuali.rice.core.api.encryption.EncryptionService;
26 import org.kuali.rice.core.api.util.ClassLoaderUtils;
27 import org.kuali.rice.core.api.util.RiceConstants;
28 import org.kuali.rice.core.api.util.RiceKeyConstants;
29 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
30 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
31 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
32 import org.kuali.rice.core.framework.persistence.jpa.metadata.EntityDescriptor;
33 import org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor;
34 import org.kuali.rice.core.framework.persistence.jpa.metadata.MetadataManager;
35 import org.kuali.rice.core.web.format.Formatter;
36 import org.kuali.rice.kew.api.KewApiConstants;
37 import org.kuali.rice.kim.api.identity.Person;
38 import org.kuali.rice.kns.datadictionary.MaintainableCollectionDefinition;
39 import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
40 import org.kuali.rice.kns.document.MaintenanceDocument;
41 import org.kuali.rice.kns.document.MaintenanceDocumentBase;
42 import org.kuali.rice.kns.document.authorization.MaintenanceDocumentAuthorizer;
43 import org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions;
44 import org.kuali.rice.kns.lookup.LookupResultsService;
45 import org.kuali.rice.kns.maintenance.Maintainable;
46 import org.kuali.rice.kns.rule.event.KualiAddLineEvent;
47 import org.kuali.rice.kns.service.KNSServiceLocator;
48 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
49 import org.kuali.rice.kns.util.KNSGlobalVariables;
50 import org.kuali.rice.kns.util.MaintenanceUtils;
51 import org.kuali.rice.kns.util.WebUtils;
52 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
53 import org.kuali.rice.kns.web.struts.form.KualiForm;
54 import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
55 import org.kuali.rice.krad.bo.DocumentAttachment;
56 import org.kuali.rice.krad.bo.PersistableAttachment;
57 import org.kuali.rice.krad.bo.PersistableBusinessObject;
58 import org.kuali.rice.krad.bo.PersistableBusinessObjectExtension;
59 import org.kuali.rice.krad.exception.DocumentTypeAuthorizationException;
60 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
61 import org.kuali.rice.krad.service.LookupService;
62 import org.kuali.rice.krad.util.GlobalVariables;
63 import org.kuali.rice.krad.util.KRADConstants;
64 import org.kuali.rice.krad.util.KRADPropertyConstants;
65 import org.kuali.rice.krad.util.ObjectUtils;
66
67 import javax.persistence.PersistenceException;
68 import javax.servlet.http.HttpServletRequest;
69 import javax.servlet.http.HttpServletResponse;
70 import java.lang.reflect.Field;
71 import java.lang.reflect.InvocationTargetException;
72 import java.security.GeneralSecurityException;
73 import java.util.ArrayList;
74 import java.util.Collection;
75 import java.util.Enumeration;
76 import java.util.HashMap;
77 import java.util.Iterator;
78 import java.util.List;
79 import java.util.Map;
80
81
82
83
84 public class KualiMaintenanceDocumentAction extends KualiDocumentActionBase {
85 protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiMaintenanceDocumentAction.class);
86
87 protected MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService = null;
88 protected EncryptionService encryptionService;
89 protected LookupService lookupService;
90 protected LookupResultsService lookupResultsService;
91
92 public KualiMaintenanceDocumentAction() {
93 super();
94 maintenanceDocumentDictionaryService = KNSServiceLocator.getMaintenanceDocumentDictionaryService();
95 encryptionService = CoreApiServiceLocator.getEncryptionService();
96 }
97
98 @Override
99 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
100 request.setAttribute(KRADConstants.PARAM_MAINTENANCE_VIEW_MODE, KRADConstants.PARAM_MAINTENANCE_VIEW_MODE_MAINTENANCE);
101 return super.execute(mapping, form, request, response);
102 }
103
104
105
106
107 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
108 request.setAttribute(KRADConstants.MAINTENANCE_ACTN, KRADConstants.MAINTENANCE_NEW_ACTION);
109 return setupMaintenance(mapping, form, request, response, KRADConstants.MAINTENANCE_NEW_ACTION);
110 }
111
112
113
114
115 public ActionForward copy(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
116
117 if (request.getParameter("document." + KRADPropertyConstants.DOCUMENT_NUMBER) == null) {
118 return setupMaintenance(mapping, form, request, response, KRADConstants.MAINTENANCE_COPY_ACTION);
119 }
120 else {
121 throw new UnsupportedOperationException("System does not support copying of maintenance documents.");
122 }
123 }
124
125
126
127
128 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
129
130 return setupMaintenance(mapping, form, request, response, KRADConstants.MAINTENANCE_EDIT_ACTION);
131 }
132
133
134
135
136 public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
137 if (isFormRepresentingLockObject((KualiDocumentFormBase)form)) {
138 return super.delete(mapping, form, request, response);
139 }
140 KNSGlobalVariables.getMessageList().add(RiceKeyConstants.MESSAGE_DELETE);
141 return setupMaintenance(mapping, form, request, response, KRADConstants.MAINTENANCE_DELETE_ACTION);
142 }
143
144
145
146
147 public ActionForward newWithExisting(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
148 return setupMaintenance(mapping, form, request, response, KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION);
149 }
150
151
152
153
154
155
156
157 protected ActionForward setupMaintenance(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String maintenanceAction) throws Exception {
158 KualiMaintenanceForm maintenanceForm = (KualiMaintenanceForm) form;
159 MaintenanceDocument document = null;
160
161
162 if (maintenanceForm.getDocument() == null) {
163 if (StringUtils.isEmpty(maintenanceForm.getBusinessObjectClassName()) && StringUtils.isEmpty(maintenanceForm.getDocTypeName())) {
164 throw new IllegalArgumentException("Document type name or bo class not given!");
165 }
166
167 String documentTypeName = maintenanceForm.getDocTypeName();
168
169 if (StringUtils.isEmpty(documentTypeName)) {
170 documentTypeName = maintenanceDocumentDictionaryService.getDocumentTypeName(Class.forName(maintenanceForm.getBusinessObjectClassName()));
171 maintenanceForm.setDocTypeName(documentTypeName);
172 }
173
174 if (StringUtils.isEmpty(documentTypeName)) {
175 throw new RuntimeException("documentTypeName is empty; does this Business Object have a maintenance document definition? " + maintenanceForm.getBusinessObjectClassName());
176 }
177
178
179 if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction) || KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceAction)) {
180 Class boClass = maintenanceDocumentDictionaryService.getDataObjectClass(documentTypeName);
181 boolean allowsNewOrCopy = getBusinessObjectAuthorizationService().canCreate(boClass, GlobalVariables.getUserSession().getPerson(), documentTypeName);
182 if (!allowsNewOrCopy) {
183 LOG.error("Document type " + documentTypeName + " does not allow new or copy actions.");
184 throw new DocumentTypeAuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalId(), "newOrCopy", documentTypeName);
185 }
186 }
187
188
189 document = (MaintenanceDocument) getDocumentService().getNewDocument(maintenanceForm.getDocTypeName());
190
191
192
193
194
195
196
197
198
199 maintenanceForm.setDocument(document);
200 }
201 else {
202 document = (MaintenanceDocument) maintenanceForm.getDocument();
203 }
204
205
206 if (!(KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) && !(KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION.equals(maintenanceAction))) {
207 Map requestParameters = buildKeyMapFromRequest(document.getNewMaintainableObject(), request);
208 PersistableBusinessObject oldBusinessObject = null;
209 try {
210 oldBusinessObject = (PersistableBusinessObject) getLookupService().findObjectBySearch(Class.forName(maintenanceForm.getBusinessObjectClassName()), requestParameters);
211 } catch ( ClassNotPersistenceCapableException ex ) {
212 if ( !document.getOldMaintainableObject().isExternalBusinessObject() ) {
213 throw new RuntimeException( "BO Class: " + maintenanceForm.getBusinessObjectClassName() + " is not persistable and is not externalizable - configuration error" );
214 }
215
216 }
217 if (oldBusinessObject == null && !document.getOldMaintainableObject().isExternalBusinessObject()) {
218 throw new RuntimeException("Cannot retrieve old record for maintenance document, incorrect parameters passed on maint url: " + requestParameters );
219 }
220
221 if(document.getOldMaintainableObject().isExternalBusinessObject()){
222 if ( oldBusinessObject == null ) {
223 try {
224 oldBusinessObject = (PersistableBusinessObject)document.getOldMaintainableObject().getBoClass().newInstance();
225 } catch ( Exception ex ) {
226 throw new RuntimeException( "External BO maintainable was null and unable to instantiate for old maintainable object.", ex );
227 }
228 }
229 populateBOWithCopyKeyValues(request, oldBusinessObject, document.getOldMaintainableObject());
230 document.getOldMaintainableObject().prepareBusinessObject(oldBusinessObject);
231 oldBusinessObject = document.getOldMaintainableObject().getBusinessObject();
232 }
233
234
235 final String TMP_NM = oldBusinessObject.getClass().getName();
236 final int START_INDEX = TMP_NM.indexOf('.', TMP_NM.indexOf('.') + 1) + 1;
237 if ( ( OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled(TMP_NM.substring(START_INDEX, TMP_NM.indexOf('.', TMP_NM.indexOf('.', START_INDEX) + 1))) ) &&
238 OrmUtils.isJpaAnnotated(oldBusinessObject.getClass()) && oldBusinessObject.getExtension() != null && OrmUtils.isJpaAnnotated(oldBusinessObject.getExtension().getClass())) {
239 if (oldBusinessObject.getExtension() != null) {
240 PersistableBusinessObjectExtension boe = oldBusinessObject.getExtension();
241 EntityDescriptor entity = MetadataManager.getEntityDescriptor(oldBusinessObject.getExtension().getClass());
242 Criteria extensionCriteria = new Criteria(boe.getClass().getName());
243 for (FieldDescriptor fieldDescriptor : entity.getPrimaryKeys()) {
244 try {
245 Field field = oldBusinessObject.getClass().getDeclaredField(fieldDescriptor.getName());
246 field.setAccessible(true);
247 extensionCriteria.eq(fieldDescriptor.getName(), field.get(oldBusinessObject));
248 } catch (Exception e) {
249 LOG.error(e.getMessage(),e);
250 }
251 }
252 try {
253 boe = (PersistableBusinessObjectExtension) new QueryByCriteria(getEntityManagerFactory().createEntityManager(), extensionCriteria).toQuery().getSingleResult();
254 } catch (PersistenceException e) {}
255 oldBusinessObject.setExtension(boe);
256 }
257 }
258
259 PersistableBusinessObject newBusinessObject = (PersistableBusinessObject) ObjectUtils.deepCopy(oldBusinessObject);
260
261
262 Class<? extends PersistableBusinessObject> businessObjectClass = ClassLoaderUtils.getClass(maintenanceForm.getBusinessObjectClassName(), PersistableBusinessObject.class);
263 document.getOldMaintainableObject().setBusinessObject(oldBusinessObject);
264 document.getOldMaintainableObject().setBoClass(businessObjectClass);
265 document.getNewMaintainableObject().setBusinessObject(newBusinessObject);
266 document.getNewMaintainableObject().setBoClass(businessObjectClass);
267
268
269
270 if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceAction)) {
271 if (!document.isFieldsClearedOnCopy()) {
272
273 Class boClass = maintenanceDocumentDictionaryService.getDataObjectClass(
274 maintenanceForm.getDocTypeName());
275 if(!maintenanceDocumentDictionaryService.getPreserveLockingKeysOnCopy(boClass))
276 clearPrimaryKeyFields(document);
277
278 clearUnauthorizedNewFields(document);
279
280 Maintainable maintainable = document.getNewMaintainableObject();
281
282 maintainable.processAfterCopy( document, request.getParameterMap() );
283
284
285 document.setFieldsClearedOnCopy(true);
286
287
288 maintainable.setGenerateBlankRequiredValues(maintenanceForm.getDocTypeName());
289 }
290 }
291 else if (KRADConstants.MAINTENANCE_EDIT_ACTION.equals(maintenanceAction)) {
292 boolean allowsEdit = getBusinessObjectAuthorizationService().canMaintain(oldBusinessObject, GlobalVariables.getUserSession().getPerson(), document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
293 if (!allowsEdit) {
294 LOG.error("Document type " + document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName() + " does not allow edit actions.");
295 throw new DocumentTypeAuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalId(), "edit", document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
296 }
297 document.getNewMaintainableObject().processAfterEdit( document, request.getParameterMap() );
298 }
299
300 else if (KRADConstants.MAINTENANCE_DELETE_ACTION.equals(maintenanceAction)) {
301 boolean allowsDelete = getBusinessObjectAuthorizationService().canMaintain(oldBusinessObject, GlobalVariables.getUserSession().getPerson(), document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
302 if (!allowsDelete) {
303 LOG.error("Document type " + document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName() + " does not allow delete actions.");
304 throw new DocumentTypeAuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalId(), "delete", document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
305 }
306
307 }
308
309
310
311
312
313
314
315 }
316
317 if (KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION.equals(maintenanceAction)) {
318
319
320
321 PersistableBusinessObject newBO = document.getNewMaintainableObject().getBusinessObject();
322 Map<String, String> parameters = buildKeyMapFromRequest(document.getNewMaintainableObject(), request);
323 copyParametersToBO(parameters, newBO);
324 newBO.refresh();
325 document.getNewMaintainableObject().setupNewFromExisting( document, request.getParameterMap() );
326 }
327
328
329 if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
330 document.getNewMaintainableObject().setGenerateDefaultValues(maintenanceForm.getDocTypeName());
331 document.getNewMaintainableObject().processAfterNew( document, request.getParameterMap() );
332
333
334 MaintenanceUtils.checkForLockingDocument(document.getNewMaintainableObject(), false);
335 }
336
337
338 document.getNewMaintainableObject().setMaintenanceAction(maintenanceAction);
339 maintenanceForm.setMaintenanceAction(maintenanceAction);
340
341
342 MaintenanceDocumentEntry entry = maintenanceDocumentDictionaryService.getMaintenanceDocumentEntry(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
343 if (LOG.isDebugEnabled()) {
344 LOG.debug("maintenanceForm.getAdditionalScriptFiles(): " + maintenanceForm.getAdditionalScriptFiles());
345 }
346 if (maintenanceForm.getAdditionalScriptFiles().isEmpty()) {
347 maintenanceForm.getAdditionalScriptFiles().addAll(entry.getWebScriptFiles());
348 }
349
350
351 document.setDisplayTopicFieldInNotes(entry.getDisplayTopicFieldInNotes());
352
353 return mapping.findForward(RiceConstants.MAPPING_BASIC);
354 }
355
356 protected void populateBOWithCopyKeyValues(HttpServletRequest request, PersistableBusinessObject oldBusinessObject, Maintainable oldMaintainableObject) throws Exception{
357 List keyFieldNamesToCopy = new ArrayList();
358 Map<String, String> parametersToCopy;
359 if (!StringUtils.isBlank(request.getParameter(KRADConstants.COPY_KEYS))) {
360 String[] copyKeys = request.getParameter(KRADConstants.COPY_KEYS).split(KRADConstants.FIELD_CONVERSIONS_SEPARATOR);
361 for (String copyKey: copyKeys) {
362 keyFieldNamesToCopy.add(copyKey);
363 }
364 }
365 parametersToCopy = getRequestParameters(keyFieldNamesToCopy, oldMaintainableObject, request);
366 if(parametersToCopy!=null && parametersToCopy.size()>0){
367 copyParametersToBO(parametersToCopy, oldBusinessObject);
368 }
369 }
370
371 protected void copyParametersToBO(Map<String, String> parameters, PersistableBusinessObject newBO) throws Exception{
372 for (String parmName : parameters.keySet()) {
373 String propertyValue = parameters.get(parmName);
374
375 if (StringUtils.isNotBlank(propertyValue)) {
376 String propertyName = parmName;
377
378 if (PropertyUtils.isWriteable(newBO, propertyName)) {
379 Class type = ObjectUtils.easyGetPropertyType(newBO, propertyName);
380 if (type != null && Formatter.getFormatter(type) != null) {
381 Formatter formatter = Formatter.getFormatter(type);
382 Object obj = formatter.convertFromPresentationFormat(propertyValue);
383 ObjectUtils.setObjectProperty(newBO, propertyName, obj.getClass(), obj);
384 }
385 else {
386 ObjectUtils.setObjectProperty(newBO, propertyName, String.class, propertyValue);
387 }
388 }
389 }
390 }
391 }
392
393
394
395
396
397
398
399
400
401
402
403 public ActionForward downloadAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
404 KualiDocumentFormBase documentForm = (KualiDocumentFormBase) form;
405 MaintenanceDocumentBase document = (MaintenanceDocumentBase) documentForm.getDocument();
406 document.refreshReferenceObject("attachment");
407 DocumentAttachment attachment = document.getAttachment();
408 if(attachment != null) {
409 streamToResponse(attachment.getAttachmentContent(), attachment.getFileName(), attachment.getContentType(), response);
410 }
411 return null;
412 }
413
414
415
416
417
418
419
420
421
422
423
424
425 public ActionForward replaceAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request,
426 HttpServletResponse response) throws Exception {
427 KualiDocumentFormBase documentForm = (KualiDocumentFormBase) form;
428 MaintenanceDocumentBase document = (MaintenanceDocumentBase) documentForm.getDocument();
429 document.refreshReferenceObject("attachment");
430 getBusinessObjectService().delete(document.getAttachment());
431 return mapping.findForward(RiceConstants.MAPPING_BASIC);
432 }
433
434
435
436
437
438
439
440
441
442
443
444 @Override
445 public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
446 KualiDocumentFormBase documentForm = (KualiDocumentFormBase) form;
447 MaintenanceDocumentBase document = (MaintenanceDocumentBase) documentForm.getDocument();
448
449 ActionForward forward = super.route(mapping, form, request, response);
450 if(document.getNewMaintainableObject().getBusinessObject() instanceof PersistableAttachment) {
451 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService().retrieve(document.getNewMaintainableObject().getBusinessObject());
452 request.setAttribute("fileName", bo.getFileName());
453 }
454
455 return forward;
456 }
457
458
459
460
461 @Override
462 public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
463 super.docHandler(mapping, form, request, response);
464 KualiMaintenanceForm kualiMaintenanceForm = (KualiMaintenanceForm) form;
465
466 if (KewApiConstants.ACTIONLIST_COMMAND.equals(kualiMaintenanceForm.getCommand()) || KewApiConstants.DOCSEARCH_COMMAND.equals(kualiMaintenanceForm.getCommand()) || KewApiConstants.SUPERUSER_COMMAND.equals(kualiMaintenanceForm.getCommand()) || KewApiConstants.HELPDESK_ACTIONLIST_COMMAND.equals(kualiMaintenanceForm.getCommand()) && kualiMaintenanceForm.getDocId() != null) {
467 if (kualiMaintenanceForm.getDocument() instanceof MaintenanceDocument) {
468 kualiMaintenanceForm.setReadOnly(true);
469 kualiMaintenanceForm.setMaintenanceAction(((MaintenanceDocument) kualiMaintenanceForm.getDocument()).getNewMaintainableObject().getMaintenanceAction());
470
471
472 Maintainable tmpMaintainable = ((MaintenanceDocument) kualiMaintenanceForm.getDocument()).getNewMaintainableObject();
473 if(tmpMaintainable.getBusinessObject() instanceof PersistableAttachment) {
474 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService().retrieve(tmpMaintainable.getBusinessObject());
475 if(bo != null)
476 request.setAttribute("fileName", bo.getFileName());
477 }
478 }
479 else {
480 LOG.error("Illegal State: document is not a maintenance document");
481 throw new IllegalStateException("Document is not a maintenance document");
482 }
483 }
484 else if (KewApiConstants.INITIATE_COMMAND.equals(kualiMaintenanceForm.getCommand())) {
485 kualiMaintenanceForm.setReadOnly(false);
486 return setupMaintenance(mapping, form, request, response, KRADConstants.MAINTENANCE_NEW_ACTION);
487 }
488 else {
489 LOG.error("We should never have gotten to here");
490 throw new IllegalStateException("docHandler called with invalid parameters");
491 }
492 return mapping.findForward(RiceConstants.MAPPING_BASIC);
493 }
494
495
496
497
498 @Override
499 public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
500 KualiMaintenanceForm maintenanceForm = (KualiMaintenanceForm) form;
501
502 WebUtils.reuseErrorMapFromPreviousRequest(maintenanceForm);
503 maintenanceForm.setDerivedValuesOnForm(request);
504
505 refreshAdHocRoutingWorkgroupLookups(request, maintenanceForm);
506 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
507
508
509 Map<String, String> requestParams = new HashMap<String, String>();
510 for (Enumeration i = request.getParameterNames(); i.hasMoreElements();) {
511 String requestKey = (String) i.nextElement();
512 String requestValue = request.getParameter(requestKey);
513 requestParams.put(requestKey, requestValue);
514 }
515
516
517 Collection<PersistableBusinessObject> rawValues = null;
518 if (StringUtils.equals(KRADConstants.MULTIPLE_VALUE, maintenanceForm.getRefreshCaller())) {
519 String lookupResultsSequenceNumber = maintenanceForm.getLookupResultsSequenceNumber();
520 if (StringUtils.isNotBlank(lookupResultsSequenceNumber)) {
521
522 String lookupResultsBOClassName = maintenanceForm.getLookupResultsBOClassName();
523 Class lookupResultsBOClass = Class.forName(lookupResultsBOClassName);
524
525 rawValues = getLookupResultsService().retrieveSelectedResultBOs(lookupResultsSequenceNumber, lookupResultsBOClass, GlobalVariables.getUserSession().getPerson().getPrincipalId());
526 }
527 }
528
529 if (rawValues != null) {
530
531
532
533
534 String collectionName = maintenanceForm.getLookedUpCollectionName();
535
536
537
538
539
540
541
542
543
544
545
546
547
548 document.getNewMaintainableObject().addMultipleValueLookupResults(document, collectionName, rawValues, false, document.getNewMaintainableObject().getBusinessObject());
549 if (LOG.isInfoEnabled()) {
550 LOG.info("********************doing editing 3 in refersh()***********************.");
551 }
552 boolean isEdit = KRADConstants.MAINTENANCE_EDIT_ACTION.equals(maintenanceForm.getMaintenanceAction());
553 boolean isCopy = KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceForm.getMaintenanceAction());
554
555 if (isEdit || isCopy) {
556 document.getOldMaintainableObject().addMultipleValueLookupResults(document, collectionName, rawValues, true, document.getOldMaintainableObject().getBusinessObject());
557 document.getOldMaintainableObject().refresh(maintenanceForm.getRefreshCaller(), requestParams, document);
558 }
559 }
560
561 document.getNewMaintainableObject().refresh(maintenanceForm.getRefreshCaller(), requestParams, document);
562
563
564 String fullParameter = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
565 if(StringUtils.contains(fullParameter, KRADConstants.CUSTOM_ACTION)){
566 String customAction = StringUtils.substringBetween(fullParameter, KRADConstants.METHOD_TO_CALL_PARM1_LEFT_DEL, KRADConstants.METHOD_TO_CALL_PARM1_RIGHT_DEL);
567 String[] actionValue = new String[1];
568 actionValue[0]= StringUtils.substringAfter(customAction, ".");
569 Map<String,String[]> paramMap = new HashMap<String,String[]>(request.getParameterMap());
570 paramMap.put(KRADConstants.CUSTOM_ACTION, actionValue);
571 doProcessingAfterPost( (KualiMaintenanceForm) form, paramMap );
572 }
573
574 return mapping.findForward(RiceConstants.MAPPING_BASIC);
575 }
576
577
578
579
580
581 protected Map buildKeyMapFromRequest(Maintainable maintainable, HttpServletRequest request) {
582 List keyFieldNames = null;
583
584
585 if (!StringUtils.isBlank(request.getParameter(KRADConstants.OVERRIDE_KEYS))) {
586 String[] overrideKeys = request.getParameter(KRADConstants.OVERRIDE_KEYS).split(KRADConstants.FIELD_CONVERSIONS_SEPARATOR);
587 keyFieldNames = new ArrayList();
588 for (String overrideKey : overrideKeys) {
589 keyFieldNames.add(overrideKey);
590 }
591 }
592 else {
593 keyFieldNames = getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(maintainable.getBusinessObject().getClass());
594 }
595 return getRequestParameters(keyFieldNames, maintainable, request);
596 }
597
598 protected Map<String, String> getRequestParameters(List keyFieldNames, Maintainable maintainable, HttpServletRequest request){
599
600 Map<String, String> requestParameters = new HashMap<String, String>();
601
602
603 for (Iterator iter = keyFieldNames.iterator(); iter.hasNext();) {
604 String keyPropertyName = (String) iter.next();
605
606 if (request.getParameter(keyPropertyName) != null) {
607 String keyValue = request.getParameter(keyPropertyName);
608
609
610 if (getBusinessObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(maintainable.getBoClass(), keyPropertyName)) {
611 try {
612 keyValue = StringUtils.removeEnd(keyValue, EncryptionService.ENCRYPTION_POST_PREFIX);
613 keyValue = encryptionService.decrypt(keyValue);
614 }
615 catch (GeneralSecurityException e) {
616 throw new RuntimeException(e);
617 }
618 }
619
620
621 requestParameters.put(keyPropertyName, keyValue);
622 }
623 }
624
625 return requestParameters;
626
627 }
628
629
630
631
632
633
634 String extractCollectionName(HttpServletRequest request, String methodToCall) {
635
636 String parameterName = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
637 String collectionName = null;
638 if (StringUtils.isNotBlank(parameterName)) {
639 collectionName = StringUtils.substringBetween(parameterName, methodToCall + ".", ".(");
640 }
641 return collectionName;
642 }
643
644 Collection extractCollection(PersistableBusinessObject bo, String collectionName) {
645
646 Collection maintCollection = (Collection) ObjectUtils.getPropertyValue(bo, collectionName);
647 return maintCollection;
648 }
649
650 Class extractCollectionClass(String docTypeName, String collectionName) {
651 return maintenanceDocumentDictionaryService.getCollectionBusinessObjectClass(docTypeName, collectionName);
652 }
653
654
655
656
657 public ActionForward addLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
658 KualiMaintenanceForm maintenanceForm = (KualiMaintenanceForm) form;
659 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
660 Maintainable oldMaintainable = document.getOldMaintainableObject();
661 Maintainable newMaintainable = document.getNewMaintainableObject();
662
663 String collectionName = extractCollectionName(request, KRADConstants.ADD_LINE_METHOD);
664 if (collectionName == null) {
665 LOG.error("Unable to get find collection name and class in request.");
666 throw new RuntimeException("Unable to get find collection name and class in request.");
667 }
668
669
670 if ((StringUtils.lastIndexOf(collectionName, "]") + 1) == collectionName.length()) {
671 collectionName = StringUtils.substringBeforeLast(collectionName, "[");
672 }
673
674 PersistableBusinessObject bo = newMaintainable.getBusinessObject();
675 Collection maintCollection = extractCollection(bo, collectionName);
676 Class collectionClass = extractCollectionClass(((MaintenanceDocument) maintenanceForm.getDocument()).getDocumentHeader().getWorkflowDocument().getDocumentTypeName(), collectionName);
677
678
679
680
681 PersistableBusinessObject addBO = newMaintainable.getNewCollectionLine(collectionName);
682 if (LOG.isDebugEnabled()) {
683 LOG.debug("obtained addBO from newCollectionLine: " + addBO);
684 }
685
686
687 getBusinessObjectService().linkUserFields(addBO);
688
689
690 newMaintainable.processBeforeAddLine(collectionName, collectionClass, addBO);
691
692
693 boolean rulePassed = false;
694 if (LOG.isDebugEnabled()) {
695 LOG.debug("about to call AddLineEvent applyRules: document=" + document + "\ncollectionName=" + collectionName + "\nBO=" + addBO);
696 }
697 rulePassed = getKualiRuleService().applyRules(new KualiAddLineEvent(document, collectionName, addBO));
698
699
700 if (rulePassed) {
701 if (LOG.isInfoEnabled()) {
702 LOG.info("********************doing editing 4 in addline()***********************.");
703 }
704
705 boolean isEdit = KRADConstants.MAINTENANCE_EDIT_ACTION.equals(maintenanceForm.getMaintenanceAction());
706 boolean isCopy = KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceForm.getMaintenanceAction());
707
708
709 if (isEdit || isCopy) {
710 PersistableBusinessObject oldBo = oldMaintainable.getBusinessObject();
711 Collection oldMaintCollection = (Collection) ObjectUtils.getPropertyValue(oldBo, collectionName);
712
713 if (oldMaintCollection == null) {
714 oldMaintCollection = new ArrayList();
715 }
716 if (PersistableBusinessObject.class.isAssignableFrom(collectionClass)) {
717 PersistableBusinessObject placeholder = (PersistableBusinessObject) collectionClass.newInstance();
718
719
720
721
722
723 placeholder.setNewCollectionRecord(true);
724 ((List) oldMaintCollection).add(placeholder);
725 }
726 else {
727 LOG.warn("Should be a instance of PersistableBusinessObject");
728 ((List) oldMaintCollection).add(collectionClass.newInstance());
729 }
730
731 ObjectUtils.setObjectProperty(oldBo, collectionName, List.class, oldMaintCollection);
732 }
733
734 newMaintainable.addNewLineToCollection(collectionName);
735 int subCollectionIndex = 0;
736 for (Object aSubCollection : maintCollection) {
737 subCollectionIndex += getSubCollectionIndex(aSubCollection, maintenanceForm.getDocTypeName());
738 }
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770 }
771 doProcessingAfterPost( (KualiMaintenanceForm) form, request );
772
773 return mapping.findForward(RiceConstants.MAPPING_BASIC);
774 }
775
776 protected int getSubCollectionIndex(Object object, String documentTypeName) {
777 int index = 1;
778 MaintainableCollectionDefinition theCollectionDefinition = null;
779 for (MaintainableCollectionDefinition maintainableCollectionDefinition : maintenanceDocumentDictionaryService.getMaintainableCollections(documentTypeName)) {
780 if (maintainableCollectionDefinition.getBusinessObjectClass().equals(object.getClass())) {
781
782 theCollectionDefinition = maintainableCollectionDefinition;
783 break;
784 }
785 }
786 if (theCollectionDefinition != null) {
787 for (MaintainableCollectionDefinition subCollDef : theCollectionDefinition.getMaintainableCollections()) {
788 String name = subCollDef.getName();
789 String capitalFirst = name.substring(0, 1).toUpperCase();
790 String methodName = "get" + capitalFirst + name.substring(1);
791 List subCollectionList = new ArrayList();
792 try {
793 subCollectionList = (List) object.getClass().getMethod(methodName).invoke(object);
794 }
795 catch (InvocationTargetException ite) {
796
797 }
798 catch (IllegalAccessException iae) {
799
800 }
801 catch (NoSuchMethodException nme) {
802
803 }
804 index += subCollectionList.size();
805 }
806 }
807 return index;
808 }
809
810
811
812
813
814
815 public ActionForward deleteLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
816 KualiMaintenanceForm maintenanceForm = (KualiMaintenanceForm) form;
817 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
818 Maintainable oldMaintainable = document.getOldMaintainableObject();
819 Maintainable newMaintainable = document.getNewMaintainableObject();
820
821 String collectionName = extractCollectionName(request, KRADConstants.DELETE_LINE_METHOD);
822 if (collectionName == null) {
823 LOG.error("Unable to get find collection name in request.");
824 throw new RuntimeException("Unable to get find collection class in request.");
825 }
826
827 PersistableBusinessObject bo = newMaintainable.getBusinessObject();
828 Collection maintCollection = extractCollection(bo, collectionName);
829 if (collectionName == null) {
830 LOG.error("Collection is null in parent business object.");
831 throw new RuntimeException("Collection is null in parent business object.");
832 }
833
834 int deleteRecordIndex = getLineToDelete(request);
835 if (deleteRecordIndex < 0 || deleteRecordIndex > maintCollection.size() - 1) {
836 if (collectionName == null) {
837 LOG.error("Invalid index for deletion of collection record: " + deleteRecordIndex);
838 throw new RuntimeException("Invalid index for deletion of collection record: " + deleteRecordIndex);
839 }
840 }
841
842 ((List) maintCollection).remove(deleteRecordIndex);
843
844
845 if (KRADConstants.MAINTENANCE_EDIT_ACTION.equals(maintenanceForm.getMaintenanceAction()) ||
846 KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceForm.getMaintenanceAction())) {
847 bo = oldMaintainable.getBusinessObject();
848 maintCollection = extractCollection(bo, collectionName);
849
850 if (collectionName == null) {
851 LOG.error("Collection is null in parent business object.");
852 throw new RuntimeException("Collection is null in parent business object.");
853 }
854
855 ((List) maintCollection).remove(deleteRecordIndex);
856 }
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896 doProcessingAfterPost( (KualiMaintenanceForm) form, request );
897
898 return mapping.findForward(RiceConstants.MAPPING_BASIC);
899 }
900
901
902
903
904 public ActionForward toggleInactiveRecordDisplay(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
905 KualiMaintenanceForm maintenanceForm = (KualiMaintenanceForm) form;
906 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
907 Maintainable oldMaintainable = document.getOldMaintainableObject();
908 Maintainable newMaintainable = document.getNewMaintainableObject();
909
910 String collectionName = extractCollectionName(request, KRADConstants.TOGGLE_INACTIVE_METHOD);
911 if (collectionName == null) {
912 LOG.error("Unable to get find collection name in request.");
913 throw new RuntimeException("Unable to get find collection class in request.");
914 }
915
916 String parameterName = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
917 boolean showInactive = Boolean.parseBoolean(StringUtils.substringBetween(parameterName, KRADConstants.METHOD_TO_CALL_BOPARM_LEFT_DEL, "."));
918
919 oldMaintainable.setShowInactiveRecords(collectionName, showInactive);
920 newMaintainable.setShowInactiveRecords(collectionName, showInactive);
921
922 return mapping.findForward(RiceConstants.MAPPING_BASIC);
923 }
924
925
926
927
928
929
930
931
932 protected void clearPrimaryKeyFields(MaintenanceDocument document) {
933
934 PersistableBusinessObject bo = document.getNewMaintainableObject().getBusinessObject();
935 List<String> keyFieldNames = getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(bo.getClass());
936
937 for (String keyFieldName : keyFieldNames) {
938 try {
939 ObjectUtils.setObjectProperty(bo, keyFieldName, null);
940 }
941 catch (Exception e) {
942 LOG.error("Unable to clear primary key field: " + e.getMessage());
943 throw new RuntimeException("Unable to clear primary key field: " + e.getMessage());
944 }
945 }
946 }
947
948
949
950
951
952
953
954 protected void clearUnauthorizedNewFields(MaintenanceDocument document) {
955
956 Person user = GlobalVariables.getUserSession().getPerson();
957
958
959 MaintenanceDocumentAuthorizer documentAuthorizer = (MaintenanceDocumentAuthorizer) getDocumentHelperService().getDocumentAuthorizer(document);
960
961
962 MaintenanceDocumentRestrictions maintenanceDocumentRestrictions = getBusinessObjectAuthorizationService().getMaintenanceDocumentRestrictions(document, user);
963
964
965 PersistableBusinessObject newBo = document.getNewMaintainableObject().getBusinessObject();
966
967 document.getNewMaintainableObject().clearBusinessObjectOfRestrictedValues(maintenanceDocumentRestrictions);
968 }
969
970
971
972
973
974
975 @SuppressWarnings("unchecked")
976 protected void doProcessingAfterPost( KualiForm form, HttpServletRequest request ) {
977 MaintenanceDocument document = (MaintenanceDocument) ((KualiMaintenanceForm)form).getDocument();
978 Maintainable maintainable = document.getNewMaintainableObject();
979 PersistableBusinessObject bo = maintainable.getBusinessObject();
980
981 getBusinessObjectService().linkUserFields(bo);
982
983 maintainable.processAfterPost(document, request.getParameterMap() );
984 }
985
986 protected void doProcessingAfterPost( KualiForm form, Map<String,String[]> parameters ) {
987 MaintenanceDocument document = (MaintenanceDocument) ((KualiMaintenanceForm)form).getDocument();
988 Maintainable maintainable = document.getNewMaintainableObject();
989 PersistableBusinessObject bo = maintainable.getBusinessObject();
990
991 getBusinessObjectService().linkUserFields(bo);
992
993 maintainable.processAfterPost(document, parameters );
994 }
995
996 protected void populateAuthorizationFields(KualiDocumentFormBase formBase){
997 super.populateAuthorizationFields(formBase);
998
999 KualiMaintenanceForm maintenanceForm = (KualiMaintenanceForm) formBase;
1000 MaintenanceDocument maintenanceDocument = (MaintenanceDocument) maintenanceForm.getDocument();
1001 MaintenanceDocumentAuthorizer maintenanceDocumentAuthorizer = (MaintenanceDocumentAuthorizer) getDocumentHelperService().getDocumentAuthorizer(maintenanceDocument);
1002 Person user = GlobalVariables.getUserSession().getPerson();
1003 maintenanceForm.setReadOnly(!formBase.getDocumentActions().containsKey(KRADConstants.KUALI_ACTION_CAN_EDIT));
1004 MaintenanceDocumentRestrictions maintenanceDocumentAuthorizations = getBusinessObjectAuthorizationService().getMaintenanceDocumentRestrictions(maintenanceDocument, user);
1005 maintenanceForm.setAuthorizations(maintenanceDocumentAuthorizations);
1006 }
1007
1008 public LookupService getLookupService() {
1009 if ( lookupService == null ) {
1010 lookupService = KRADServiceLocatorWeb.getLookupService();
1011 }
1012 return this.lookupService;
1013 }
1014
1015 public LookupResultsService getLookupResultsService() {
1016 if ( lookupResultsService == null ) {
1017 lookupResultsService = KNSServiceLocator.getLookupResultsService();
1018 }
1019 return this.lookupResultsService;
1020 }
1021
1022 }