class PatientEntityToDetailsTransformer implements Transformer<PatientEntity, PatientDetails> {

    @Override
    public PatientDetails transform(final PatientEntity patientEntity) {
        final PatientDetails patient = new PatientDetails();


        String[] address = new String[]{StringUtils.trimToNull(patientEntity.getAddress1()),
            StringUtils.trimToNull(patientEntity.getAddress2()),
            StringUtils.trimToNull(patientEntity.getAddress3()),
            StringUtils.trimToNull(patientEntity.getAddress4()),
            StringUtils.trimToNull(patientEntity.getAddress5())};

        final String name = patientEntity.getFirstName() + " " + patientEntity.getLastName();
        final String patientId = patientEntity.getNhsNumber();

        patient.setId(String.valueOf(patientEntity.getId()));
        patient.setName(name);
        patient.setTitle(patientEntity.getTitle());
        patient.setForename(patientEntity.getFirstName());
        patient.setSurname(patientEntity.getLastName());
        patient.setGender(patientEntity.getGender());
        patient.setDateOfBirth(patientEntity.getDateOfBirth());
        patient.setNhsNumber(patientId);
        patient.setPasNumber(patientEntity.getPasNumber());
        patient.setAddress(address);
        patient.setPostcode(patientEntity.getPostcode());
        patient.setTelephone(patientEntity.getPhone());
        patient.setPasNumber(patientEntity.getPasNumber());
        patient.setLastUpdated(patientEntity.getLastUpdated());
        patient.setRegistrationStartDateTime(patientEntity.getRegistrationStartDateTime());
        patient.setRegistrationEndDateTime(patientEntity.getRegistrationEndDateTime());
        patient.setRegistrationStatus(patientEntity.getRegistrationStatus());
        patient.setRegistrationType(patientEntity.getRegistrationType());
        patient.setMaritalStatus(patientEntity.getMaritalStatus());
        patient.setManagingOrganization(patientEntity.getManagingOrganization());

        PractitionerEntity gp = patientEntity.getPractitioner();

        if (gp != null) {
            patient.setGpDetails(gp.getNamePrefix() + " " + gp.getNameGiven() + " " + gp.getNameFamily());
            patient.setGpId(gp.getId());
        }

        Date deceased = patientEntity.getDeceasedDateTime();
        if (deceased != null) {
            patient.setDeceased(deceased);
        }

        patient.setSensitive(patientEntity.isSensitive());
        
        populateTelecoms(patient, patientEntity);

        return patient;
    }

}

class PatientEntityToDetailsTransformerTest {

    private static final String PATIENT_ID;
    private PatientEntityToDetailsTransformer transformer;

    @Test
    public void shouldRemoveEmptyLinesFromAddressString() {

        final PatientEntity patientEntity = dummyPatientEntity();

        patientEntity.setAddress1("line 1");
        patientEntity.setAddress2(null);
        patientEntity.setAddress3("line 3");
        patientEntity.setAddress5("");
        patientEntity.setAddress5("line 5");
        patientEntity.setPostcode("postcode");

        final PatientDetails patientDetails = transformer.transform(patientEntity);

        assertNotNull(patientDetails);
        // TODO Commented out for 1.2.2 build. Need to check this, get Address now returns an array of 5 strings one per line
        // Changes at 1.2.2 remove the use of teh text field and stupulate a more strcutured address so this test no longer applies
        //assertEquals("line 1, line 3, postcode", patientDetails.getAddress());
    }

}