View Javadoc

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