Search in sources :

Example 1 with FormResource

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

the class FormServiceTest method duplicateForm_shouldCopyResourcesForOldFormToNewForm.

/**
 * @throws ParseException
 * @see FormService#duplicateForm(Form)
 */
@Test
public void duplicateForm_shouldCopyResourcesForOldFormToNewForm() throws ParseException {
    // save an original resource
    Form form = Context.getFormService().getForm(1);
    String name = "Start Date";
    FormResource resource = new FormResource();
    resource.setForm(form);
    resource.setName(name);
    resource.setDatatypeClassname("org.openmrs.customdatatype.datatype.DateDatatype");
    Date expected = new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-16");
    resource.setValue(expected);
    resource = Context.getFormService().saveFormResource(resource);
    Integer resourceId = resource.getFormResourceId();
    // duplicate the form
    Form newForm = Context.getFormService().duplicateForm(form);
    // get the resource
    FormResource actual = Context.getFormService().getFormResource(newForm, name);
    // check it
    Assert.assertNotNull(actual);
    Assert.assertEquals(expected, actual.getValue());
}
Also used : Form(org.openmrs.Form) FormResource(org.openmrs.FormResource) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 2 with FormResource

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

the class FormServiceTest method saveFormResource_shouldPersistAFormResource.

/**
 * @throws ParseException
 * @see FormService#saveFormResource(org.openmrs.FormResource)
 */
@Test
public void saveFormResource_shouldPersistAFormResource() throws ParseException {
    Form form = Context.getFormService().getForm(1);
    FormResource resource = new FormResource();
    resource.setForm(form);
    resource.setName("Start Date");
    resource.setDatatypeClassname("org.openmrs.customdatatype.datatype.DateDatatype");
    Date expected = new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-16");
    resource.setValue(expected);
    Context.getFormService().saveFormResource(resource);
    Integer resourceId = resource.getFormResourceId();
    Context.clearSession();
    FormResource actual = Context.getFormService().getFormResource(resourceId);
    Assert.assertNotNull(actual);
    Assert.assertEquals(expected, actual.getValue());
}
Also used : Form(org.openmrs.Form) FormResource(org.openmrs.FormResource) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 3 with FormResource

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

the class FormServiceImpl method saveFormResource.

/**
 * @see org.openmrs.api.FormService#saveFormResource(org.openmrs.FormResource)
 */
@Override
public FormResource saveFormResource(FormResource formResource) throws APIException {
    if (formResource == null) {
        return null;
    }
    // If a form resource with same name exists, replace it with current value
    FormResource toPersist = formResource;
    FormResource original = Context.getFormService().getFormResource(formResource.getForm(), formResource.getName());
    if (original != null) {
        original.setName(formResource.getName());
        original.setValue(formResource.getValue());
        original.setDatatypeClassname(formResource.getDatatypeClassname());
        original.setDatatypeConfig(formResource.getDatatypeConfig());
        original.setPreferredHandlerClassname(formResource.getPreferredHandlerClassname());
        toPersist = original;
    }
    try {
        CustomDatatypeUtil.saveIfDirty(toPersist);
    } catch (ConstraintViolationException ex) {
        throw new InvalidFileTypeException(ex.getMessage(), ex);
    }
    return dao.saveFormResource(toPersist);
}
Also used : InvalidFileTypeException(org.openmrs.api.InvalidFileTypeException) FormResource(org.openmrs.FormResource) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException)

Example 4 with FormResource

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

the class FormServiceImpl method duplicateFormResources.

/**
 * duplicates form resources from one form to another
 *
 * @param source the form to copy resources from
 * @param destination the form to copy resources to
 */
private void duplicateFormResources(Form source, Form destination) {
    FormService service = Context.getFormService();
    for (FormResource resource : service.getFormResourcesForForm(source)) {
        FormResource newResource = new FormResource(resource);
        newResource.setForm(destination);
        service.saveFormResource(newResource);
    }
}
Also used : FormService(org.openmrs.api.FormService) FormResource(org.openmrs.FormResource)

Example 5 with FormResource

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

the class FormServiceTest method purgeFormResource_shouldDeleteAFormResource.

/**
 * @throws ParseException
 * @see FormService#purgeFormResource(Form,String,String)
 */
@Test
public void purgeFormResource_shouldDeleteAFormResource() throws ParseException {
    // save an original resource
    Form form = Context.getFormService().getForm(1);
    String name = "Start Date";
    FormResource resource = new FormResource();
    resource.setForm(form);
    resource.setName(name);
    resource.setDatatypeClassname("org.openmrs.customdatatype.datatype.DateDatatype");
    Date previous = new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-16");
    resource.setValue(previous);
    resource = Context.getFormService().saveFormResource(resource);
    Integer resourceId = resource.getFormResourceId();
    // clear the session
    Context.clearSession();
    // find and delete the resource
    resource = Context.getFormService().getFormResource(resourceId);
    Context.getFormService().purgeFormResource(resource);
    // clear the session
    Context.flushSession();
    // try to find the resource
    resource = Context.getFormService().getFormResource(resourceId);
    Assert.assertNull(resource);
}
Also used : Form(org.openmrs.Form) FormResource(org.openmrs.FormResource) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

FormResource (org.openmrs.FormResource)8 Test (org.junit.Test)6 Form (org.openmrs.Form)6 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)6 SimpleDateFormat (java.text.SimpleDateFormat)5 Date (java.util.Date)5 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 FormService (org.openmrs.api.FormService)1 InvalidFileTypeException (org.openmrs.api.InvalidFileTypeException)1