001    /*
002     * Copyright 2007-2009 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.bo.entity.dto;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.kuali.rice.kim.bo.entity.KimEntity;
022    import org.kuali.rice.kim.bo.entity.KimEntityAffiliation;
023    import org.kuali.rice.kim.bo.entity.KimEntityCitizenship;
024    import org.kuali.rice.kim.bo.entity.KimEntityEmploymentInformation;
025    import org.kuali.rice.kim.bo.entity.KimEntityEntityType;
026    import org.kuali.rice.kim.bo.entity.KimEntityEthnicity;
027    import org.kuali.rice.kim.bo.entity.KimEntityExternalIdentifier;
028    import org.kuali.rice.kim.bo.entity.KimEntityName;
029    import org.kuali.rice.kim.bo.entity.KimEntityResidency;
030    import org.kuali.rice.kim.bo.entity.KimEntityVisa;
031    import org.kuali.rice.kim.bo.entity.KimPrincipal;
032    
033    /**
034     * This is a data transfer object containing all information related to a KIM
035     * entity.
036     * 
037     * @author Kuali Rice Team (kuali-rice@googlegroups.com)
038     */
039    public class KimEntityInfo extends KimInactivatableInfo implements KimEntity {
040    
041        private static final long serialVersionUID = 1L;
042    
043        private List<KimEntityAffiliationInfo> affiliations;
044        private KimEntityBioDemographicsInfo bioDemographics;
045        private List<KimEntityCitizenshipInfo> citizenships;
046        private List<KimEntityEmploymentInformationInfo> employmentInformation;
047        private String entityId;
048        private List<KimEntityEntityTypeInfo> entityTypes;
049        private List<KimEntityExternalIdentifierInfo> externalIdentifiers;
050        private List<KimEntityNameInfo> names;
051        private List<KimPrincipalInfo> principals;
052        private KimEntityPrivacyPreferencesInfo privacyPreferences;
053        private List<KimEntityEthnicityInfo> ethnicities;
054        private List<KimEntityResidencyInfo> residencies;
055        private List<KimEntityVisaInfo> visas;
056    
057        /**
058         * This constructs an empty KimEntityInfo
059         */
060        public KimEntityInfo() {
061            super();
062            active = true;
063        }
064    
065        /**
066         * This constructs a KimEntityInfo derived from the {@link KimEntity} passed in.
067         * 
068         * @param entity the {@link KimEntity} that this KimEntityInfo is derived from.  If null, then an empty 
069         * KimEntityInfo will be constructed.
070         */
071        public KimEntityInfo(KimEntity entity) {
072            this();
073    
074            if (entity != null) {
075    
076                entityId = entity.getEntityId();
077                active = entity.isActive();
078    
079                // See comments by utility method deriveCollection for why this is used.  
080                // Essentially, the constructor was too darned long.
081    
082                principals = deriveCollection(entity.getPrincipals(), new XForm<KimPrincipal, KimPrincipalInfo>() {
083                    public KimPrincipalInfo xform(KimPrincipal source) {
084                        return new KimPrincipalInfo(source);
085                    }
086                });
087    
088                if (entity.getBioDemographics() != null) {
089                    bioDemographics = new KimEntityBioDemographicsInfo(entity.getBioDemographics());
090                }
091    
092                if (entity.getPrivacyPreferences() != null) {
093                    privacyPreferences = new KimEntityPrivacyPreferencesInfo(entity.getPrivacyPreferences());
094                }
095    
096                names = deriveCollection(entity.getNames(), new XForm<KimEntityName, KimEntityNameInfo>() {
097                    public KimEntityNameInfo xform(KimEntityName source) {
098                        return new KimEntityNameInfo(source);
099                    } 
100                });
101    
102                entityTypes = deriveCollection(entity.getEntityTypes(), new XForm<KimEntityEntityType, KimEntityEntityTypeInfo>() {
103                    public KimEntityEntityTypeInfo xform(KimEntityEntityType source) {
104                        return new KimEntityEntityTypeInfo(source);
105                    }
106                });
107    
108                affiliations = deriveCollection(entity.getAffiliations(), new XForm<KimEntityAffiliation, KimEntityAffiliationInfo>() {
109                    public KimEntityAffiliationInfo xform(KimEntityAffiliation source) {
110                        return new KimEntityAffiliationInfo(source);
111                    }
112                });
113    
114                employmentInformation = deriveCollection(entity.getEmploymentInformation(), 
115                        new XForm<KimEntityEmploymentInformation, KimEntityEmploymentInformationInfo>() {
116                    public KimEntityEmploymentInformationInfo xform(KimEntityEmploymentInformation source) {
117                        return new KimEntityEmploymentInformationInfo(source);
118                    }
119                });
120    
121                externalIdentifiers = deriveCollection(entity.getExternalIdentifiers(), 
122                        new XForm<KimEntityExternalIdentifier, KimEntityExternalIdentifierInfo>() {
123                    public KimEntityExternalIdentifierInfo xform(KimEntityExternalIdentifier source) {
124                        return new KimEntityExternalIdentifierInfo(source); 
125                    }
126                });        
127    
128                citizenships = deriveCollection(entity.getCitizenships(), new XForm<KimEntityCitizenship, KimEntityCitizenshipInfo>() {
129                    public KimEntityCitizenshipInfo xform(KimEntityCitizenship source) {
130                        return new KimEntityCitizenshipInfo(source);
131                    }
132                });
133    
134                ethnicities = deriveCollection(entity.getEthnicities(), new XForm<KimEntityEthnicity, KimEntityEthnicityInfo>() {
135                    public KimEntityEthnicityInfo xform(KimEntityEthnicity source) {
136                        return new KimEntityEthnicityInfo(source);
137                    }
138                });
139    
140                residencies = deriveCollection(entity.getResidencies(), new XForm<KimEntityResidency, KimEntityResidencyInfo>() {
141                    public KimEntityResidencyInfo xform(KimEntityResidency source) {
142                        return new KimEntityResidencyInfo(source);
143                    }
144                });
145    
146                visas = deriveCollection(entity.getVisas(), new XForm<KimEntityVisa, KimEntityVisaInfo>() {
147                    public KimEntityVisaInfo xform(KimEntityVisa source) {
148                        return new KimEntityVisaInfo(source);
149                    }
150                });
151            }
152        }
153    
154        /** 
155         * {@inheritDoc}
156         * @see KimEntity#getAffiliations()
157         */
158        public List<KimEntityAffiliationInfo> getAffiliations() {
159            // If our reference is null, assign and return an empty List
160            return (affiliations != null) ? affiliations : (affiliations = new ArrayList<KimEntityAffiliationInfo>());
161    
162        }
163    
164        /** 
165         * Setter for this {@link KimEntityInfo}'s affiliations.  Note the behavior of {@link #getAffiliations()} if
166         * this is set to null;
167         */
168        public void setAffiliations(List<KimEntityAffiliationInfo> affiliations) {
169            this.affiliations = affiliations;
170        }
171    
172        /** 
173         * {@inheritDoc}
174         * @see KimEntity#getDefaultAffiliation()
175         */
176        public KimEntityAffiliationInfo getDefaultAffiliation() {
177            KimEntityAffiliationInfo result = null;
178            if (affiliations != null)
179                for (KimEntityAffiliationInfo affiliation : affiliations) {
180                    if (result == null) {
181                        result = affiliation;
182                    }
183                    if (affiliation.isDefault()) {
184                        result = affiliation;
185                        break;
186                    }
187                }
188            return result;
189        }
190    
191        /** 
192         * {@inheritDoc}
193         * @see KimEntity#getBioDemographics()
194         */
195        public KimEntityBioDemographicsInfo getBioDemographics() {
196            return bioDemographics;
197        }
198    
199        /** 
200         * Setter for this {@link KimEntityInfo}'s demographic information.  Note the behavior of 
201         * {@link #getBioDemographics()} if this is set to null;
202         */
203        public void setBioDemographics(KimEntityBioDemographicsInfo bioDemographics) {
204            this.bioDemographics = bioDemographics;
205        }
206    
207        /** 
208         * {@inheritDoc}
209         * @see KimEntity#getCitizenships()
210         */
211        public List<KimEntityCitizenshipInfo> getCitizenships() {
212            // If our reference is null, assign and return an empty List
213            return (citizenships != null) ? citizenships : (citizenships = new ArrayList<KimEntityCitizenshipInfo>());
214    
215        }
216    
217        /** 
218         * Setter for this {@link KimEntityInfo}'s demographic information.  Note the behavior of 
219         * {@link #getCitizenships()} if this is set to null;
220         */
221        public void setCitizenships(List<KimEntityCitizenshipInfo> citizenships) {
222            this.citizenships = citizenships;
223        }
224    
225        /** 
226         * {@inheritDoc}
227         * @see KimEntity#getEmploymentInformation()
228         */
229        public List<KimEntityEmploymentInformationInfo> getEmploymentInformation() {
230            // If our reference is null, assign and return an empty List
231            return (employmentInformation != null) ? employmentInformation
232                    : (employmentInformation = new ArrayList<KimEntityEmploymentInformationInfo>());
233        }
234    
235        /** 
236         * Setter for this {@link KimEntityInfo}'s employment information.  Note the behavior of 
237         * {@link #getEmploymentInformation()} if this is set to null;
238         */
239        public void setEmploymentInformation(List<KimEntityEmploymentInformationInfo> employmentInformation) {
240            this.employmentInformation = employmentInformation;
241        }
242    
243        /** 
244         * {@inheritDoc}
245         * @see KimEntity#getPrimaryEmployment()
246         */
247        public KimEntityEmploymentInformationInfo getPrimaryEmployment() {
248            KimEntityEmploymentInformationInfo result = null;
249            if (employmentInformation != null)
250                for (KimEntityEmploymentInformationInfo employment : employmentInformation) {
251                    if (employment.isPrimary()) {
252                        result = employment;
253                    }
254                }
255            return result;
256        }
257    
258        /**
259         * {@inheritDoc}
260         * @see KimEntity#getEntityId()
261         */
262        public String getEntityId() {
263            return entityId;
264        }
265    
266        /** 
267         * Setter for this {@link KimEntityInfo}'s entity id.  Note the behavior of 
268         * {@link #getEntityId()} if this is set to null;
269         */
270        public void setEntityId(String entityId) {
271            this.entityId = entityId;
272        }
273    
274        /**
275         * {@inheritDoc}
276         * @see KimEntity#getEntityTypes()
277         */
278        public List<KimEntityEntityTypeInfo> getEntityTypes() {
279            // If our reference is null, assign and return an empty List
280            return (entityTypes != null) ? entityTypes : (entityTypes = new ArrayList<KimEntityEntityTypeInfo>());
281        }
282    
283        /** 
284         * Setter for this {@link KimEntityInfo}'s entity types.  Note the behavior of 
285         * {@link #getEntityTypes()} if this is set to null;
286         */
287        public void setEntityTypes(List<KimEntityEntityTypeInfo> entityTypes) {
288            this.entityTypes = entityTypes;
289        }
290    
291        /**
292         * {@inheritDoc}
293         * @see KimEntity#getExternalIdentifiers()
294         */
295        public List<KimEntityExternalIdentifierInfo> getExternalIdentifiers() {
296            // If our reference is null, assign and return an empty List
297            return (externalIdentifiers != null) ? externalIdentifiers
298                    : (externalIdentifiers = new ArrayList<KimEntityExternalIdentifierInfo>());
299        }
300    
301        /** 
302         * Setter for this {@link KimEntityInfo}'s external identifiers.  Note the behavior of 
303         * {@link #getExternalIdentifiers()} if this is set to null;
304         */
305        public void setExternalIdentifiers(List<KimEntityExternalIdentifierInfo> externalIdentifiers) {
306            this.externalIdentifiers = externalIdentifiers;
307        }
308    
309        /**
310         * {@inheritDoc}
311         * @see KimEntity#getNames()
312         */
313        public List<KimEntityNameInfo> getNames() {
314            // If our reference is null, assign and return an empty List
315            return (names != null) ? names : (names = new ArrayList<KimEntityNameInfo>());
316        }
317    
318        /** 
319         * Setter for this {@link KimEntityInfo}'s names.  Note the behavior of 
320         * {@link #getNames()} if this is set to null;
321         */
322        public void setNames(List<KimEntityNameInfo> names) {
323            this.names = names;
324        }
325    
326        /**
327         * {@inheritDoc}
328         * @see KimEntity#getDefaultName()
329         */
330        public KimEntityNameInfo getDefaultName() {
331            KimEntityNameInfo result = null;
332            for (KimEntityNameInfo name : this.getNames()) {
333                if (result == null) {
334                    result = name;
335                }
336                if (name.isDefault()) {
337                    result = name;
338                    break;
339                }
340            }
341            return result;
342        }
343    
344        /**
345         * {@inheritDoc}
346         * @see KimEntity#getPrincipals()
347         */
348        public List<KimPrincipalInfo> getPrincipals() {
349            // If our reference is null, assign and return an empty List
350            return (principals != null) ? principals : (principals = new ArrayList<KimPrincipalInfo>());
351        }
352    
353        /** 
354         * Setter for this {@link KimEntityInfo}'s principals.  Note the behavior of 
355         * {@link #getPrincipals()} if this is set to null;
356         */
357        public void setPrincipals(List<KimPrincipalInfo> principals) {
358            this.principals = principals;
359        }
360    
361        /**
362         * {@inheritDoc}
363         * @see KimEntity#getPrivacyPreferences()
364         */
365        public KimEntityPrivacyPreferencesInfo getPrivacyPreferences() {
366            return privacyPreferences;
367        }
368    
369        /** 
370         * Setter for this {@link KimEntityInfo}'s privacy preferences.  Note the behavior of 
371         * {@link #getPrivacyPreferences()} if this is set to null;
372         */
373        public void setPrivacyPreferences(KimEntityPrivacyPreferencesInfo privacyPreferences) {
374            this.privacyPreferences = privacyPreferences;
375        }
376    
377        /**
378         * {@inheritDoc}
379         * @see KimEntity#getEthnicities()
380         */
381        public List<KimEntityEthnicityInfo> getEthnicities() {
382            // If our reference is null, assign and return an empty List
383            return (ethnicities != null) ? ethnicities : (ethnicities = new ArrayList<KimEntityEthnicityInfo>());
384        }
385    
386        /** 
387         * Setter for this {@link KimEntityInfo}'s ethnicities.  Note the behavior of 
388         * {@link #getEthnicities()} if this is set to null;
389         */
390        public void setEthnicities(List<KimEntityEthnicityInfo> ethnicities) {
391            this.ethnicities = ethnicities;
392        }
393    
394        /**
395         * {@inheritDoc}
396         * @see KimEntity#getResidencies()
397         */
398        public List<KimEntityResidencyInfo> getResidencies() {
399            // If our reference is null, assign and return an empty List
400            return (residencies != null) ? residencies : (residencies = new ArrayList<KimEntityResidencyInfo>());
401        }
402    
403        /** 
404         * Setter for this {@link KimEntityInfo}'s residencies.  Note the behavior of 
405         * {@link #getResidencies()} if this is set to null;
406         */
407        public void setResidencies(List<KimEntityResidencyInfo> residencies) {
408            this.residencies = residencies;
409        }
410    
411        /**
412         * {@inheritDoc}
413         * @see KimEntity#getVisas()
414         */
415        public List<KimEntityVisaInfo> getVisas() {
416            // If our reference is null, assign and return an empty List
417            return (visas != null) ? visas : (visas = new ArrayList<KimEntityVisaInfo>());
418        }
419    
420        /** 
421         * Setter for this {@link KimEntityInfo}'s visas.  Note the behavior of 
422         * {@link #getVisas()} if this is set to null;
423         */
424        public void setVisas(List<KimEntityVisaInfo> visas) {
425            this.visas = visas;
426        }
427    
428        /**
429         * {@inheritDoc}
430         * @see KimEntity#getEntityExternalIdentifier(String)
431         */
432        public KimEntityExternalIdentifier getEntityExternalIdentifier(String externalIdentifierTypeCode) {
433            KimEntityExternalIdentifier result = null;
434    
435            List<KimEntityExternalIdentifierInfo> externalIdentifiers = getExternalIdentifiers();
436            if (externalIdentifiers != null)
437                for (KimEntityExternalIdentifier eid : externalIdentifiers) {
438                    if (eid.getExternalIdentifierTypeCode().equals(externalIdentifierTypeCode)) {
439                        result = eid;
440                    }
441                }
442            return result;
443        }
444    
445        /**
446         * {@inheritDoc}
447         * @see KimEntity#getEntityType(String)
448         */
449        public KimEntityEntityType getEntityType(String entityTypeCode) {
450            KimEntityEntityType result = null;
451    
452            if (entityTypes != null)
453                for (KimEntityEntityType eType : entityTypes) {
454                    if (eType.getEntityTypeCode().equals(entityTypeCode)) {
455                        result = eType;
456                    }
457                }
458            return result;
459        }
460        
461        /*
462            // utility method converts this monstrous block:
463            
464            if (entity.getEntityTypes() != null) {
465                entityTypes = new ArrayList<KimEntityEntityTypeInfo>(entity.getEntityTypes().size());
466    
467                for (KimEntityEntityType entityEntityType : entity.getEntityTypes()) if (entityEntityType != null) {
468                    entityTypes.add(new KimEntityEntityTypeInfo(entityEntityType));
469                }
470            } else {
471                entityTypes = new ArrayList<KimEntityEntityTypeInfo>();
472            }
473            
474            // to this:
475            
476            entityTypes = deriveCollection(entity.getEntityTypes(), new XForm<KimEntityEntityType, KimEntityEntityTypeInfo>() {
477                public KimEntityEntityTypeInfo xform(KimEntityEntityType source) {
478                    return new KimEntityEntityTypeInfo(source);
479                }
480            });
481         
482            // Note that generic type C is required because some of the source collections use wildcards
483         */
484        private static <A,B,C> List<B> deriveCollection(List<A> source, XForm<C,B> transformer) {
485            List<B> result = null;
486            if (source != null) {
487                result = new ArrayList<B>(source.size());
488                
489                for (A element : source) if (element != null) {
490                    B mutant = transformer.xform((C)element);
491                    if (mutant != null) {
492                        result.add(mutant);
493                    }
494                }
495            } else {
496                result = new ArrayList();
497            }
498            return result;
499        }
500        
501        private static interface XForm<A,B> {
502            public B xform(A source);
503        }
504    
505    }