001    package org.kuali.ole.license.controller;
002    
003    import org.kuali.ole.DataCarrierService;
004    import org.kuali.ole.OLEConstants;
005    import org.kuali.ole.license.bo.OleLicenseRequestBo;
006    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
007    import org.kuali.rice.kew.actionitem.ActionItem;
008    import org.kuali.rice.kew.actionrequest.ActionRequestValue;
009    import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
010    import org.kuali.rice.kew.api.document.DocumentStatus;
011    import org.kuali.rice.kew.service.KEWServiceLocator;
012    import org.kuali.rice.kim.api.identity.Person;
013    import org.kuali.rice.krad.document.Document;
014    import org.kuali.rice.krad.maintenance.MaintenanceDocument;
015    import org.kuali.rice.krad.maintenance.MaintenanceDocumentAuthorizerBase;
016    import java.util.List;
017    
018    /**
019     * LicenseMaintenanceDocumentAuthorizerBase authorises permission for License Maintenance Document.
020     */
021    public class LicenseMaintenanceDocumentAuthorizerBase extends MaintenanceDocumentAuthorizerBase {
022        public DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
023        /**
024         *  Check user has credential to modify maintenanceDocument.
025         * @param document
026         * @param user
027         * @return boolean
028         */
029        @Override
030        public boolean canEdit(Document document, Person user) {
031            boolean canEdit =  super.canEdit(document,user);
032            String statusCode = document.getDocumentHeader().getWorkflowDocument().getStatus().getCode();
033            MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
034            OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo)maintenanceDocument.getNewMaintainableObject().getDataObject();
035            canEdit &=oleLicenseRequestBo.getAssignee()!=null &&
036                         !(statusCode.equalsIgnoreCase(DocumentStatus.INITIATED.getCode())||statusCode.equalsIgnoreCase(DocumentStatus.SAVED.getCode()))?
037                           oleLicenseRequestBo.getAssignee().equalsIgnoreCase(user.getPrincipalId()):true;
038           String canEditStr = null;
039           String routeHeaderId = document.getDocumentHeader().getDocumentNumber();
040            if(statusCode.equalsIgnoreCase(DocumentStatus.SAVED.getCode())){
041                ActionRequestService actionReqSrv = KEWServiceLocator.getActionRequestService();
042                List<ActionRequestValue> actionReqValues = actionReqSrv.findAllPendingRequests(routeHeaderId);
043                for(ActionRequestValue actionRequest:actionReqValues){
044                    List<ActionItem> actionItems = actionRequest.getActionItems();
045                    canEditStr = actionItems!=null && actionItems.size()>0? actionItems.get(0).getPrincipalId():null;
046                }
047                return  (oleLicenseRequestBo.getAssignee()!=null && oleLicenseRequestBo.getAssignee().equalsIgnoreCase(user.getPrincipalId())) ||
048                        (canEditStr!=null && canEditStr.equalsIgnoreCase(user.getPrincipalId()));
049            }
050            return canEdit;
051        }
052        public boolean canSave(Document document, Person user) {
053            boolean canSave = super.canSave(document,user);
054            String statusCode = document.getDocumentHeader().getWorkflowDocument().getStatus().getCode();
055            MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
056            OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo)maintenanceDocument.getNewMaintainableObject().getDataObject();
057            String canSaveStr = null;
058            String routeHeaderId = document.getDocumentHeader().getDocumentNumber();
059            if(statusCode.equalsIgnoreCase(DocumentStatus.SAVED.getCode())){
060                ActionRequestService actionReqSrv = KEWServiceLocator.getActionRequestService();
061                List<ActionRequestValue> actionReqValues = actionReqSrv.findAllPendingRequests(routeHeaderId);
062                for(ActionRequestValue actionRequest:actionReqValues){
063                    List<ActionItem> actionItems = actionRequest.getActionItems();
064                    canSaveStr = actionItems!=null && actionItems.size()>0? actionItems.get(0).getPrincipalId():null;
065                }
066                return  (oleLicenseRequestBo.getAssignee()!=null && oleLicenseRequestBo.getAssignee().equalsIgnoreCase(user.getPrincipalId())) ||
067                        (canSaveStr!=null && canSaveStr.equalsIgnoreCase(user.getPrincipalId()));
068            }
069            return canSave;
070        }
071    
072        public boolean canRoute(Document document, Person user) {
073            boolean canRoute = super.canRoute(document, user);
074            String statusCode = document.getDocumentHeader().getWorkflowDocument().getStatus().getCode();
075            MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
076            OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo)maintenanceDocument.getNewMaintainableObject().getDataObject();
077            String canRouteStr = null;
078            String routeHeaderId = document.getDocumentHeader().getDocumentNumber();
079            if(statusCode.equalsIgnoreCase(DocumentStatus.SAVED.getCode())){
080                ActionRequestService actionReqSrv = KEWServiceLocator.getActionRequestService();
081                List<ActionRequestValue> actionReqValues = actionReqSrv.findAllPendingRequests(routeHeaderId);
082                for(ActionRequestValue actionRequest:actionReqValues){
083                    List<ActionItem> actionItems = actionRequest.getActionItems();
084                    canRouteStr = actionItems!=null && actionItems.size()>0? actionItems.get(0).getPrincipalId():null;
085                }
086                return  (oleLicenseRequestBo.getAssignee()!=null && oleLicenseRequestBo.getAssignee().equalsIgnoreCase(user.getPrincipalId())) ||
087                        (canRouteStr!=null && canRouteStr.equalsIgnoreCase(user.getPrincipalId()));
088            }
089            return canRoute;
090        }
091    }