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