Search in sources :

Example 16 with PersonAttributeType

use of org.openmrs.PersonAttributeType in project openmrs-core by openmrs.

the class PersonAttributeTypeValidatorTest method validate_shouldFailValidationIfNameAlreadyInUse.

/**
 * @see PersonAttributeTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfNameAlreadyInUse() {
    PersonAttributeType type = new PersonAttributeType();
    type.setName("Birthplace");
    Errors errors = new BindException(type, "patObj");
    new PersonAttributeTypeValidator().validate(type, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
}
Also used : Errors(org.springframework.validation.Errors) PersonAttributeType(org.openmrs.PersonAttributeType) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 17 with PersonAttributeType

use of org.openmrs.PersonAttributeType in project openmrs-core by openmrs.

the class PersonAttributeTypeValidatorTest method validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace.

/**
 * @see PersonAttributeTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace() {
    PersonAttributeType type = new PersonAttributeType();
    type.setName("name");
    type.setDescription(null);
    Errors errors = new BindException(type, "type");
    new PersonAttributeTypeValidator().validate(type, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
    type.setDescription("");
    errors = new BindException(type, "type");
    new PersonAttributeTypeValidator().validate(type, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
    type.setDescription(" ");
    errors = new BindException(type, "type");
    new PersonAttributeTypeValidator().validate(type, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
}
Also used : Errors(org.springframework.validation.Errors) PersonAttributeType(org.openmrs.PersonAttributeType) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 18 with PersonAttributeType

use of org.openmrs.PersonAttributeType in project openmrs-core by openmrs.

the class PersonAttributeTypeValidatorTest method validate_shouldFailValidationIfFormatIsEmpty.

/**
 * @see PersonAttributeTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfFormatIsEmpty() {
    PersonAttributeType type = new PersonAttributeType();
    type.setName("Zodiac");
    type.setDescription("Zodiac Description");
    type.setFormat("");
    Errors errors = new BindException(type, "patObj");
    new PersonAttributeTypeValidator().validate(type, errors);
    Assert.assertTrue(errors.hasFieldErrors("format"));
}
Also used : Errors(org.springframework.validation.Errors) PersonAttributeType(org.openmrs.PersonAttributeType) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 19 with PersonAttributeType

use of org.openmrs.PersonAttributeType in project openmrs-core by openmrs.

the class PersonAttributeTypeValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see PersonAttributeTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    PersonAttributeType type = new PersonAttributeType();
    type.setName("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    type.setFormat("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    type.setRetireReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    Errors errors = new BindException(type, "patObj");
    new PersonAttributeTypeValidator().validate(type, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
    Assert.assertTrue(errors.hasFieldErrors("format"));
    Assert.assertTrue(errors.hasFieldErrors("retireReason"));
}
Also used : Errors(org.springframework.validation.Errors) PersonAttributeType(org.openmrs.PersonAttributeType) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 20 with PersonAttributeType

use of org.openmrs.PersonAttributeType in project openmrs-core by openmrs.

the class PersonServiceImpl method savePersonAttributeType.

/**
 * @see org.openmrs.api.PersonService#savePersonAttributeType(org.openmrs.PersonAttributeType)
 */
@Override
public PersonAttributeType savePersonAttributeType(PersonAttributeType type) throws APIException {
    checkIfPersonAttributeTypesAreLocked();
    if (type.getSortWeight() == null) {
        List<PersonAttributeType> allTypes = Context.getPersonService().getAllPersonAttributeTypes();
        if (!allTypes.isEmpty()) {
            type.setSortWeight(allTypes.get(allTypes.size() - 1).getSortWeight() + 1);
        } else {
            type.setSortWeight(1.0);
        }
    }
    boolean updateExisting = false;
    if (type.getId() != null) {
        updateExisting = true;
        String oldTypeName = dao.getSavedPersonAttributeTypeName(type);
        String newTypeName = type.getName();
        if (!oldTypeName.equals(newTypeName)) {
            List<GlobalProperty> props = new ArrayList<>();
            AdministrationService as = Context.getAdministrationService();
            for (String propName : OpenmrsConstants.GLOBAL_PROPERTIES_OF_PERSON_ATTRIBUTES) {
                props.add(as.getGlobalPropertyObject(propName));
            }
            for (GlobalProperty prop : props) {
                if (prop != null) {
                    String propVal = prop.getPropertyValue();
                    if (propVal != null && propVal.contains(oldTypeName)) {
                        prop.setPropertyValue(propVal.replaceFirst(oldTypeName, newTypeName));
                        as.saveGlobalProperty(prop);
                    }
                }
            }
        }
    }
    PersonAttributeType attributeType = dao.savePersonAttributeType(type);
    if (updateExisting) {
        // we need to update index in case searchable property has changed
        Context.updateSearchIndexForType(PersonAttribute.class);
    }
    return attributeType;
}
Also used : AdministrationService(org.openmrs.api.AdministrationService) ArrayList(java.util.ArrayList) PersonAttributeType(org.openmrs.PersonAttributeType) GlobalProperty(org.openmrs.GlobalProperty)

Aggregations

PersonAttributeType (org.openmrs.PersonAttributeType)40 Test (org.junit.Test)33 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)33 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)7 PersonAttribute (org.openmrs.PersonAttribute)4 Location (org.openmrs.Location)3 PatientIdentifierType (org.openmrs.PatientIdentifierType)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Patient (org.openmrs.Patient)2 ImportPatientFromWebService (org.openmrs.module.importpatientfromws.api.ImportPatientFromWebService)2 RemoteServerConfiguration (org.openmrs.module.importpatientfromws.api.RemoteServerConfiguration)2 DLD (ca.uhn.hl7v2.model.v25.datatype.DLD)1 IS (ca.uhn.hl7v2.model.v25.datatype.IS)1 File (java.io.File)1 URL (java.net.URL)1 Random (java.util.Random)1 Ignore (org.junit.Ignore)1 GlobalProperty (org.openmrs.GlobalProperty)1