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();
}
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"));
}
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());
}
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());
}
}
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());
}
Aggregations