- Timestamp:
- 05/19/08 23:47:49 (6 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs/branches/complex-obs/src/api/org/openmrs/obs/handler/ImageHandler.java
r4239 r4265 17 17 import java.io.File; 18 18 import java.io.IOException; 19 import java.text.NumberFormat; 19 20 import java.text.SimpleDateFormat; 20 21 import java.util.Date; 21 import java.util.Hash Map;22 import java.util. Map;22 import java.util.HashSet; 23 import java.util.Set; 23 24 24 25 import javax.imageio.ImageIO; … … 36 37 */ 37 38 public class ImageHandler implements ComplexObsHandler { 38 39 39 40 public Log log = LogFactory.getLog(this.getClass()); 40 41 public ImageHandler( ) { } 42 41 private NumberFormat nf; 42 SimpleDateFormat longfmt; 43 // default mime type 44 public static final String MIME = "bmp"; 45 private Set<String> mimes; 46 43 47 /** 44 * @see org.openmrs.obs.ComplexDataHandler#renderObs() 48 * Constructor initializes formats for alternative file names to protect 49 * from unintentionally overwriting existing files. 45 50 */ 46 public void renderObs() { 47 // TODO Auto-generated method stub 51 public ImageHandler() { 52 nf = NumberFormat.getInstance(); 53 nf.setMaximumFractionDigits(0); 54 nf.setMinimumIntegerDigits(2); 55 longfmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 56 // Create a HashSet to quickly check for supported mime types. 57 mimes = new HashSet<String>(); 58 for (String mt : ImageIO.getWriterFormatNames()) { 59 mimes.add(mt); 60 } 61 } 48 62 49 } 50 51 public Obs getObs(Obs obs, String context) { 63 /** 64 * Currently supports all views and is the same as {@link #getObs(Obs)} 65 * 66 * @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, 67 * java.lang.String) 68 */ 69 public Obs getObs(Obs obs, String view) { 52 70 return this.getObs(obs); 53 71 } … … 57 75 */ 58 76 public Obs saveObs(Obs obs) { 59 BufferedImage img = (BufferedImage) obs.getComplexData() 60 .getData(); 61 String title = obs.getComplexData().getTitle(); 62 SimpleDateFormat longfmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 63 String now = longfmt.format(new Date()); 64 65 66 File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context.getAdministrationService().getGlobalProperty("obs.complex_obs_dir")); 77 // Get the buffered image from the ComplexData. 78 BufferedImage img = (BufferedImage) obs.getComplexData().getData(); 79 if (img == null) { 80 log.error("Cannot save complex obs where obsId=" + obs.getObsId() 81 + " because its ComplexData.getData() is null."); 82 return obs; 83 } 84 85 // Get the title and remove the mime type. 86 String t = obs.getComplexData().getTitle(); 87 String[] titles = t.split("\\."); 88 System.out.println(titles.length); 89 String mime = (titles.length < 2) ? titles[0] : titles[titles.length-1]; 90 String MIME = mimes.contains(mime) ? mime : ImageHandler.MIME; 91 String title = obs.getComplexData().getTitle().replace("." + MIME, ""); 92 93 File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context.getAdministrationService() 94 .getGlobalProperty("obs.complex_obs_dir")); 67 95 File outputfile = null; 68 96 … … 70 98 try { 71 99 if (null == title) { 100 String now = longfmt.format(new Date()); 72 101 outputfile = new File(dir, now); 73 102 } else { 74 outputfile = new File(dir, title); 103 outputfile = new File(dir, title + "." + MIME); 104 //outputfile = new File(dir, title); 75 105 } 76 if (outputfile.exists()) { 77 File altfile = new File(outputfile.getAbsolutePath() + now); 78 ImageIO.write(img, "jpg", altfile); 106 int i = 0; 107 String tmp = null; 108 // If the Obs does not exist, but the File does, append a two-digit 109 // count number to the filename and save it. 110 while (obs.getObsId() == null && outputfile.exists() && i < 100) { 111 tmp = null; 112 // Remove the mime type from the filename. 113 tmp = new String(outputfile.getAbsolutePath() 114 .replace("." + MIME, "")); 115 outputfile = null; 116 // Append two-digit count number to the filename. 117 String filename = (i < 1) ? tmp + "_" 118 + nf.format(Integer.valueOf(++i)) 119 : tmp.replace(nf.format(Integer.valueOf(i)), 120 nf.format(Integer.valueOf(++i))); 121 // Append the mime type to the filename. 122 //outputfile = new File(filename); 123 outputfile = new File(filename + "." + MIME); 79 124 } 80 else {81 ImageIO.write(img, "jpg", outputfile);82 }125 // Write the file to the file system. 126 // ImageIO.write(img, MIME, outputfile); 127 ImageIO.write(img, MIME, outputfile); 83 128 } catch (IOException ioe) { 84 129 log.error("Trying to write complex obs to the file system. ", ioe); 85 130 } 86 131 87 132 // Set the Title and URI for the valueComplex 88 //obs.setValueComplex("JPG Image |" + outputfile.getAbsolutePath()); 89 obs.setValueComplex("JPG Image |" + outputfile.getName()); 133 obs.setValueComplex(MIME + " image |" + outputfile.getName()); 90 134 91 // TODO: Will the ComplexData instance remain in memory or be gc'd?135 // Remove the ComlexData from the Obs 92 136 obs.setComplexData(null); 93 137 … … 96 140 97 141 public Obs getObs(Obs obs) { 98 File file = new File(obs.getValueComplex()); 142 File file = ImageHandler.getComplexDataFile(obs); 143 System.out.println("getObs: " + obs.getValueComplex()); 144 System.out.println("getObs: " + file.getAbsolutePath()); 99 145 BufferedImage img = null; 100 146 try { 101 147 img = ImageIO.read(file); 102 148 } catch (IOException e) { 103 // System.out.println(e); 149 System.out.println("Trying to read file: " + file.getAbsolutePath() 150 + " " + e); 104 151 } 105 152 106 153 ComplexData complexData = new ComplexData(file.getName(), img); 107 154 108 155 obs.setComplexData(complexData); 109 156 110 157 return obs; 111 158 } 112 159 113 160 /** 114 * TODO: Implement this method! 161 * @see org.openmrs.obs.ComplexObsHandler#purgeComplexData(org.openmrs.Obs) 162 */ 163 public boolean purgeComplexData(Obs obs) { 164 File file = ImageHandler.getComplexDataFile(obs); 165 if (file.exists() && file.delete()) { 166 // obs.setComplexData(null); 167 // obs.setValueComplex(null); 168 return true; 169 } 170 log.debug("Could not delete complex data object for obsId=" 171 + obs.getObsId() + " located at " + file.getAbsolutePath()); 172 return false; 173 } 174 175 public ComplexData getComplexData(Obs obs, String view) { 176 return this.getObs(obs).getComplexData(); 177 } 178 179 /** 180 * Convenience method to create and return a file for the stored 181 * ComplexData.data Object 115 182 * 116 183 * @param obs 117 184 * @return 118 185 */ 119 public Obs purgeObs(Obs obs) { 120 return obs; 186 public static File getComplexDataFile(Obs obs) { 187 String[] names = obs.getValueComplex().split("\\|"); 188 String filename = names.length < 2 ? names[0] : names[names.length - 1]; 189 File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context.getAdministrationService() 190 .getGlobalProperty("obs.complex_obs_dir")); 191 return new File(dir, filename); 121 192 } 122 123 public ComplexData getComplexData(Obs obs, String view) { 124 return this.getObs(obs).getComplexData(); 125 } 126 193 127 194 }