com.sfs.whichdoctor.formatter.PersonFormatter.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.formatter.PersonFormatter.java

Source

/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.whichdoctor.formatter;

import com.sfs.Formatter;
import com.sfs.beans.ObjectTypeBean;
import com.sfs.whichdoctor.beans.AccreditationBean;
import com.sfs.whichdoctor.beans.AddressBean;
import com.sfs.whichdoctor.beans.DebitBean;
import com.sfs.whichdoctor.beans.EmailBean;
import com.sfs.whichdoctor.beans.ExamBean;
import com.sfs.whichdoctor.beans.FinancialSummaryBean;
import com.sfs.whichdoctor.beans.IsbEntityBean;
import com.sfs.whichdoctor.beans.ItemBean;
import com.sfs.whichdoctor.beans.MembershipBean;
import com.sfs.whichdoctor.beans.MemoBean;
import com.sfs.whichdoctor.beans.PersonBean;
import com.sfs.whichdoctor.beans.PhoneBean;
import com.sfs.whichdoctor.beans.PreferencesBean;
import com.sfs.whichdoctor.beans.RotationBean;
import com.sfs.whichdoctor.beans.SpecialtyBean;
import com.sfs.whichdoctor.beans.TagBean;
import com.sfs.whichdoctor.beans.QualificationBean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;

public class PersonFormatter {

