Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
05/19/08 23:47:49 (6 months ago)
Author:
bmckown
Message:

complex_obs branch: Added ability to override handlers via Spring. Added methods to update and purge ComplexData from the file system. Added support for multiple mime types in ImageHandler based on file name. Added temporary files complexObsList.jsp and complexObsForm.jsp

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs/branches/complex-obs/src/api/org/openmrs/obs/handler/ImageHandler.java

    r4239 r4265  
    1717import java.io.File; 
    1818import java.io.IOException; 
     19import java.text.NumberFormat; 
    1920import java.text.SimpleDateFormat; 
    2021import java.util.Date; 
    21 import java.util.HashMap
    22 import java.util.Map
     22import java.util.HashSet
     23import java.util.Set
    2324 
    2425import javax.imageio.ImageIO; 
     
    3637 */ 
    3738public class ImageHandler implements ComplexObsHandler { 
    38          
     39 
    3940        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 
    4347        /** 
    44          * @see org.openmrs.obs.ComplexDataHandler#renderObs() 
     48         * Constructor initializes formats for alternative file names to protect 
     49         * from unintentionally overwriting existing files. 
    4550         */ 
    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        } 
    4862 
    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) { 
    5270                return this.getObs(obs); 
    5371        } 
     
    5775         */ 
    5876        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")); 
    6795                File outputfile = null; 
    6896 
     
    7098                try { 
    7199                        if (null == title) { 
     100                                String now = longfmt.format(new Date()); 
    72101                                outputfile = new File(dir, now); 
    73102                        } else { 
    74                                 outputfile = new File(dir, title); 
     103                                outputfile = new File(dir, title + "." + MIME); 
     104                                //outputfile = new File(dir, title); 
    75105                        } 
    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); 
    79124                        } 
    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); 
    83128                } catch (IOException ioe) { 
    84129                        log.error("Trying to write complex obs to the file system. ", ioe); 
    85130                } 
    86                  
     131 
    87132                // 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()); 
    90134 
    91                 // TODO: Will the ComplexData instance remain in memory or be gc'd? 
     135                // Remove the ComlexData from the Obs 
    92136                obs.setComplexData(null); 
    93137 
     
    96140 
    97141        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()); 
    99145                BufferedImage img = null; 
    100146                try { 
    101147                        img = ImageIO.read(file); 
    102148                } catch (IOException e) { 
    103                         // System.out.println(e); 
     149                        System.out.println("Trying to read file: " + file.getAbsolutePath() 
     150                                + " " + e); 
    104151                } 
    105152 
    106153                ComplexData complexData = new ComplexData(file.getName(), img); 
    107                  
     154 
    108155                obs.setComplexData(complexData); 
    109156 
    110157                return obs; 
    111158        } 
    112          
     159 
    113160        /** 
    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 
    115182         *  
    116183         * @param obs 
    117184         * @return 
    118185         */ 
    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); 
    121192        } 
    122          
    123         public ComplexData getComplexData(Obs obs, String view) { 
    124                 return this.getObs(obs).getComplexData(); 
    125         } 
    126          
     193 
    127194}