View Javadoc

1   package org.kuali.ole.deliver.request.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       * @param objectClassName
28       * @param documentTypeName
29       * @param maintenanceAction
30       * @return MaintenanceDocument
31       */
32      @Override
33      public MaintenanceDocument setupNewMaintenanceDocument(String objectClassName, String documentTypeName,
34                                                             String maintenanceAction) {
35          if (StringUtils.isEmpty(objectClassName) && StringUtils.isEmpty(documentTypeName)) {
36              throw new IllegalArgumentException("Document type name or bo class not given!");
37          }
38  
39          // get document type if not passed
40          if (StringUtils.isEmpty(documentTypeName)) {
41              try {
42                  documentTypeName =
43                          getDocumentDictionaryService().getMaintenanceDocumentTypeName(Class.forName(objectClassName));
44              } catch (ClassNotFoundException e) {
45                  throw new RuntimeException(e);
46              }
47  
48              if (StringUtils.isEmpty(documentTypeName)) {
49                  throw new RuntimeException(
50                          "documentTypeName is empty; does this Business Object have a maintenance document definition? " +
51                                  objectClassName);
52              }
53          }
54  
55          // check doc type allows new or copy if that action was requested
56          if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction) ||
57                  KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceAction)) {
58              Class<?> boClass =
59                      getDocumentDictionaryService().getMaintenanceDataObjectClass(documentTypeName);
60              boolean allowsNewOrCopy = getDataObjectAuthorizationService()
61                      .canCreate(boClass, GlobalVariables.getUserSession().getPerson(), documentTypeName);
62              if (!allowsNewOrCopy) {
63                  LOG.error("Document type " + documentTypeName + " does not allow new or copy actions.");
64                  throw new DocumentTypeAuthorizationException(
65                          GlobalVariables.getUserSession().getPerson().getPrincipalId(), "newOrCopy", documentTypeName);
66              }
67          }
68  
69          // get new document from service
70          try {
71              return (MaintenanceDocument) getDocumentService().getNewDocument(documentTypeName);
72          } catch (WorkflowException e) {
73              LOG.error("Cannot get new maintenance document instance for doc type: " + documentTypeName, e);
74              throw new RuntimeException("Cannot get new maintenance document instance for doc type: " + documentTypeName,
75                      e);
76          }
77      }
78  
79      /**
80       * @see org.kuali.rice.krad.service.impl.MaintenanceDocumentServiceImpl#setupMaintenanceObject
81       */
82      /**
83       * This method creates maintenance object for delete operation using maintenanceAction.
84       * @param document
85       * @param maintenanceAction
86       * @param requestParameters
87       */
88      public void setupMaintenanceObjectForDelete(MaintenanceDocument document, String maintenanceAction,
89                                                  Map<String, String[]> requestParameters) {
90          document.getNewMaintainableObject().setMaintenanceAction(maintenanceAction);
91          document.getOldMaintainableObject().setMaintenanceAction(maintenanceAction);
92  
93          Object oldDataObject = retrieveObjectForMaintenance(document, requestParameters);
94          Object newDataObject = ObjectUtils.deepCopy((Serializable) oldDataObject);
95  
96          document.getOldMaintainableObject().setDataObject(oldDataObject);
97          document.getNewMaintainableObject().setDataObject(newDataObject);
98      }
99  
100 }