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