View Javadoc
1   package org.kuali.ole.select.controller;
2   
3   import org.kuali.ole.DataCarrierService;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.select.bo.OleLicenseRequestBo;
6   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
7   import org.kuali.rice.kew.actionitem.ActionItem;
8   import org.kuali.rice.kew.actionrequest.ActionRequestValue;
9   import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
10  import org.kuali.rice.kew.api.document.DocumentStatus;
11  import org.kuali.rice.kew.service.KEWServiceLocator;
12  import org.kuali.rice.kim.api.identity.Person;
13  import org.kuali.rice.krad.document.Document;
14  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
15  import org.kuali.rice.krad.maintenance.MaintenanceDocumentAuthorizerBase;
16  
17  import java.util.List;
18  
19  /**
20   * LicenseMaintenanceDocumentAuthorizerBase authorises permission for License Maintenance Document.
21   */
22  public class LicenseMaintenanceDocumentAuthorizerBase extends MaintenanceDocumentAuthorizerBase {
23      public DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
24  
25      /**
26       * Check user has credential to modify maintenanceDocument.
27       *
28       * @param document
29       * @param user
30       * @return boolean
31       */
32      @Override
33      public boolean canEdit(Document document, Person user) {
34          boolean canEdit = super.canEdit(document, user);
35          String statusCode = document.getDocumentHeader().getWorkflowDocument().getStatus().getCode();
36          MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
37          OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) maintenanceDocument.getNewMaintainableObject().getDataObject();
38          if (!canEdit) {
39              canEdit &= oleLicenseRequestBo.getAssignee() != null &&
40                      !(statusCode.equalsIgnoreCase(DocumentStatus.INITIATED.getCode()) || statusCode.equalsIgnoreCase(DocumentStatus.SAVED.getCode())) ?
41                      oleLicenseRequestBo.getAssignee().equalsIgnoreCase(user.getPrincipalId()) : true;
42              String canEditStr = null;
43              String routeHeaderId = document.getDocumentHeader().getDocumentNumber();
44              if (statusCode.equalsIgnoreCase(DocumentStatus.SAVED.getCode())) {
45                  ActionRequestService actionReqSrv = KEWServiceLocator.getActionRequestService();
46                  List<ActionRequestValue> actionReqValues = actionReqSrv.findAllPendingRequests(routeHeaderId);
47                  for (ActionRequestValue actionRequest : actionReqValues) {
48                      List<ActionItem> actionItems = actionRequest.getActionItems();
49                      canEditStr = actionItems != null && actionItems.size() > 0 ? actionItems.get(0).getPrincipalId() : null;
50                  }
51                  return (oleLicenseRequestBo.getAssignee() != null && oleLicenseRequestBo.getAssignee().equalsIgnoreCase(user.getPrincipalId())) ||
52                          (canEditStr != null && canEditStr.equalsIgnoreCase(user.getPrincipalId()));
53              }
54          }
55          return canEdit;
56      }
57  
58      /*public boolean canSave(Document document, Person user) {
59          boolean canSave = super.canSave(document,user);
60          String statusCode = document.getDocumentHeader().getWorkflowDocument().getStatus().getCode();
61          MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
62          OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo)maintenanceDocument.getNewMaintainableObject().getDataObject();
63          String canSaveStr = null;
64          String routeHeaderId = document.getDocumentHeader().getDocumentNumber();
65          if(statusCode.equalsIgnoreCase(DocumentStatus.SAVED.getCode())){
66              ActionRequestService actionReqSrv = KEWServiceLocator.getActionRequestService();
67              List<ActionRequestValue> actionReqValues = actionReqSrv.findAllPendingRequests(routeHeaderId);
68              for(ActionRequestValue actionRequest:actionReqValues){
69                  List<ActionItem> actionItems = actionRequest.getActionItems();
70                  canSaveStr = actionItems!=null && actionItems.size()>0? actionItems.get(0).getPrincipalId():null;
71              }
72              return  (oleLicenseRequestBo.getAssignee()!=null && oleLicenseRequestBo.getAssignee().equalsIgnoreCase(user.getPrincipalId())) ||
73                      (canSaveStr!=null && canSaveStr.equalsIgnoreCase(user.getPrincipalId()));
74          }
75          return canSave;
76      }*/
77      public boolean canRoute(Document document, Person user) {
78          boolean canRoute = super.canRoute(document, user);
79          String statusCode = document.getDocumentHeader().getWorkflowDocument().getStatus().getCode();
80          MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
81          OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) maintenanceDocument.getNewMaintainableObject().getDataObject();
82          String canRouteStr = null;
83          String routeHeaderId = document.getDocumentHeader().getDocumentNumber();
84          if (statusCode.equalsIgnoreCase(DocumentStatus.SAVED.getCode())) {
85              ActionRequestService actionReqSrv = KEWServiceLocator.getActionRequestService();
86              List<ActionRequestValue> actionReqValues = actionReqSrv.findAllPendingRequests(routeHeaderId);
87              for (ActionRequestValue actionRequest : actionReqValues) {
88                  List<ActionItem> actionItems = actionRequest.getActionItems();
89                  canRouteStr = actionItems != null && actionItems.size() > 0 ? actionItems.get(0).getPrincipalId() : null;
90              }
91              return (oleLicenseRequestBo.getAssignee() != null && oleLicenseRequestBo.getAssignee().equalsIgnoreCase(user.getPrincipalId())) ||
92                      (canRouteStr != null && canRouteStr.equalsIgnoreCase(user.getPrincipalId()));
93          }
94          return canRoute;
95      }
96  }