View Javadoc
1   /**
2    * Copyright 2005-2016 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.ACTIVE,
41      CoreConstants.CommonElements.VERSION_NUMBER,
42      CoreConstants.CommonElements.OBJECT_ID,
43      CoreConstants.CommonElements.FUTURE_ELEMENTS
44  })
45  public final class Principal extends AbstractDataTransferObject
46      implements PrincipalContract
47  {
48  
49      @XmlElement(name = Elements.PRINCIPAL_ID, required = false)
50      private final String principalId;
51      @XmlElement(name = Elements.PRINCIPAL_NAME, required = false)
52      private final String principalName;
53      @XmlElement(name = Elements.ENTITY_ID, required = false)
54      private final String entityId;
55      @XmlElement(name = Elements.ACTIVE, required = false)
56      private final boolean active;
57      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
58      private final Long versionNumber;
59      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
60      private final String objectId;
61      @SuppressWarnings("unused")
62      @XmlAnyElement
63      private final Collection<Element> _futureElements = null;
64  
65      /**
66       * Private constructor used only by JAXB.
67       * 
68       */
69      private Principal() {
70          this.principalId = null;
71          this.principalName = null;
72          this.entityId = null;
73          this.active = false;
74          this.versionNumber = null;
75          this.objectId = null;
76      }
77  
78      private Principal(Builder builder) {
79          this.principalId = builder.getPrincipalId();
80          this.principalName = builder.getPrincipalName();
81          this.entityId = builder.getEntityId();
82          this.active = builder.isActive();
83          this.versionNumber = builder.getVersionNumber();
84          this.objectId = builder.getObjectId();
85      }
86  
87      @Override
88      public String getPrincipalId() {
89          return this.principalId;
90      }
91  
92      @Override
93      public String getPrincipalName() {
94          return this.principalName;
95      }
96  
97      @Override
98      public String getEntityId() {
99          return this.entityId;
100     }
101 
102     @Override
103     public boolean isActive() {
104         return this.active;
105     }
106 
107     @Override
108     public Long getVersionNumber() {
109         return this.versionNumber;
110     }
111 
112     @Override
113     public String getObjectId() {
114         return this.objectId;
115     }
116 
117     /**
118      * A builder which can be used to construct {@link Principal} instances.  Enforces the constraints of the {@link PrincipalContract}.
119      * 
120      */
121     public final static class Builder
122         implements Serializable, ModelBuilder, PrincipalContract
123     {
124 
125         private String principalId;
126         private String principalName;
127         private String entityId;
128         private boolean active;
129         private Long versionNumber;
130         private String objectId;
131 
132         private Builder(String principalName) {
133             setPrincipalName(principalName);
134         }
135 
136         public static Builder create(String principalName) {
137             return new Builder(principalName);
138         }
139 
140         public static Builder create(PrincipalContract contract) {
141             if (contract == null) {
142                 throw new IllegalArgumentException("contract was null");
143             }
144             Builder builder = create(contract.getPrincipalName());
145             builder.setPrincipalId(contract.getPrincipalId());
146             builder.setEntityId(contract.getEntityId());
147             builder.setActive(contract.isActive());
148             builder.setVersionNumber(contract.getVersionNumber());
149             builder.setObjectId(contract.getObjectId());
150             return builder;
151         }
152 
153         public Principal build() {
154             return new Principal(this);
155         }
156 
157 
158         @Override
159         public String getPrincipalId() {
160             return this.principalId;
161         }
162 
163         @Override
164         public String getPrincipalName() {
165             return this.principalName;
166         }
167 
168         @Override
169         public String getEntityId() {
170             return this.entityId;
171         }
172 
173         @Override
174         public boolean isActive() {
175             return this.active;
176         }
177 
178         @Override
179         public Long getVersionNumber() {
180             return this.versionNumber;
181         }
182 
183         @Override
184         public String getObjectId() {
185             return this.objectId;
186         }
187 
188         public void setPrincipalId(String principalId) {
189             if (StringUtils.isWhitespace(principalId)) {
190                 throw new IllegalArgumentException("principalId is blank");
191             }
192             this.principalId = principalId;
193         }
194 
195         public void setPrincipalName(String principalName) {
196             if (StringUtils.isEmpty(principalName)) {
197                 throw new IllegalArgumentException("principalName is blank");
198             }
199             this.principalName = principalName;
200         }
201 
202         public void setEntityId(String entityId) {
203             this.entityId = entityId;
204         }
205 
206         public void setActive(boolean active) {
207             this.active = active;
208         }
209 
210         public void setVersionNumber(Long versionNumber) {
211             this.versionNumber = versionNumber;
212         }
213 
214         public void setObjectId(String objectId) {
215             this.objectId = objectId;
216         }
217 
218     }
219 
220 
221     /**
222      * Defines some internal constants used on this class.
223      * 
224      */
225     static class Constants {
226 
227         final static String ROOT_ELEMENT_NAME = "principal";
228         final static String TYPE_NAME = "PrincipalType";
229     }
230 
231 
232     /**
233      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
234      * 
235      */
236     static class Elements {
237 
238         final static String PRINCIPAL_ID = "principalId";
239         final static String PRINCIPAL_NAME = "principalName";
240         final static String ENTITY_ID = "entityId";
241         final static String ACTIVE = "active";
242 
243     }
244 
245     public static class Cache {
246         public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + Principal.Constants.TYPE_NAME;
247     }
248 }