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