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