001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.cg.document.service.impl;
017    
018    import org.kuali.kfs.module.cg.CGConstants;
019    import org.kuali.kfs.module.cg.document.service.ResearchDocumentPermissionsService;
020    import org.kuali.rice.kew.dto.ActionRequestDTO;
021    import org.kuali.rice.kew.dto.DocumentDetailDTO;
022    import org.kuali.rice.kew.dto.ReportCriteriaDTO;
023    import org.kuali.rice.kew.exception.WorkflowException;
024    import org.kuali.rice.kew.service.WorkflowInfo;
025    import org.kuali.rice.kns.service.BusinessObjectService;
026    
027    public class ResearchDocumentPermissionsServiceImpl implements ResearchDocumentPermissionsService {
028    
029        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ResearchDocumentPermissionsServiceImpl.class);
030    
031        private BusinessObjectService businessObjectService;
032    
033    
034        /**
035         * @see org.kuali.module.kra.budget.service.BudgetPermissionsService#getBudgetPermissionType(String orgXml, String documentType,
036         *      String uuid)
037         */
038        public boolean isUserInOrgHierarchy(String orgXml, String documentType, String uuid) {
039            ReportCriteriaDTO criteria = new ReportCriteriaDTO();
040            criteria.setDocumentTypeName(documentType);
041            criteria.setNodeNames(new String[] { CGConstants.ORG_REVIEW_NODE_NAME });
042            criteria.setRuleTemplateNames(new String[] { CGConstants.ORG_REVIEW_TEMPLATE_NAME });
043            criteria.setXmlContent(orgXml);
044            WorkflowInfo info = new WorkflowInfo();
045            try {
046                DocumentDetailDTO detail = info.routingReport(criteria);
047                return isUserInRequests(detail.getActionRequests(), uuid);
048            }
049            catch (WorkflowException e) {
050                throw new RuntimeException("Exception generating routing report: " + e);
051            }
052        }
053    
054        /**
055         * Check whether given user is in the given action requests.
056         * 
057         * @param ActionRequestDTO[] requests
058         * @param String uuid
059         * @return boolean
060         */
061        protected boolean isUserInRequests(ActionRequestDTO[] requests, String principalId) {
062            for (int i = 0; i < requests.length; i++) {
063                ActionRequestDTO request = (ActionRequestDTO) requests[i];
064                if (request.getPrincipalId().equals(principalId)) {
065                    return true;
066                }
067                if (isUserInRequests(request.getChildrenRequests(), principalId)) {
068                    return true;
069                }
070            }
071            return false;
072        }
073    
074        /**
075         * Setter for BusinessObjectService property.
076         * 
077         * @param BusinessObjectService businessObjectService
078         */
079        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
080            this.businessObjectService = businessObjectService;
081        }
082    }
083