resource = Context.getFormService().getFormResource(resourceId); Context.getFormService().purgeFormResource(resource); resource = Context.getFormService().getFormResource(resourceId); Assert.assertNull(resource);
/** * @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()); }
FormResource actual = Context.getFormService().getFormResource(form, name);
/** * @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); }
FormResource actual = Context.getFormService().getFormResource(form, name); assertEquals(expected, actual.getValue()); Assert.assertNull(Context.getFormService().getFormResource(savedId));
/** * @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()); }