Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
05/24/08 14:37:02 (8 months ago)
Author:
bwolfe
Message:

Merging api-refactoring to trunk [3595]:[4355]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs/trunk/src/api/org/openmrs/api/db/hibernate/HibernateObsDAO.java

    r4190 r4358  
    1515 
    1616import java.util.Date; 
    17 import java.util.HashSet; 
    1817import java.util.List; 
    19 import java.util.Set; 
    20 import java.util.Vector; 
    2118 
    2219import org.apache.commons.logging.Log; 
    2320import org.apache.commons.logging.LogFactory; 
    2421import org.hibernate.Criteria; 
    25 import org.hibernate.Query; 
    2622import org.hibernate.SessionFactory; 
     23import org.hibernate.criterion.DetachedCriteria; 
    2724import org.hibernate.criterion.Expression; 
    2825import org.hibernate.criterion.Order; 
     26import org.hibernate.criterion.Property; 
    2927import org.hibernate.criterion.Restrictions; 
    30 import org.openmrs.Cohort
     28import org.hibernate.criterion.Subqueries
    3129import org.openmrs.Concept; 
    3230import org.openmrs.Encounter; 
     
    3432import org.openmrs.MimeType; 
    3533import org.openmrs.Obs; 
     34import org.openmrs.Patient; 
    3635import org.openmrs.Person; 
    37 import org.openmrs.api.ObsService
     36import org.openmrs.User
    3837import org.openmrs.api.db.DAOException; 
    3938import org.openmrs.api.db.ObsDAO; 
    40 import org.openmrs.util.OpenmrsUtil
     39import org.openmrs.util.OpenmrsConstants.PERSON_TYPE
    4140 
    4241/** 
     42 * Hibernate specific Observation related functions 
     43 *  
     44 * This class should not be used directly.  All calls should go through the 
     45 * {@link org.openmrs.api.ObsService} methods. 
    4346 *  
    4447 * @see org.openmrs.api.db.ObsDAO 
     
    4952        protected final Log log = LogFactory.getLog(getClass()); 
    5053 
    51         /** 
    52          * Hibernate session factory 
    53          */ 
    54         private SessionFactory sessionFactory; 
    55  
    56         public HibernateObsDAO() { 
    57         } 
    58  
    59         /** 
    60          * Set session factory 
     54        protected SessionFactory sessionFactory; 
     55 
     56        /** 
     57         * Set session factory that allows us to connect to the database 
     58         * that Hibernate knows about. 
    6159         *  
    6260         * @param sessionFactory 
     
    6765 
    6866        /** 
    69          * @see org.openmrs.api.db.ObsDAO#createObs(org.openmrs.Obs) 
    70          */ 
    71         public void createObs(Obs obs) throws DAOException { 
    72                 sessionFactory.getCurrentSession().persist(obs); 
    73         } 
    74  
    75         /** 
    7667         * @see org.openmrs.api.db.ObsService#deleteObs(org.openmrs.Obs) 
    7768         */ 
     
    8677                return (Obs) sessionFactory.getCurrentSession().get(Obs.class, obsId); 
    8778        } 
    88          
    89         /** 
    90          * @see org.openmrs.api.db.ObsDAO#findObservations(java.lang.Integer, boolean, java.lang.Integer) 
     79 
     80        /** 
     81         * @see org.openmrs.api.db.ObsDAO#getMimeType(java.lang.Integer) 
     82         */ 
     83        public MimeType getMimeType(Integer mimeTypeId) throws DAOException { 
     84                return (MimeType)sessionFactory.getCurrentSession().get( 
     85                                MimeType.class, mimeTypeId); 
     86        } 
     87         
     88        /** 
     89         * @see org.openmrs.api.db.ObsDAO#getAllMimeTypes(boolean) 
    9190         */ 
    9291        @SuppressWarnings("unchecked") 
    93         public List<Obs> findObservations(Integer id, boolean includeVoided, Integer personType) 
    94                         throws DAOException { 
    95  
    96                 Criteria criteria = sessionFactory.getCurrentSession() 
    97                                 .createCriteria(Obs.class) 
    98                                 .createAlias("person", "p") 
    99                                 .createAlias("encounter", "e") 
    100                                 .add( 
    101                                         Expression.or( 
    102                                                 Expression.eq("p.personId", id), 
    103                                                 Expression.like("e.encounterId", id) 
    104                                         ) 
    105                                 ); 
    106  
    107                 if (includeVoided == false) { 
    108                         criteria.add(Expression.eq("voided", new Boolean(false))); 
    109                 } 
    110                  
    111                 getCriteriaPersonModifier(criteria, personType); 
    112  
    113                 return criteria.list(); 
    114         } 
    115  
    116         /* 
    117          * @see org.openmrs.api.db.ObsDAO#getMimeType(java.lang.Integer) 
    118          */ 
    119         public MimeType getMimeType(Integer mimeTypeId) throws DAOException { 
    120                 MimeType mimeType = new MimeType(); 
    121                 mimeType = (MimeType) sessionFactory.getCurrentSession().get( 
    122                                 MimeType.class, mimeTypeId); 
    123  
     92        public List<MimeType> getAllMimeTypes(boolean includeRetired) throws DAOException { 
     93                Criteria crit = sessionFactory.getCurrentSession() 
     94                                                .createCriteria(MimeType.class); 
     95                 
     96                if (includeRetired == false) 
     97                        crit.add(Expression.eq("retired", Boolean.FALSE)); 
     98                 
     99                return crit.list(); 
     100        } 
     101         
     102        /** 
     103         * @see org.openmrs.api.db.ObsSDAO#saveMimeType(org.openmrs.MimeType) 
     104         */ 
     105        public MimeType saveMimeType(MimeType mimeType) throws DAOException { 
     106                sessionFactory.getCurrentSession().saveOrUpdate(mimeType); 
    124107                return mimeType; 
    125108        } 
    126  
    127         /** 
    128          * @see org.openmrs.api.db.ObsDAO#getMimeTypes() 
    129          */ 
    130         @SuppressWarnings("unchecked") 
    131         public List<MimeType> getMimeTypes() throws DAOException { 
    132                 List<MimeType> mimeTypes = sessionFactory.getCurrentSession() 
    133                                 .createCriteria(MimeType.class).list(); 
    134  
    135                 return mimeTypes; 
    136         } 
    137  
    138         /** 
    139          * @see org.openmrs.api.db.ObsDAO#updateObs(org.openmrs.Obs) 
    140          */ 
    141         public void updateObs(Obs obs) throws DAOException { 
    142                 if (obs.getObsId() == null) 
    143                         createObs(obs); 
    144                 else { 
    145                         if (obs.hasGroupMembers()) { 
    146                                 // hibernate has a problem updating child collections 
    147                                 // if the parent object was already saved so we do it  
    148                                 // explicitly here 
    149                                 for (Obs member : obs.getGroupMembers()) 
    150                                         if (member.getObsId() == null) 
    151                                                 updateObs(member); 
    152                         } 
    153                          
    154                         Obs o = (Obs)sessionFactory.getCurrentSession().merge(obs); 
    155                 } 
    156         } 
    157  
    158         /** 
    159          * @see org.openmrs.api.db.ObsDAO#getObservations(org.openmrs.Concept, org.openmrs.Location, java.lang.String, java.lang.Integer) 
    160          */ 
    161         @SuppressWarnings("unchecked") 
    162         public List<Obs> getObservations(Concept c, Location location, String sort, Integer personType) { 
    163                 String q = "select obs " + getHqlPersonModifier(personType, "obs.location = :loc and obs.concept = :concept"); 
    164                 if (sort != null && sort.length() > 0) 
    165                         q += " order by :sort"; 
    166  
    167                 Query query = sessionFactory.getCurrentSession().createQuery(q); 
    168                 query.setParameter("loc", location); 
    169                 query.setParameter("concept", c); 
    170  
    171                 if (sort != null && sort.length() > 0) 
    172                         query.setParameter("sort", "obs." + sort); 
    173  
    174                 return query.list(); 
    175         } 
    176  
    177         /** 
    178          * @see org.openmrs.api.db.ObsDAO#getObservations(org.openmrs.Encounter) 
    179          */ 
    180         @SuppressWarnings("unchecked") 
    181         public Set<Obs> getObservations(Encounter whichEncounter) { 
    182                 Query query = sessionFactory.getCurrentSession().createQuery( 
    183                                 "from Obs obs where obs.encounter = :e"); 
    184                 query.setParameter("e", whichEncounter); 
    185                 Set<Obs> ret = new HashSet<Obs>(query.list()); 
    186  
    187                 return ret; 
    188         } 
    189  
    190         /** 
    191          * @see org.openmrs.api.db.ObsDAO#getObservations(org.openmrs.Person, org.openmrs.Concept) 
    192          */ 
    193         @SuppressWarnings("unchecked") 
    194         public Set<Obs> getObservations(Person who, Concept question, boolean includeVoided) { 
    195                 String s = "from Obs obs where obs.person = :p and obs.concept = :c"; 
    196                 if (!includeVoided) 
    197                         s += " and obs.voided = false"; 
    198                 Query query = sessionFactory.getCurrentSession().createQuery(s); 
    199                 query.setParameter("p", who); 
    200                 query.setParameter("c", question); 
    201                 Set<Obs> ret = new HashSet<Obs>(query.list()); 
    202  
    203                 return ret; 
    204         } 
    205  
    206         /** 
    207          * @see org.openmrs.api.db.ObsDAO#getLastNObservations(java.lang.Integer, org.openmrs.Person, org.openmrs.Concept) 
    208          */ 
    209         @SuppressWarnings("unchecked") 
    210         public List<Obs> getLastNObservations(Integer n, Person who, Concept question) { 
    211                 Query query = sessionFactory 
    212                                 .getCurrentSession() 
    213                                 .createQuery( 
    214                                                 "from Obs obs where obs.person = :p and obs.concept = :c order by obs.obsDatetime desc"); 
    215                 query.setParameter("p", who); 
    216                 query.setParameter("c", question); 
    217                 query.setMaxResults(n); 
    218  
    219                 return query.list(); 
    220         } 
    221  
    222         /** 
    223          * @see org.openmrs.api.db.ObsDAO#getObservations(org.openmrs.Concept, java.lang.String, java.lang.Integer) 
    224          */ 
    225         @SuppressWarnings("unchecked") 
    226         public List<Obs> getObservations(Concept question, String sort, Integer personType) { 
    227                  
    228                 if (sort == null || sort.equals("")) 
    229                         sort = "obsId"; 
    230                  
    231                 Query query = sessionFactory.getCurrentSession().createQuery( 
    232                                 "select obs " + getHqlPersonModifier(personType, "obs.concept = :c and obs.voided = false") +  
    233                                 " order by obs." + sort) 
    234                                 .setParameter("c", question); 
    235  
    236                 return query.list(); 
    237         } 
    238          
    239         /** 
    240          * @see org.openmrs.api.db.ObsDAO#getObservationsAnsweredByConcept(org.openmrs.Concept, java.lang.Integer) 
    241          */ 
    242         @SuppressWarnings("unchecked") 
    243         public List<Obs> getObservationsAnsweredByConcept(Concept answer, Integer personType) { 
    244                 Query query = sessionFactory.getCurrentSession().createQuery( 
    245                                 "select obs " + getHqlPersonModifier(personType, "obs.valueCoded = :c and obs.voided = false")) 
    246                                 .setParameter("c", answer); 
    247  
    248                 return query.list(); 
    249         } 
    250          
    251          
    252         /** 
    253          * @see org.openmrs.api.db.ObsDAO#getNumericAnswersForConcept(org.openmrs.Concept, java.lang.Boolean, java.lang.Integer) 
    254          */ 
    255         @SuppressWarnings("unchecked") 
    256         public List<Object[]> getNumericAnswersForConcept(Concept answer, Boolean sortByValue, Integer personType) { 
    257                  
    258                 String sort = "obs.obsDatetime desc"; 
    259                 if (sortByValue) 
    260                         sort = "obs.valueNumeric asc"; 
    261                  
    262                 String sql = ""; 
    263                 sql += "select obs.obsId, obs.obsDatetime, obs.valueNumeric "; 
    264                 sql += getHqlPersonModifier(personType, "obs.concept = :c and obs.valueNumeric is not null and obs.voided = false"); 
    265                 sql += " order by " + sort; 
    266                  
    267                 Query query = sessionFactory.getCurrentSession().createQuery(sql) 
    268                                 .setParameter("c", answer); 
    269  
    270                 return query.list(); 
    271         } 
    272          
    273         /** 
     109         
     110        /** 
     111         * @see org.openmrs.api.db.ObsDAO#deleteMimeType(org.openmrs.MimeType) 
     112         */ 
     113        public void deleteMimeType(MimeType mimeType) throws DAOException { 
     114                sessionFactory.getCurrentSession().delete(mimeType); 
     115        } 
     116         
     117        /** 
     118         * @see org.openmrs.api.db.ObsDAO#saveObs(org.openmrs.Obs) 
     119         */ 
     120        public Obs saveObs(Obs obs) throws DAOException { 
     121                if (obs.hasGroupMembers() && obs.getObsId() != null) { 
     122                        // hibernate has a problem updating child collections 
     123                        // if the parent object was already saved so we do it  
     124                        // explicitly here 
     125                        for (Obs member : obs.getGroupMembers()) 
     126                                if (member.getObsId() == null) 
     127                                        saveObs(member); 
     128                } 
     129                 
     130                sessionFactory.getCurrentSession().saveOrUpdate(obs); 
     131                 
     132                return obs; 
     133        } 
     134 
     135        /** 
     136     * @see org.openmrs.api.db.ObsDAO#getObservations(java.util.List, java.util.List, java.util.List, java.util.List, java.util.List, java.util.List, java.lang.String, java.lang.Integer, java.lang.Integer, java.util.Date, java.util.Date, boolean) 
     137     */ 
     138    @SuppressWarnings("unchecked") 
     139    public List<Obs> getObservations(List<Person> whom, 
     140            List<Encounter> encounters, List<Concept> questions, 
     141            List<Concept> answers, List<PERSON_TYPE> personTypes, 
     142            List<Location> locations, List<String> sortList, Integer mostRecentN, 
     143            Integer obsGroupId, Date fromDate, Date toDate, 
     144            boolean includeVoidedObs) throws DAOException { 
     145         
     146        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Obs.class, "obs"); 
     147         
     148        if (whom.size() > 0) 
     149                criteria.add(Restrictions.in("person", whom)); 
     150                 
     151        if (encounters.size() > 0) 
     152                criteria.add(Restrictions.in("encounter", encounters)); 
     153         
     154        if (questions.size() > 0) 
     155                criteria.add(Restrictions.in("concept", questions)); 
     156         
     157        if (answers.size() > 0) 
     158                criteria.add(Restrictions.in("valueCoded", answers)); 
     159         
     160        getCriteriaPersonModifier(criteria, personTypes); 
     161         
     162        if (locations.size() > 0) 
     163                criteria.add(Restrictions.in("location", locations)); 
     164         
     165        // TODO add an option for each sort item to be asc/desc 
     166        if (sortList.size() > 0) { 
     167                for (String sort : sortList) { 
     168                        if (sort != null && !sort.isEmpty()) 
     169                                criteria.addOrder(Order.desc(sort)); 
     170                } 
     171        } 
     172         
     173        if (mostRecentN > 0) 
     174                criteria.setMaxResults(mostRecentN); 
     175         
     176        if (obsGroupId != null) { 
     177                criteria.createAlias("obsGroup", "og"); 
     178                criteria.add(Restrictions.eq("og.obsId", obsGroupId)); 
     179        } 
     180         
     181        if ( fromDate != null ) 
     182                        criteria.add(Restrictions.gt("obsDatetime", fromDate)); 
     183                 
     184                if ( toDate != null ) 
     185                        criteria.add(Restrictions.lt("obsDatetime", toDate)); 
     186                 
     187                if ( includeVoidedObs == false ) 
     188                        criteria.add(Restrictions.eq("voided", false)); 
     189         
     190            return criteria.list(); 
     191    } 
     192         
     193        /** 
     194         * Convenience method that adds an expression to the given <code>criteria</code> 
     195         * according to what types of person objects is wanted 
    274196         *  
     197         * @param criteria 
    275198         * @param personType 
    276          * @return 
    277          */ 
    278         private String getHqlPersonModifier(Integer personType, String whereClause) { 
    279                 String from = "from Obs obs"; 
    280                 List<String> whereClauses = new Vector<String>(); 
    281                  
    282                 if (whereClause != null && whereClause.length() > 0) { 
    283                         whereClauses.add("(" + whereClause + ")"); 
    284                 } 
    285                  
    286                 if ((personType & ObsService.PATIENT) == ObsService.PATIENT) { 
    287                         from += ", Patient p"; 
    288                         whereClauses.add("p.patientId = obs.person.personId"); 
    289                 } 
    290                  
    291                 if ((personType & ObsService.USER) == ObsService.USER) { 
    292                         from += ", User u"; 
    293                         whereClauses.add("u.userId = obs.person.personId"); 
    294                 } 
    295                  
    296                 if ((personType & ObsService.PERSON) == ObsService.PERSON) { 
    297                         // all observations are already on person's.  Limit to non-patient and non-users here? 
    298                         //from += ", Person person"; 
    299                         //whereClauses.add("person.personId = obs.personId"); 
    300                 } 
    301                  
    302                 if (whereClauses.size() > 0) 
    303                         from = from + " where " + OpenmrsUtil.join(whereClauses, " and "); 
    304                  
    305                 return from + " "; 
    306                  
    307         } 
    308          
    309         private void getCriteriaPersonModifier(Criteria criteria, Integer personType) { 
    310                 if ((personType & ObsService.PATIENT) == ObsService.PATIENT) { 
    311                         criteria.createAlias("Patient", "p"); 
    312                         criteria.add(Restrictions.eqProperty("obs.person.personId", "p.patientId")); 
    313                 } 
    314                  
    315                 if ((personType & ObsService.USER) == ObsService.USER) { 
    316                         criteria.createAlias("User", "u"); 
    317                         criteria.add(Restrictions.eqProperty("obs.person.personId", "u.userId")); 
    318                 } 
    319                  
    320                 if ((personType & ObsService.PERSON) == ObsService.PERSON) { 
     199         * @return the given criteria (for chaining) 
     200         */ 
     201        private Criteria getCriteriaPersonModifier(Criteria criteria, List<PERSON_TYPE> personTypes) { 
     202                if (personTypes.contains(PERSON_TYPE.PATIENT)) { 
     203                        DetachedCriteria crit = DetachedCriteria.forClass(Patient.class, "patient") 
     204                                .setProjection(Property.forName("patientId")); 
     205                        criteria.add(Subqueries.propertyIn("person.personId", crit)); 
     206                } 
     207                 
     208                if (personTypes.contains(PERSON_TYPE.USER)) { 
     209                        DetachedCriteria crit = DetachedCriteria.forClass(User.class, "user") 
     210                                .setProjection(Property.forName("userId")); 
     211                        criteria.add(Subqueries.propertyIn("person.personId", crit)); 
     212                } 
     213                 
     214                if (personTypes.contains(PERSON_TYPE.PERSON)) { 
    321215                        // all observations are already on person's.  Limit to non-patient and non-users here? 
    322216                        //criteria.createAlias("Person", "person"); 
     
    324218                } 
    325219                 
    326         } 
    327  
    328         /** 
    329          * @see org.openmrs.api.db.ObsDAO#getObservations(org.openmrs.Person) 
    330          */ 
    331         @SuppressWarnings("unchecked") 
    332         public Set<Obs> getObservations(Person who, boolean includeVoided) { 
    333                 String s = "from Obs obs where obs.person = :p"; 
    334                 if (!includeVoided) 
    335                         s += " and obs.voided = false"; 
    336                 Query query = sessionFactory.getCurrentSession().createQuery(s); 
    337                 query.setParameter("p", who); 
    338                 Set<Obs> ret = new HashSet<Obs>(query.list()); 
    339  
    340                 return ret; 
    341         } 
    342  
    343         /** 
    344          * @see org.openmrs.api.db.ObsDAO#getVoidedObservations() 
    345          */ 
    346         @SuppressWarnings("unchecked") 
    347         public List<Obs> getVoidedObservations() throws DAOException { 
    348                 Query query = sessionFactory 
    349                                 .getCurrentSession() 
    350                                 .createQuery( 
    351                                                 "from Obs obs where obs.voided = true order by obs.dateVoided desc"); 
    352  
    353                 return query.list(); 
    354         } 
    355  
    356         /** 
    357          * @see org.openmrs.api.db.ObsDAO#findObsByGroupId(java.lang.Integer) 
    358          * @deprecated -- should use obs.getGroupMembers 
    359          */ 
    360         @SuppressWarnings("unchecked") 
    361         public List<Obs> findObsByGroupId(Integer obsGroupId) throws DAOException { 
    362                 Criteria criteria = sessionFactory.getCurrentSession().createCriteria( 
    363                                 Obs.class); 
    364                 criteria.createAlias("obsGroup", "og"); 
    365                 criteria.add(Restrictions.eq("og.obsId", obsGroupId)); 
    366                 return criteria.list(); 
    367         } 
    368  
    369         /** 
    370          * @see org.openmrs.api.ObsService#getObservations(java.util.List<org.openmrs.Concept>, java.util.Date, java.util.Data, boolean) 
    371          */ 
    372         @SuppressWarnings("unchecked") 
    373         public List<Obs> getObservations(Cohort patients, List<Concept> concepts, Date fromDate, Date toDate) 
    374                         throws DAOException { 
    375                 Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Obs.class); 
    376                 if ( patients != null ) { 
    377                         if (patients.getMemberIds() != null) { 
    378                                 criteria.add(Restrictions.in("person.personId", patients.getMemberIds())); 
    379                         } 
    380                 } 
    381                 if ( concepts != null ) { 
    382                         criteria.add(Restrictions.in("concept", concepts)); 
    383                 } 
    384                 if ( fromDate != null ) { 
    385                         criteria.add(Restrictions.gt("obsDatetime", fromDate)); 
    386                 } 
    387                 if ( toDate != null ) { 
    388                         criteria.add(Restrictions.lt("obsDatetime", toDate)); 
    389                 } 
    390                 criteria.addOrder(Order.desc("obsDatetime")); 
    391                 return (List<Obs>)criteria.list(); 
    392         } 
    393  
    394         /** 
    395          * @see org.openmrs.api.ObsService#getObservations(java.util.List<org.openmrs.Concept>, java.util.Date, java.util.Data, boolean) 
    396          */ 
    397         @SuppressWarnings("unchecked") 
    398         public List<Obs> getObservations(List<Concept> concepts, Date fromDate, Date toDate, boolean includeVoided) 
    399                         throws DAOException { 
    400                 Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Obs.class); 
    401                 if ( concepts != null ) { 
    402                         criteria.add(Restrictions.in("concept", concepts)); 
    403                 } 
    404                 if ( fromDate != null ) { 
    405                         criteria.add(Restrictions.gt("obsDatetime", fromDate)); 
    406                 } 
    407                 if ( toDate != null ) { 
    408                         criteria.add(Restrictions.lt("obsDatetime", toDate)); 
    409                 } 
    410                 if ( !includeVoided ) { 
    411                         criteria.add(Restrictions.eq("voided", false)); 
    412                 } 
    413                 criteria.addOrder(Order.desc("obsDatetime")); 
    414                 return (List<Obs>)criteria.list(); 
    415         } 
    416  
     220                return criteria; 
     221        } 
     222         
    417223}