View Javadoc

1   /**
2    * Copyright 2005-2012 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                 affiliationBuilders.add(EntityAffiliation.Builder.create(aff));
285                 if ( aff.isActive() && aff.isDefaultValue() ) {
286                   builder.setDefaultAffiliation( EntityAffiliation.Builder.create(aff) );
287                 }
288             }
289             builder.setAffiliations(affiliationBuilders);
290           
291             builder.setEmployment(contract.getPrimaryEmployment() == null ? null : EntityEmployment.Builder.create(contract.getPrimaryEmployment()) );
292             List<EntityExternalIdentifier.Builder> externalIdBuilders = new ArrayList<EntityExternalIdentifier.Builder>();
293             for ( EntityExternalIdentifierContract id : contract.getExternalIdentifiers() ) {
294                 externalIdBuilders.add( EntityExternalIdentifier.Builder.create(id) );
295             }
296             builder.setExternalIdentifiers( externalIdBuilders );
297 
298             return builder;
299         }
300 
301         public EntityDefault build() {
302             return new EntityDefault(this);
303         }
304 
305         public String getEntityId() {
306             return entityId;
307         }
308 
309         public void setEntityId(String entityId) {
310             this.entityId = entityId;
311         }
312 
313         public EntityName.Builder getName() {
314             return name;
315         }
316 
317         public void setName(EntityName.Builder name) {
318             this.name = name;
319         }
320 
321         public List<Principal.Builder> getPrincipals() {
322             return principals;
323         }
324 
325         public void setPrincipals(List<Principal.Builder> principals) {
326             this.principals = principals;
327         }
328 
329         public List<EntityTypeContactInfoDefault.Builder> getEntityTypeContactInfos() {
330             return entityTypeContactInfos;
331         }
332 
333         public void setEntityTypeContactInfos(List<EntityTypeContactInfoDefault.Builder> entityTypeContactInfos) {
334             this.entityTypeContactInfos = entityTypeContactInfos;
335         }
336 
337         public List<EntityAffiliation.Builder> getAffiliations() {
338             return affiliations;
339         }
340 
341         public void setAffiliations(List<EntityAffiliation.Builder> affiliations) {
342             this.affiliations = affiliations;
343         }
344 
345         public EntityAffiliation.Builder getDefaultAffiliation() {
346             return defaultAffiliation;
347         }
348 
349         public void setDefaultAffiliation(EntityAffiliation.Builder defaultAffiliation) {
350             this.defaultAffiliation = defaultAffiliation;
351         }
352 
353         public EntityEmployment.Builder getEmployment() {
354             return employment;
355         }
356 
357         public void setEmployment(EntityEmployment.Builder employment) {
358             this.employment = employment;
359         }
360 
361         public List<EntityExternalIdentifier.Builder> getExternalIdentifiers() {
362             return externalIdentifiers;
363         }
364 
365         public void setExternalIdentifiers(List<EntityExternalIdentifier.Builder> externalIdentifiers) {
366             this.externalIdentifiers = externalIdentifiers;
367         }
368 
369         public EntityPrivacyPreferences.Builder getPrivacyPreferences() {
370             return privacyPreferences;
371         }
372 
373         public void setPrivacyPreferences(EntityPrivacyPreferences.Builder privacyPreferences) {
374             this.privacyPreferences = privacyPreferences;
375         }
376 
377         public boolean isActive() {
378             return this.active;
379         }
380 
381         public void setActive(boolean active) {
382             this.active = active;
383         }
384     }
385     /**
386      * Defines some internal constants used on this class.
387      *
388      */
389     static class Constants {
390 
391         final static String ROOT_ELEMENT_NAME = "entityDefault";
392         final static String TYPE_NAME = "EntityDefaultType";
393     }
394 
395 
396     /**
397      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
398      *
399      */
400     static class Elements {
401         final static String ENTITY_ID = "entityId";
402         final static String NAME = "name";
403         final static String PRINCIPALS = "principals";
404         final static String PRINCIPAL = "principal";
405         final static String ENTITY_TYPE_CONTACT_INFOS = "entityTypeContactInfos";
406         final static String ENTITY_TYPE_CONTACT_INFO = "entityTypeContactInfo";
407         final static String AFFILIATIONS = "affiliations";
408         final static String AFFILIATION = "affiliation";
409         final static String DEFAULT_AFFILIATION = "defaultAffiliation";
410         final static String EMPLOYMENT = "employment";
411         final static String EXTERNAL_IDENTIFIERS = "externalIdentifiers";
412         final static String EXTERNAL_IDENTIFIER = "externalIdentifier";
413         final static String PRIVACY_PREFERENCES = "privacyPreferences";
414         final static String ACTIVE = "active";
415 
416     }
417 
418     public static class Cache {
419         public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + EntityDefault.Constants.TYPE_NAME;
420     }
421 }