Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Changeset 4289

Show
Ignore:
Timestamp:
05/21/08 01:41:31 (8 months ago)
Author:
bmckown
Message:

complex_obs branch: TextHandler is now able to do file i/o for files using byte stream. Cleaned up WebImageHandler giving it very limited function. Added byte array to ComplexData to facilitate byte streaming for files.

Files:

Legend:

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

    r4265 r4289  
    1717 
    1818/** 
    19  * Child class of Concept that has a ConceptComplexHandler associated with the 
     19 * Child class of Concept that has a ComplexObsHandler associated with the 
    2020 * Concept. 
    2121 */ 
     
    3434        } 
    3535         
     36        /** 
     37         * Constructor from Concept. 
     38         *  
     39         * @param c 
     40         */ 
    3641        public ConceptComplex(Concept c) { 
    3742                this.setAnswers(c.getAnswers(true)); 
     
    8085 
    8186        /** 
    82          * Set the ConceptComplexHandler 
     87         * Set the ConceptComplexHandler.  This should be the ComplexObsHandler key  
    8388         *  
    8489         * @param handler 
     
    8994 
    9095        /** 
    91          * Get the ConceptComplexHandler associated with this ConceptComlex 
     96         * Get the key to the ComplexObsHandler associated with this ConceptComplex. 
    9297         *  
    9398         * @return 
  • openmrs/branches/complex-obs/src/api/org/openmrs/obs/ComplexData.java

    r4166 r4289  
    1414package org.openmrs.obs; 
    1515 
    16 import org.openmrs.ConceptComplex; 
    17 import org.openmrs.Obs; 
    1816 
    1917/** 
    2018 * ComplexObs is a transient Object that extends Obs but is not itself persisted 
    21  * in the database. It has a data Object and a title. 
     19 * in the database. It has a data Object and a title. Alternatively, it can have 
     20 * a byte array instead of an Object. 
    2221 *  
    2322 */ 
     
    2726 
    2827        private Object data; 
     28        private byte[] bytes; 
    2929        private String title; 
     30 
     31        public static enum Type { 
     32                OBJECT, BYTE 
     33        } 
     34 
     35        private Type type; 
    3036 
    3137        /** 
     
    3844                setTitle(title); 
    3945                setData(data); 
     46                this.type = Type.OBJECT; 
    4047        } 
    4148 
    4249        /** 
    43          * Set the data Object. 
     50         * Alternative constructor for using byte array. <br/>Example use: <br/>File 
     51         * file = new File("test.txt"); <br/>byte[] bytes = 
     52         * OpenmrsUtil.getFileAsBytes(file); <br/>ComplexData data = new 
     53         * ComplexData("test name", bytes); 
    4454         *  
    45          * @param data 
     55         * @param title 
     56         * @param bytes 
    4657         */ 
    47         public void setData(Object data) { 
    48                 this.data = data; 
    49         } 
    50  
    51         /** 
    52          * Get the data Object 
    53          *  
    54          * @return 
    55          */ 
    56         public Object getData() { 
    57                 return this.data; 
     58        public ComplexData(String title, byte[] bytes) { 
     59                setBytes(bytes); 
     60                setTitle(title); 
     61                this.type = Type.BYTE; 
    5862        } 
    5963 
     
    6367         * @param title 
    6468         */ 
    65         public void setTitle(String title) { 
     69        private void setTitle(String title) { 
    6670                this.title = title; 
    6771        } 
     
    7579                return this.title; 
    7680        } 
     81 
     82        /** 
     83         * Set the data Object. 
     84         *  
     85         * @param data 
     86         */ 
     87        private void setData(Object data) { 
     88                if (this.type == Type.OBJECT) 
     89                        this.data = data; 
     90        } 
     91 
     92        /** 
     93         * Get the data Object. If this was initialized with a byte array, the 
     94         * output may not be reliable. TODO: Should this even try to return the byte 
     95         * array? 
     96         *  
     97         * @return 
     98         */ 
     99        public Object getData() { 
     100                if (this.type != Type.OBJECT) { 
     101                        return getBytes(); 
     102                } 
     103                return this.data; 
     104        } 
     105 
     106        /** 
     107         * Set the byte array. This is alternative to {@link #setData(Object)} 
     108         *  
     109         * @param bytes 
     110         */ 
     111        private void setBytes(byte[] bytes) { 
     112                this.bytes = bytes; 
     113        } 
     114 
     115        /** 
     116         * Get the byte array. If this was initialized with an Object, returns 
     117         * Object.toString.getBytes(). <br/>Example use: <br/> File file = new 
     118         * File("text"); <br/> FileOutputStream fout = new FileOutputStream(file); 
     119         * <br/> fout.write(complexData.getBytes()); <br/> 
     120         *  
     121         * @return 
     122         */ 
     123        public byte[] getBytes() { 
     124                if (this.type != Type.BYTE && null != data) { 
     125                        return data.toString().getBytes(); 
     126                } 
     127                return this.bytes; 
     128        } 
     129 
     130        /** 
     131         * Returns the ComplexData.Type that this ComplexData was initialized. If 
     132         * initialized with a byte array, returns ComplexData.Type.BYTE. If 
     133         * initialized with an Object, returns ComplexData.Type.OBJECT. 
     134         *  
     135         * @return ComplexData.Type.OBJECT or ComplexData.Type.BYTE 
     136         */ 
     137        public ComplexData.Type getType() { 
     138                return this.type; 
     139        } 
     140 
     141        /** 
     142         * Convenience method for getting the className of the data. Same as 
     143         * this.getData().getClass().getName() except that it safely returns the 
     144         * String "null" if this.data is null. 
     145         *  
     146         * TODO: This method is not used anywhere. Should it be deleted? 
     147         *  
     148         * @return 
     149         */ 
     150        public String dataClass() { 
     151                if (null != data) { 
     152                        return this.data.getClass().getName(); 
     153                } 
     154                return "null"; 
     155        } 
    77156} 
  • openmrs/branches/complex-obs/src/api/org/openmrs/obs/ComplexObsHandler.java

    r4265 r4289  
    2121 * Interface for handling complex obs. Implementing classes are responsible for 
    2222 * the storage and retrieval of ComplexData associated with an Obs that is 
    23  * complex (where Obs.isComplex() returns true. 
     23 * complex - where Obs.isComplex() returns true. 
    2424 */ 
    2525public interface ComplexObsHandler { 
  • openmrs/branches/complex-obs/src/api/org/openmrs/obs/handler/ImageHandler.java

    r4265 r4289  
    3434 
    3535/** 
    36  * Handler for storing basic images for complex obs to the file system. 
     36 * Handler for storing basic images for complex obs to the file system. The 
     37 * image mime type used is taken from the image name. if the .* image name 
     38 * suffix matches {@link javax.imageio.ImageIO#getWriterFormatNames()} then that 
     39 * mime type will be used to save the image. Otherwise the default mime type is 
     40 * ImageHandler.MIME (currently "jpg"). 
     41 *  
     42 * Images are stored in the location specified by the global property: 
     43 * "obs.complex_obs_dir"   
    3744 */ 
    3845public class ImageHandler implements ComplexObsHandler { 
     
    4249        SimpleDateFormat longfmt; 
    4350        // default mime type 
    44         public static final String MIME = "bmp"; 
     51        public static final String MIME = "jpg"; 
    4552        private Set<String> mimes; 
    4653 
     
    8794                String[] titles = t.split("\\."); 
    8895                System.out.println(titles.length); 
    89                 String mime = (titles.length < 2) ? titles[0] : titles[titles.length-1]; 
     96                String mime = (titles.length < 2) ? titles[0] 
     97                        : titles[titles.length - 1]; 
    9098                String MIME = mimes.contains(mime) ? mime : ImageHandler.MIME; 
    9199                String title = obs.getComplexData().getTitle().replace("." + MIME, ""); 
     
    102110                        } else { 
    103111                                outputfile = new File(dir, title + "." + MIME); 
    104                                 //outputfile = new File(dir, title); 
     112                                // outputfile = new File(dir, title); 
    105113                        } 
    106114                        int i = 0; 
     
    112120                                // Remove the mime type from the filename. 
    113121                                tmp = new String(outputfile.getAbsolutePath() 
    114                                                                                        .replace("." + MIME, "")); 
     122                                                           .replace("." + MIME, "")); 
    115123                                outputfile = null; 
    116124                                // Append two-digit count number to the filename. 
     
    120128                                                      nf.format(Integer.valueOf(++i))); 
    121129                                // Append the mime type to the filename. 
    122                                 //outputfile = new File(filename); 
     130                                // outputfile = new File(filename); 
    123131                                outputfile = new File(filename + "." + MIME); 
    124132                        } 
     
    164172                File file = ImageHandler.getComplexDataFile(obs); 
    165173                if (file.exists() && file.delete()) { 
    166                         // obs.setComplexData(null); 
     174                        obs.setComplexData(null); 
    167175                        // obs.setValueComplex(null); 
    168176                        return true; 
     
    173181        } 
    174182 
     183        /** 
     184         * @see org.openmrs.obs.ComplexObsHandler#getComplexData(org.openmrs.Obs, java.lang.String) 
     185         */ 
    175186        public ComplexData getComplexData(Obs obs, String view) { 
    176187                return this.getObs(obs).getComplexData(); 
  • openmrs/branches/complex-obs/src/api/org/openmrs/obs/handler/TextHandler.java

    r4265 r4289  
    1414package org.openmrs.obs.handler; 
    1515 
    16 import java.awt.image.BufferedImage; 
    1716import java.io.File; 
     17import java.io.FileOutputStream; 
    1818import java.io.IOException; 
     19import java.text.NumberFormat; 
    1920import java.text.SimpleDateFormat; 
    2021import java.util.Date; 
    21  
    22 import javax.imageio.ImageIO; 
    2322 
    2423import org.apache.commons.logging.Log; 
     
    3130 
    3231/** 
    33  * TODO: This is not a text handler yet.  It is a copy of the ImageHandler. 
    34  * Change it to handle text files. 
     32 * Handler for storing files for complex obs to the file system.  
    3533 *  
    36  * Handler for storing basic images for complex obs to the file system. 
     34 * Files are stored in the location specified by the global property: 
     35 * "obs.complex_obs_dir" 
    3736 */ 
    3837public class TextHandler implements ComplexObsHandler { 
    39          
     38 
    4039        public Log log = LogFactory.getLog(this.getClass()); 
    41          
     40        private NumberFormat nf; 
     41        SimpleDateFormat longfmt; 
     42 
     43        /** 
     44         * Constructor initializes formats for alternative file names to protect 
     45         * from unintentionally overwriting existing files. 
     46         */ 
     47        public TextHandler() { 
     48                nf = NumberFormat.getInstance(); 
     49                nf.setMaximumFractionDigits(0); 
     50                nf.setMinimumIntegerDigits(2); 
     51                longfmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
     52        } 
     53 
     54        /** 
     55         * Currently supports all views and is the same as {@link #getObs(Obs)} 
     56         *  
     57         * @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, 
     58         *      java.lang.String) 
     59         */ 
     60        public Obs getObs(Obs obs, String view) { 
     61                return this.getObs(obs); 
     62        } 
     63 
    4264        /** 
    4365         * @see org.openmrs.obs.ComplexDataHandler#saveObs() 
    4466         */ 
    4567        public Obs saveObs(Obs obs) { 
    46                 BufferedImage img = (BufferedImage) obs.getComplexData() 
    47                                                        .getData(); 
    48                 String title = obs.getComplexData().getTitle(); 
    49                 SimpleDateFormat longfmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
    50                 String now = longfmt.format(new Date()); 
    51                  
    52                  
    53                 File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context.getAdministrationService().getGlobalProperty("obs.complex_obs_dir")); 
     68                // Get the buffered file  from the ComplexData. 
     69                ComplexData data = obs.getComplexData(); 
     70                if (data == null) { 
     71                        log.error("Cannot save complex data where obsId=" + obs.getObsId() 
     72                                + " because its ComplexData is null."); 
     73                        return obs; 
     74                } 
     75 
     76                // Get the title and remove the mime type. 
     77                String t = obs.getComplexData().getTitle(); 
     78                String[] titles = t.split("\\."); 
     79                System.out.println(titles.length); 
     80                String mime = (titles.length < 2) ? titles[0] 
     81                        : titles[titles.length - 1]; 
     82                String MIME = (null != mime && !"".equals(mime)) ? mime : "raw"; 
     83                String title = obs.getComplexData().getTitle().replace("." + MIME, ""); 
     84 
     85                File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context.getAdministrationService() 
     86                                                                                     .getGlobalProperty("obs.complex_obs_dir")); 
    5487                File outputfile = null; 
    5588 
    56                 // Write the image to the file system. 
     89                // Write the file to the file system. 
    5790                try { 
    5891                        if (null == title) { 
     92                                String now = longfmt.format(new Date()); 
    5993                                outputfile = new File(dir, now); 
    6094                        } else { 
    61                                 outputfile = new File(dir, title); 
     95                                outputfile = new File(dir, title + "." + MIME); 
     96                                // outputfile = new File(dir, title); 
    6297                        } 
    63                         if (outputfile.exists()) { 
    64                                 File altfile = new File(outputfile.getAbsolutePath() + now); 
    65                                 ImageIO.write(img, "jpg", altfile); 
     98                        int i = 0; 
     99                        String tmp = null; 
     100                        // If the Obs does not exist, but the File does, append a two-digit 
     101                        // count number to the filename and save it. 
     102                        while (obs.getObsId() == null && outputfile.exists() && i < 100) { 
     103                                tmp = null; 
     104                                // Remove the mime type from the filename. 
     105                                tmp = new String(outputfile.getAbsolutePath() 
     106                                                           .replace("." + MIME, "")); 
     107                                outputfile = null; 
     108                                // Append two-digit count number to the filename. 
     109                                String filename = (i < 1) ? tmp + "_" 
     110                                        + nf.format(Integer.valueOf(++i)) 
     111                                        : tmp.replace(nf.format(Integer.valueOf(i)), 
     112                                                      nf.format(Integer.valueOf(++i))); 
     113                                // Append the mime type to the filename. 
     114                                // outputfile = new File(filename); 
     115                                outputfile = new File(filename + "." + MIME); 
    66116                        } 
    67                         else { 
    68                                ImageIO.write(img, "jpg", outputfile); 
    69                         } 
     117                        // Write the file to the file system. 
     118                        FileOutputStream fout = new FileOutputStream(outputfile); 
     119                        fout.write(data.getBytes()); 
    70120                } catch (IOException ioe) { 
    71121                        log.error("Trying to write complex obs to the file system. ", ioe); 
    72122                } 
    73                  
     123 
    74124                // Set the Title and URI for the valueComplex 
    75                 obs.setValueComplex("JPG Image |" + outputfile.getAbsolutePath()); 
     125                obs.setValueComplex(MIME + " file |" + outputfile.getName()); 
    76126 
    77                 // TODO: Will the ComplexData instance remain in memory or be gc'd? 
     127                // Remove the ComplexData from the Obs 
    78128                obs.setComplexData(null); 
    79129 
     
    81131        } 
    82132 
     133        /** 
     134         * @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs) 
     135         */ 
    83136        public Obs getObs(Obs obs) { 
    84                 File file = new File(obs.getValueComplex()); 
    85                 BufferedImage img = null; 
     137                File file = TextHandler.getComplexDataFile(obs); 
     138                System.out.println("getObs: " + obs.getValueComplex()); 
     139                System.out.println("getObs: " + file.getAbsolutePath()); 
     140                ComplexData complexData = null; 
    86141                try { 
    87                         img = ImageIO.read(file); 
     142                        complexData = new ComplexData(file.getName(), 
     143                                                      OpenmrsUtil.getFileAsBytes(file)); 
    88144                } catch (IOException e) { 
    89                         // System.out.println(e); 
     145                        System.out.println("Trying to read file: " + file.getAbsolutePath() 
     146                                + " " + e); 
    90147                } 
    91148 
    92                 ComplexData complexData = new ComplexData(file.getName(), img); 
    93                  
    94149                obs.setComplexData(complexData); 
    95150 
    96151                return obs; 
    97152        } 
    98          
     153 
     154        /** 
     155         * @see org.openmrs.obs.ComplexObsHandler#purgeComplexData(org.openmrs.Obs) 
     156         */ 
    99157        public boolean purgeComplexData(Obs obs) { 
     158                File file = TextHandler.getComplexDataFile(obs); 
     159                if (file.exists() && file.delete()) { 
     160                        obs.setComplexData(null); 
     161                        // obs.setValueComplex(null); 
     162                        return true; 
     163                } 
     164                log.debug("Could not delete complex data object for obsId=" 
     165                        + obs.getObsId() + " located at " + file.getAbsolutePath()); 
    100166                return false; 
    101167        } 
    102168 
    103169        /** 
    104          * TODO: Implement for context 
     170         * @see org.openmrs.obs.ComplexObsHandler#getComplexData(org.openmrs.Obs, 
     171         *      java.lang.String) 
    105172         */ 
    106         public Obs saveObs(Obs obs, String context) { 
    107                 return this.saveObs(obs); 
    108         } 
    109          
    110         /** 
    111          * TODO: IMplement for context. 
    112          * @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, java.lang.String) 
    113          */ 
    114         public Obs getObs(Obs obs, String context) { 
    115                 return this.getObs(obs); 
     173        public ComplexData getComplexData(Obs obs, String view) { 
     174                return this.getObs(obs).getComplexData(); 
    116175        } 
    117176 
    118177        /** 
    119      * @see org.openmrs.obs.ComplexObsHandler#getComplexData(org.openmrs.Obs, java.lang.String) 
    120      */ 
    121     public ComplexData getComplexData(Obs obs, String view) { 
    122             // TODO Auto-generated method stub 
    123             return null; 
    124     } 
    125          
     178         * Convenience method to create and return a file for the stored 
     179         * ComplexData.data Object 
     180         *  
     181         * @param obs 
     182         * @return 
     183         */ 
     184        public static File getComplexDataFile(Obs obs) { 
     185                String[] names = obs.getValueComplex().split("\\|"); 
     186                String filename = names.length < 2 ? names[0] : names[names.length - 1]; 
     187                File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context.getAdministrationService() 
     188                                                                                     .getGlobalProperty("obs.complex_obs_dir")); 
     189                return new File(dir, filename); 
     190        } 
     191 
    126192} 
  • openmrs/branches/complex-obs/src/web/org/openmrs/web/controller/observation/handler/WebImageHandler.java

    r4265 r4289  
    1616import org.openmrs.Obs; 
    1717import org.openmrs.obs.ComplexData; 
    18 import org.openmrs.obs.ComplexObsHandler; 
    1918import org.openmrs.obs.handler.ImageHandler; 
    2019import org.openmrs.web.WebConstants; 
    2120 
    2221/** 
    23  * 
     22 * Extends functionality of ImageHandler for web specific views. 
    2423 */ 
    25 public class WebImageHandler extends ImageHandler { //implements ComplexObsHandler { 
     24public class WebImageHandler extends ImageHandler { 
    2625 
    2726        /** 
    28          *  
     27         * Default Constructor 
    2928         */ 
    30         public WebImageHandler() {  
    31                  
     29        public WebImageHandler() { 
    3230        } 
    3331 
    34         public Obs getObs(Obs obs, String view) { 
    35                 Obs wobs = super.getObs(obs); 
    36                 // TODO: Do something here... 
    37                 return wobs; 
     32        /** 
     33         * Returns the ComplexData for an Obs depending on the view. Currently 
     34         * supported views are listed in WebConstants.*_VIEW. Currently the only 
     35         * implemented view is WebConstants.HYPERLINK_VIEW, which is a lightweight 
     36         * alternative to returning the ComplexData from the parent class since this 
     37         * does not require access to the service layer. 
     38         *  
     39         * @see org.openmrs.obs.handler.ImageHandler#getComplexData(org.openmrs.Obs, 
     40         *      java.lang.String) 
     41         */ 
     42        public ComplexData getComplexData(Obs obs, String view) { 
     43 
     44                if (WebConstants.HYPERLINK_VIEW.equals(view)) { 
     45                        String hyperlink = "/complexObsServlet?obsId=" + obs.getObsId(); 
     46                        return new ComplexData(obs.getValueAsString(null), hyperlink); 
     47                } else if (WebConstants.HTML_VIEW.equals(view)) { 
     48                        // TODO: Do something for html view. 
     49                } else if (WebConstants.HTML_THUMNAIL_VIEW.equals(view)) { 
     50                        // TODO: Probably return a hyperlink view with extra parameters for 
     51                        // width & height. 
     52                } 
     53                return super.getObs(obs, view).getComplexData(); 
    3854        } 
    39          
    40         public ComplexData getComplexData(Obs obs, String view) { 
    41                 Obs cobs = super.getObs(obs, view); 
    42                 ComplexData data = cobs.getComplexData(); 
    43                 if (WebConstants.HTML_VIEW.equals(view)) { 
    44                         //TODO: Do something for html view. 
    45                 } 
    46                 else if (WebConstants.HYPERLINK_VIEW.equals(view)) { 
    47                         data.setData(this.getHyperlink(obs)); 
    48                 } 
    49                 return data; 
    50         } 
    51          
    52         private String getHyperlink(Obs obs) { 
    53                 String[] valComplex = obs.getValueComplex().split("\\|"); 
    54                 String path = (valComplex.length < 2) ? valComplex[0] : valComplex[valComplex.length-1]; 
    55                  
    56                 //TODO: return a 'real' hyperlink 
    57                 return path; 
    58         } 
    59          
    6055 
    61          
    6256} 
  • openmrs/branches/complex-obs/test/api/org/openmrs/test/api/ComplexObsTest.java

    r4265 r4289  
    1616import java.awt.image.BufferedImage; 
    1717import java.io.File; 
     18import java.io.FileOutputStream; 
    1819import java.io.IOException; 
     20import java.nio.Buffer; 
     21import java.nio.ByteBuffer; 
    1922import java.util.Date; 
    2023import java.util.HashSet; 
     
    3134import org.openmrs.obs.handler.ImageHandler; 
    3235import org.openmrs.test.BaseContextSensitiveTest; 
     36import org.openmrs.util.OpenmrsUtil; 
    3337 
    3438/** 
     
    4953                return false; 
    5054        } 
    51          
    52         public void testGetRegisteredHandlers( ) throws Exception { 
    53                 /* 
    54                  * Step 10. Print the list of registered ComplexObsHandlers. 
     55 
     56        public void testGetRegisteredHandlers() throws Exception { 
     57                /* 
     58                 * Print the list of registered ComplexObsHandlers. 
    5559                 */ 
    5660                List handlers = Context.getObsService().getComplexObsHandlers(); 
    57                 for (int i=0; i<handlers.size(); i++)
     61                for (int i = 0; i < handlers.size(); i++)
    5862                        System.out.println(handlers.get(i).getClass()); 
    5963                } 
     
    7276         * @throws Exception 
    7377         */ 
    74         public void testSaveGetComplexObs() throws Exception { 
     78        public void disable_testSaveGetImageComplexObs() throws Exception { 
    7579 
    7680                /* 
     
    119123                ComplexData cdat = new ComplexData("hoss-collab", img); 
    120124                obs.setComplexData(cdat); 
    121                 /* TODO: At this point the obs.isComplex() will return false. This is 
     125                /* 
     126                 * TODO: At this point the obs.isComplex() will return false. This is 
    122127                 * because we do not want the Obs pojo to know anyting of the 
    123128                 * ComplexObsHandler, and it is the responsibility of the 
     
    125130                 * considered complex if obs.getValueComplex() != null. 
    126131                 */ 
    127                  
    128                 /* 
    129                  * Step 4. Persist the Complex Obs.  
     132 
     133                /* 
     134                 * Step 4. Persist the Complex Obs. 
    130135                 */ 
    131136                Context.getObsService().createObs((Obs) obs); 
    132                 /*TODO: Starting here the obs.isComplex() will return true. 
     137                /* 
     138                 * TODO: Starting here the obs.isComplex() will return true. 
    133139                 */ 
    134140                // Save a complex obs to the database for testing. 
    135                 //setComplete(); 
    136                  
     141                // setComplete(); 
    137142                /* 
    138143                 * Step 5. Assert that the Complex Obs now has an obsId. 
     
    145150                 */ 
    146151                System.out.println(obs.getValueComplex()); 
    147                 //assertNotNull(obs.getValueComplex()); 
     152                // assertNotNull(obs.getValueComplex()); 
    148153 
    149154                /* 
     
    152157                Integer obsId = obs.getObsId(); 
    153158                Obs cobs = Context.getObsService().getComplexObs(obsId, null); 
    154                  
    155                 System.out.println(((ConceptComplex)cobs.getConcept()).getHandler()); 
    156  
    157                 /* 
    158                  * Step 8. Save the ComplexData to the file system. 
    159                  * Assert that the ComplexData is not null. 
     159 
     160                System.out.println(((ConceptComplex) cobs.getConcept()).getHandler()); 
     161 
     162                /* 
     163                 * Step 8. Save the ComplexData to the file system. Assert that the 
     164                 * ComplexData is not null. 
    160165                 */ 
    161166                try { 
     
    164169                                mimes.add(mt); 
    165170                        } 
    166                          
    167                          
     171 
    168172                        File outputfile = new File("/home/bmckown/Desktop/test", 
    169173                                                   cobs.getComplexData().getTitle()); 
    170174                        String[] titles = cobs.getComplexData().getTitle().split("\\."); 
    171                         String mime = titles.length < 2 ? titles[0] : titles[titles.length-1]; 
     175                        String mime = titles.length < 2 ? titles[0] 
     176                                : titles[titles.length - 1]; 
    172177                        String MIME = mimes.contains(mime) ? mime : ImageHandler.MIME; 
    173                                          
    174                         BufferedImage outImg = (BufferedImage)cobs.getComplexData().getData(); 
     178 
     179                        BufferedImage outImg = (BufferedImage) cobs.getComplexData() 
     180                                                                   .getData(); 
     181                        System.out.println("Class.getCanononicalName(): " 
     182                                + outImg.getClass().getCanonicalName()); 
     183                        System.out.println("Class.getName(): " 
     184                                + outImg.getClass().getName()); 
    175185                        ImageIO.write(outImg, MIME, outputfile); 
    176186                } catch (IOException e2) { 
     
    190200                 */ 
    191201                List handlers = Context.getObsService().getComplexObsHandlers(); 
    192                 for (int i=0; i<handlers.size(); i++)
     202                for (int i = 0; i < handlers.size(); i++)
    193203                        System.out.println(handlers.get(i).getClass()); 
    194204                } 
     
    199209         * Test to get an existing complex obs. TODO: This should have an in memory 
    200210         * database. 
    201          *        
     211         *  
    202212         * @throws Exception 
    203213         */ 
    204         public void disable_testGetExistingComplexObs() throws Exception { 
    205  
     214        public void disable_testGetExistingImageComplexObs() throws Exception { 
     215 
     216                // Set obsId to the obsId of an existing complex obs. 
    206217                Integer obsId = Integer.valueOf(28879); 
     218 
    207219                Obs cobs = Context.getObsService().getComplexObs(obsId, null); 
    208                  
     220                // Make sure the obs exists. 
     221                assertNotNull(cobs); 
     222                assertNotNull(cobs.getObsId()); 
     223                // If this is not a complex obs, then fail. 
     224                assertTrue(cobs.isComplex()); 
     225 
    209226                try { 
    210227                        File outputfile = new File("/home/bmckown/Desktop/test", 
    211228                                                   cobs.getComplexData().getTitle()); 
    212                         BufferedImage outImg = (BufferedImage)cobs.getComplexData().getData(); 
    213                         ImageIO.write(outImg, MIME, outputfile); 
     229                        BufferedImage outImg = (BufferedImage) cobs.getComplexData() 
     230                                                                   .getData(); 
     231                        ImageIO.write(outImg, this.MIME, outputfile); 
    214232                } catch (IOException e2) { 
    215233                        System.out.println(e2); 
    216234                } 
     235                assertNotNull(cobs.getComplexData().getData()); 
     236        } 
     237 
     238        /** 
     239         * Test to purge an existing complex obs. Warning: This will indeed remove 
     240         * an existing file from the file system. 
     241         *  
     242         * @throws Exception 
     243         */ 
     244        public void disabled_testPurgeExistingComplexObs() throws Exception { 
     245 
     246                // Set obsId to the obsId of an existing complex obs. 
     247                Integer obsId = Integer.valueOf(28879); 
     248 
     249                Obs cobs = Context.getObsService().getComplexObs(obsId, null); 
     250                // Make sure the obs exists. 
     251                assertNotNull(cobs); 
     252                assertNotNull(cobs.getObsId()); 
     253                // If this is not a complex obs, then fail. 
     254                assertTrue(cobs.isComplex()); 
     255 
    217256                assertNotNull(cobs.getComplexData()); 
    218257 
     
    221260                        + cobs.getValueAsString(null)); 
    222261 
    223 /* 
    224                 String[] mimetypes = ImageIO.getWriterMIMETypes(); 
    225                 for (int i=0; i<mimetypes.length; i++) { 
    226                         System.out.println(mimetypes[i]); 
    227                 } 
    228                 System.out.println(); 
    229                 String[] readtypes = ImageIO.getWriterFormatNames(); 
    230                 for (int i=0; i<readtypes.length; i++) { 
    231                         System.out.println(readtypes[i]); 
    232                 } 
    233 */ 
    234         } 
    235          
     262                // Cheat to find out if the file exists. 
     263                // TODO: Should ObsService provide a convenience method for this? 
     264                File file = org.openmrs.obs.handler.ImageHandler.getComplexDataFile(cobs); 
     265                assertTrue(file.exists()); 
     266 
     267                // Delete the file. 
     268                Context.getObsService().deleteObs(cobs); 
     269 
     270                // Assert that the file was deleted. 
     271                assertFalse(file.exists()); 
     272        } 
     273 
    236274        /** 
    237          * Test to get an existing complex obs. TODO: This should have an in memory 
     275         * Test to save and get a complex obs. TODO: This should have an in memory 
    238276         * database.