001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kim.api.identity.entity;
017    
018    import org.apache.commons.collections.CollectionUtils;
019    import org.kuali.rice.core.api.CoreConstants;
020    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021    import org.kuali.rice.core.api.mo.ModelBuilder;
022    import org.kuali.rice.kim.api.KimConstants;
023    import org.kuali.rice.kim.api.identity.EntityUtils;
024    import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
025    import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationContract;
026    import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
027    import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
028    import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierContract;
029    import org.kuali.rice.kim.api.identity.name.EntityName;
030    import org.kuali.rice.kim.api.identity.principal.Principal;
031    import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
032    import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
033    import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoContract;
034    import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
035    import org.w3c.dom.Element;
036    
037    import javax.xml.bind.annotation.XmlAccessType;
038    import javax.xml.bind.annotation.XmlAccessorType;
039    import javax.xml.bind.annotation.XmlAnyElement;
040    import javax.xml.bind.annotation.XmlElement;
041    import javax.xml.bind.annotation.XmlElementWrapper;
042    import javax.xml.bind.annotation.XmlRootElement;
043    import javax.xml.bind.annotation.XmlType;
044    import java.io.Serializable;
045    import java.util.ArrayList;
046    import java.util.Collection;
047    import java.util.Collections;
048    import java.util.List;
049    
050    @XmlRootElement(name = EntityDefault.Constants.ROOT_ELEMENT_NAME)
051    @XmlAccessorType(XmlAccessType.NONE)
052    @XmlType(name = EntityDefault.Constants.TYPE_NAME, propOrder = {
053        EntityDefault.Elements.ENTITY_ID,
054        EntityDefault.Elements.NAME,
055        EntityDefault.Elements.PRINCIPALS,
056        EntityDefault.Elements.ENTITY_TYPE_CONTACT_INFOS,
057        EntityDefault.Elements.AFFILIATIONS,
058        EntityDefault.Elements.DEFAULT_AFFILIATION,
059        EntityDefault.Elements.EMPLOYMENT,
060        EntityDefault.Elements.EXTERNAL_IDENTIFIERS,
061        EntityDefault.Elements.PRIVACY_PREFERENCES,
062        EntityDefault.Elements.ACTIVE,
063        CoreConstants.CommonElements.FUTURE_ELEMENTS
064    })
065    public class EntityDefault extends AbstractDataTransferObject {
066        @XmlElement(name = Elements.ENTITY_ID, required = false)
067        private final String entityId;
068        @XmlElement(name = Elements.NAME, required = false)
069        private final EntityName name;
070        @XmlElementWrapper(name = Elements.PRINCIPALS, required = false)
071        @XmlElement(name = Elements.PRINCIPAL, required = false)
072        private final List<Principal> principals;
073        @XmlElementWrapper(name = Elements.ENTITY_TYPE_CONTACT_INFOS, required = false)
074        @XmlElement(name = Elements.ENTITY_TYPE_CONTACT_INFO, required = false)
075        private final List<EntityTypeContactInfoDefault> entityTypeContactInfos;
076        @XmlElementWrapper(name = Elements.AFFILIATIONS, required = false)
077        @XmlElement(name = Elements.AFFILIATION, required = false)
078        private final List<EntityAffiliation> affiliations;
079        @XmlElement(name = Elements.DEFAULT_AFFILIATION, required = false)
080        private final EntityAffiliation defaultAffiliation;
081        @XmlElement(name = Elements.EMPLOYMENT, required = false)
082        private final EntityEmployment employment;
083        @XmlElementWrapper(name = Elements.EXTERNAL_IDENTIFIERS, required = false)
084        @XmlElement(name = Elements.EXTERNAL_IDENTIFIER, required = false)
085        private final List<EntityExternalIdentifier> externalIdentifiers;
086        @XmlElement(name = Elements.PRIVACY_PREFERENCES, required = false)
087        private final EntityPrivacyPreferences privacyPreferences;
088        @XmlElement(name = Elements.ACTIVE, required = false)
089        private final boolean active;
090        @SuppressWarnings("unused")
091        @XmlAnyElement
092        private final Collection<Element> _futureElements = null;
093    
094        private EntityDefault() {
095            entityId = null;
096            name = null;
097            principals = null;
098            affiliations = null;
099            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    }