easycare.alc.web.patient.HstPrescriptionView.java Source code

Java tutorial

Introduction

Here is the source code for easycare.alc.web.patient.HstPrescriptionView.java

Source

package easycare.alc.web.patient;

import easycare.alc.model.HstPatientStatus;
import easycare.alc.model.HstPrescription;
import easycare.alc.web.patient.PrescriptionSettings.HstMaskType;
import easycare.alc.web.patient.PrescriptionSettings.HstMaskTypeEnum;
import easycare.alc.web.patient.PrescriptionSettings.HstTherapySetting;
import easycare.alc.web.patient.PrescriptionSettings.HstTherapySettingEnum;
import easycare.alc.web.patient.PrescriptionSettings.HstTherapySettingValue;
import easycare.helper.TimeZoneHelper;
import easycare.model.Address;
import easycare.model.Organisation;
import easycare.model.User;
import lombok.Delegate;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.joda.time.DateTime;

import javax.validation.Valid;
import javax.validation.constraints.Digits;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang.StringUtils.substring;

@EqualsAndHashCode
public final class HstPrescriptionView {

    protected static final double DEFAULT_MINIMUM_PRESSURE = 4.0;
    protected static final double DEFAULT_MAXIMUM_PRESSURE = 20.0;
    protected static final double DEFAULT_SET_PRESSURE = 8.0;
    protected static final String DEFAULT_THERAPY = "APAP";
    protected static final String DEFAULT_MASK = "fullFace";
    protected static final String DEFAULT_HUMIDIFIER = "required";
    protected static final String DEFAULT_TUBING = "heated";
    protected static final String DEFAULT_FILTER = "nonDisposable";
    protected static final int AHI_INTEGER = 3;
    protected static final int AHI_FRACTION = 1;

    @Getter
    @Delegate(excludes = AdditionalNotesSetter.class)
    private HstPrescription hstPrescription;

    @Getter
    @Setter
    private String password;

    private boolean signed;

    @Getter
    private int maxAdditionalNotesLength = HstPrescription.MAX_ADDITIONAL_NOTES_LENGTH;

    @Getter
    @Setter
    private List<HstMaskType> maskTypeList;

    @Getter
    private Map<String, List<HstTherapySetting>> therapySettings;

    @Getter
    @Setter
    private String timezone;

    @Getter
    @Setter
    private String countryCode;

    @Getter
    @Setter
    private String stateCode;

    @Getter
    private List<HstTherapySettingValue> therapySettingValues;

    @Getter
    private String image;

    @Getter
    @Setter
    private int reportHash;

    @Getter
    private boolean resultSendButtonDisabled;

    @Getter
    private boolean additionalNotesVisible;

    @Valid
    @Digits(integer = AHI_INTEGER, fraction = AHI_FRACTION)
    @Getter
    @Setter
    private Double ahiField;

    public HstPrescriptionView() {
        hstPrescription = new HstPrescription();
        setDefaultPrescriptionSettings();
        signed = false;
    }

    public HstPrescriptionView(HstPrescription script, TimeZoneHelper timeZoneHelper,
            HstPatientStatus hstPatientStatus) {
        hstPrescription = script;
        setPrescriptionSettingsFromScript(script);
        setSignatureDetails(timeZoneHelper);
        signed = true;
        resultSendButtonDisabled = (hstPatientStatus == HstPatientStatus.RESULTS_SENT
                || hstPatientStatus == HstPatientStatus.PATIENT_ACCEPTED);
        additionalNotesVisible = (hstPrescription.getApneaHypopneaIndex() != null
                || (hstPrescription.getAdditionalNotesField() != null
                        && !hstPrescription.getAdditionalNotesField().isEmpty()));
    }

    private void setSignatureDetails(TimeZoneHelper timeZoneHelper) {
        setCountryCode(hstPrescription.getState().getCountry().getCountryCode());
        setStateCode(hstPrescription.getState().getStateCode());

        DateTime dateTimeAndZone = timeZoneHelper
                .convertHardwareLocalDateTimeToSystemTZDateTime(hstPrescription.getSignedAt());
        setSignedAt(dateTimeAndZone.toLocalDateTime());
        setTimezone(dateTimeAndZone.getZone().getShortName(0));
    }

