View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.impl.extension;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.CoreConstants;
21  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
22  import org.kuali.rice.kew.api.extension.ExtensionDefinition;
23  import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;
24  import org.kuali.rice.kew.rule.bo.RuleAttribute;
25  import org.kuali.rice.kew.rule.service.RuleAttributeService;
26  import org.w3c.dom.Element;
27  
28  import javax.xml.bind.annotation.XmlAccessType;
29  import javax.xml.bind.annotation.XmlAccessorType;
30  import javax.xml.bind.annotation.XmlAnyElement;
31  import javax.xml.bind.annotation.XmlElement;
32  import javax.xml.bind.annotation.XmlRootElement;
33  import javax.xml.bind.annotation.XmlType;
34  import java.util.ArrayList;
35  import java.util.Collection;
36  import java.util.Collections;
37  import java.util.List;
38  
39  /**
40   * Reference implementation of the {@code ExtensionRepositoryService}.  This implementation
41   * essentially sits on top of the legacy "RuleAttribute" service.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  @XmlRootElement(name = ExtensionRepositoryServiceImpl.Constants.ROOT_ELEMENT_NAME)
46  @XmlAccessorType(XmlAccessType.NONE)
47  @XmlType(name = ExtensionRepositoryServiceImpl.Constants.TYPE_NAME, propOrder = {
48      ExtensionRepositoryServiceImpl.Elements.RULE_ATTRIBUTE_SERVICE,
49      CoreConstants.CommonElements.FUTURE_ELEMENTS
50  })
51  public class ExtensionRepositoryServiceImpl implements ExtensionRepositoryService {
52  
53      /**
54       * Private constructor used only by JAXB.
55       *
56       */
57      private ExtensionRepositoryServiceImpl() {
58          this.ruleAttributeService = null;
59      }
60  
61      @XmlElement(name = Elements.RULE_ATTRIBUTE_SERVICE, required = true)
62      private RuleAttributeService ruleAttributeService;
63  
64  
65      @Override
66      public ExtensionDefinition getExtensionById(String id) throws RiceIllegalArgumentException {
67          if (StringUtils.isBlank(id)) {
68              throw new RiceIllegalArgumentException("id was null or blank");
69          }
70          RuleAttribute ruleAttribute = ruleAttributeService.findByRuleAttributeId(id);
71          return translateFromRuleAttribute(ruleAttribute);
72      }
73  
74  
75      @Override
76      public ExtensionDefinition getExtensionByName(String name) throws RiceIllegalArgumentException {
77          if (StringUtils.isBlank(name)) {
78              throw new RiceIllegalArgumentException("name was null or blank");
79          }
80          RuleAttribute ruleAttribute = ruleAttributeService.findByName(name);
81          return translateFromRuleAttribute(ruleAttribute);
82      }
83  
84      @Override
85      public List<ExtensionDefinition> getExtensionsByResourceDescriptor(
86              String resourceDescriptor) throws RiceIllegalArgumentException {
87          if (StringUtils.isBlank(resourceDescriptor)) {
88              throw new RiceIllegalArgumentException("resourceDescriptor was null or blank");
89          }
90          List<RuleAttribute> ruleAttributes = ruleAttributeService.findByClassName(resourceDescriptor);
91          if (CollectionUtils.isNotEmpty(ruleAttributes)) {
92              List<ExtensionDefinition> definitions = new ArrayList<ExtensionDefinition>();
93              for (RuleAttribute ruleAttribute : ruleAttributes) {
94                  definitions.add(translateFromRuleAttribute(ruleAttribute));
95              }
96              return Collections.unmodifiableList(definitions);
97          }
98          
99          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 }