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.kim.api.identity.principal;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.kuali.rice.kim.api.KimConstants;
23  import org.w3c.dom.Element;
24  
25  import javax.xml.bind.annotation.XmlAccessType;
26  import javax.xml.bind.annotation.XmlAccessorType;
27  import javax.xml.bind.annotation.XmlAnyElement;
28  import javax.xml.bind.annotation.XmlElement;
29  import javax.xml.bind.annotation.XmlRootElement;
30  import javax.xml.bind.annotation.XmlType;
31  import java.io.Serializable;
32  import java.util.Collection;
33  
34  @XmlRootElement(name = Principal.Constants.ROOT_ELEMENT_NAME)
35  @XmlAccessorType(XmlAccessType.NONE)
36  @XmlType(name = Principal.Constants.TYPE_NAME, propOrder = {
37      Principal.Elements.PRINCIPAL_ID,
38      Principal.Elements.PRINCIPAL_NAME,
39      Principal.Elements.ENTITY_ID,
40      Principal.Elements.PASSWORD,
41      Principal.Elements.ACTIVE,
42      CoreConstants.CommonElements.VERSION_NUMBER,
43      CoreConstants.CommonElements.OBJECT_ID,
44      CoreConstants.CommonElements.FUTURE_ELEMENTS
45  })
46  public final class Principal extends AbstractDataTransferObject
47      implements PrincipalContract
48  {
49  
50      @XmlElement(name = Elements.PASSWORD, required = false)
51      private final String password;
52      @XmlElement(name = Elements.PRINCIPAL_ID, required = false)
53      private final String principalId;
54      @XmlElement(name = Elements.PRINCIPAL_NAME, required = false)
55      private final String principalName;
56      @XmlElement(name = Elements.ENTITY_ID, required = false)
57      private final String entityId;
58      @XmlElement(name = Elements.ACTIVE, required = false)
59      private final boolean active;
60      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
61      private final Long versionNumber;
62      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
63      private final String objectId;
64      @SuppressWarnings("unused")
65      @XmlAnyElement
66      private final Collection<Element> _futureElements = null;
67  
68      /**
69       * Private constructor used only by JAXB.
70       * 
71       */
72      private Principal() {
73          this.password = null;
74          this.principalId = null;
75          this.principalName = null;
76          this.entityId = null;
77          this.active = false;
78          this.versionNumber = null;
79          this.objectId = null;
80      }
81  
82      private Principal(Builder builder) {
83          this.password = builder.getPassword();
84          this.principalId = builder.getPrincipalId();
85          this.principalName = builder.getPrincipalName();
86          this.entityId = builder.getEntityId();
87          this.active = builder.isActive();
88          this.versionNumber = builder.getVersionNumber();
89          this.objectId = builder.getObjectId();
90      }
91  
92      @Override
93      public String getPassword() {
94          return this.password;
95      }
96  
97      @Override
98      public String getPrincipalId() {
99          return this.principalId;
100     }
101 
102     @Override
103     public String getPrincipalName() {
104         return this.principalName;
105     }
106 
107     @Override
108     public String getEntityId() {
109         return this.entityId;
110     }
111 
112     @Override
113     public boolean isActive() {
114         return this.active;
115     }
116 
117     @Override
118     public Long getVersionNumber() {
119         return this.versionNumber;
120     }
121 
122     @Override
123     public String getObjectId() {
124         return this.objectId;
125     }
126 
127     /**
128      * A builder which can be used to construct {@link Principal} instances.  Enforces the constraints of the {@link PrincipalContract}.
129      * 
130      */
131     public final static class Builder
132         implements Serializable, ModelBuilder, PrincipalContract
133     {
134 
135         private String password;
136         private String principalId;
137         private String principalName;
138         private String entityId;
139         private boolean active;
140         private Long versionNumber;
141         private String objectId;
142 
143         private Builder(String principalName) {
144             setPrincipalName(principalName);
145         }
146 
147         public static Builder create(String principalName) {
148             // TODO modify as needed to pass any required values and add them to the signature of the 'create' method
149             return new Builder(principalName);
150         }
151 
152         public static Builder create(PrincipalContract contract) {
153             if (contract == null) {
154                 throw new IllegalArgumentException("contract was null");
155             }
156             // TODO if create() is modified to accept required parameters, this will need to be modified
157             Builder builder = create(contract.getPrincipalName());
158             builder.setPassword(contract.getPassword());
159             builder.setPrincipalId(contract.getPrincipalId());
160             builder.setEntityId(contract.getEntityId());
161             builder.setActive(contract.isActive());
162             builder.setVersionNumber(contract.getVersionNumber());
163             builder.setObjectId(contract.getObjectId());
164             return builder;
165         }
166 
167         public Principal build() {
168             return new Principal(this);
169         }
170 
171         @Override
172         public String getPassword() {
173             return this.password;
174         }
175 
176         @Override
177         public String getPrincipalId() {
178             return this.principalId;
179         }
180 
181         @Override
182         public String getPrincipalName() {
183             return this.principalName;
184         }
185 
186         @Override
187         public String getEntityId() {
188             return this.entityId;
189         }
190 
191         @Override
192         public boolean isActive() {
193             return this.active;
194         }
195 
196         @Override
197         public Long getVersionNumber() {
198             return this.versionNumber;
199         }
200 
201         @Override
202         public String getObjectId() {
203             return this.objectId;
204         }
205 
206         public void setPassword(String password) {
207             this.password = password;
208         }
209 
210         public void setPrincipalId(String principalId) {
211             if (StringUtils.isWhitespace(principalId)) {
212                 throw new IllegalArgumentException("principalId is blank");
213             }
214             this.principalId = principalId;
215         }
216 
217         public void setPrincipalName(String principalName) {
218             if (StringUtils.isEmpty(principalName)) {
219                 throw new IllegalArgumentException("principalName is blank");
220             }
221             this.principalName = principalName;
222         }
223 
224         public void setEntityId(String entityId) {
225             this.entityId = entityId;
226         }
227 
228         public void setActive(boolean active) {
229             this.active = active;
230         }
231 
232         public void setVersionNumber(Long versionNumber) {
233             this.versionNumber = versionNumber;
234         }
235 
236         public void setObjectId(String objectId) {
237             this.objectId = objectId;
238         }
239 
240     }
241 
242 
243     /**
244      * Defines some internal constants used on this class.
245      * 
246      */
247     static class Constants {
248 
249         final static String ROOT_ELEMENT_NAME = "principal";
250         final static String TYPE_NAME = "PrincipalType";
251     }
252 
253 
254     /**
255      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
256      * 
257      */
258     static class Elements {
259 
260         final static String PASSWORD = "password";
261         final static String PRINCIPAL_ID = "principalId";
262         final static String PRINCIPAL_NAME = "principalName";
263         final static String ENTITY_ID = "entityId";
264         final static String ACTIVE = "active";
265 
266     }
267 
268     public static class Cache {
269         public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + Principal.Constants.TYPE_NAME;
270     }
271 }