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