Search in sources :

Example 11 with Field

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

the class FormServiceImpl method mergeDuplicateFields.

/**
 * @see FormService#mergeDuplicateFields()
 */
@Override
public int mergeDuplicateFields() throws APIException {
    List<Field> fields = dao.getAllFields(true);
    Set<Field> fieldsToDelete = new HashSet<>();
    Map<String, Integer> fieldNameAsKeyAndFieldIdAsValueMap = new HashMap<>();
    for (Field field : fields) {
        if (fieldNameAsKeyAndFieldIdAsValueMap.containsKey(field.getName())) {
            Field fieldToCompareTo = dao.getField(fieldNameAsKeyAndFieldIdAsValueMap.get(field.getName()));
            if (fieldsAreSimilar(field, fieldToCompareTo)) {
                // get the formFields that use this duplicate field
                List<FormField> formFields = dao.getFormFieldsByField(field);
                // replace with field from outer loop
                for (FormField formField : formFields) {
                    formField.setField(fieldToCompareTo);
                    dao.saveFormField(formField);
                    fieldsToDelete.add(field);
                }
            } else {
                fieldNameAsKeyAndFieldIdAsValueMap.put(field.getName(), field.getId());
            }
        } else {
            fieldNameAsKeyAndFieldIdAsValueMap.put(field.getName(), field.getId());
        }
    }
    for (Field field : fieldsToDelete) {
        dao.deleteField(field);
    }
    return fieldsToDelete.size();
}
Also used : FormField(org.openmrs.FormField) Field(org.openmrs.Field) HashMap(java.util.HashMap) FormField(org.openmrs.FormField) HashSet(java.util.HashSet)

Example 12 with Field

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

the class FieldValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see FieldValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    Field ff = new Field();
    FieldType ft = new FieldType();
    Boolean retired = Boolean.FALSE;
    ft.setId(0xdeadcafe);
    ff.setFieldType(ft);
    ff.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");
    ff.setRetired(retired);
    Boolean multiple = Boolean.FALSE;
    ff.setSelectMultiple(multiple);
    ff.setTableName("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");
    ff.setAttributeName("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");
    ff.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(ff, "field");
    new FieldValidator().validate(ff, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
    Assert.assertTrue(errors.hasFieldErrors("tableName"));
    Assert.assertTrue(errors.hasFieldErrors("attributeName"));
    Assert.assertTrue(errors.hasFieldErrors("retireReason"));
}
Also used : Field(org.openmrs.Field) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) FieldType(org.openmrs.FieldType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 13 with Field

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

the class FieldValidatorTest method validate_shouldPassValidationIfAllFieldsAreCorrect.

/**
 * @see FieldValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfAllFieldsAreCorrect() {
    Field ff = new Field();
    FieldType ft = new FieldType();
    Boolean retired = Boolean.FALSE;
    ft.setId(0xdeadcafe);
    ff.setFieldType(ft);
    ff.setName("valid");
    ff.setRetired(retired);
    Boolean multiple = Boolean.FALSE;
    ff.setSelectMultiple(multiple);
    Errors errors = new BindException(ff, "name");
    new FieldValidator().validate(ff, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Field(org.openmrs.Field) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) FieldType(org.openmrs.FieldType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 14 with Field

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

the class FormServiceTest method saveFormField_shouldInjectFormFieldsFromSerializableComplexObsHandlers.

/**
 * @see FormService#saveFormField(FormField)
 */
@SuppressWarnings("unchecked")
@Test
public void saveFormField_shouldInjectFormFieldsFromSerializableComplexObsHandlers() {
    executeDataSet("org/openmrs/api/include/ConceptComplex.xml");
    Context.getObsService().registerHandler("NeigborHandler", new NeighborHandler());
    Concept concept = Context.getConceptService().getConcept(6043);
    Field field = new Field();
    field.setName("neighbor");
    field.setConcept(concept);
    FormField formField = new FormField();
    formField.setField(field);
    FormService fs = Context.getFormService();
    formField.setForm(fs.getForm(1));
    List<FormField> originalFormFields = fs.getAllFormFields();
    int initialFormFieldCount = originalFormFields.size();
    formField = fs.saveFormField(formField);
    List<FormField> updatedFormFields = fs.getAllFormFields();
    // should have this and the two form fields from the handler
    Assert.assertEquals(initialFormFieldCount += 3, updatedFormFields.size());
    // get the formfields added by the handler and check their parent
    List<FormField> childFormFields = ListUtils.subtract(updatedFormFields, originalFormFields);
    // exclude this form field
    childFormFields.remove(formField);
    for (FormField ff : childFormFields) {
        Assert.assertEquals(formField, ff.getParent());
    }
}
Also used : Concept(org.openmrs.Concept) FormField(org.openmrs.FormField) Field(org.openmrs.Field) FormField(org.openmrs.FormField) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 15 with Field

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

the class FormServiceTest method getForms_shouldReturnDuplicateFormWhenGivenFieldsIncludedInFormMultipleTimes.

/**
 * Make sure that multiple forms are returned if a field is on a form more than once
 *
 * @see {@link FormService#getForms(String, Boolean, java.util.Collection, Boolean, java.util.Collection, java.util.Collection, java.util.Collection)
 */
@Test
public void getForms_shouldReturnDuplicateFormWhenGivenFieldsIncludedInFormMultipleTimes() {
    executeDataSet(INITIAL_FIELDS_XML);
    executeDataSet("org/openmrs/api/include/FormServiceTest-formFields.xml");
    FormService formService = Context.getFormService();
    List<Field> fields = new ArrayList<>();
    fields.add(new Field(1));
    List<Form> forms = formService.getForms(null, null, null, null, null, null, fields);
    assertEquals(3, forms.size());
}
Also used : FormField(org.openmrs.FormField) Field(org.openmrs.Field) Form(org.openmrs.Form) ArrayList(java.util.ArrayList) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Field (org.openmrs.Field)16 Test (org.junit.Test)13 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)13 FieldType (org.openmrs.FieldType)9 BindException (org.springframework.validation.BindException)8 Errors (org.springframework.validation.Errors)8 FormField (org.openmrs.FormField)7 Concept (org.openmrs.Concept)3 Form (org.openmrs.Form)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ComplexObsHandler (org.openmrs.obs.ComplexObsHandler)1 SerializableComplexObsHandler (org.openmrs.obs.SerializableComplexObsHandler)1