View Javadoc

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