    public static String getField(final PersonBean person, final String field, final String format,
            final PreferencesBean preferences) {

        String value = "";
        if (person == null || field == null) {
            return value;
        }

        if (field.compareTo("Tags") == 0) {
            value = OutputFormatter.toTagList(person.getTags());
        }

        if (field.compareTo("GUID") == 0) {
            if (person.getGUID() > 0) {
                value = String.valueOf(person.getGUID());
            }
        }

        if (field.compareTo("PersonId") == 0) {
            if (person.getId() > 0) {
                value = String.valueOf(person.getId());
            }
        }

        if (field.compareTo("Gender") == 0) {
            value = person.getGender();
        }

        if (StringUtils.equals(field, "Title") || StringUtils.equals(field, "SALUTATION-1")) {
            value = person.getTitle();
        }
        if (field.compareTo("First Name") == 0) {
            value = person.getFirstName();
        }
        if (StringUtils.equals(field, "INVESTOR-GIVENNAME-1")) {
            value = person.getFirstName();
            if (StringUtils.isNotBlank(person.getMiddleName())) {
                value += " " + person.getMiddleName();
            }
        }
        if (StringUtils.equals(field, "Preferred Name") || StringUtils.equals(field, "INVESTOR-PREFERREDNAME-1")) {
            value = person.getPreferredName();
        }
        if (field.compareTo("Middle Name") == 0) {
            value = person.getMiddleName();
        }
        if (StringUtils.equals(field, "Last Name") || StringUtils.equals(field, "INVESTOR-SURNAME-1")) {
            value = person.getLastName();
        }
        if (field.compareTo("Maiden Name") == 0) {
            value = person.getMaidenName();
        }
        if (field.compareTo("Formatted Name") == 0) {
            value = OutputFormatter.toFormattedName(person);
        }
        if (field.compareTo("Honors") == 0) {
            value = person.getHonors();
        }
        if (field.compareTo("Age") == 0) {
            value = person.getAge();
        }
        if (StringUtils.equals(field, "MIN") || StringUtils.equals(field, "HOLDER-NUMBER")
                || StringUtils.equals(field, "ALTERNATE-ID")) {
            value = String.valueOf(person.getPersonIdentifier());
        }

        String status = "";

        if (person.getPrimaryMembership() != null) {
            MembershipBean membership = person.getPrimaryMembership();
            status = membership.getField("Status");

            if (StringUtils.equals(field, "Candidate Number")) {
                value = membership.getField("Candidate Number");
            }

            if (StringUtils.equals(field, "Status")) {
                value = status;
            }

            if (StringUtils.equals(field, "Membership Class")) {
                ObjectTypeBean object = membership.getObjectTypeField("Membership Type");
                value = object.getClassName();
            }

            if (StringUtils.equals(field, "Membership Type") || StringUtils.equals(field, "RACP-MEMBERSHIP-TYPE")) {
                ObjectTypeBean object = membership.getObjectTypeField("Membership Type");
                value = object.getName();
            }

            if (StringUtils.equals(field, "Date Registered")) {
                if (membership.getJoinedDate() != null) {
                    value = Formatter.conventionalDate(membership.getJoinedDate());
                }
            }

            if (StringUtils.equals(field, "Date Retired")) {
                if (membership.getLeftDate() != null) {
                    value = Formatter.conventionalDate(membership.getLeftDate());
                }
            }

            if (StringUtils.equals(field, "Financial Exemption")) {
                value = membership.getField("Financial Excemption");
            }

            if (StringUtils.equals(field, "Division")) {
                value = membership.getField("Division");
            }

            if (StringUtils.equals(field, "Supervisor Status")) {
                value = membership.getField("Supervisor Status");
            }

            if (StringUtils.equals(field, "Training Type")) {
                value = membership.getField("Training Type");
            }
        }

        if (StringUtils.equals(field, "Works Overseas")) {
            value = Formatter.convertBoolean(person.getWorksOverseas());
        }
        if (StringUtils.equals(field, "Exam Deadline")) {
            value = Formatter.conventionalDate(person.getExamDeadline());
        }
        if (StringUtils.equals(field, "Birthdate") && person.getBirthDate() != null) {
            value = Formatter.conventionalDate(person.getBirthDate());
        }

        if (StringUtils.equals(field, "DATE-OF-BIRTH") && person.getBirthDate() != null) {
            value = Formatter.numericDate(person.getBirthDate(), "yyyyMMdd");
        }

        if (StringUtils.equals(field, "Date Deceased") && person.getDeceasedDate() != null) {
            value = Formatter.conventionalDate(person.getDeceasedDate());
        }

        if (StringUtils.equals(field, "INVESTOR-DECEASED")) {
            value = "N";
            if (person.getDeceasedDate() != null) {
                value = "Y";
            }
        }

        /** Training curriculum year **/
        if (field.compareTo("Training Curriculum Year") == 0 && person.getSpecialtyList() != null) {
            value = person.getCurriculumYear();
        }

        if (field.compareTo("Training Status") == 0) {
            value = person.getTrainingStatus();
            if (StringUtils.isNotBlank(person.getTrainingStatusDetail())) {
                value += " - " + person.getTrainingStatusDetail();
            }
        }

        if (field.compareTo("CHCCH-TRAINEE") == 0) {
            value = "N";

            if (isCCHTrainee(person)) {
                value = "Y";
            }
        }

        /** Fellowship information **/

        if (person.getMembershipDetails() != null) {
            for (MembershipBean membership : person.getMembershipDetails()) {
                if (StringUtils.equals(membership.getMembershipClass(), "RACP")
                        && StringUtils.equals(membership.getMembershipType(), "Fellowship Details")) {

                    if (StringUtils.equals(field, "FRACP")) {
                        if (value.length() > 0) {
                            value += " and ";
                        }
                        value += membership.getField("FRACP");
                    }
                    if (StringUtils.equals(field, "Admitting S.A.C.")) {
                        if (value.length() > 0) {
                            value += " and ";
                        }
                        value += membership.getField("Admitting SAC");
                    }
                    if (StringUtils.equals(field, "Admitting Country")
                            || StringUtils.equals(field, "RACP-ADMITTING-COUNTRY")) {
                        if (value.length() > 0) {
                            value += " and ";
                        }
                        value += membership.getField("Admitting Country");
                    }
                    if (StringUtils.equals(field, "Fellowship Date")
                            && membership.getDateField("Fellowship Date") != null) {
                        if (value.length() > 0) {
                            value += " and ";
                        }
                        value += Formatter.conventionalDate(membership.getDateField("Fellowship Date"));
                    }

                    if (StringUtils.equals(field, "DATE-FIRST-REGISTERED")
                            && membership.getDateField("Fellowship Date") != null) {
                        value = Formatter.numericDate(membership.getDateField("Fellowship Date"), "yyyyMMdd");
                    }
                }

                if (StringUtils.equals(membership.getMembershipClass(), "RACP")
                        && StringUtils.equals(membership.getMembershipType(), "Affiliation")) {

                    if (StringUtils.equals(field, "RACP-FACULTY")) {
                        ObjectTypeBean affiliation = membership.getObjectTypeField("Affiliation");
                        if (value.length() > 0) {
                            value += " and ";
                        }
                        value = affiliation.getAbbreviation();
                    }
                }
            }
        }

        if (StringUtils.equals(field, "CLASSIFICATION-CODE")) {
            String subType = "";

            // Is a trainee
            if (person.getPrimaryMembership() != null) {
                String membershipType = person.getPrimaryMembership().getObjectTypeField("Membership Type")
                        .getName();

                if (StringUtils.containsIgnoreCase(membershipType, "Trainee")) {
                    subType = "C";

                    if (StringUtils.equalsIgnoreCase(person.getTrainingStatus(), "Training")
                            && StringUtils.equalsIgnoreCase(person.getTrainingStatusDetail(), "Post-FRACP")) {
                        // The person is a Post-FRACP trainee, so they qualify as a 'B'
                        subType = "B";
                    }
                }

            }

            // Is a Fellow
            if (person.getMembershipDetails() != null) {
                for (MembershipBean membership : person.getMembershipDetails()) {
                    if (StringUtils.equals(membership.getMembershipClass(), "RACP")
                            && StringUtils.equals(membership.getMembershipType(), "Fellowship Details")) {

                        if (StringUtils.startsWith(status, "Active")
                                || StringUtils.equalsIgnoreCase(status, "Life Fellow")
                                || StringUtils.equalsIgnoreCase(status, "Retired")
                                || StringUtils.equalsIgnoreCase(status, "Semi-retired")) {
                            subType = "B";
                        }
                    }
                }
            }

            // Is a Community Child Health trainee
            if (isCCHTrainee(person)) {
                subType = "D";
            }

            // Is an Honorary Fellow
            if (person.getTags() != null) {
                for (TagBean tag : person.getTags()) {
                    if (StringUtils.equalsIgnoreCase(tag.getTagName(), "Honorary Fellow")) {
                        subType = "A";
                    }
                }
            }

            if (StringUtils.isNotBlank(subType)) {
                value = "RACP-" + subType;
            }
        }

        if (field.compareTo("Last Workplace") == 0 && person.getEmployers() != null) {
            String workplaceName = "";
            for (String identifier : person.getEmployers().keySet()) {
                final ItemBean workplace = person.getEmployers().get(identifier);
                if (workplace != null && StringUtils.isBlank(workplaceName)) {
                    workplaceName = workplace.getName();
                }
            }
            value = workplaceName;
        }

        if (field.compareTo("Last Rotation Description") == 0 && person.getRotations() != null) {
            for (RotationBean rotation : person.getRotations()) {
                if (rotation.getDescription() != null) {
                    value = rotation.getDescription();
                }
            }
        }

        if (field.compareTo("Last Rotation Date") == 0 && person.getRotations() != null) {
            for (RotationBean rotation : person.getRotations()) {
                if (rotation.getEndDate() != null) {
                    value = Formatter.conventionalDate(rotation.getEndDate());
                }
            }
        }

        if (field.compareTo("Accred: Basic (wks)") == 0) {
            value = calculateTrainingSummaryTotal(person.getTrainingSummary("Basic Training"), false);
        }

        if (field.compareTo("Accred: Basic (mths)") == 0) {
            value = calculateTrainingSummaryTotal(person.getTrainingSummary("Basic Training"), true);
        }

        if (field.compareTo("Accred: Advanced (wks)") == 0) {
            value = calculateTrainingSummaryTotal(person.getTrainingSummary("Advanced Training"), false);
        }

        if (field.compareTo("Accred: Advanced (mths)") == 0) {
            value = calculateTrainingSummaryTotal(person.getTrainingSummary("Advanced Training"), true);
        }

        if (field.compareTo("Accred: Post-FRACP (wks)") == 0) {
            value = calculateTrainingSummaryTotal(person.getTrainingSummary("Post-FRACP Training"), false);
        }

        if (field.compareTo("Accred: Post-FRACP (mths)") == 0) {
            value = calculateTrainingSummaryTotal(person.getTrainingSummary("Post-FRACP Training"), true);
        }

        if (field.compareTo("Opening Balance") == 0) {
            FinancialSummaryBean openResults = person.getFinancialSummary();
            FinancialSummaryBean restrictedResults = person.getRestrictedFinancialSummary();

            double balance = 0;
            if (openResults != null) {
                balance += openResults.getOpeningBalance();
            }
            if (restrictedResults != null) {
                balance += restrictedResults.getOpeningBalance();
            }
            value = Formatter.toCurrency(balance, "$");
        }

        if (field.compareTo("Closing Balance") == 0) {
            FinancialSummaryBean openResults = person.getFinancialSummary();
            FinancialSummaryBean restrictedResults = person.getRestrictedFinancialSummary();

            double balance = 0;
            if (openResults != null) {
                balance += openResults.getClosingBalance();
            }
            if (restrictedResults != null) {
                balance += restrictedResults.getClosingBalance();
            }
            value = Formatter.toCurrency(balance, "$");
        }

        if (field.compareTo("Outstanding Summary") == 0) {
            final StringBuffer debits = new StringBuffer();
            if (person.getDebits() != null) {
                for (DebitBean debit : person.getDebits()) {
                    if (debits.length() > 0) {
                        debits.append(", ");
                    }
                    debits.append(debit.getAbbreviation());
                    debits.append(" ");
                    debits.append(debit.getNumber());
                }
            }
            value = debits.toString();
        }

        if (field.compareTo("Country B.M.Q. Awarded") == 0 && person.getQualifications() != null) {
            for (QualificationBean qualification : person.getQualifications()) {
                if (StringUtils.equalsIgnoreCase(qualification.getQualificationSubType(),
                        "Basic medical qualification")) {
                    // Qualification is flagged as BMQ, record value of BMQ country
                    if (value.compareTo("") != 0 && value.compareTo(qualification.getCountry()) != 0) {
                        // BMQ country has already been set, add an ' and ' as a delimiter
                        value += " and ";
                    }
                    if (value.compareTo(qualification.getCountry()) != 0) {
                        // Only add to value if qualification country is unique
                        value += qualification.getCountry();
                    }
                }
            }
        }
        if (field.compareTo("Year B.M.Q. Awarded") == 0 && person.getQualifications() != null) {
            for (QualificationBean qualification : person.getQualifications()) {
                if (StringUtils.equalsIgnoreCase(qualification.getQualificationSubType(),
                        "Basic medical qualification")) {
                    // Qualification is flagged as BMQ, record value of BMQ year
                    if (value.compareTo("") != 0 && value.compareTo(String.valueOf(qualification.getYear())) != 0) {
                        // BMQ year has already been set, add an ' and ' as a delimiter
                        value += " and ";
                    }
                    if (value.compareTo(String.valueOf(qualification.getYear())) != 0) {
                        // Only add to value if qualification year is unique
                        value += qualification.getYear();
                    }
                }
            }
        }
        if (field.compareTo("ISB Targets") == 0) {
            if (person.getIsbEntities() != null) {
                for (IsbEntityBean isbEntity : person.getIsbEntities()) {
                    if (value.length() > 0) {
                        value += ", ";
                    }
                    value += isbEntity.getTarget();
                }
            }
        }

        // Contact details
        if (person.getFirstEmailAddress() != null) {

            if (StringUtils.equals(field, "Email Address") || StringUtils.equals(field, "EMAIL-ADDR")) {
                value = person.getFirstEmailAddress().getEmail();
                if (StringUtils.equals(format, "html")) {
                    value = "<a href=\"mailto:" + value + "\">" + value + "</a>";
                }
            }
        }

        if (field.compareTo("Phone Number") == 0 && person.getFirstPhoneNo() != null) {
            value = Formatter.getPhone(String.valueOf(person.getFirstPhoneNo().getCountryCode()),
                    String.valueOf(person.getFirstPhoneNo().getAreaCode()), person.getFirstPhoneNo().getNumber());
        }

        if (StringUtils.equals(field, "PHONE-NO") || StringUtils.equals(field, "MOBILE-NO")
                || StringUtils.equals(field, "FAX-NO")) {

            String phoneNumber = "";
            boolean phonePreferred = false;
            String mobileNumber = "";
            boolean mobilePreferred = false;
            String faxNumber = "";
            boolean faxPreferred = false;

            if (person.getPhone() != null) {
                for (PhoneBean phone : person.getPhone()) {
                    String type = phone.getContactType();
                    StringBuffer number = new StringBuffer();

                    if (phone.getCountryCode() > 0) {
                        number.append(phone.getCountryCode());
                        number.append(" ");
                    }
                    if (phone.getAreaCode() > 0) {
                        number.append(phone.getAreaCode());
                        number.append(" ");
                    }
                    if (StringUtils.isNotBlank(phone.getNumber())) {
                        number.append(phone.getNumber());
                    }

                    if (StringUtils.equalsIgnoreCase(type, "Home") || StringUtils.equalsIgnoreCase(type, "Work")) {
                        if (!phonePreferred) {
                            phoneNumber = number.toString();
                            if (phone.getPrimary()) {
                                phonePreferred = true;
                            }
                        }
                    }
                    if (StringUtils.equalsIgnoreCase(type, "Mobile")) {
                        if (!mobilePreferred) {
                            mobileNumber = number.toString();
                            if (phone.getPrimary()) {
                                mobilePreferred = true;
                            }
                        }
                    }
                    if (StringUtils.equalsIgnoreCase(type, "Home Fax")
                            || StringUtils.equalsIgnoreCase(type, "Work Fax")) {
                        if (!faxPreferred) {
                            faxNumber = number.toString();
                            if (phone.getPrimary()) {
                                faxPreferred = true;
                            }
                        }
                    }
                }
            }

            if (StringUtils.equals(field, "PHONE-NO")) {
                value = phoneNumber;
            }
            if (StringUtils.equals(field, "MOBILE-NO")) {
                value = mobileNumber;
            }
            if (StringUtils.equals(field, "FAX-NO")) {
                value = faxNumber;
            }
        }

        if (StringUtils.equals(field, "Region")) {
            value = person.getRegion();
        }

        if (StringUtils.equals(field, "Resident Country") || StringUtils.equals(field, "RESIDENT-COUNTRY")) {
            value = person.getResidentCountry();
        }

        if (person.getFirstAddress() != null) {
            if (field.compareTo("Street Address") == 0) {
                AddressBean address = person.getFirstAddress();
                for (int y = 0; y < address.getAddressFieldCount(); y++) {
                    if (value.compareTo("") != 0) {
                        value += ", ";
                    }
                    value += address.getAddressField(y);
                }
            }

            if (field.compareTo("State") == 0) {
                value = person.getFirstAddress().getState();
            }

            if (StringUtils.equals(field, "STATE-CODE")) {
                if (StringUtils.isNotBlank(person.getFirstAddress().getState())) {
                    value = person.getFirstAddress().getState();
                }
                if (StringUtils.isNotBlank(person.getFirstAddress().getStateAbbreviation())) {
                    value = person.getFirstAddress().getStateAbbreviation();
                }
            }

            if (field.compareTo("Country") == 0) {
                value = person.getFirstAddress().getCountry();
            }

            if (StringUtils.equals(field, "DOMICILE-CODE")) {
                value = person.getFirstAddress().getCountryAbbreviation();
            }

            if (StringUtils.equals(field, "Postcode") || StringUtils.equals(field, "POST-CODE")) {
                value = person.getFirstAddress().getPostCode();
            }

            if (field.compareTo("Formatted Address") == 0) {
                value = OutputFormatter.toAddress(person.getFirstAddress(), preferences);
            }

            if (field.compareTo("Address (mail merge)") == 0) {
                value = OutputFormatter.toAddress(person.getFirstAddress(), preferences);
            }

            if (field.compareTo("Street Address (mail merge)") == 0) {
                value = OutputFormatter.toStreetAddress(person.getFirstAddress(), preferences);
            }

            if (StringUtils.equals(field, "ADDR-LINE-1")) {
                if (displayAddressField(person.getFirstAddress(), 0)) {
                    value = person.getFirstAddress().getAddressField(0);
                }
            }

            if (StringUtils.equals(field, "ADDR-LINE-2")) {
                if (displayAddressField(person.getFirstAddress(), 1)) {
                    value = person.getFirstAddress().getAddressField(1);
                }
            }

            if (StringUtils.equals(field, "ADDR-LINE-3")) {
                if (displayAddressField(person.getFirstAddress(), 2)) {
                    value = person.getFirstAddress().getAddressField(2);
                }
            }

            if (StringUtils.equals(field, "ADDR-LINE-4")) {
                if (displayAddressField(person.getFirstAddress(), 3)) {
                    value = person.getFirstAddress().getAddressField(3);
                }
            }

            if (StringUtils.equals(field, "SUBURB")) {
                value = PersonFormatter.getCityValue(person.getFirstAddress());
            }

            if (StringUtils.equals(field, "RETURN-MAIL")) {
                value = "N";
                if (person.getFirstAddress().getReturnedMail() || person.getFirstAddress().getRequestNoMail()) {
                    value = "Y";
                }
            }

            if (StringUtils.equals(field, "NO-MAIL-FLAG")) {
                value = "N";
                if (person.getFirstAddress().getRequestNoMail()) {
                    value = "Y";
                }
            }
        }

        if (field.compareTo("Created By") == 0) {
            value = person.getCreatedBy();
        }

        if (field.compareTo("Date Created") == 0 && person.getCreatedDate() != null) {
            value = Formatter.conventionalDate(person.getCreatedDate());
        }

        if (field.compareTo("Modified By") == 0) {
            value = person.getModifiedBy();
        }

        if (field.compareTo("Date Modified") == 0 && person.getModifiedDate() != null) {
            value = Formatter.conventionalDate(person.getModifiedDate());
        }

        if (value == null) {
            value = "";
        }
        return value.trim();
    }

