001/**
002 * Copyright 2005-2016 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.jdom.Element;
019import org.kuali.rice.core.api.impex.ExportDataSet;
020import org.kuali.rice.core.api.reflect.ObjectDefinition;
021import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
022import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
023import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
024import org.kuali.rice.kew.rule.bo.RuleAttribute;
025import org.kuali.rice.kew.rule.dao.RuleAttributeDAO;
026import org.kuali.rice.kew.rule.service.RuleAttributeService;
027import org.kuali.rice.kew.xml.RuleAttributeXmlParser;
028import org.kuali.rice.kew.xml.export.RuleAttributeXmlExporter;
029
030import javax.xml.namespace.QName;
031import java.io.FileNotFoundException;
032import java.io.InputStream;
033import java.util.ArrayList;
034import java.util.Collection;
035import java.util.List;
036
037public class RuleAttributeServiceImpl implements RuleAttributeService {
038    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleAttributeServiceImpl.class);
039
040    private static final String RULE_ATTRIBUTE_NAME_REQUIRED = "rule.attribute.name.required";
041    private static final String RULE_ATTRIBUTE_CLASS_REQUIRED = "rule.attribute.className.required";
042
043    private static final String XML_FILE_NOT_FOUND = "general.error.filenotfound";
044    private static final String XML_PARSE_ERROR = "general.error.parsexml";
045
046    private RuleAttributeDAO ruleAttributeDAO;
047
048    public void save(RuleAttribute ruleAttribute) {
049        validate(ruleAttribute);
050        getRuleAttributeDAO().save(ruleAttribute);
051    }
052
053    public void delete(String ruleAttributeId) {
054        getRuleAttributeDAO().delete(ruleAttributeId);
055    }
056
057    public List<RuleAttribute> findByRuleAttribute(RuleAttribute ruleAttribute) {
058        return getRuleAttributeDAO().findByRuleAttribute(ruleAttribute);
059    }
060
061    public RuleAttribute findByRuleAttributeId(String ruleAttributeId) {
062        return getRuleAttributeDAO().findByRuleAttributeId(ruleAttributeId);
063    }
064
065    public List<RuleAttribute> findAll() {
066        return getRuleAttributeDAO().getAllRuleAttributes();
067    }
068
069    public RuleAttribute findByName(String name) {
070        return getRuleAttributeDAO().findByName(name);
071    }
072
073    public RuleAttributeDAO getRuleAttributeDAO() {
074        return ruleAttributeDAO;
075    }
076
077    public void setRuleAttributeDAO(RuleAttributeDAO ruleAttributeDAO) {
078        this.ruleAttributeDAO = ruleAttributeDAO;
079    }
080
081    private void validate(RuleAttribute ruleAttribute) {
082        LOG.debug("validating ruleAttribute");
083        Collection errors = new ArrayList();
084        if (ruleAttribute.getName() == null || ruleAttribute.getName().trim().equals("")) {
085            errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute name.", RULE_ATTRIBUTE_NAME_REQUIRED));
086            LOG.error("Rule attribute name is missing");
087        } else {
088                ruleAttribute.setName(ruleAttribute.getName().trim());
089            if (ruleAttribute.getId() == null) {
090                RuleAttribute nameInUse = findByName(ruleAttribute.getName());
091                if (nameInUse != null) {
092                    errors.add(new WorkflowServiceErrorImpl("Rule attribute name already in use", "routetemplate.ruleattribute.name.duplicate"));
093                    LOG.error("Rule attribute name already in use");
094                }
095            }
096        }
097        if (ruleAttribute.getResourceDescriptor() == null || ruleAttribute.getResourceDescriptor().trim().equals("")) {
098            errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute class name.", RULE_ATTRIBUTE_CLASS_REQUIRED));
099            LOG.error("Rule attribute class name is missing");
100        } else {
101                ruleAttribute.setResourceDescriptor(ruleAttribute.getResourceDescriptor().trim());
102        }
103
104        LOG.debug("end validating ruleAttribute");
105        if (!errors.isEmpty()) {
106            throw new WorkflowServiceErrorException("RuleAttribute Validation Error", errors);
107        }
108    }
109
110    @Override
111    public Object loadRuleAttributeService(RuleAttribute attribute) {
112        return loadRuleAttributeService(attribute, null);
113    }
114
115    @Override
116    public Object loadRuleAttributeService(RuleAttribute attribute, String defaultApplicationId) {
117        Object attributeService = null;
118        // first check if the class name is a valid and available java class
119        String attributeName = attribute.getResourceDescriptor();
120        ObjectDefinition attributeObjectDefinition = getAttributeObjectDefinition(attribute, defaultApplicationId);
121        attributeService = GlobalResourceLoader.getObject(attributeObjectDefinition);
122        if (attributeService == null) {
123            // if we can't find a class, try a service
124            attributeService = GlobalResourceLoader.getService(QName.valueOf(attributeName));
125        }
126        return attributeService;
127    }
128
129    protected ObjectDefinition getAttributeObjectDefinition(RuleAttribute ruleAttribute, String defaultApplicationId) {
130        if (ruleAttribute.getApplicationId() == null && defaultApplicationId != null) {
131            return new ObjectDefinition(ruleAttribute.getResourceDescriptor(), defaultApplicationId);
132        } else {
133            return new ObjectDefinition(ruleAttribute.getResourceDescriptor(), ruleAttribute.getApplicationId());
134        }
135    }
136
137    public void loadXml(InputStream inputStream, String principalId) {
138        RuleAttributeXmlParser parser = new RuleAttributeXmlParser();
139        try {
140            parser.parseRuleAttributes(inputStream);
141        } catch(FileNotFoundException e) {
142            throw new WorkflowServiceErrorException("XML file not found", new WorkflowServiceErrorImpl("Rule Attribute XML file not found", XML_FILE_NOT_FOUND) );
143        } catch (Exception e) { //any other exception
144            LOG.error("Error loading xml file", e);
145            throw new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
146        }
147    }
148
149    public Element export(ExportDataSet dataSet) {
150        RuleAttributeXmlExporter exporter = new RuleAttributeXmlExporter();
151        return exporter.export(dataSet);
152    }
153    
154        @Override
155        public boolean supportPrettyPrint() {
156                return true;
157        }
158
159        public List<RuleAttribute> findByClassName(String className) {
160                return this.ruleAttributeDAO.findByClassName(className);
161        }
162}