001/**
002 * Copyright 2005-2015 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.impl.extension;
017
018import org.apache.commons.collections.CollectionUtils;
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.core.api.CoreConstants;
021import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
022import org.kuali.rice.kew.api.extension.ExtensionDefinition;
023import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;
024import org.kuali.rice.kew.rule.bo.RuleAttribute;
025import org.kuali.rice.kew.rule.service.RuleAttributeService;
026import org.w3c.dom.Element;
027
028import javax.xml.bind.annotation.XmlAccessType;
029import javax.xml.bind.annotation.XmlAccessorType;
030import javax.xml.bind.annotation.XmlAnyElement;
031import javax.xml.bind.annotation.XmlElement;
032import javax.xml.bind.annotation.XmlRootElement;
033import javax.xml.bind.annotation.XmlType;
034import java.util.ArrayList;
035import java.util.Collection;
036import java.util.Collections;
037import java.util.List;
038
039/**
040 * Reference implementation of the {@code ExtensionRepositoryService}.  This implementation
041 * essentially sits on top of the legacy "RuleAttribute" service.
042 *
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 */
045@XmlRootElement(name = ExtensionRepositoryServiceImpl.Constants.ROOT_ELEMENT_NAME)
046@XmlAccessorType(XmlAccessType.NONE)
047@XmlType(name = ExtensionRepositoryServiceImpl.Constants.TYPE_NAME, propOrder = {
048    ExtensionRepositoryServiceImpl.Elements.RULE_ATTRIBUTE_SERVICE,
049    CoreConstants.CommonElements.FUTURE_ELEMENTS
050})
051public class ExtensionRepositoryServiceImpl implements ExtensionRepositoryService {
052
053    /**
054     * Private constructor used only by JAXB.
055     *
056     */
057    private ExtensionRepositoryServiceImpl() {
058        this.ruleAttributeService = null;
059    }
060
061    @XmlElement(name = Elements.RULE_ATTRIBUTE_SERVICE, required = true)
062    private RuleAttributeService ruleAttributeService;
063
064
065    @Override
066    public ExtensionDefinition getExtensionById(String id) throws RiceIllegalArgumentException {
067        if (StringUtils.isBlank(id)) {
068            throw new RiceIllegalArgumentException("id was null or blank");
069        }
070        RuleAttribute ruleAttribute = ruleAttributeService.findByRuleAttributeId(id);
071        return translateFromRuleAttribute(ruleAttribute);
072    }
073
074
075    @Override
076    public ExtensionDefinition getExtensionByName(String name) throws RiceIllegalArgumentException {
077        if (StringUtils.isBlank(name)) {
078            throw new RiceIllegalArgumentException("name was null or blank");
079        }
080        RuleAttribute ruleAttribute = ruleAttributeService.findByName(name);
081        return translateFromRuleAttribute(ruleAttribute);
082    }
083
084    @Override
085    public List<ExtensionDefinition> getExtensionsByResourceDescriptor(
086            String resourceDescriptor) throws RiceIllegalArgumentException {
087        if (StringUtils.isBlank(resourceDescriptor)) {
088            throw new RiceIllegalArgumentException("resourceDescriptor was null or blank");
089        }
090        List<RuleAttribute> ruleAttributes = ruleAttributeService.findByClassName(resourceDescriptor);
091        if (CollectionUtils.isNotEmpty(ruleAttributes)) {
092            List<ExtensionDefinition> definitions = new ArrayList<ExtensionDefinition>();
093            for (RuleAttribute ruleAttribute : ruleAttributes) {
094                definitions.add(translateFromRuleAttribute(ruleAttribute));
095            }
096            return Collections.unmodifiableList(definitions);
097        }
098        
099        return Collections.emptyList();
100    }
101
102    private ExtensionDefinition translateFromRuleAttribute(RuleAttribute ruleAttribute) {
103        return RuleAttribute.to(ruleAttribute);
104    }
105
106    /**
107         * Sets the rule attribute service.
108         * @param ruleAttributeService the rule attribute service to set.
109         */
110    public void setRuleAttributeService(RuleAttributeService ruleAttributeService) {
111        this.ruleAttributeService = ruleAttributeService;
112    }
113
114    @SuppressWarnings("unused")
115    @XmlAnyElement
116    private final Collection<Element> _futureElements = null;
117
118    /**
119     * Defines some internal constants used on this class.
120     *
121     */
122    static class Constants {
123        static final String ROOT_ELEMENT_NAME = "extensionRepositoryService";
124        static final String TYPE_NAME = "ExtensionRepositoryService";
125    }
126
127    /**
128     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
129     *
130     */
131    static class Elements {
132        static final String RULE_ATTRIBUTE_SERVICE = "ruleAttributeService";
133    }
134}