    public static String getField(final PersonBean person, final Object object, final String section,
            final String field, final String format, final PreferencesBean preferences) {

        String value = "";

        if (section == null || field == null) {
            return value;
        }

        if (field.compareTo("GUID") == 0) {
            value = String.valueOf(person.getGUID());
        }

        if (field.compareTo("GUID") == 0) {
            if (person.getGUID() > 0) {
                value = String.valueOf(person.getGUID());
            }
        }

        if (field.compareTo("Title") == 0) {
            value = person.getTitle();
        }

        if (field.compareTo("Preferred Name") == 0) {
            value = person.getPreferredName();
        }

        if (field.compareTo("Last Name") == 0) {
            value = person.getLastName();
        }
        if (field.compareTo("Formatted Name") == 0) {
            value = OutputFormatter.toFormattedName(person);
        }

        if (field.compareTo("MIN") == 0) {
            value = String.valueOf(person.getPersonIdentifier());
        }

        if (field.compareTo("Training Status") == 0) {
            value = person.getTrainingStatus();
            if (StringUtils.isNotBlank(person.getTrainingStatusDetail())) {
                value += " - " + person.getTrainingStatusDetail();
            }
        }

        if (person.getPrimaryMembership() != null) {
            MembershipBean mb = person.getPrimaryMembership();

            if (StringUtils.equals(field, "Candidate Number")) {
                value = mb.getField("Candidate Number");
            }

            if (StringUtils.equals(field, "Status")) {
                value = mb.getField("Status");
            }

            if (StringUtils.equals(field, "Membership Class")) {
                ObjectTypeBean membershipType = mb.getObjectTypeField("Membership Type");
                if (membershipType != null) {
                    value = membershipType.getClassName();
                }
            }

            if (StringUtils.equals(field, "Membership Type")) {
                ObjectTypeBean membershipType = mb.getObjectTypeField("Membership Type");
                if (membershipType != null) {
                    value = membershipType.getName();
                }
            }

            if (StringUtils.equals(field, "Division")) {
                ObjectTypeBean membershipType = mb.getObjectTypeField("Division");
                if (membershipType != null) {
                    value = membershipType.getClassName();
                }
            }

            if (StringUtils.equals(field, "Training Type")) {
                value = mb.getField("Training Type");
            }
        }

        if (section.compareTo("Specialties") == 0) {
            SpecialtyBean specialty = (SpecialtyBean) object;

            if (field.compareTo("Training Organisation") == 0) {
                value = specialty.getTrainingOrganisation();
            }
            if (field.compareTo("Training Program") == 0) {
                value = specialty.getTrainingProgram();
            }
            if (field.compareTo("Curriculum Year") == 0) {
                if (specialty.getTrainingProgramYear() > 0) {
                    value = String.valueOf(specialty.getTrainingProgramYear());
                }
            }
            if (field.compareTo("Specialty Status") == 0) {
                value = specialty.getStatus();
            }
        }

        if (section.compareTo("Addresses") == 0) {
            AddressBean address = (AddressBean) object;

            if (field.compareTo("Type") == 0) {
                value = address.getContactClass() + " - " + address.getContactType();
            }

            if (field.compareTo("Description") == 0) {
                value = address.getDescription();
            }

            if (field.compareTo("Address") == 0) {
                for (int y = 0; y < address.getAddressFieldCount(); y++) {
                    if (value.compareTo("") != 0) {
                        value += ", ";
                    }
                    value += address.getAddressField(y);
                }
            }
            if (field.compareTo("State") == 0) {
                value = address.getState();
            }

            if (field.compareTo("Country") == 0) {
                value = address.getCountry();
            }

            if (field.compareTo("Postcode") == 0) {
                value = address.getPostCode();
            }

            if (field.compareTo("Formatted Address") == 0) {
                value = OutputFormatter.toAddress(address, preferences);
            }

            if (field.compareTo("Preferred") == 0) {
                value = Formatter.convertBoolean(address.getPrimary());
            }

            if (field.compareTo("Returned Mail") == 0) {
                value = Formatter.convertBoolean(address.getReturnedMail());
            }

            if (field.compareTo("Request No Mail") == 0) {
                value = Formatter.convertBoolean(address.getRequestNoMail());
            }
        }

        if (section.compareTo("Phone Numbers") == 0) {
            PhoneBean phone = (PhoneBean) object;

            if (field.compareTo("Description") == 0) {
                value = phone.getDescription();
            }
            if (field.compareTo("Phone Number") == 0) {
                value = Formatter.getPhone(String.valueOf(phone.getCountryCode()),
                        String.valueOf(phone.getAreaCode()), phone.getNumber());
            }
            if (field.compareTo("Preferred") == 0) {
                value = Formatter.convertBoolean(phone.getPrimary());
            }
        }

        if (section.compareTo("Email Addresses") == 0) {
            EmailBean email = (EmailBean) object;

            if (field.compareTo("Type") == 0) {
                value = email.getContactType();
            }
            if (field.compareTo("Description") == 0) {
                value = email.getDescription();
            }
            if (field.compareTo("Email Address") == 0) {
                value = email.getEmail();
            }
            if (field.compareTo("Preferred") == 0) {
                value = Formatter.convertBoolean(email.getPrimary());
            }
            if (field.compareTo("Email CME Questions") == 0) {
                value = Formatter.convertBoolean(email.getEmailQuestions());
            }
        }

        if (section.compareTo("Workplaces") == 0) {
            ItemBean workplace = (ItemBean) object;

            if (field.compareTo("Workplace") == 0) {
                value = workplace.getName();
            }
            if (field.compareTo("Position") == 0) {
                value = workplace.getDescription();
            }
            if (field.compareTo("Started Employment") == 0) {
                value = Formatter.conventionalDate(workplace.getStartDate());
            }
            if (field.compareTo("Ceased Employment") == 0) {
                value = Formatter.conventionalDate(workplace.getEndDate());
            }
        }

        if (section.compareTo("Exams") == 0) {
            ExamBean exam = (ExamBean) object;

            if (field.compareTo("Exam Type") == 0) {
                value = exam.getType();
            }
            if (field.compareTo("Date Sat") == 0) {
                value = Formatter.conventionalDate(exam.getDateSat());
            }
            if (field.compareTo("Exam Location") == 0) {
                value = exam.getLocation();
            }
            if (field.compareTo("Exam Mark") == 0) {
                if (exam.getAwardedMarks() > 0 && exam.getTotalMarks() > 0) {
                    value = Formatter.toPercent(exam.getResultPercentage(), 2, "%");
                }
            }
            if (field.compareTo("Exam Status") == 0) {
                value = exam.getStatus();
                if (exam.getStatusLevel().compareTo("") != 0) {
                    value += " - " + exam.getStatusLevel();
                }
            }
        }

        if (section.compareTo("Qualifications") == 0) {
            QualificationBean qualification = (QualificationBean) object;

            if (field.compareTo("Qualification Level") == 0) {
                value = qualification.getQualificationType();
            }
            if (field.compareTo("Qualification Type") == 0) {
                value = qualification.getQualificationSubType();
            }
            if (field.compareTo("Qualification Name") == 0) {
                value = qualification.getQualification();
            }
            if (field.compareTo("Institution") == 0) {
                value = qualification.getInstitution();
            }
            if (field.compareTo("Graduated") == 0) {
                value = String.valueOf(qualification.getYear());
            }
            if (field.compareTo("Country") == 0) {
                value = qualification.getCountry();
            }
        }

        if (section.compareTo("Affiliations") == 0) {
            MembershipBean membership = (MembershipBean) object;

            ObjectTypeBean affiliation = membership.getObjectTypeField("Affiliation");

            if (affiliation != null) {
                if (field.compareTo("Aff. Abbreviation") == 0) {
                    value = affiliation.getAbbreviation();
                }
                if (field.compareTo("Aff. Organisation") == 0) {
                    value = affiliation.getName();
                }
                if (field.compareTo("Aff. Type") == 0) {
                    value = affiliation.getClassName();
                }
                if (field.compareTo("Aff. Website") == 0) {
                    value = affiliation.getSecurity();
                }
            }
        }

        if (value == null) {
            value = "";
        }
        return value.trim();
    }

