Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Technical Workflow

Code Block

Save a complex obs and data:ConceptComplex conceptComplex = Context.getConceptService().getConceptComplex(1867);
// this is assumed to have happened
// conceptComplex.setHandler("ImageHandler");
 
// Set the required properties.
Obs obs = new Obs(new Person(48609), conceptComplex, new Date(), new Location());
 
BufferedImage img = ImageIO.read(new File("/home/bmckown/Desktop/test/logo.png"));
// or:
// InputStream img = new FileInputStream(new File("folder", "filename"));
 
ComplexData complexData = new ComplexData("test-image.jpg", img);
obs.setComplexData(complexData);
 
Context.getObsService().saveObs(obs, null);
 
// obs.getComplexData() will be null here
Retrieve a complex obs and its dataInteger obsId = obs.getObsId();
Obs complexObs = Context.getObsService().getComplexObs(obsId, OpenmrsConstants.RAW_VIEW);
ComplexData complexData = complexObs.getComplexData();
Object object = complexData.getData();
// object will be a BufferedImage object
Code Block
languagejava
titleGetting back binary-data / binary-stream compleObs -- eg: for Video files, Zip files, kind of large files, etc
        Obs complexObs = Context.getObsService().getComplexObs(videoObsId, OpenmrsConstants.RAW_VIEW);
        ComplexData complexData = complexObs.getComplexData();
        byte[] videoObjectData = ((byte[]) complexData.getData()); // cast Object --to--> byte array object

How to Create a ComplexObsHandler

...

  • Found in the org.openmrs.web.controller.observation.handler package
  • Extends org.openmrs.obs.handler.ImageHandler
  • NOTE! Uses ImageHandler to saveObs().
  • Overrides the getComplexData() method to provide web-specific ComplexData
    • Provides hyperlink to ComplexObsServlet instead of the heavyweight data.

      Code Block
      
      public class WebImageHandler extends ImageHandler {
        public Obs getComplexData(Obs obs, String view) {
           if (Webutils.HYPERLINK_VIEW.equals(view)) {
               String link = "/ComplexObsServlet?obsId=" + obs.getObsId();
               obs.set(new ComplexData("some title", link);
               return obs;
           }
           return super.getComplexObs(obs, view);
        }
      }
      

...

  • Register WebImageHandler in openmrs-servlet.xml so that it is only seen if used in the webapp...not in the jar alone.
  • Or alternatively, for a module register in ModuleApplicationContext.xml

    Code Block
    
        <bean parent="obsServiceTarget" >
            <property name="handlers">
                <map>
                    <entry>
                        <key><value>ImageHandler</value></key>
                        <bean class="org.openmrs.web.controller.observation.handler.WebImageHandler"/>
                    </entry>
                </map>
            </property>
        </bean>