/** * @see org.openmrs.customdatatype.CustomDatatype#fromReferenceString(java.lang.String) */ @Override public String fromReferenceString(String referenceString) throws InvalidCustomValueException { return Context.getDatatypeService().getClobDatatypeStorageByUuid(referenceString).getValue(); }
/** * @param dt the datatype that this handler should be for * @param preferredHandlerClassname * @param handlerConfig * @return a configured datatype handler with the given classname and configuration */ public static CustomDatatypeHandler getHandler(CustomDatatype<?> dt, String preferredHandlerClassname, String handlerConfig) { if (preferredHandlerClassname != null) { try { Class<? extends CustomDatatypeHandler> clazz = (Class<? extends CustomDatatypeHandler>) Context .loadClass(preferredHandlerClassname); CustomDatatypeHandler handler = clazz.newInstance(); if (handlerConfig != null) { handler.setHandlerConfiguration(handlerConfig); } return handler; } catch (Exception ex) { log.warn("Failed to instantiate and configure preferred handler with class " + preferredHandlerClassname + " and config " + handlerConfig, ex); } } // if we couldn't get the preferred handler (or none was specified) we get the default one by datatype return Context.getDatatypeService().getHandler(dt, handlerConfig); }
/** * @param handler * @param datatype * @return whether or not handler is compatible with datatype */ public static boolean isCompatibleHandler(CustomDatatypeHandler handler, CustomDatatype<?> datatype) { List<Class<? extends CustomDatatypeHandler>> handlerClasses = Context.getDatatypeService().getHandlerClasses( (Class<? extends CustomDatatype<?>>) datatype.getClass()); return handlerClasses.contains(handler.getClass()); }
/** * @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.POST) @ResponseBody public String create(@RequestParam MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { ClobDatatypeStorage clobData = new ClobDatatypeStorage(); String encoding = request.getHeader("Content-Encoding"); clobData.setValue(IOUtils.toString(file.getInputStream(), encoding)); clobData = datatypeService.saveClobDatatypeStorage(clobData); response.setStatus(HttpServletResponse.SC_CREATED); return clobData.getUuid(); }
/** * @return fully-qualified classnames of all registered datatypes */ public static List<String> getDatatypeClassnames() { List<String> ret = new ArrayList<>(); for (Class<?> c : Context.getDatatypeService().getAllDatatypeClasses()) { ret.add(c.getName()); } return ret; }
/** * @return full-qualified classnames of all registered handlers */ public static List<String> getHandlerClassnames() { List<String> ret = new ArrayList<>(); for (Class<?> c : Context.getDatatypeService().getAllHandlerClasses()) { ret.add(c.getName()); } return ret; }
/** * @param datatypeClassname * @param datatypeConfig * @return a configured datatype with the given classname and configuration */ public static CustomDatatype<?> getDatatype(String datatypeClassname, String datatypeConfig) { try { Class dtClass = Context.loadClass(datatypeClassname); CustomDatatype<?> ret = (CustomDatatype<?>) Context.getDatatypeService().getDatatype(dtClass, datatypeConfig); if (ret == null) { throw new CustomDatatypeException("Can't find datatype: " + datatypeClassname); } return ret; } catch (Exception ex) { throw new CustomDatatypeException("Error loading " + datatypeClassname + " and configuring it with " + datatypeConfig, ex); } }
@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(); } } } }
/** * @see DatatypeService#getHandler(CustomDatatype,String) */ @Test public void getHandler_shouldReturnAHandlerForTheSpecifiedDatatype() { DatatypeService service = Context.getDatatypeService(); CustomDatatype dateDatatype = CustomDatatypeUtil.getDatatype(DateDatatype.class.getName(), null); Assert.assertEquals(DateDatatypeHandler.class, service.getHandler(dateDatatype, null).getClass()); }
private List<CustomDatatypeRepresentation> getAllCustomDatatypes() { List<String> datatypeClassnames = CustomDatatypeUtil.getDatatypeClassnames(); List<CustomDatatypeRepresentation> datatypes = new ArrayList<CustomDatatypeRepresentation>(); for (String datatypeClassname : datatypeClassnames) { CustomDatatypeRepresentation datatype = new CustomDatatypeRepresentation(); datatypes.add(datatype); datatype.setDatatypeClassname(datatypeClassname); Class<CustomDatatype<?>> datatypeClass; try { datatypeClass = (Class<CustomDatatype<?>>) Context.loadClass(datatypeClassname); } catch (ClassNotFoundException e) { throw new ConversionException("Failed to load datatype class", e); } List<Class<? extends CustomDatatypeHandler>> handlerClasses = Context.getDatatypeService().getHandlerClasses( datatypeClass); for (Class<? extends CustomDatatypeHandler> handlerClass : handlerClasses) { datatype.getHandlers().add(new CustomDatatypeHandlerRepresentation(datatype, handlerClass.getName())); } } return datatypes; }
@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()); }
/** * @see DatatypeService#getHandler(CustomDatatype,String) */ @Test public void getHandler_shouldReturnAHandlerForADatatypeThatExtendsAGenericSuperclass() { DatatypeService service = Context.getDatatypeService(); CustomDatatype locationDatatype = CustomDatatypeUtil.getDatatype(LocationDatatype.class.getName(), null); Assert.assertEquals(LocationDatatypeHandler.class, service.getHandler(locationDatatype, null).getClass()); } }
@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()); }