Search in sources :

Example 6 with FormField

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

the class FormServiceImpl method saveFormField.

/**
 * @see org.openmrs.api.FormService#saveFormField(org.openmrs.FormField)
 */
@Override
public FormField saveFormField(FormField formField) throws APIException {
    Field field = formField.getField();
    if (field.getCreator() == null) {
        field.setCreator(Context.getAuthenticatedUser());
    }
    if (field.getDateCreated() == null) {
        field.setDateCreated(new Date());
    }
    // set the uuid here because the RequiredDataAdvice only looks at child lists
    if (field.getUuid() == null) {
        field.setUuid(UUID.randomUUID().toString());
    }
    FormField tmpFormField = dao.saveFormField(formField);
    // Include all formfields from all serializable complex obs handlers
    Concept concept = tmpFormField.getField().getConcept();
    if (concept != null && concept.isComplex()) {
        ComplexObsHandler handler = Context.getObsService().getHandler(((ConceptComplex) concept).getHandler());
        if (handler instanceof SerializableComplexObsHandler) {
            SerializableComplexObsHandler sHandler = (SerializableComplexObsHandler) handler;
            if (sHandler.getFormFields() != null) {
                for (FormField ff : sHandler.getFormFields()) {
                    ff.setParent(tmpFormField);
                    ff.setForm(tmpFormField.getForm());
                    ff.setCreator(tmpFormField.getCreator());
                    ff.setDateCreated(tmpFormField.getDateCreated());
                    dao.saveFormField(ff);
                }
            }
        }
    }
    return tmpFormField;
}
Also used : Concept(org.openmrs.Concept) FormField(org.openmrs.FormField) Field(org.openmrs.Field) SerializableComplexObsHandler(org.openmrs.obs.SerializableComplexObsHandler) ComplexObsHandler(org.openmrs.obs.ComplexObsHandler) SerializableComplexObsHandler(org.openmrs.obs.SerializableComplexObsHandler) FormField(org.openmrs.FormField) Date(java.util.Date)

Example 7 with FormField

use of org.openmrs.FormField 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 8 with FormField

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

the class FormServiceImpl method saveForm.

/**
 * @see org.openmrs.api.FormService#saveForm(org.openmrs.Form)
 */
@Override
public Form saveForm(Form form) throws APIException {
    checkIfFormsAreLocked();
    BindException errors = new BindException(form, "form");
    formValidator.validate(form, errors);
    if (errors.hasErrors()) {
        throw new APIException(errors);
    }
    if (form.getFormFields() != null) {
        for (FormField ff : form.getFormFields()) {
            if (ff.getForm() == null) {
                ff.setForm(form);
            } else if (!ff.getForm().equals(form)) {
                throw new APIException("Form.contains.FormField.error", new Object[] { ff });
            }
        }
    }
    return dao.saveForm(form);
}
Also used : APIException(org.openmrs.api.APIException) BindException(org.springframework.validation.BindException) FormField(org.openmrs.FormField)

Example 9 with FormField

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

the class FormServiceTest method getFormField_shouldIgnoreFormFieldsPassedToIgnoreFormFields.

/**
 * @see FormService#getFormField(Form,Concept,Collection<QFormField;>,null)
 */
@Test
public void getFormField_shouldIgnoreFormFieldsPassedToIgnoreFormFields() {
    executeDataSet(INITIAL_FIELDS_XML);
    executeDataSet("org/openmrs/api/include/FormServiceTest-formFields.xml");
    FormField ff = Context.getFormService().getFormField(new Form(1), new Concept(1), null, false);
    // sanity check
    assertNotNull(ff);
    // test that the first formfield is ignored when a second fetch
    // is done on the same form and same concept
    List<FormField> ignoreFormFields = new ArrayList<>();
    ignoreFormFields.add(ff);
    FormField ff2 = Context.getFormService().getFormField(new Form(1), new Concept(1), ignoreFormFields, false);
    assertNotNull(ff2);
    assertNotSame(ff, ff2);
}
Also used : Concept(org.openmrs.Concept) Form(org.openmrs.Form) ArrayList(java.util.ArrayList) FormField(org.openmrs.FormField) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 10 with FormField

use of org.openmrs.FormField 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)

Aggregations

FormField (org.openmrs.FormField)14 Test (org.junit.Test)6 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)6 Form (org.openmrs.Form)5 Concept (org.openmrs.Concept)4 Field (org.openmrs.Field)4 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 Criteria (org.hibernate.Criteria)2 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 APIException (org.openmrs.api.APIException)1 ComplexObsHandler (org.openmrs.obs.ComplexObsHandler)1 SerializableComplexObsHandler (org.openmrs.obs.SerializableComplexObsHandler)1 BindException (org.springframework.validation.BindException)1