    public static HstPrescriptionView getDefault() {
        HstPrescriptionView hstPrescriptionView = new HstPrescriptionView();
        hstPrescriptionView.setTherapyDeviceField(DEFAULT_THERAPY);
        hstPrescriptionView.setMaskSelectField(DEFAULT_MASK);
        hstPrescriptionView.setHumidifierRequiredField(DEFAULT_HUMIDIFIER);
        hstPrescriptionView.setTubingSelectField(DEFAULT_TUBING);
        hstPrescriptionView.setFilterSelectField(DEFAULT_FILTER);
        hstPrescriptionView.setMinimumPressure(DEFAULT_MINIMUM_PRESSURE);
        hstPrescriptionView.setMaximumPressure(DEFAULT_MAXIMUM_PRESSURE);
        hstPrescriptionView.setSetPressure(DEFAULT_SET_PRESSURE);
        return hstPrescriptionView;
    }

    public static HstPrescriptionView getFromScript(HstPrescription script, TimeZoneHelper timeZoneHelper,
            HstPatientStatus hstPatientStatus) {
        return new HstPrescriptionView(script, timeZoneHelper, hstPatientStatus);
    }

    public boolean isSigned() {
        return signed;
    }

    private void setDefaultPrescriptionSettings() {
        therapySettings = getDefaultTherapySettings();
        image = HstTherapySettingEnum.valueOf(DEFAULT_THERAPY).getImage();
    }

    private void setPrescriptionSettingsFromScript(HstPrescription script) {
        setDefaultPrescriptionSettings();

        if (script.getTherapyDeviceField() != null && !script.getTherapyDeviceField().isEmpty()) {
            HstTherapySettingEnum therapySettingsEnum = HstTherapySettingEnum
                    .valueOf(script.getTherapyDeviceField());
            image = therapySettingsEnum.getImage();

            therapySettingValues = new ArrayList<>();
            for (HstTherapySetting therapySetting : therapySettingsEnum.getTherapySettings()) {
                therapySettingValues.add(new HstTherapySettingValue(therapySetting, script));
            }
        }
    }

    public List<HstMaskType> getDefaultMaskList(boolean isResMedPrescription) {
        return HstMaskTypeEnum.generateDefaultValues(isResMedPrescription);
    }

    private Map<String, List<HstTherapySetting>> getDefaultTherapySettings() {
        return HstTherapySettingEnum.DEFAULT_VALUES;
    }

    public HstPrescription getHstPrescriptionWithSigningPhysician(User physician) {
        hstPrescription.setPhysicianUsername(physician.getUsername());
        hstPrescription.setPhysicianTitle(physician.getTitle());
        hstPrescription.setPhysicianFirstName(physician.getFirstName());
        hstPrescription.setPhysicianLastName(physician.getLastName());
        hstPrescription.setPhysicianLicenseNumber(physician.getLicenseNumber());

        Organisation organisation = physician.getOrganisation();
        Address address = organisation.getPrimaryContact().getContactInformation().getAddress();

        hstPrescription.setPhysicianAddressLine1(address.getAddressLine1());
        hstPrescription.setPhysicianAddressLine2(address.getAddressLine2());
        hstPrescription.setPhysicianCitySuburb(address.getCitySuburb());
        hstPrescription.setPhysicianPostcode(address.getPostcode());
        hstPrescription.setState(address.getState());
        hstPrescription.setPhysicianOrgName(organisation.getName());

        hstPrescription.setApneaHypopneaIndex(getAhiField());

        setTherapyDeviceOtherField();

        return hstPrescription;
    }

    protected void setTherapyDeviceOtherField() {
        if (!HstTherapySettingEnum.Other.name().equals(getTherapyDeviceField())) {
            hstPrescription.setTherapyDeviceOther(null);
        }
    }

    public void setAdditionalNotesField(String notes) {
        String crFreeString = notes.replaceAll("\r\n", "\n");
        hstPrescription.setAdditionalNotesField(substring(crFreeString, 0, maxAdditionalNotesLength));
    }

    private interface AdditionalNotesSetter {
        void setAdditionalNotesField(String notes);
    }

    public boolean isOtherTherapyDeviceInvalid() {
        return HstTherapySettingEnum.Other.name().equals(getTherapyDeviceField())
                && isBlank(getTherapyDeviceOther());
    }

}