View Javadoc

1   /**
2    * Copyright 2005-2011 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.api.rule;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  import javax.xml.bind.annotation.XmlAccessType;
21  import javax.xml.bind.annotation.XmlAccessorType;
22  import javax.xml.bind.annotation.XmlAnyElement;
23  import javax.xml.bind.annotation.XmlElement;
24  import javax.xml.bind.annotation.XmlRootElement;
25  import javax.xml.bind.annotation.XmlType;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.rice.core.api.CoreConstants;
29  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
30  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
31  import org.kuali.rice.core.api.mo.ModelBuilder;
32  import org.w3c.dom.Element;
33  
34  @XmlRootElement(name = RoleName.Constants.ROOT_ELEMENT_NAME)
35  @XmlAccessorType(XmlAccessType.NONE)
36  @XmlType(name = RoleName.Constants.TYPE_NAME, propOrder = {
37      RoleName.Elements.NAME,
38      RoleName.Elements.BASE_NAME,
39      RoleName.Elements.RETURN_URL,
40      RoleName.Elements.LABEL,
41      CoreConstants.CommonElements.FUTURE_ELEMENTS
42  })
43  public final class RoleName
44      extends AbstractDataTransferObject
45      implements RoleNameContract
46  {
47  
48      @XmlElement(name = Elements.NAME, required = true)
49      private final String name;
50      @XmlElement(name = Elements.BASE_NAME, required = true)
51      private final String baseName;
52      @XmlElement(name = Elements.RETURN_URL, required = false)
53      private final String returnUrl;
54      @XmlElement(name = Elements.LABEL, required = true)
55      private final String label;
56      @SuppressWarnings("unused")
57      @XmlAnyElement
58      private final Collection<Element> _futureElements = null;
59  
60      /**
61       * Private constructor used only by JAXB.
62       *
63       */
64      private RoleName() {
65          this.name = null;
66          this.baseName = null;
67          this.returnUrl = null;
68          this.label = null;
69      }
70  
71      private RoleName(Builder builder) {
72          this.name = builder.getName();
73          this.baseName = builder.getBaseName();
74          this.returnUrl = builder.getReturnUrl();
75          this.label = builder.getLabel();
76      }
77  
78      public RoleName(String attributeClassName, String baseName, String label) {
79          this(RoleName.Builder.createWithClassName(attributeClassName, baseName, label));
80      }
81  
82      @Override
83      public String getName() {
84          return this.name;
85      }
86  
87      @Override
88      public String getBaseName() {
89          return this.baseName;
90      }
91  
92      @Override
93      public String getReturnUrl() {
94          return this.returnUrl;
95      }
96  
97      @Override
98      public String getLabel() {
99          return this.label;
100     }
101 
102     public static String constructRoleValue(String attributeClassName, String roleName) {
103     	return attributeClassName + "!" + roleName;
104     }
105 
106 
107     /**
108      * A builder which can be used to construct {@link RoleName} instances.  Enforces the constraints of the {@link RoleNameContract}.
109      *
110      */
111     public final static class Builder
112         implements Serializable, ModelBuilder, RoleNameContract
113     {
114 
115         private String name;
116         private String baseName;
117         private String returnUrl;
118         private String label;
119 
120         private Builder(String name, String baseName, String label) {
121             setName(name);
122             setBaseName(baseName);
123             setLabel(label);
124         }
125 
126         public static Builder createWithClassName(String attributeClassName, String baseName, String label) {
127             return new Builder(constructRoleValue(attributeClassName, baseName), baseName, label);
128         }
129 
130         public static Builder create(String name, String baseName, String label) {
131             return new Builder(name, baseName, label);
132         }
133 
134         public static Builder create(RoleNameContract contract) {
135             if (contract == null) {
136                 throw new IllegalArgumentException("contract was null");
137             }
138             Builder builder = create(contract.getName(), contract.getBaseName(), contract.getLabel());
139             builder.setReturnUrl(contract.getReturnUrl());
140             return builder;
141         }
142 
143         public RoleName build() {
144             return new RoleName(this);
145         }
146 
147         @Override
148         public String getName() {
149             return this.name;
150         }
151 
152         @Override
153         public String getBaseName() {
154             return this.baseName;
155         }
156 
157         @Override
158         public String getReturnUrl() {
159             return this.returnUrl;
160         }
161 
162         @Override
163         public String getLabel() {
164             return this.label;
165         }
166 
167         public void setName(String name) {
168             if (StringUtils.isBlank(name)) {
169                 throw new RiceIllegalArgumentException("name is blank");
170             }
171             this.name = name;
172         }
173 
174         public void setBaseName(String baseName) {
175             if (StringUtils.isBlank(baseName)) {
176                 throw new RiceIllegalArgumentException("baseName is blank");
177             }
178             this.baseName = baseName;
179         }
180 
181         public void setReturnUrl(String returnUrl) {
182             this.returnUrl = returnUrl;
183         }
184 
185         public void setLabel(String label) {
186             if (StringUtils.isBlank(label)) {
187                 throw new RiceIllegalArgumentException("label is blank");
188             }
189             this.label = label;
190         }
191 
192     }
193 
194 
195     /**
196      * Defines some internal constants used on this class.
197      *
198      */
199     static class Constants {
200 
201         final static String ROOT_ELEMENT_NAME = "roleName";
202         final static String TYPE_NAME = "RoleNameType";
203 
204     }
205 
206 
207     /**
208      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
209      *
210      */
211     static class Elements {
212 
213         final static String NAME = "name";
214         final static String BASE_NAME = "baseName";
215         final static String RETURN_URL = "returnUrl";
216         final static String LABEL = "label";
217 
218     }
219 
220 }