| 104 | | |
|---|
| 105 | | // /** |
|---|
| 106 | | // * @see org.openmrs.obs.ComplexObsHandler#saveObs(org.openmrs.Obs) |
|---|
| 107 | | // */ |
|---|
| 108 | | /* |
|---|
| 109 | | * public Obs saveObs(Obs obs) { // Get the buffered image from the |
|---|
| 110 | | * ComplexData. AnnotatedImage img = (AnnotatedImage) |
|---|
| 111 | | * obs.getComplexData().getData(); if (img == null) { |
|---|
| 112 | | * log.error("Cannot save complex obs where obsId=" + obs.getObsId() + |
|---|
| 113 | | * " because its ComplexData.getData() is null."); return obs; } |
|---|
| 114 | | * |
|---|
| 115 | | * // Get the title and remove the mime type. String t = |
|---|
| 116 | | * obs.getComplexData().getTitle(); String[] titles = t.split("\\."); |
|---|
| 117 | | * System.out.println(titles.length); String mime = (titles.length < 2) ? |
|---|
| 118 | | * titles[0] : titles[titles.length - 1]; String MIME = mimes.contains(mime) |
|---|
| 119 | | * ? mime : AnnotatedImageHandler.MIME; String title = |
|---|
| 120 | | * obs.getComplexData().getTitle().replace("." + MIME, ""); |
|---|
| 121 | | * |
|---|
| 122 | | * File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context |
|---|
| 123 | | * .getAdministrationService().getGlobalProperty( "obs.complex_obs_dir")); |
|---|
| 124 | | * File outputfile = null; |
|---|
| 125 | | * |
|---|
| 126 | | * // Write the image to the file system. try { if (null == title) { String |
|---|
| 127 | | * now = longfmt.format(new Date()); outputfile = new File(dir, now); } else |
|---|
| 128 | | * { outputfile = new File(dir, title + "." + MIME); // outputfile = new |
|---|
| 129 | | * File(dir, title); } int i = 0; String tmp = null; // If the Obs does not |
|---|
| 130 | | * exist, but the File does, append a two-digit // count number to the |
|---|
| 131 | | * filename and save it. while (obs.getObsId() == null && |
|---|
| 132 | | * outputfile.exists() && i < 100) { tmp = null; // Remove the mime type |
|---|
| 133 | | * from the filename. tmp = new String(outputfile.getAbsolutePath().replace( |
|---|
| 134 | | * "." + MIME, "")); outputfile = null; // Append two-digit count number to |
|---|
| 135 | | * the filename. String filename = (i < 1) ? tmp + "_" + |
|---|
| 136 | | * nf.format(Integer.valueOf(++i)) : tmp.replace(nf |
|---|
| 137 | | * .format(Integer.valueOf(i)), nf.format(Integer .valueOf(++i))); // Append |
|---|
| 138 | | * the mime type to the filename. // outputfile = new File(filename); |
|---|
| 139 | | * outputfile = new File(filename + "." + MIME); } // Write the file to the |
|---|
| 140 | | * file system. // ImageIO.write(img.getImage(), MIME, outputfile); |
|---|
| 141 | | * |
|---|
| 142 | | * File metadataFile = new File(outputfile.getCanonicalPath() + ".xml"); |
|---|
| 143 | | * |
|---|
| 144 | | * DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); |
|---|
| 145 | | * DocumentBuilder builder = factory.newDocumentBuilder(); Document xmldoc; |
|---|
| 146 | | * |
|---|
| 147 | | * if (!metadataFile.exists()) { metadataFile.createNewFile(); |
|---|
| 148 | | * DOMImplementation domImpl = builder.getDOMImplementation(); xmldoc = |
|---|
| 149 | | * domImpl.createDocument(null, "Annotations", null); } else { xmldoc = |
|---|
| 150 | | * builder.parse(metadataFile); } Element root = |
|---|
| 151 | | * xmldoc.getDocumentElement(); |
|---|
| 152 | | * |
|---|
| 153 | | * for (ImageAnnotation a : img.getAnnotations()) { Element e = |
|---|
| 154 | | * xmldoc.createElementNS(null, "Annotation"); Node n = |
|---|
| 155 | | * xmldoc.createTextNode(a.getText()); e.setAttributeNS(null, "xcoordinate", |
|---|
| 156 | | * a.getLocation().x + ""); e.setAttributeNS(null, "ycoordinate", |
|---|
| 157 | | * a.getLocation().y + ""); e.setAttributeNS(null, "userid", |
|---|
| 158 | | * a.getUser().getUserId() + ""); e.setAttributeNS(null, "date", |
|---|
| 159 | | * a.getDate().getTime() + ""); e.appendChild(n); root.appendChild(e); } |
|---|
| 160 | | * |
|---|
| 161 | | * Transformer transformer = TransformerFactory.newInstance() |
|---|
| 162 | | * .newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, |
|---|
| 163 | | * "UTF8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
|---|
| 164 | | * transformer.transform(new DOMSource(xmldoc), new StreamResult( |
|---|
| 165 | | * metadataFile)); |
|---|
| 166 | | * |
|---|
| 167 | | * } catch (IOException ioe) { |
|---|
| 168 | | * log.error("Trying to write complex obs to the file system. ", ioe); } |
|---|
| 169 | | * catch (TransformerException e) { |
|---|
| 170 | | * log.error("Trying to write complex obs to the file system. ", e); } catch |
|---|
| 171 | | * (SAXException e) { |
|---|
| 172 | | * log.error("Trying to write complex obs to the file system. ", e); } catch |
|---|
| 173 | | * (ParserConfigurationException e) { |
|---|
| 174 | | * log.error("Trying to write complex obs to the file system. ", e); } |
|---|
| 175 | | * |
|---|
| 176 | | * // Set the Title and URI for the valueComplex obs.setValueComplex(MIME + |
|---|
| 177 | | * " image |" + outputfile.getName()); |
|---|
| 178 | | * |
|---|
| 179 | | * // Remove the ComlexData from the Obs obs.setComplexData(null); |
|---|
| 180 | | * |
|---|
| 181 | | * return obs; } |
|---|
| 182 | | */ |
|---|