    public static Collection<Object> getCollection(final PersonBean person, final String section) {

        Collection<Object> collection = new ArrayList<Object>();

        if (section != null) {
            if (section.compareTo("Specialties") == 0 && person.getSpecialtyList() != null) {
                for (SpecialtyBean specialty : person.getSpecialtyList()) {
                    collection.add(specialty);
                }
            }
            if (section.compareTo("Addresses") == 0 && person.getAddress() != null) {
                for (AddressBean address : person.getAddress()) {
                    collection.add(address);
                }
            }
            if (section.compareTo("Phone Numbers") == 0 && person.getPhone() != null) {
                for (PhoneBean phone : person.getPhone()) {
                    collection.add(phone);
                }
            }
            if (section.compareTo("Email Addresses") == 0 && person.getEmail() != null) {
                for (EmailBean email : person.getEmail()) {
                    collection.add(email);
                }
            }
            if (section.compareTo("Exams") == 0 && person.getExams() != null) {
                for (ExamBean exam : person.getExams()) {
                    collection.add(exam);
                }
            }
            if (section.compareTo("Qualifications") == 0 && person.getMembershipDetails() != null) {
                for (QualificationBean qualification : person.getQualifications()) {
                    collection.add(qualification);
                }
            }
            if (section.compareTo("Affiliations") == 0 && person.getMembershipDetails() != null) {
                for (MembershipBean membership : person.getMembershipDetails()) {

                    if (StringUtils.equalsIgnoreCase(membership.getMembershipClass(), "RACP")
                            && StringUtils.equalsIgnoreCase(membership.getMembershipType(), "Affiliation")) {
                        collection.add(membership);
                    }
                }
            }
            if (section.compareTo("Workplaces") == 0 && person.getEmployers() != null) {
                for (String index : person.getEmployers().keySet()) {
                    ItemBean employer = person.getEmployers().get(index);
                    collection.add(employer);
                }
            }
            if (section.compareTo("Memos") == 0 && person.getMemo() != null) {
                for (MemoBean memo : person.getMemo()) {
                    collection.add(memo);
                }
            }
        }
        return collection;
    }

