View Javadoc

1   package org.kuali.ole.deliver.service;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.kew.api.exception.WorkflowException;
5   import org.kuali.rice.krad.exception.DocumentTypeAuthorizationException;
6   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
7   import org.kuali.rice.krad.service.impl.MaintenanceDocumentServiceImpl;
8   import org.kuali.rice.krad.util.GlobalVariables;
9   import org.kuali.rice.krad.util.KRADConstants;
10  import org.kuali.rice.krad.util.ObjectUtils;
11  
12  import java.io.Serializable;
13  import java.util.Map;
14  
15  /**
16   * Created with IntelliJ IDEA.
17   * User: ?
18   * Date: 10/11/12
19   * Time: 7:29 PM
20   * To change this template use File | Settings | File Templates.
21   */
22  public class OleDeliverRequestMaintenanceDocumentServiceImpl extends MaintenanceDocumentServiceImpl {
23      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleDeliverRequestMaintenanceDocumentServiceImpl.class);
24  
25      /**
26       * This method creates new maintenance document instance for doc type
27       *
28       * @param objectClassName
29       * @param documentTypeName
30       * @param maintenanceAction
31       * @return MaintenanceDocument
32       */
33      @Override
34      public MaintenanceDocument setupNewMaintenanceDocument(String objectClassName, String documentTypeName,
35                                                             String maintenanceAction) {
36          if (StringUtils.isEmpty(objectClassName) && StringUtils.isEmpty(documentTypeName)) {
37              throw new IllegalArgumentException("Document type name or bo class not given!");
38          }
39  
40          // get document type if not passed
41          if (StringUtils.isEmpty(documentTypeName)) {
42              try {
43                  documentTypeName =
44                          getDocumentDictionaryService().getMaintenanceDocumentTypeName(Class.forName(objectClassName));
45              } catch (ClassNotFoundException e) {
46                  LOG.error(e, e);
47                  throw new RuntimeException(e);
48              }
49  
50              if (StringUtils.isEmpty(documentTypeName)) {
51                  throw new RuntimeException(
52                          "documentTypeName is empty; does this Business Object have a maintenance document definition? " +
53                                  objectClassName);
54              }
55          }
56  
57          // check doc type allows new or copy if that action was requested
58          if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction) ||
59                  KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceAction)) {
60              Class<?> boClass =
61                      getDocumentDictionaryService().getMaintenanceDataObjectClass(documentTypeName);
62              boolean allowsNewOrCopy = getDataObjectAuthorizationService()
63                      .canCreate(boClass, GlobalVariables.getUserSession().getPerson(), documentTypeName);
64              if (!allowsNewOrCopy) {
65                  LOG.error("Document type " + documentTypeName + " does not allow new or copy actions.");
66                  throw new DocumentTypeAuthorizationException(
67                          GlobalVariables.getUserSession().getPerson().getPrincipalId(), "newOrCopy", documentTypeName);
68              }
69          }
70  
71          // get new document from service
72          try {
73              return (MaintenanceDocument) getDocumentService().getNewDocument(documentTypeName);
74          } catch (WorkflowException e) {
75              LOG.error("Cannot get new maintenance document instance for doc type: " + documentTypeName, e);
76              throw new RuntimeException("Cannot get new maintenance document instance for doc type: " + documentTypeName,
77                      e);
78          }
79      }
80  
81      /**
82       * @see org.kuali.rice.krad.service.impl.MaintenanceDocumentServiceImpl#setupMaintenanceObject
83       */
84      /**
85       * This method creates maintenance object for delete operation using maintenanceAction.
86       *
87       * @param document
88       * @param maintenanceAction
89       * @param requestParameters
90       */
91      public void setupMaintenanceObjectForDelete(MaintenanceDocument document, String maintenanceAction,
92                                                  Map<String, String[]> requestParameters) {
93          document.getNewMaintainableObject().setMaintenanceAction(maintenanceAction);
94          document.getOldMaintainableObject().setMaintenanceAction(maintenanceAction);
95  
96          Object oldDataObject = retrieveObjectForMaintenance(document, requestParameters);
97          Object newDataObject = ObjectUtils.deepCopy((Serializable) oldDataObject);
98  
99          document.getOldMaintainableObject().setDataObject(oldDataObject);
100         document.getNewMaintainableObject().setDataObject(newDataObject);
101     }
102 
103 }