View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.impl.identity;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.type.KualiDecimal;
20  import org.kuali.rice.kim.api.KimConstants;
21  import org.kuali.rice.kim.api.identity.IdentityService;
22  import org.kuali.rice.kim.api.identity.Person;
23  import org.kuali.rice.kim.api.identity.PersonService;
24  import org.kuali.rice.kim.api.identity.address.EntityAddress;
25  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
26  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationContract;
27  import org.kuali.rice.kim.api.identity.email.EntityEmailContract;
28  import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
29  import org.kuali.rice.kim.api.identity.entity.EntityDefault;
30  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
31  import org.kuali.rice.kim.api.identity.name.EntityName;
32  import org.kuali.rice.kim.api.identity.phone.EntityPhoneContract;
33  import org.kuali.rice.kim.api.identity.principal.Principal;
34  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
35  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentStatusBo;
37  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentTypeBo;
38  import org.kuali.rice.krad.bo.TransientBusinessObjectBase;
39  
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  public class PersonImpl extends TransientBusinessObjectBase implements Person {
46  
47  	private static final long serialVersionUID = 1L;
48  	
49  	protected static PersonService personService;
50  	protected static IdentityService identityService;
51  
52  	private String lookupRoleNamespaceCode;
53  	private String lookupRoleName;
54  	
55  	// principal data
56  	protected String principalId;
57  	protected String principalName;
58  	protected String entityId;
59  	protected String entityTypeCode;
60  	// name data
61  	protected String firstName = "";
62  	protected String middleName = "";
63  	protected String lastName = "";
64  
65  	protected String name = "";
66  	// address data
67      protected EntityAddress address;
68  	/*protected String addressLine1 = "";
69  	protected String addressLine2 = "";
70  	protected String addressLine3 = "";
71  	protected String addressCity = "";
72  	protected String addressStateProvinceCode = "";
73  	protected String addressPostalCode = "";
74  	protected String addressCountryCode = "";*/
75  	// email data
76  	protected String emailAddress = "";
77  	// phone data
78  	protected String phoneNumber = "";
79  	// privacy preferences data
80  	protected boolean suppressName = false;
81  	protected boolean suppressAddress = false;
82  	protected boolean suppressPhone = false;
83  	protected boolean suppressPersonal = false;
84  	protected boolean suppressEmail = false;
85  	// affiliation data
86  	protected List<? extends EntityAffiliationContract> affiliations;
87  	
88  	protected String campusCode = "";
89  	//protected Campus campus;
90  	// external identifier data
91  	protected Map<String,String> externalIdentifiers = null;
92  	// employment data
93  	protected String employeeStatusCode = "";
94  	protected EntityEmploymentStatusBo employeeStatus;
95  	protected String employeeTypeCode = "";
96  	protected EntityEmploymentTypeBo employeeType;
97  	protected String primaryDepartmentCode = "";
98  	protected String employeeId = "";
99  	
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 }