    /**
     * Calculate training summary total.
     *
     * @param summary the summary
     * @param inMonths the in months flag
     * @return the string
     */
    private static String calculateTrainingSummaryTotal(final TreeMap<String, AccreditationBean[]> summary,
            final boolean inMonths) {
        StringBuffer value = new StringBuffer();

        int coreTotal = 0;
        int nonCoreTotal = 0;

        if (summary != null) {
            for (String key : summary.keySet()) {
                try {
                    AccreditationBean[] details = summary.get(key);

                    AccreditationBean core = details[0];
                    AccreditationBean nonCore = details[1];

                    coreTotal += core.getWeeksCertified();
                    nonCoreTotal += nonCore.getWeeksCertified();
                } finally {
                }
            }
        }

        if (inMonths) {
            coreTotal = Formatter.getWholeMonths(coreTotal);
            nonCoreTotal = Formatter.getWholeMonths(nonCoreTotal);
        }

        value.append(coreTotal);
        value.append(" (");
        value.append(nonCoreTotal);
        value.append(")");

        return value.toString();
    }

    /**
     * Gets the city value.
     *
     * @param address the address
     * @return the city value
     */
    private static String getCityValue(final AddressBean address) {
        String city = "";

        if (address != null) {
            city = address.getCity();
            if (StringUtils.equalsIgnoreCase(address.getCountry(), "Australia")) {
                if (StringUtils.equalsIgnoreCase(city, "Sydney") || StringUtils.equalsIgnoreCase(city, "Adelaide")
                        || StringUtils.equalsIgnoreCase(city, "Melbourne")
                        || StringUtils.equalsIgnoreCase(city, "Darwin")
                        || StringUtils.equalsIgnoreCase(city, "Canberra")) {
                    city = address.getSuburb();
                }
            }
        }
        return city;
    }

