Search in sources :

Example 11 with FormField

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

the class FormServiceTest method getForms_shouldReturnFormsContainingAllFormFieldsInContainingAllFormFields.

/**
 * @
 * @see FormService#getForms(String,Boolean,Collection,Boolean,Collection,Collection,Collection)
 */
@Test
public void getForms_shouldReturnFormsContainingAllFormFieldsInContainingAllFormFields() {
    executeDataSet(INITIAL_FIELDS_XML);
    executeDataSet("org/openmrs/api/include/FormServiceTest-formFields.xml");
    FormService formService = Context.getFormService();
    Set<FormField> formFields = new HashSet<>();
    formFields.add(new FormField(3));
    formFields.add(new FormField(5));
    formFields.add(new FormField(7));
    List<Form> forms = formService.getForms(null, null, null, null, null, formFields, null);
    assertEquals(1, forms.size());
    formFields = new HashSet<>();
    formFields.add(new FormField(2));
    formFields.add(new FormField(4));
    formFields.add(new FormField(6));
    forms = formService.getForms(null, null, null, null, null, formFields, null);
    assertEquals(0, forms.size());
}
Also used : Form(org.openmrs.Form) FormField(org.openmrs.FormField) HashSet(java.util.HashSet) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 12 with FormField

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

the class FormUtil method getFormStructure.

/**
 * Returns a sorted and structured map of <code>FormField</code>s for the given OpenMRS form.
 * The root sections of the schema are stored under a key of zero (i.e.,
 * <code>java.lang.Integer.<em>valueOf(0)</em></code>). All other entries represent sequences of
 * children stored under the identifier (<code>formField.<em>getFormFieldId()</em></code>) of
 * their parent FormField. The form structure is sorted by the natural sorting order of the
 * <code>FormField</code>s (as defined by the <em>.equals()</em> and <em>.compareTo()</em>
 * methods).
 *
 * @param form form for which structure is requested
 * @return sorted map of <code>FormField</code>s, where the top-level fields are under the key
 *         zero and all other leaves are stored under their parent <code>FormField</code>'s id.
 */
public static Map<Integer, TreeSet<FormField>> getFormStructure(Form form) {
    Map<Integer, TreeSet<FormField>> formStructure = new TreeMap<>();
    Integer base = 0;
    formStructure.put(base, new TreeSet<>());
    for (FormField formField : form.getFormFields()) {
        FormField parent = formField.getParent();
        if (parent == null) {
            // top-level branches should be added to the base
            formStructure.get(base).add(formField);
        } else {
            // child branches/leaves are added to their parent's branch
            if (!formStructure.containsKey(parent.getFormFieldId())) {
                formStructure.put(parent.getFormFieldId(), new TreeSet<>());
            }
            formStructure.get(parent.getFormFieldId()).add(formField);
        }
    }
    return formStructure;
}
Also used : TreeSet(java.util.TreeSet) TreeMap(java.util.TreeMap) FormField(org.openmrs.FormField)

Example 13 with FormField

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

the class HibernateFormDAO method getFormCriteria.

/**
 * Convenience method to create the same hibernate criteria object for both getForms and
 * getFormCount
 *
 * @param partialName
 * @param published
 * @param encounterTypes
 * @param retired
 * @param containingAnyFormField
 * @param containingAllFormFields
 * @param fields
 * @return
 */
private Criteria getFormCriteria(String partialName, Boolean published, Collection<EncounterType> encounterTypes, Boolean retired, Collection<FormField> containingAnyFormField, Collection<FormField> containingAllFormFields, Collection<Field> fields) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(Form.class, "form");
    if (StringUtils.isNotEmpty(partialName)) {
        crit.add(Restrictions.or(Restrictions.like("name", partialName, MatchMode.START), Restrictions.like("name", " " + partialName, MatchMode.ANYWHERE)));
    }
    if (published != null) {
        crit.add(Restrictions.eq("published", published));
    }
    if (!encounterTypes.isEmpty()) {
        crit.add(Restrictions.in("encounterType", encounterTypes));
    }
    if (retired != null) {
        crit.add(Restrictions.eq("retired", retired));
    }
    // TODO junit test
    if (!containingAnyFormField.isEmpty()) {
        // Convert form field persistents to integers
        Set<Integer> anyFormFieldIds = new HashSet<>();
        for (FormField ff : containingAnyFormField) {
            anyFormFieldIds.add(ff.getFormFieldId());
        }
        DetachedCriteria subquery = DetachedCriteria.forClass(FormField.class, "ff");
        subquery.setProjection(Projections.property("ff.form.formId"));
        subquery.add(Restrictions.in("ff.formFieldId", anyFormFieldIds));
        crit.add(Subqueries.propertyIn("form.formId", subquery));
    }
    // select * from form where len(containingallformfields) = (select count(*) from form_field ff where ff.form_id = form_id and form_field_id in (containingallformfields);
    if (!containingAllFormFields.isEmpty()) {
        // Convert form field persistents to integers
        Set<Integer> allFormFieldIds = new HashSet<>();
        for (FormField ff : containingAllFormFields) {
            allFormFieldIds.add(ff.getFormFieldId());
        }
        DetachedCriteria subquery = DetachedCriteria.forClass(FormField.class, "ff");
        subquery.setProjection(Projections.count("ff.formFieldId"));
        subquery.add(Restrictions.eqProperty("ff.form", "form"));
        subquery.add(Restrictions.in("ff.formFieldId", allFormFieldIds));
        crit.add(Subqueries.eq((long) containingAllFormFields.size(), subquery));
    }
    // get all forms (dupes included) that have this field on them
    if (!fields.isEmpty()) {
        Criteria crit2 = crit.createCriteria("formFields", "ff");
        crit2.add(Restrictions.eqProperty("ff.form.formId", "form.formId"));
        crit2.add(Restrictions.in("ff.field", fields));
    }
    return crit;
}
Also used : DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) FormField(org.openmrs.FormField) HashSet(java.util.HashSet)

Example 14 with FormField

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

the class HibernateFormDAO method getFormField.

/**
 * @see org.openmrs.api.FormService#getFormField(org.openmrs.Form, org.openmrs.Concept,
 *      java.util.Collection, boolean)
 * @see org.openmrs.api.db.FormDAO#getFormField(org.openmrs.Form, org.openmrs.Concept,
 *      java.util.Collection, boolean)
 */
@Override
@SuppressWarnings("unchecked")
public FormField getFormField(Form form, Concept concept, Collection<FormField> ignoreFormFields, boolean force) throws DAOException {
    if (form == null) {
        log.debug("form is null, no fields will be matched");
        return null;
    }
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(FormField.class, "ff").createAlias("field", "field").add(Restrictions.eq("field.concept", concept)).add(Restrictions.eq("form", form));
    // get the list of all formfields with this concept for this form
    List<FormField> formFields = crit.list();
    String err = "FormField warning.  No FormField matching concept '" + concept + "' for form '" + form + "'";
    if (formFields.isEmpty()) {
        log.debug(err);
        return null;
    }
    // save the first formfield in case we're not a in a "force" situation
    FormField backupPlan = formFields.get(0);
    // remove the formfields we're supposed to ignore from the return list
    formFields.removeAll(ignoreFormFields);
    // in a "force" situation
    if (formFields.isEmpty()) {
        if (!force) {
            return backupPlan;
        } else {
            log.debug(err);
            return null;
        }
    } else {
        // if formFields.size() is still greater than 0
        return (FormField) formFields.get(0);
    }
}
Also used : Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) FormField(org.openmrs.FormField)

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