001/**
002 * Copyright 2005-2016 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.impl.identity;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.kuali.rice.core.api.util.type.KualiDecimal;
024import org.kuali.rice.kim.api.KimConstants;
025import org.kuali.rice.kim.api.identity.IdentityService;
026import org.kuali.rice.kim.api.identity.Person;
027import org.kuali.rice.kim.api.identity.PersonService;
028import org.kuali.rice.kim.api.identity.address.EntityAddress;
029import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
030import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationContract;
031import org.kuali.rice.kim.api.identity.email.EntityEmailContract;
032import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
033import org.kuali.rice.kim.api.identity.entity.EntityDefault;
034import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
035import org.kuali.rice.kim.api.identity.name.EntityName;
036import org.kuali.rice.kim.api.identity.phone.EntityPhoneContract;
037import org.kuali.rice.kim.api.identity.principal.Principal;
038import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
039import org.kuali.rice.kim.api.services.KimApiServiceLocator;
040import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentStatusBo;
041import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentTypeBo;
042import org.kuali.rice.krad.bo.TransientBusinessObjectBase;
043
044public class PersonImpl extends TransientBusinessObjectBase implements Person {
045
046        private static final long serialVersionUID = 1L;
047
048        protected static PersonService personService;
049        protected static IdentityService identityService;
050
051        private String lookupRoleNamespaceCode;
052        private String lookupRoleName;
053
054        // principal data
055        protected String principalId;
056        protected String principalName;
057        protected String entityId;
058        protected String entityTypeCode;
059        // name data
060        protected String firstName = "";
061        protected String middleName = "";
062        protected String lastName = "";
063
064        protected String name = "";
065        // address data
066    protected EntityAddress address;
067        /*protected String addressLine1 = "";
068        protected String addressLine2 = "";
069        protected String addressLine3 = "";
070        protected String addressCity = "";
071        protected String addressStateProvinceCode = "";
072        protected String addressPostalCode = "";
073        protected String addressCountryCode = "";*/
074        // email data
075        protected String emailAddress = "";
076        // phone data
077        protected String phoneNumber = "";
078        // privacy preferences data
079        protected boolean suppressName = false;
080        protected boolean suppressAddress = false;
081        protected boolean suppressPhone = false;
082        protected boolean suppressPersonal = false;
083        protected boolean suppressEmail = false;
084        // affiliation data
085        protected List<? extends EntityAffiliationContract> affiliations;
086
087        protected String campusCode = "";
088        //protected Campus campus;
089        // external identifier data
090        protected Map<String,String> externalIdentifiers = null;
091        // employment data
092        protected String employeeStatusCode = "";
093        protected EntityEmploymentStatusBo employeeStatus;
094        protected String employeeTypeCode = "";
095        protected EntityEmploymentTypeBo employeeType;
096        protected String primaryDepartmentCode = "";
097        protected String employeeId = "";
098
099        protected KualiDecimal baseSalaryAmount = KualiDecimal.ZERO;
100        protected boolean active = true;
101
102        public PersonImpl() {}
103
104        public PersonImpl( Principal principal, String personEntityTypeCode ) {
105                this( principal, null, personEntityTypeCode );
106        }
107
108        public PersonImpl( Principal principal, EntityDefault entity, String personEntityTypeCode ) {
109                setPrincipal( principal, entity, personEntityTypeCode );
110        }
111
112        public PersonImpl( String principalId, String personEntityTypeCode ) {
113                this( getIdentityService().getPrincipal(principalId), personEntityTypeCode );
114        }
115
116        public PersonImpl( EntityDefaultInfoCacheBo p ) {
117                entityId = p.getEntityId();
118                principalId = p.getPrincipalId();
119                principalName = p.getPrincipalName();
120                entityTypeCode = p.getEntityTypeCode();
121                firstName = p.getFirstName();
122                middleName = p.getMiddleName();
123                lastName = p.getLastName();
124                name = p.getName();
125                campusCode = p.getCampusCode();
126                primaryDepartmentCode = p.getPrimaryDepartmentCode();
127                employeeId = p.getEmployeeId();
128                affiliations = new ArrayList<EntityAffiliation>( 0 );
129                externalIdentifiers = new HashMap<String,String>( 0 );
130        }
131
132        /**
133         * Sets the principal object and populates the person object from that.
134         */
135        public void setPrincipal(Principal principal, EntityDefault entity, String personEntityTypeCode) {
136                populatePrincipalInfo( principal );
137                if ( entity == null ) {
138                        entity = getIdentityService().getEntityDefault( principal.getEntityId() );
139                }
140                populateEntityInfo( entity, principal, personEntityTypeCode );
141        }
142
143
144        protected void populatePrincipalInfo( Principal principal ) {
145                entityId = principal.getEntityId();
146                principalId = principal.getPrincipalId();
147                principalName = principal.getPrincipalName();
148                active = principal.isActive();
149        }
150
151        protected void populateEntityInfo( EntityDefault entity, Principal principal, String personEntityTypeCode ) {
152                if(entity!=null){
153                    populatePrivacyInfo (entity );
154                        EntityTypeContactInfoDefault entityTypeContactInfoDefault = entity.getEntityType( personEntityTypeCode );
155                        entityTypeCode = personEntityTypeCode;
156                        populateNameInfo( personEntityTypeCode, entity, principal );
157                        populateAddressInfo( entityTypeContactInfoDefault );
158                        populateEmailInfo( entityTypeContactInfoDefault );
159                        populatePhoneInfo( entityTypeContactInfoDefault );
160                        populateAffiliationInfo( entity );
161                        populateEmploymentInfo( entity );
162                        populateExternalIdentifiers( entity );
163                }
164        }
165
166        protected void populateNameInfo( String entityTypeCode, EntityDefault entity, Principal principal ) {
167                if(entity!=null){
168                        EntityName entityName = entity.getName();
169                        if ( entityName != null ) {
170                                firstName = unNullify( entityName.getFirstName());
171                                middleName = unNullify( entityName.getMiddleName() );
172                                lastName = unNullify( entityName.getLastName() );
173                                if ( entityTypeCode.equals( KimConstants.EntityTypes.SYSTEM ) ) {
174                                        name = principal.getPrincipalName().toUpperCase();
175                                } else {
176                                        name = unNullify( entityName.getCompositeName() );
177                                        if(name.equals("") || name == null){
178                                                name = lastName + ", " + firstName;
179                                        }
180                                }
181                        } else {
182                                firstName = "";
183                                middleName = "";
184                                if ( entityTypeCode.equals( KimConstants.EntityTypes.SYSTEM ) ) {
185                                        name = principal.getPrincipalName().toUpperCase();
186                                        lastName = principal.getPrincipalName().toUpperCase();
187                                } else {
188                                        name = "";
189                                        lastName = "";
190                                }
191                        }
192                }
193        }
194
195        protected void populatePrivacyInfo (EntityDefault entity) {
196            if(entity!=null) {
197            if (entity.getPrivacyPreferences() != null) {
198                    suppressName = entity.getPrivacyPreferences().isSuppressName();
199                    suppressAddress = entity.getPrivacyPreferences().isSuppressAddress();
200                    suppressPhone = entity.getPrivacyPreferences().isSuppressPhone();
201                    suppressPersonal = entity.getPrivacyPreferences().isSuppressPersonal();
202                    suppressEmail = entity.getPrivacyPreferences().isSuppressEmail();
203            }
204            }
205        }
206
207        protected void populateAddressInfo( EntityTypeContactInfoDefault contactInfoDefault ) {
208                if(contactInfoDefault!=null){
209                        EntityAddress defaultAddress = contactInfoDefault.getDefaultAddress();
210                        if ( defaultAddress != null ) {
211                address = defaultAddress;
212                        } else {
213                EntityAddress.Builder builder = EntityAddress.Builder.create();
214                builder.setCity("");
215                builder.setCountryCode("");
216                builder.setLine1("");
217                builder.setLine2("");
218                builder.setLine3("");
219                builder.setCity("");
220                builder.setPostalCode("");
221                builder.setStateProvinceCode("");
222                builder.setActive(true);
223                                address = builder.build();
224                        }
225                }
226        }
227
228        protected void populateEmailInfo( EntityTypeContactInfoDefault contactInfoDefault ) {
229                if(contactInfoDefault!=null){
230                        EntityEmailContract entityEmail = contactInfoDefault.getDefaultEmailAddress();
231                        if ( entityEmail != null ) {
232                                emailAddress = unNullify( entityEmail.getEmailAddressUnmasked() );
233                        } else {
234                                emailAddress = "";
235                        }
236                }
237        }
238
239        protected void populatePhoneInfo( EntityTypeContactInfoDefault contactInfoDefault ) {
240                if(contactInfoDefault!=null){
241                        EntityPhoneContract entityPhone = contactInfoDefault.getDefaultPhoneNumber();
242                        if ( entityPhone != null ) {
243                                phoneNumber = unNullify( entityPhone.getFormattedPhoneNumberUnmasked() );
244                        } else {
245                                phoneNumber = "";
246                        }
247                }
248        }
249
250        protected void populateAffiliationInfo(EntityDefault entity ) {
251                if(entity!=null){
252                        affiliations = entity.getAffiliations();
253                        EntityAffiliation defaultAffiliation = entity.getDefaultAffiliation();
254                        if ( defaultAffiliation != null  ) {
255                                campusCode = unNullify( defaultAffiliation.getCampusCode() );
256                        } else {
257                                campusCode = "";
258                        }
259                }
260        }
261
262        protected void populateEmploymentInfo( EntityDefault entity ) {
263                if(entity!=null){
264                        EntityEmployment employmentInformation = entity.getEmployment();
265                        if ( employmentInformation != null ) {
266                                employeeStatusCode = unNullify( employmentInformation.getEmployeeStatus() != null ? employmentInformation.getEmployeeStatus().getCode() : null);
267                                employeeTypeCode = unNullify( employmentInformation.getEmployeeType() != null ? employmentInformation.getEmployeeType().getCode() : null);
268                                primaryDepartmentCode = unNullify( employmentInformation.getPrimaryDepartmentCode() );
269                                employeeId = unNullify( employmentInformation.getEmployeeId() );
270                                if ( employmentInformation.getBaseSalaryAmount() != null ) {
271                                        baseSalaryAmount = employmentInformation.getBaseSalaryAmount();
272                                } else {
273                                        baseSalaryAmount = KualiDecimal.ZERO;
274                                }
275                        } else {
276                                employeeStatusCode = "";
277                                employeeTypeCode = "";
278                                primaryDepartmentCode = "";
279                                employeeId = "";
280                                baseSalaryAmount = KualiDecimal.ZERO;
281                        }
282                }
283        }
284
285        protected void populateExternalIdentifiers( EntityDefault entity ) {
286                if(entity!=null){
287                        List<? extends EntityExternalIdentifier> externalIds = entity.getExternalIdentifiers();
288                        externalIdentifiers = new HashMap<String,String>( externalIds.size() );
289                        for ( EntityExternalIdentifier eei : externalIds ) {
290                                externalIdentifiers.put( eei.getExternalIdentifierTypeCode(), eei.getExternalId() );
291                        }
292                }
293        }
294
295        /** So users of this class don't need to program around nulls. */
296        private String unNullify( String str ) {
297                if ( str == null ) {
298                        return "";
299                }
300                return str;
301        }
302
303        /**
304         * @see org.kuali.rice.kim.api.identity.Person#getEntityId()
305         */
306        @Override
307    public String getEntityId() {
308                return entityId;
309        }
310
311        /**
312         * @see org.kuali.rice.kim.api.identity.Person#getPrincipalId()
313         */
314        @Override
315    public String getPrincipalId() {
316                return principalId;
317        }
318
319        /**
320         * This overridden method ...
321         *
322         * @see org.kuali.rice.kim.api.identity.Person#getPrincipalName()
323         */
324        @Override
325    public String getPrincipalName() {
326                return principalName;
327        }
328
329        /**
330         * @see org.kuali.rice.kim.api.identity.Person#getFirstName()
331         */
332        @Override
333    public String getFirstName() {
334            if (suppressName){
335                return KimConstants.RESTRICTED_DATA_MASK;
336            }
337                return firstName;
338        }
339
340        /**
341     * @see org.kuali.rice.kim.api.identity.Person#getFirstNameUnmasked()
342     */
343    @Override
344    public String getFirstNameUnmasked() {
345        return firstName;
346    }
347
348        /**
349         * @see org.kuali.rice.kim.api.identity.Person#getMiddleName()
350         */
351        @Override
352    public String getMiddleName() {
353            if (suppressName){
354            return KimConstants.RESTRICTED_DATA_MASK;
355        }
356                return middleName;
357        }
358
359        /**
360     * @see org.kuali.rice.kim.api.identity.Person#getMiddleNameUnmasked()
361     */
362        @Override
363    public String getMiddleNameUnmasked() {
364            return middleName;
365        }
366
367        /**
368         * @see org.kuali.rice.kim.api.identity.Person#getLastName()
369         */
370        @Override
371    public String getLastName() {
372            if (suppressName){
373            return KimConstants.RESTRICTED_DATA_MASK;
374        }
375                return lastName;
376        }
377
378        /**
379     * @see org.kuali.rice.kim.api.identity.Person#getLastNameUnmasked()
380     */
381    @Override
382    public String getLastNameUnmasked() {
383        return lastName;
384    }
385
386        /**
387         * @see org.kuali.rice.kim.api.identity.Person#getName()
388         */
389        @Override
390    public String getName() {
391        if (suppressName) {
392            return KimConstants.RESTRICTED_DATA_MASK;
393        }
394        return name;
395    }
396
397        @Override
398    public String getNameUnmasked() {
399            return this.name;
400        }
401
402        /**
403         * @see org.kuali.rice.kim.api.identity.Person#getPhoneNumber()
404         */
405        @Override
406    public String getPhoneNumber() {
407            if (suppressPhone){
408            return KimConstants.RESTRICTED_DATA_MASK;
409        }
410                return phoneNumber;
411        }
412
413           /**
414     * @see org.kuali.rice.kim.api.identity.Person#getPhoneNumberUnmasked()
415     */
416    @Override
417    public String getPhoneNumberUnmasked() {
418        return phoneNumber;
419    }
420
421        /**
422         * @see org.kuali.rice.kim.api.identity.Person#getEmailAddress()
423         */
424        @Override
425    public String getEmailAddress() {
426            if (suppressEmail){
427            return KimConstants.RESTRICTED_DATA_MASK;
428        }
429                return emailAddress;
430        }
431
432        @Override
433    public String getEmailAddressUnmasked() {
434            return emailAddress;
435        }
436
437        public List<? extends EntityAffiliationContract> getAffiliations() {
438                return affiliations;
439        }
440
441        /**
442         * This overridden method ...
443         *
444         * @see org.kuali.rice.kim.api.identity.Person#hasAffiliationOfType(java.lang.String)
445         */
446        @Override
447    public boolean hasAffiliationOfType(String affiliationTypeCode) {
448                return getCampusCodesForAffiliationOfType(affiliationTypeCode).size() > 0;
449        }
450
451
452        @Override
453    public List<String> getCampusCodesForAffiliationOfType(String affiliationTypeCode) {
454                ArrayList<String> campusCodes = new ArrayList<String>( 3 );
455                if ( affiliationTypeCode == null ) {
456                        return campusCodes;
457                }
458                for ( EntityAffiliationContract a : getAffiliations() ) {
459                        if ( a.getAffiliationType().getCode().equals(affiliationTypeCode)  ) {
460                                campusCodes.add( a.getCampusCode() );
461                        }
462                }
463                return campusCodes;
464        }
465
466        /**
467         * @see org.kuali.rice.kim.api.identity.Person#getExternalId(java.lang.String)
468         */
469        @Override
470    public String getExternalId(String externalIdentifierTypeCode) {
471                return externalIdentifiers.get( externalIdentifierTypeCode );
472        }
473
474        /**
475         * Pulls the campus code from the default affiliation for the identity.
476         * Returns null if no default affiliation is set.
477         * @see org.kuali.rice.kim.api.identity.Person#getCampusCode()
478         */
479        @Override
480    public String getCampusCode() {
481                return campusCode;
482        }
483
484        /**
485         * @return the personService
486         */
487        @SuppressWarnings("unchecked")
488        public static PersonService getPersonService() {
489                if ( personService == null ) {
490                        personService = KimApiServiceLocator.getPersonService();
491                }
492                return personService;
493        }
494
495        /**
496         * @return the identityService
497         */
498        public static IdentityService getIdentityService() {
499                if ( identityService == null ) {
500                        identityService = KimApiServiceLocator.getIdentityService();
501                }
502                return identityService;
503        }
504
505        /**
506         * @see org.kuali.rice.kim.api.identity.Person#getExternalIdentifiers()
507         */
508        @Override
509    public Map<String,String> getExternalIdentifiers() {
510                return externalIdentifiers;
511        }
512
513        @Override
514    public String getAddressLine1() {
515            return address.getLine1();
516        }
517
518        @Override
519    public String getAddressLine1Unmasked() {
520            return address.getLine1Unmasked();
521        }
522
523        @Override
524    public String getAddressLine2() {
525            return address.getLine2();
526        }
527
528        @Override
529    public String getAddressLine2Unmasked() {
530        return address.getLine2Unmasked();
531    }
532
533        @Override
534    public String getAddressLine3() {
535            return address.getLine3();
536        }
537
538        @Override
539    public String getAddressLine3Unmasked() {
540        return address.getLine3Unmasked();
541    }
542
543        @Override
544    public String getAddressCity() {
545            return address.getCity();
546        }
547
548        @Override
549    public String getAddressCityUnmasked() {
550        return address.getCityUnmasked();
551    }
552
553        @Override
554    public String getAddressStateProvinceCode() {
555            return address.getStateProvinceCode();
556        }
557
558        @Override
559    public String getAddressStateProvinceCodeUnmasked() {
560        return address.getStateProvinceCodeUnmasked();
561    }
562
563        @Override
564    public String getAddressPostalCode() {
565            return address.getPostalCode();
566        }
567
568        @Override
569    public String getAddressPostalCodeUnmasked() {
570        return address.getPostalCodeUnmasked();
571    }
572
573        @Override
574    public String getAddressCountryCode() {
575            return address.getCountryCode();
576        }
577
578        @Override
579    public String getAddressCountryCodeUnmasked() {
580        return address.getCountryCodeUnmasked();
581    }
582
583        @Override
584    public String getEmployeeStatusCode() {
585                return this.employeeStatusCode;
586        }
587
588        @Override
589    public String getEmployeeTypeCode() {
590                return this.employeeTypeCode;
591        }
592
593        @Override
594    public KualiDecimal getBaseSalaryAmount() {
595                return this.baseSalaryAmount;
596        }
597
598        @Override
599    public String getEmployeeId() {
600                return this.employeeId;
601        }
602
603        @Override
604    public String getPrimaryDepartmentCode() {
605                return this.primaryDepartmentCode;
606        }
607
608        @Override
609    public String getEntityTypeCode() {
610                return this.entityTypeCode;
611        }
612
613        @Override
614    public boolean isActive() {
615                return this.active;
616        }
617
618        public void setActive(boolean active) {
619                this.active = active;
620        }
621
622        /**
623         * @return the lookupRoleNamespaceCode
624         */
625        public String getLookupRoleNamespaceCode() {
626                return this.lookupRoleNamespaceCode;
627        }
628
629        /**
630         * @param lookupRoleNamespaceCode the lookupRoleNamespaceCode to set
631         */
632        public void setLookupRoleNamespaceCode(String lookupRoleNamespaceCode) {
633                this.lookupRoleNamespaceCode = lookupRoleNamespaceCode;
634        }
635
636        /**
637         * @return the lookupRoleName
638         */
639        public String getLookupRoleName() {
640                return this.lookupRoleName;
641        }
642
643        /**
644         * @param lookupRoleName the lookupRoleName to set
645         */
646        public void setLookupRoleName(String lookupRoleName) {
647                this.lookupRoleName = lookupRoleName;
648        }
649
650        /**
651         * @param principalName the principalName to set
652         */
653        public void setPrincipalName(String principalName) {
654                this.principalName = principalName;
655        }
656
657        /**
658         * @param name the name to set
659         */
660        public void setName(String name) {
661                this.name = name;
662        }
663
664        //public Campus getCampus() {
665        //      return this.campus;
666        //}
667
668        public EntityEmploymentStatusBo getEmployeeStatus() {
669                return this.employeeStatus;
670        }
671
672        public EntityEmploymentTypeBo getEmployeeType() {
673                return this.employeeType;
674        }
675}