Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
05/11/08 23:52:52 (7 months ago)
Author:
bmckown
Message:

complex_obs branch: Changed to mapping the ComplexObsHandlers instead of using ConceptComplexHandler class.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs/branches/complex_obs/src/api/org/openmrs/api/impl/ObsServiceImpl.java

    r4129 r4166  
    1717import java.util.Date; 
    1818import java.util.HashSet; 
     19import java.util.LinkedHashMap; 
    1920import java.util.List; 
    2021import java.util.Locale; 
     22import java.util.Map; 
    2123import java.util.Set; 
    2224import java.util.SortedSet; 
     
    2729import org.apache.commons.logging.LogFactory; 
    2830import org.openmrs.Concept; 
     31import org.openmrs.ConceptComplex; 
    2932import org.openmrs.Encounter; 
    3033import org.openmrs.Location; 
     
    4245import org.openmrs.obs.ComplexObsHandler; 
    4346import org.openmrs.reporting.PatientSet; 
     47import org.openmrs.util.OpenmrsClassLoader; 
    4448import org.openmrs.util.OpenmrsConstants; 
    4549 
     
    5559        private ObsDAO dao; 
    5660 
     61        /** 
     62         * Report handlers that have been registered. 
     63         *  
     64         * This is filled via {@link #setHandlers(Map)} and spring's applicationContext-service.xml object 
     65         */ 
     66        private static Map<Class<? extends ComplexObsHandler>, ComplexObsHandler> handlers = null; 
     67         
     68         
    5769        public ObsServiceImpl() { 
    5870        } 
     
    8294         */ 
    8395        public void createObs(Obs obs) throws APIException { 
    84                 ComplexObsHandler handler = ComplexData.getHandler(obs); 
     96                ComplexObsHandler handler = getHandler(obs); 
    8597                if (null != handler) { 
    8698                        handler.saveObs(obs); 
     
    167179        public Obs getObs(Integer obsId) throws APIException { 
    168180                return getObsDAO().getObs(obsId); 
     181        } 
     182         
     183        /** 
     184         * @see org.openmrs.api.ObsService#getComplexObs(java.lang.Integer) 
     185         */ 
     186        public Obs getComplexObs(Integer obsId) throws APIException { 
     187                Obs obs = getObsDAO().getObs(obsId); 
     188                if (obs.isComplex()) { 
     189                        return getHandler(obs).getObs(obs); 
     190                } 
     191                return obs; 
     192        } 
     193         
     194        /** 
     195         * @see org.openmrs.api.ObsService#getComplexData(java.lang.Integer) 
     196         */ 
     197        public ComplexData getComplexData(Integer obsId) throws APIException { 
     198                return this.getComplexObs(obsId).getComplexData(); 
    169199        } 
    170200 
     
    218248                } 
    219249                // Handle complex obs. 
    220                 ComplexObsHandler handler = ComplexData.getHandler(obs); 
     250                ComplexObsHandler handler = getHandler(obs); 
    221251                if (null != handler) { 
    222252                        handler.saveObs(obs); 
     
    418448                return getObsDAO().getObservations(patients, concepts, fromDate, toDate); 
    419449        } 
     450         
     451        /** 
     452         * Static convenience method to get the ComplexObsHandler associated with a 
     453         * complex Obs. Returns the ComplexObsHandler. Returns null if the 
     454         * Obs.isComplexObs() is false or there is an error instantiating the 
     455         * handler class.  
     456         *  
     457         * TODO: Should ComplexObsHandler be an abstract class and 
     458         * put this method there instead? 
     459         *  
     460         * @param obs A complex Obs. 
     461         * @return ComplexObsHandler for the complex Obs. or null on error. 
     462         */ 
     463        public ComplexObsHandler getHandler(Obs obs) { 
     464                if (obs.getConcept().isComplex()) { 
     465                        return getHandler( ((ConceptComplex)obs.getConcept()).getHandler() ); 
     466                } 
     467                return null; 
     468                 
     469                /* 
     470                String className = ((ConceptComplex) obs.getConcept()).getHandler(); 
     471                ComplexObsHandler handler = null; 
     472                try { 
     473                        Class c = OpenmrsClassLoader.getInstance().loadClass(className); 
     474                        handler = (ComplexObsHandler)c.newInstance(); 
     475                        //Class handlerClass = Class.forName(className); 
     476                        //handler = (ComplexObsHandler)className.getClass().newInstance(); 
     477                        //handler = (ComplexObsHandler) handlerClass.newInstance(); 
     478                } catch (ClassNotFoundException cnfe) { 
     479                        return null; 
     480                } catch (InstantiationException ie) { 
     481                        return null; 
     482                } catch (IllegalAccessException iae) { 
     483                        return null; 
     484                } 
     485                return handler; 
     486                */ 
     487        } 
     488 
     489        /** 
     490         * @see org.openmrs.api.ObsService#getHandler(java.lang.Class) 
     491         */ 
     492        public ComplexObsHandler getHandler(Class<? extends ComplexObsHandler> clazz) { 
     493                try { 
     494                        return handlers.get(clazz); 
     495                } catch (Exception ex) { 
     496                        log.error("Failed to get report renderer for " + clazz, ex); 
     497                        return null; 
     498                } 
     499        } 
     500         
     501        public List<ComplexObsHandler> getComplexObsHandlers() { 
     502                //List handlers = new ArrayList<ComplexObsHandler>(); 
     503                //handlers.addAll(getHandlers().values()); 
     504                return new ArrayList<ComplexObsHandler>(getHandlers().values()); 
     505                //return handlers; 
     506        } 
     507         
     508 
     509        /** 
     510         * @see org.openmrs.api.ObsService#getHandler(java.lang.String) 
     511         */ 
     512        public ComplexObsHandler getHandler(String className) { 
     513                try { 
     514                        return handlers.get(OpenmrsClassLoader.getInstance().loadClass(className)); 
     515                } catch (Exception ex) { 
     516                        log.error("Failed to get report renderer for " + className, ex); 
     517                        return null; 
     518                } 
     519    } 
     520         
     521        /** 
     522         * ADDs handlers...doesn't replace them. 
     523         * @see org.openmrs.api.ObsService#setHandlers(Map) 
     524         */ 
     525        public void setHandlers(Map<Class<? extends ComplexObsHandler>, ComplexObsHandler> newHandlers) throws APIException { 
     526                for (Map.Entry<Class<? extends ComplexObsHandler>, ComplexObsHandler> entry : newHandlers.entrySet()) { 
     527                        registerHandler(entry.getKey(), entry.getValue()); 
     528                } 
     529        } 
     530         
     531        /** 
     532         * @see org.openmrs.api.ObsService#getHandlers() 
     533         */ 
     534        public Map<Class<? extends ComplexObsHandler>, ComplexObsHandler> getHandlers() throws APIException { 
     535                if (handlers == null) 
     536                        handlers = new LinkedHashMap<Class<? extends ComplexObsHandler>, ComplexObsHandler>(); 
     537                 
     538                return handlers; 
     539        } 
     540 
     541        /** 
     542         * @see org.openmrs.api.ObsService#registerHandler(java.lang.Class, org.openmrs.obs.ComplexObsHandler) 
     543         */ 
     544        public void registerHandler(Class<? extends ComplexObsHandler> handlerClass, ComplexObsHandler handler) throws APIException { 
     545                getHandlers().put(handlerClass, handler); 
     546        } 
     547         
     548        /** 
     549         * @see org.openmrs.api.ObsService#registerHandler(java.lang.String) 
     550         */ 
     551        @SuppressWarnings("unchecked") 
     552    public void registerHandler(String handlerClass) throws APIException { 
     553                try { 
     554                Class loadedClass = OpenmrsClassLoader.getInstance().loadClass(handlerClass); 
     555                registerHandler(loadedClass, (ComplexObsHandler)loadedClass.newInstance()); 
     556                 
     557        } catch (Exception e) { 
     558                throw new APIException("Unable to load and instantiate handler", e); 
     559        } 
     560        } 
     561         
     562        /** 
     563         * @see org.openmrs.api.ObsService#removeHandler(java.lang.Class) 
     564         */ 
     565        public void removeHandler(Class<? extends ComplexObsHandler> renderingClass) { 
     566                handlers.remove(renderingClass); 
     567        } 
    420568 
    421569}