    /**
     * Display address field.
     *
     * @param address the address
     * @param id the id
     * @return true, if successful
     */
    private static boolean displayAddressField(final AddressBean address, final int id) {
        boolean displayField = false;

        if (address != null && StringUtils.isNotBlank(address.getAddressField(id))) {
            String city = address.getCity();
            String derivedCity = getCityValue(address);

            if (!StringUtils.equals(address.getAddressField(id), derivedCity)) {
                displayField = true;
            }

            if (!StringUtils.equals(city, derivedCity)) {
                // The city and derived city fields do not match, perform a second test
                if (StringUtils.equals(address.getAddressField(id), city)) {
                    displayField = false;
                }
            }
        }
        return displayField;
    }

    /**
     * Checks if is Community Child Health (CCH) trainee.
     *
     * @param person the person
     * @return true, if is a CCH trainee
     */
    private static boolean isCCHTrainee(final PersonBean person) {

        boolean cchTrainee = false;

        if (person.getSpecialtyList() != null) {
            for (SpecialtyBean sp : person.getSpecialtyList()) {

                if (StringUtils.equalsIgnoreCase(sp.getTrainingOrganisation(), "RACP")
                        && StringUtils.equalsIgnoreCase(sp.getTrainingProgram(), "Community Child Health")) {
                    if (StringUtils.equalsIgnoreCase(sp.getStatus(), "In training")) {
                        cchTrainee = true;
                    }
                }
            }
        }
        return cchTrainee;
    }
}