001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.kim.api.identity.entity;
017
018import org.apache.commons.collections.CollectionUtils;
019import org.kuali.rice.core.api.CoreConstants;
020import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021import org.kuali.rice.core.api.mo.ModelBuilder;
022import org.kuali.rice.kim.api.KimConstants;
023import org.kuali.rice.kim.api.identity.EntityUtils;
024import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
025import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationContract;
026import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
027import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
028import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierContract;
029import org.kuali.rice.kim.api.identity.name.EntityName;
030import org.kuali.rice.kim.api.identity.principal.Principal;
031import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
032import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
033import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoContract;
034import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
035import org.w3c.dom.Element;
036
037import javax.xml.bind.annotation.XmlAccessType;
038import javax.xml.bind.annotation.XmlAccessorType;
039import javax.xml.bind.annotation.XmlAnyElement;
040import javax.xml.bind.annotation.XmlElement;
041import javax.xml.bind.annotation.XmlElementWrapper;
042import javax.xml.bind.annotation.XmlRootElement;
043import javax.xml.bind.annotation.XmlType;
044import java.io.Serializable;
045import java.util.ArrayList;
046import java.util.Collection;
047import java.util.Collections;
048import 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})
065public class EntityDefault extends AbstractDataTransferObject {
066    @XmlElement(name = Elements.ENTITY_ID, required = false)
067    private final String entityId;
068
069    @XmlElement(name = Elements.NAME, required = false)
070    private final EntityName name;
071
072    @XmlElementWrapper(name = Elements.PRINCIPALS, required = false)
073    @XmlElement(name = Elements.PRINCIPAL, required = false)
074    private final List<Principal> principals;
075
076    @XmlElementWrapper(name = Elements.ENTITY_TYPE_CONTACT_INFOS, required = false)
077    @XmlElement(name = Elements.ENTITY_TYPE_CONTACT_INFO, required = false)
078    private final List<EntityTypeContactInfoDefault> entityTypeContactInfos;
079
080    @XmlElementWrapper(name = Elements.AFFILIATIONS, required = false)
081    @XmlElement(name = Elements.AFFILIATION, required = false)
082    private final List<EntityAffiliation> affiliations;
083
084    @XmlElement(name = Elements.DEFAULT_AFFILIATION, required = false)
085    private final EntityAffiliation defaultAffiliation;
086
087    @XmlElement(name = Elements.EMPLOYMENT, required = false)
088    private final EntityEmployment employment;
089
090    @XmlElementWrapper(name = Elements.EXTERNAL_IDENTIFIERS, required = false)
091    @XmlElement(name = Elements.EXTERNAL_IDENTIFIER, required = false)
092    private final List<EntityExternalIdentifier> externalIdentifiers;
093
094    @XmlElement(name = Elements.PRIVACY_PREFERENCES, required = false)
095    private final EntityPrivacyPreferences privacyPreferences;
096
097    @XmlElement(name = Elements.ACTIVE, required = false)
098    private final boolean active;
099
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}