001/**
002 * Copyright 2005-2017 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 */
016package org.kuali.rice.kew.rule.service.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.jdom.Element;
020import org.kuali.rice.core.api.impex.ExportDataSet;
021import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
022import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
023import org.kuali.rice.kew.rule.RuleDelegationBo;
024import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
025import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
026import org.kuali.rice.kew.rule.service.RuleDelegationService;
027import org.kuali.rice.kew.rule.service.RuleTemplateService;
028import org.kuali.rice.kew.service.KEWServiceLocator;
029import org.kuali.rice.kew.xml.RuleXmlParser;
030import org.kuali.rice.kew.xml.export.RuleDelegationXmlExporter;
031import org.kuali.rice.kim.api.group.Group;
032import org.kuali.rice.kim.api.group.GroupService;
033import org.kuali.rice.kim.api.services.KimApiServiceLocator;
034
035import java.io.InputStream;
036import java.util.ArrayList;
037import java.util.Collection;
038import java.util.Collections;
039import java.util.List;
040import java.util.Map;
041
042
043/**
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 */
046public class RuleDelegationServiceImpl implements RuleDelegationService {
047        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
048                        .getLogger(RuleDelegationServiceImpl.class);
049        
050        private static final String XML_PARSE_ERROR = "general.error.parsexml";
051        
052    private RuleDelegationDAO dao;
053
054    public List<RuleDelegationBo> findByDelegateRuleId(String ruleId) {
055        if (ruleId == null) return Collections.EMPTY_LIST;
056        return dao.findByDelegateRuleId(ruleId);
057    }
058
059    public void save(RuleDelegationBo ruleDelegation) {
060        dao.save(ruleDelegation);
061    }
062
063    public void setRuleDelegationDAO(RuleDelegationDAO dao) {
064        this.dao = dao;
065    }
066    public List<RuleDelegationBo> findAllCurrentRuleDelegations(){
067        return dao.findAllCurrentRuleDelegations();
068    }
069    public void delete(String ruleDelegationId){
070        dao.delete(ruleDelegationId);
071    }
072
073    public RuleDelegationBo findByRuleDelegationId(String ruleDelegationId){
074        return dao.findByRuleDelegationId(ruleDelegationId);
075    }
076
077    public List<RuleDelegationBo> findByResponsibilityId(String responsibilityId) {
078        //return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
079        return findByResponsibilityId(responsibilityId, false);
080    }
081
082    public List<RuleDelegationBo> search(String parentRuleBaseVaueId, String parentResponsibilityId,  String docTypeName, String ruleId, String ruleTemplateId, String ruleDescription, String groupId, String principalId,
083            String delegationType, Boolean activeInd, Map extensionValues, String workflowIdDirective) {
084        return dao.search(parentRuleBaseVaueId, parentResponsibilityId, docTypeName, ruleId, ruleTemplateId, ruleDescription, groupId, principalId, delegationType,
085                activeInd, extensionValues, workflowIdDirective);
086    }
087
088    public List<RuleDelegationBo> searchByTemplate(String parentRuleBaseVaueId, String parentResponsibilityId,  String docTypeName, String ruleTemplateName, String ruleDescription, String groupId, String principalId,
089            Boolean workgroupMember, String delegationType, Boolean activeInd, Map extensionValues, Collection<String> actionRequestCodes) {
090
091        if ( (StringUtils.isEmpty(docTypeName)) &&
092                (StringUtils.isEmpty(ruleTemplateName)) &&
093                (StringUtils.isEmpty(ruleDescription)) &&
094                (StringUtils.isEmpty(groupId)) &&
095                (StringUtils.isEmpty(principalId)) &&
096                (extensionValues.isEmpty()) &&
097                (actionRequestCodes.isEmpty()) ) {
098            // all fields are empty
099            throw new IllegalArgumentException("At least one criterion must be sent");
100        }
101
102        RuleTemplateBo ruleTemplate = getRuleTemplateService().findByRuleTemplateName(ruleTemplateName);
103        String ruleTemplateId = null;
104        if (ruleTemplate != null) {
105            ruleTemplateId = ruleTemplate.getId();
106        }
107
108        if ( ( (extensionValues != null) && (!extensionValues.isEmpty()) ) &&
109                (ruleTemplateId == null) ) {
110            // cannot have extensions without a correct template
111            throw new IllegalArgumentException("A Rule Template Name must be given if using Rule Extension values");
112        }
113
114        Collection<String> workgroupIds = new ArrayList<String>();
115        if (principalId != null) {
116            if ( (workgroupMember == null) || (workgroupMember.booleanValue()) ) {
117                workgroupIds = getGroupService().getGroupIdsByPrincipalId(principalId);
118            } else {
119                // user was passed but workgroups should not be parsed... do nothing
120            }
121        } else if (groupId != null) {
122            Group group = KEWServiceLocator.getIdentityHelperService().getGroup(groupId);
123            if (group == null) {
124                throw new IllegalArgumentException("Group does not exist in for given group id: " + groupId);
125            } else  {
126                workgroupIds.add(group.getId());
127            }
128        }
129
130        return dao.search(parentRuleBaseVaueId, parentResponsibilityId, docTypeName, ruleTemplateId, ruleDescription, workgroupIds, principalId,
131                delegationType,activeInd, extensionValues, actionRequestCodes);
132    }
133    
134    public void loadXml(InputStream inputStream, String principalId) {
135        RuleXmlParser parser = new RuleXmlParser();
136        try {
137            parser.parseRuleDelegations(inputStream);
138        } catch (Exception e) { //any other exception
139            LOG.error("Error loading xml file", e);
140            WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
141            wsee.initCause(e);
142            throw wsee;
143        }
144        }
145
146        public Element export(ExportDataSet dataSet) {
147                RuleDelegationXmlExporter exporter = new RuleDelegationXmlExporter();
148                return exporter.export(dataSet);
149        }
150        
151        @Override
152        public boolean supportPrettyPrint() {
153                return true;
154        }
155
156        private GroupService getGroupService() {
157        return KimApiServiceLocator.getGroupService();
158    }
159
160    private RuleTemplateService getRuleTemplateService() {
161        return KEWServiceLocator.getRuleTemplateService();
162    }
163    
164
165    
166    public List findByResponsibilityId(String responsibilityId, boolean ignoreCache) {
167                return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
168    }
169}