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.entity;
17  
18  import org.apache.commons.collections.CollectionUtils;
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.kuali.rice.kim.api.identity.EntityUtils;
24  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
25  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationContract;
26  import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
27  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
28  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierContract;
29  import org.kuali.rice.kim.api.identity.name.EntityName;
30  import org.kuali.rice.kim.api.identity.principal.Principal;
31  import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
32  import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
33  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoContract;
34  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
35  import org.w3c.dom.Element;
36  
37  import javax.xml.bind.annotation.XmlAccessType;
38  import javax.xml.bind.annotation.XmlAccessorType;
39  import javax.xml.bind.annotation.XmlAnyElement;
40  import javax.xml.bind.annotation.XmlElement;
41  import javax.xml.bind.annotation.XmlElementWrapper;
42  import javax.xml.bind.annotation.XmlRootElement;
43  import javax.xml.bind.annotation.XmlType;
44  import java.io.Serializable;
45  import java.util.ArrayList;
46  import java.util.Collection;
47  import java.util.Collections;
48  import java.util.List;
49  
50  @XmlRootElement(name = EntityDefault.Constants.ROOT_ELEMENT_NAME)
51  @XmlAccessorType(XmlAccessType.NONE)
52  @XmlType(name = EntityDefault.Constants.TYPE_NAME, propOrder = {
53      EntityDefault.Elements.ENTITY_ID,
54      EntityDefault.Elements.NAME,
55      EntityDefault.Elements.PRINCIPALS,
56      EntityDefault.Elements.ENTITY_TYPE_CONTACT_INFOS,
57      EntityDefault.Elements.AFFILIATIONS,
58      EntityDefault.Elements.DEFAULT_AFFILIATION,
59      EntityDefault.Elements.EMPLOYMENT,
60      EntityDefault.Elements.EXTERNAL_IDENTIFIERS,
61      EntityDefault.Elements.PRIVACY_PREFERENCES,
62      EntityDefault.Elements.ACTIVE,
63      CoreConstants.CommonElements.FUTURE_ELEMENTS
64  })
65  public class EntityDefault extends AbstractDataTransferObject {
66      @XmlElement(name = Elements.ENTITY_ID, required = false)
67      private final String entityId;
68      @XmlElement(name = Elements.NAME, required = false)
69      private final EntityName name;
70      @XmlElementWrapper(name = Elements.PRINCIPALS, required = false)
71      @XmlElement(name = Elements.PRINCIPAL, required = false)
72      private final List<Principal> principals;
73      @XmlElementWrapper(name = Elements.ENTITY_TYPE_CONTACT_INFOS, required = false)
74      @XmlElement(name = Elements.ENTITY_TYPE_CONTACT_INFO, required = false)
75      private final List<EntityTypeContactInfoDefault> entityTypeContactInfos;
76      @XmlElementWrapper(name = Elements.AFFILIATIONS, required = false)
77      @XmlElement(name = Elements.AFFILIATION, required = false)
78      private final List<EntityAffiliation> affiliations;
79      @XmlElement(name = Elements.DEFAULT_AFFILIATION, required = false)
80      private final EntityAffiliation defaultAffiliation;
81      @XmlElement(name = Elements.EMPLOYMENT, required = false)
82      private final EntityEmployment employment;
83      @XmlElementWrapper(name = Elements.EXTERNAL_IDENTIFIERS, required = false)
84      @XmlElement(name = Elements.EXTERNAL_IDENTIFIER, required = false)
85      private final List<EntityExternalIdentifier> externalIdentifiers;
86      @XmlElement(name = Elements.PRIVACY_PREFERENCES, required = false)
87      private final EntityPrivacyPreferences privacyPreferences;
88      @XmlElement(name = Elements.ACTIVE, required = false)
89      private final boolean active;
90      @SuppressWarnings("unused")
91      @XmlAnyElement
92      private final Collection<Element> _futureElements = null;
93  
94      private EntityDefault() {
95          entityId = null;
96          name = null;
97          principals = null;
98          affiliations = null;
99          defaultAffiliation = null;
100         entityTypeContactInfos = null;
101         employment = null;
102         externalIdentifiers = null;
103         privacyPreferences = null;
104         active = false;
105     }
106 
107     public EntityDefault(String entityId, EntityName name, List<Principal> principals,
108             List<EntityTypeContactInfoDefault> entityTypes, List<EntityAffiliation> affiliations,
109             EntityAffiliation defaultAffiliation, EntityEmployment employment,
110             List<EntityExternalIdentifier> externalIdentifiers, EntityPrivacyPreferences privacyPreferences, boolean active) {
111         this.entityId = entityId;
112         this.name = name;
113         this.principals = principals;
114         this.entityTypeContactInfos = entityTypes;
115         this.affiliations = affiliations;
116         this.defaultAffiliation = defaultAffiliation;
117         this.employment = employment;
118         this.externalIdentifiers = externalIdentifiers;
119         this.privacyPreferences = privacyPreferences;
120         this.active = active;
121     }
122 
123     public EntityDefault(Builder builder) {
124         this.entityId = builder.entityId;
125         this.name = builder.getName() == null ? null : builder.getName().build();
126         this.principals = new ArrayList<Principal>();
127         if (CollectionUtils.isNotEmpty(builder.getPrincipals())) {
128             for (Principal.Builder principal : builder.getPrincipals()) {
129                 this.principals.add(principal.build());
130             }
131         }
132         this.entityTypeContactInfos = new ArrayList<EntityTypeContactInfoDefault>();
133         if (CollectionUtils.isNotEmpty(builder.getEntityTypeContactInfos())) {
134             for (EntityTypeContactInfoDefault.Builder entityType : builder.getEntityTypeContactInfos()) {
135                 this.entityTypeContactInfos.add(entityType.build());
136             }
137         }
138         this.affiliations = new ArrayList<EntityAffiliation>();
139         if (CollectionUtils.isNotEmpty(builder.getAffiliations())) {
140             for (EntityAffiliation.Builder affiliation : builder.getAffiliations()) {
141                 this.affiliations.add(affiliation.build());
142             }
143         }
144         if (builder.getDefaultAffiliation() == null
145                 && CollectionUtils.isNotEmpty(this.affiliations)) {
146             this.defaultAffiliation = EntityUtils.getDefaultItem(this.affiliations);
147         } else {
148             this.defaultAffiliation = builder.getDefaultAffiliation() == null ? null : builder.getDefaultAffiliation().build();
149         }
150         this.employment = builder.getEmployment() == null ? null : builder.getEmployment().build();
151         this.externalIdentifiers = new ArrayList<EntityExternalIdentifier>();
152         if (CollectionUtils.isNotEmpty(builder.getExternalIdentifiers())) {
153             for (EntityExternalIdentifier.Builder externalId : builder.getExternalIdentifiers()) {
154                 this.externalIdentifiers.add(externalId.build());
155             }
156         }
157         this.privacyPreferences = builder.getPrivacyPreferences() == null ? null : builder.getPrivacyPreferences().build();
158         this.active = builder.isActive();
159     }
160 
161     public String getEntityId() {
162         return entityId;
163     }
164 
165     public EntityName getName() {
166         return name;
167     }
168 
169     public List<Principal> getPrincipals() {
170         return Collections.unmodifiableList(principals);
171     }
172 
173     public List<EntityTypeContactInfoDefault> getEntityTypeContactInfos() {
174         return Collections.unmodifiableList(entityTypeContactInfos);
175     }
176 
177     public List<EntityAffiliation> getAffiliations() {
178         return Collections.unmodifiableList(affiliations);
179     }
180 
181     public EntityAffiliation getDefaultAffiliation() {
182         return defaultAffiliation;
183     }
184 
185     public EntityEmployment getEmployment() {
186         return employment;
187     }
188 
189     public List<EntityExternalIdentifier> getExternalIdentifiers() {
190         return Collections.unmodifiableList(externalIdentifiers);
191     }
192 
193     public EntityPrivacyPreferences getPrivacyPreferences() {
194         return privacyPreferences;
195     }
196 
197     public boolean isActive() {
198         return active;
199     }
200 
201     /**
202      * Gets this {@link EntityDefault}'s {@link EntityTypeContactInfoDefault} for the given type code.
203      * @return the {@link org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault} for the given type code for this {@link EntityTypeContactInfoDefault},
204      * or null if none has been assigned.
205      */
206     public EntityTypeContactInfoDefault getEntityType(String entityTypeCode) {
207         if (entityTypeContactInfos == null) {
208             return null;
209         }
210         for (EntityTypeContactInfoDefault entType : entityTypeContactInfos) {
211             if (entType.getEntityTypeCode().equals(entityTypeCode)) {
212                 return entType;
213             }
214         }
215         return null;
216     }
217 
218    
219     /**
220      * A builder which can be used to construct {@link EntityDefault} instances.
221      * 
222      */
223     public final static class Builder
224         implements Serializable, ModelBuilder
225     {
226         private String entityId;
227         private EntityName.Builder name;
228         private List<Principal.Builder> principals;
229         private List<EntityTypeContactInfoDefault.Builder> entityTypeContactInfos;
230         private List<EntityAffiliation.Builder> affiliations;
231         private EntityAffiliation.Builder defaultAffiliation;
232         private EntityEmployment.Builder employment;
233         private List<EntityExternalIdentifier.Builder> externalIdentifiers;
234         private EntityPrivacyPreferences.Builder privacyPreferences;
235         private boolean active;
236 
237         private Builder() { }
238 
239         public static Builder create() {
240             return new Builder();
241         }
242 
243         public static Builder create(String entityId) {
244             Builder builder = new Builder();
245             builder.setEntityId(entityId);
246             return builder;
247         }
248         
249         public static Builder create(EntityContract contract) {
250             if (contract == null) {
251                 throw new IllegalArgumentException("contract was null");
252             }
253             Builder builder = new Builder();
254             builder.setEntityId(contract.getId());
255             builder.setActive(contract.isActive());
256             List<Principal.Builder> principalBuilders = new ArrayList<Principal.Builder>();
257             for ( PrincipalContract p : contract.getPrincipals() ) {
258               principalBuilders.add( Principal.Builder.create(p) );
259             }
260             builder.setPrincipals(principalBuilders);
261           
262             builder.setPrivacyPreferences(contract.getPrivacyPreferences() == null ?
263                     EntityPrivacyPreferences.Builder.create(contract.getId()) : EntityPrivacyPreferences.Builder.create(contract.getPrivacyPreferences()));
264 
265             builder.setName(contract.getDefaultName() == null ? null : EntityName.Builder.create(contract.getDefaultName()));
266             List<EntityTypeContactInfoDefault.Builder> typeBuilders = new ArrayList<EntityTypeContactInfoDefault.Builder>();
267             for ( EntityTypeContactInfoContract etContract : contract.getEntityTypeContactInfos() ) {
268                 typeBuilders.add(EntityTypeContactInfoDefault.Builder.create(etContract));
269             }
270             builder.setEntityTypeContactInfos(typeBuilders);
271           
272             List<EntityAffiliation.Builder> affiliationBuilders = new ArrayList<EntityAffiliation.Builder>( );
273             for ( EntityAffiliationContract aff : contract.getAffiliations() ) {
274                 affiliationBuilders.add(EntityAffiliation.Builder.create(aff));
275                 if ( aff.isActive() && aff.isDefaultValue() ) {
276                   builder.setDefaultAffiliation( EntityAffiliation.Builder.create(aff) );
277                 }
278             }
279             builder.setAffiliations(affiliationBuilders);
280           
281             builder.setEmployment(contract.getPrimaryEmployment() == null ? null : EntityEmployment.Builder.create(contract.getPrimaryEmployment()) );
282             List<EntityExternalIdentifier.Builder> externalIdBuilders = new ArrayList<EntityExternalIdentifier.Builder>();
283             for ( EntityExternalIdentifierContract id : contract.getExternalIdentifiers() ) {
284                 externalIdBuilders.add( EntityExternalIdentifier.Builder.create(id) );
285             }
286             builder.setExternalIdentifiers( externalIdBuilders );
287 
288             return builder;
289         }
290 
291         public EntityDefault build() {
292             return new EntityDefault(this);
293         }
294 
295         public String getEntityId() {
296             return entityId;
297         }
298 
299         public void setEntityId(String entityId) {
300             this.entityId = entityId;
301         }
302 
303         public EntityName.Builder getName() {
304             return name;
305         }
306 
307         public void setName(EntityName.Builder name) {
308             this.name = name;
309         }
310 
311         public List<Principal.Builder> getPrincipals() {
312             return principals;
313         }
314 
315         public void setPrincipals(List<Principal.Builder> principals) {
316             this.principals = principals;
317         }
318 
319         public List<EntityTypeContactInfoDefault.Builder> getEntityTypeContactInfos() {
320             return entityTypeContactInfos;
321         }
322 
323         public void setEntityTypeContactInfos(List<EntityTypeContactInfoDefault.Builder> entityTypeContactInfos) {
324             this.entityTypeContactInfos = entityTypeContactInfos;
325         }
326 
327         public List<EntityAffiliation.Builder> getAffiliations() {
328             return affiliations;
329         }
330 
331         public void setAffiliations(List<EntityAffiliation.Builder> affiliations) {
332             this.affiliations = affiliations;
333         }
334 
335         public EntityAffiliation.Builder getDefaultAffiliation() {
336             return defaultAffiliation;
337         }
338 
339         public void setDefaultAffiliation(EntityAffiliation.Builder defaultAffiliation) {
340             this.defaultAffiliation = defaultAffiliation;
341         }
342 
343         public EntityEmployment.Builder getEmployment() {
344             return employment;
345         }
346 
347         public void setEmployment(EntityEmployment.Builder employment) {
348             this.employment = employment;
349         }
350 
351         public List<EntityExternalIdentifier.Builder> getExternalIdentifiers() {
352             return externalIdentifiers;
353         }
354 
355         public void setExternalIdentifiers(List<EntityExternalIdentifier.Builder> externalIdentifiers) {
356             this.externalIdentifiers = externalIdentifiers;
357         }
358 
359         public EntityPrivacyPreferences.Builder getPrivacyPreferences() {
360             return privacyPreferences;
361         }
362 
363         public void setPrivacyPreferences(EntityPrivacyPreferences.Builder privacyPreferences) {
364             this.privacyPreferences = privacyPreferences;
365         }
366 
367         public boolean isActive() {
368             return this.active;
369         }
370 
371         public void setActive(boolean active) {
372             this.active = active;
373         }
374     }
375     /**
376      * Defines some internal constants used on this class.
377      *
378      */
379     static class Constants {
380 
381         final static String ROOT_ELEMENT_NAME = "entityDefault";
382         final static String TYPE_NAME = "EntityDefaultType";
383     }
384 
385 
386     /**
387      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
388      *
389      */
390     static class Elements {
391         final static String ENTITY_ID = "entityId";
392         final static String NAME = "name";
393         final static String PRINCIPALS = "principals";
394         final static String PRINCIPAL = "principal";
395         final static String ENTITY_TYPE_CONTACT_INFOS = "entityTypeContactInfos";
396         final static String ENTITY_TYPE_CONTACT_INFO = "entityTypeContactInfo";
397         final static String AFFILIATIONS = "affiliations";
398         final static String AFFILIATION = "affiliation";
399         final static String DEFAULT_AFFILIATION = "defaultAffiliation";
400         final static String EMPLOYMENT = "employment";
401         final static String EXTERNAL_IDENTIFIERS = "externalIdentifiers";
402         final static String EXTERNAL_IDENTIFIER = "externalIdentifier";
403         final static String PRIVACY_PREFERENCES = "privacyPreferences";
404         final static String ACTIVE = "active";
405 
406     }
407 
408     public static class Cache {
409         public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + EntityDefault.Constants.TYPE_NAME;
410     }
411 }