easycare.web.patient.PhysicianConverter.java Source code

Java tutorial

Introduction

Here is the source code for easycare.web.patient.PhysicianConverter.java

Source

package easycare.web.patient;

import easycare.common.ApplicationMessageSource;
import easycare.model.Address;
import easycare.model.CareProvider;
import easycare.model.Organisation;
import easycare.model.PhysicianPartner;
import easycare.model.User;
import easycare.model.physician.Physician;
import easycare.model.physician.PhysicianOrg;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class PhysicianConverter {
    @Autowired
    private ApplicationMessageSource messageSource;

    public List<PhysicianOrg> convertUserListToPhysicianOrgList(List<User> physicianUsers) {

        List<PhysicianOrg> physicianOrgs = new ArrayList<PhysicianOrg>();
        if (physicianUsers.isEmpty()) {
            return physicianOrgs;
        }
        Organisation currentOrg = null;

        PhysicianOrg currentPhysicianOrg = null;

        for (User currentUser : physicianUsers) {
            if (!currentUser.getOrganisation().equals(currentOrg)) {
                currentOrg = currentUser.getOrganisation();
                currentPhysicianOrg = generatePhysicianOrgFromOrganisation(currentOrg, currentOrg.getId());
                physicianOrgs.add(currentPhysicianOrg);
            }
            currentPhysicianOrg.getPhysiciansInOrg()
                    .add(convertUserIntoPhysician(currentUser, currentUser.getId()));
        }
        return physicianOrgs;
    }

    protected Physician convertUserIntoPhysician(User currentUser, long idOfPhysician) {
        return new Physician(idOfPhysician, generatePhysicianName(currentUser),
                generatePhysicianAddress(currentUser), generatePhysicianLicenceNumber(currentUser));
    }

    public String generatePhysicianName(User currentUser) {
        String titleOfPhysician = null;
        if (currentUser.getTitle() != null) {
            titleOfPhysician = messageSource.getMessage(currentUser.getTitle().generateMessageKeyForTitle());
        }
        return StringUtils
                .join(new String[] { titleOfPhysician, currentUser.getFirstName(), currentUser.getLastName() }, ' ')
                .trim();

    }

    protected String generatePhysicianAddress(User currentUser) {
        if (currentUser.getContactInformation() != null
                && currentUser.getContactInformation().getAddress() != null) {
            Address userAddressToGenerateFrom = currentUser.getContactInformation().getAddress();
            if (!StringUtils.isEmpty(userAddressToGenerateFrom.getCitySuburb())
                    || userAddressToGenerateFrom.getState() != null
                    || !StringUtils.isEmpty(userAddressToGenerateFrom.getPostcode())) {
                return generatePhysicianAddressFromAddress(userAddressToGenerateFrom);
            }
        }
        return generatePhysicianAddressFromOrganisation(currentUser.getOrganisation());

    }

    public String generatePhysicianAddressFromOrganisation(Organisation orgToGetAddressOf) {
        return generatePhysicianAddressFromAddress(
                orgToGetAddressOf.getPrimaryContact().getContactInformation().getAddress());
    }

    protected String generatePhysicianAddressFromAddress(Address userAddressToGenerateFrom) {
        if (userAddressToGenerateFrom == null) {
            return "";
        }
        String addressSeparator = ", ";
        String address = StringUtils.join(new String[] { userAddressToGenerateFrom.getCitySuburb(),
                userAddressToGenerateFrom.getState() == null ? null
                        : userAddressToGenerateFrom.getState().getStateCode().toUpperCase(),
                userAddressToGenerateFrom.getPostcode() }, addressSeparator);
        return StringUtils.strip(address, addressSeparator);
    }

    public List<PhysicianOrg> convertCareProviderListToPhysicianOrgList(List<CareProvider> listToConvert) {
        List<PhysicianOrg> convertedPhysicianOrgs = new ArrayList<PhysicianOrg>();
        for (CareProvider currentCareProvider : listToConvert) {

            convertedPhysicianOrgs.add(convertCareProviderToPhysicianOrg(currentCareProvider));
        }
        return convertedPhysicianOrgs;
    }

    protected PhysicianOrg convertCareProviderToPhysicianOrg(CareProvider currentCareProvider) {
        PhysicianOrg physicianOrgFromCareProvider = generatePhysicianOrgFromOrganisation(
                currentCareProvider.getOrganisation(), currentCareProvider.getId());
        if (currentCareProvider.getUser() != null) {
            User userOfCareProvider = currentCareProvider.getUser();
            physicianOrgFromCareProvider.getPhysiciansInOrg().add(convertUserIntoPhysician(userOfCareProvider, 0L));
        }
        return physicianOrgFromCareProvider;
    }

    public List<String> convertUserListToPhysicianNames(List<User> expectedPhysicians) {
        List<String> physicianNames = new ArrayList<String>();
        for (User userToConvert : expectedPhysicians) {
            physicianNames.add(generatePhysicianName(userToConvert));
        }
        return physicianNames;
    }

    public List<Physician> convertUserListToPhysicianList(List<User> physicianUsers) {
        List<Physician> physicianList = new ArrayList<>();

        for (User user : physicianUsers) {
            physicianList.add(convertUserIntoPhysicianStartWithLastName(user));
        }
        return physicianList;
    }

    public Physician convertUserIntoPhysicianStartWithLastName(User currentUser) {
        return new Physician(currentUser.getId(), generatePhysicianNameStartWithLastName(currentUser),
                generatePhysicianAddress(currentUser), generatePhysicianLicenceNumber(currentUser),
                generatePhysicianOrgName(currentUser));
    }

    public List<Physician> convertPhysicianPartnerListToPhysicianList(List<PhysicianPartner> physicianPartners) {
        List<Physician> physicianList = new ArrayList<>();

        for (PhysicianPartner physicianPartner : physicianPartners) {
            physicianList.add(
                    convertUserIntoPhysicianStartWithLastName(physicianPartner.getPhysicianPartnerId().getUser()));
        }
        return physicianList;
    }

    private PhysicianOrg generatePhysicianOrgFromOrganisation(Organisation currentOrg, Long idOfPhysicianOrg) {
        PhysicianOrg currentPhysicianOrg;
        currentPhysicianOrg = new PhysicianOrg(currentOrg.getName(), idOfPhysicianOrg);
        return currentPhysicianOrg;
    }

    private String generatePhysicianLicenceNumber(User currentUser) {
        return StringUtils.isEmpty(currentUser.getLicenseNumber()) ? "" : currentUser.getLicenseNumber();
    }

    private String generatePhysicianNameStartWithLastName(User currentUser) {
        return StringUtils.join(new String[] { currentUser.getLastName(), currentUser.getFirstName() }, ", ")
                .trim();
    }

    private String generatePhysicianOrgName(User currentUser) {
        return currentUser.getOrganisation().getName();
    }

}