/** * @see org.openmrs.customdatatype.CustomDatatype#fromReferenceString(java.lang.String) */ @Override public String fromReferenceString(String referenceString) throws InvalidCustomValueException { return Context.getDatatypeService().getClobDatatypeStorageByUuid(referenceString).getValue(); }
/** * @see org.openmrs.customdatatype.CustomDatatype#save(java.lang.Object, java.lang.String) */ @Override public String save(String typedValue, String existingValueReference) throws InvalidCustomValueException { // get existing object or create a new one ClobDatatypeStorage storage = existingValueReference != null ? Context.getDatatypeService() .getClobDatatypeStorageByUuid(existingValueReference) : new ClobDatatypeStorage(); storage.setValue(typedValue); storage = Context.getDatatypeService().saveClobDatatypeStorage(storage); return storage.getUuid(); }
@RequestMapping(method = RequestMethod.DELETE, value = "/{uuid}") public void delete(@PathVariable("uuid") String uuid, HttpServletRequest request, HttpServletResponse response) { ClobDatatypeStorage clobData = datatypeService.getClobDatatypeStorageByUuid(uuid); if (clobData != null) { datatypeService.deleteClobDatatypeStorage(clobData); response.setStatus(HttpServletResponse.SC_OK); } else { response.setStatus(HttpServletResponse.SC_NO_CONTENT); } } }
@RequestMapping(method = RequestMethod.GET, value = "/{uuid}") public void retrieve(@PathVariable("uuid") String uuid, HttpServletRequest request, HttpServletResponse response) throws Exception { ClobDatatypeStorage clobData = datatypeService.getClobDatatypeStorageByUuid(uuid); if (clobData == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); } else { PrintWriter writer = null; try { writer = response.getWriter(); writer.print(clobData.getValue()); writer.flush(); } finally { if (writer != null) { writer.close(); } } } }
@Test public void shouldDeleteAnExistingClobData() throws Exception { ClobDatatypeStorage clob = datatypeService .getClobDatatypeStorageByUuid(RestTestConstants1_9.CLOBDATATYPESTORAGE_RESOURCE_UUID); Assert.assertNotNull(clob); MockHttpServletResponse response = handle(newDeleteRequest(getURI() + "/" + RestTestConstants1_9.CLOBDATATYPESTORAGE_RESOURCE_UUID)); clob = datatypeService.getClobDatatypeStorageByUuid(RestTestConstants1_9.CLOBDATATYPESTORAGE_RESOURCE_UUID); Assert.assertNull(clob); Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus()); }
@Test public void shouldReturnClobDataAsFileByUuid() throws Exception { ClobDatatypeStorage clob = datatypeService .getClobDatatypeStorageByUuid(RestTestConstants1_9.CLOBDATATYPESTORAGE_RESOURCE_UUID); Assert.assertNotNull(clob); int size = clob.getValue().getBytes().length; MockHttpServletResponse response = handle(newGetRequest(getURI() + "/" + RestTestConstants1_9.CLOBDATATYPESTORAGE_RESOURCE_UUID)); Assert.assertEquals(size, response.getContentAsByteArray().length); }
@Test public void shouldRetrieveResourceValueAsFile() throws Exception { // Get the clobData for the resource FormResource resource = formService.getFormResourceByUuid(getUuid()); ClobDatatypeStorage clobData = datatypeService.getClobDatatypeStorageByUuid(resource.getValueReference()); MockHttpServletResponse response = handle(newGetRequest(getURI() + "/" + getUuid() + "/value")); String expected = "attachment;filename=\"" + resource.getName() + "\""; Assert.assertTrue(StringUtils.equals((String) response.getHeader("Content-Disposition"), expected)); Assert.assertEquals(clobData.getValue(), response.getContentAsString()); }
@Test public void shouldPostFormResourceValue() throws Exception { byte[] fileData = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream( RestTestConstants1_9.TEST_RESOURCE_FILE)); String valueReferenceBefore = formService.getFormResourceByUuid(getUuid()).getValueReference(); MockMultipartFile toUpload = new MockMultipartFile("value", "formresource.txt", "text/plain", fileData); MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); //Posting to uri of the form /ws/rest/v1/form/{uuid}/resource/{uuid}/value String uri = getBaseRestURI() + getURI() + "/" + getUuid() + "/value"; request.setRequestURI(uri); request.setMethod(RequestMethod.POST.name()); request.addHeader("Content-Type", "multipart/form-data"); request.addFile(toUpload); MockHttpServletResponse response = handle(request); String valueReferenceAfter = formService.getFormResourceByUuid(getUuid()).getValueReference(); Assert.assertNotEquals(valueReferenceBefore, valueReferenceAfter); Assert.assertNotNull(datatypeService.getClobDatatypeStorageByUuid(valueReferenceAfter)); Assert.assertEquals(HttpServletResponse.SC_CREATED, response.getStatus()); }