Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
05/28/08 05:16:52 (8 months ago)
Author:
bmckown
Message:

complex_obs branch: Merging trunk to complex-obs [3891] [4378] TODO: sqldiff.

Files:

Legend:

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

    • Property svn:eol-style set to CRLF
    r3685 r4417  
    1818import java.sql.PreparedStatement; 
    1919import java.sql.SQLException; 
    20 import java.util.Collection; 
    2120import java.util.HashSet; 
    22 import java.util.LinkedHashSet; 
    2321import java.util.List; 
    2422import java.util.Set; 
     
    2826import org.apache.commons.logging.LogFactory; 
    2927import org.hibernate.Criteria; 
    30 import org.hibernate.ObjectNotFoundException
     28import org.hibernate.Hibernate
    3129import org.hibernate.Query; 
    3230import org.hibernate.SessionFactory; 
    3331import org.hibernate.criterion.Expression; 
     32import org.hibernate.criterion.LogicalExpression; 
    3433import org.hibernate.criterion.MatchMode; 
    3534import org.hibernate.criterion.Order; 
    36 import org.hibernate.criterion.Projections; 
    3735import org.hibernate.criterion.Restrictions; 
     36import org.openmrs.Location; 
    3837import org.openmrs.Patient; 
    3938import org.openmrs.PatientIdentifier; 
     
    4948 
    5049/** 
    51  * Patient related database hibernate specific methods 
     50 * Hibernate specific database methods for the PatientService 
     51 *  
     52 * @see org.openmrs.api.context.Context 
     53 * @see org.openmrs.api.db.PatientDAO 
     54 * @see org.openmrs.api.PatientService 
    5255 */ 
    5356public class HibernatePatientDAO implements PatientDAO { 
     
    5962         */ 
    6063        private SessionFactory sessionFactory; 
    61          
    62         public HibernatePatientDAO() { } 
    6364         
    6465        /** 
     
    7273         
    7374        /** 
    74          * @see org.openmrs.api.db.PatientService#getPatient(java.lang.Long) 
     75         * @see org.openmrs.api.PatientService#getPatient(java.lang.Long) 
    7576         */ 
    7677        public Patient getPatient(Integer patientId) { 
    77                 try { 
    78                         return (Patient) sessionFactory.getCurrentSession().get(Patient.class, patientId); 
    79                 } catch (ObjectNotFoundException ex) { 
    80                         return null; 
    81                 } 
    82         } 
    83          
    84  
    85         /** 
    86          * @see org.openmrs.api.db.PatientDAO#createPatient(org.openmrs.Patient) 
    87          */ 
    88         public Patient createPatient(Patient patient) throws DAOException { 
     78                return (Patient) sessionFactory.getCurrentSession() 
     79                                               .get(Patient.class, patientId); 
     80        } 
     81         
     82 
     83        /** 
     84         * @see org.openmrs.api.db.PatientDAO#savePatient(org.openmrs.Patient) 
     85         */ 
     86        public Patient savePatient(Patient patient) throws DAOException { 
     87                if (patient.getPatientId() == null) { 
     88                        // if we're saving a new patient, just do the normal thing  
     89                        // and rows in the person and patient table will be created by  
     90                        // hibernate 
    8991                sessionFactory.getCurrentSession().saveOrUpdate(patient); 
    90                 //sessionFactory.getCurrentSession().refresh(patient); 
    91                  
    9292                return patient; 
    93         } 
    94  
    95  
    96         /** 
    97          * @see org.openmrs.api.db.PatientDAO#updatePatient(org.openmrs.Patient) 
    98          */ 
    99         public Patient updatePatient(Patient patient) throws DAOException { 
    100                 if (patient.getPatientId() == null) 
    101                         // TODO this check/call should be moved up from the DB layer to the API layer 
    102                         return createPatient(patient); 
    103                 else { 
     93                } else { 
     94                        // if we're updating a patient, its possible that a person 
     95                        // row exists but a patient row does not. hibernate does not deal 
     96                        // with this correctly right now, so we must create a dummy row 
     97                        // in the patient table before saving 
    10498                         
    10599                        // Check to make sure we have a row in the patient table already. 
    106                         // If we don't have a row, create it so Hibernate doesn't bung things up 
    107                         Object obj = sessionFactory.getCurrentSession().get(Patient.class, patient.getPatientId()); 
     100                        // If we don't have a row, create it so Hibernate doesn't bung 
     101                        // things up 
     102                        Object obj = sessionFactory.getCurrentSession() 
     103                                                   .get(Patient.class, 
     104                                                        patient.getPatientId()); 
    108105                        if (!(obj instanceof Patient)) { 
    109106                                insertPatientStub(patient); 
    110107                        } 
    111108                         
    112                         patient = (Patient)sessionFactory.getCurrentSession().merge(patient); 
    113                         //sessionFactory.getCurrentSession().update("org.openmrs.Person", (Object)patient); 
     109                        // do a merge here because of the previous get 2 lines up 
     110                        patient = (Patient) sessionFactory.getCurrentSession() 
     111                                                          .merge(patient); 
     112 
    114113                        return patient; 
    115114                } 
     
    133132         
    134133                        ps.executeUpdate(); 
    135                 } 
    136                 catch (SQLException e) { 
     134                } catch (SQLException e) { 
    137135                        log.warn("SQL Exception while trying to create a patient stub", e); 
    138136                } 
     
    141139        } 
    142140 
    143         @SuppressWarnings("unchecked") 
    144         public Set<Patient> getPatientsByIdentifier(String identifier, boolean includeVoided) throws DAOException { 
    145                 Query query; 
    146                  
    147                 String sql = "select patient from Patient patient, PersonName name join patient.identifiers ids where ids.identifier = :id and patient.patientId = name.person.personId"; 
    148                 String order = " order by name.givenName asc, name.middleName asc, name.familyName asc"; 
    149                  
    150                 if (includeVoided) { 
    151                         query = sessionFactory.getCurrentSession().createQuery(sql + order); 
    152                         query.setString("id", identifier); 
    153                 } 
    154                 else { 
    155                         query = sessionFactory.getCurrentSession().createQuery(sql + " and patient.voided = :void" + order); 
    156                         query.setString("id", identifier); 
    157                         query.setBoolean("void", includeVoided); 
    158                 } 
    159                  
    160                 Set<Patient> returnSet = new LinkedHashSet<Patient>(); 
    161                 returnSet.addAll(query.list()); 
    162                  
    163                 return returnSet; 
    164         } 
    165          
    166          
    167  
    168         /** 
    169          * @see org.openmrs.api.db.PatientDAO#getPatientsByIdentifierPattern(java.lang.String, boolean) 
    170          */ 
    171         @SuppressWarnings("unchecked") 
    172         public Collection<Patient> getPatientsByIdentifierPattern(String identifier, boolean includeVoided) throws DAOException { 
    173                  
    174                 Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientIdentifier.class); 
    175                 criteria.setProjection(Projections.property("patient")); 
    176                  
    177                 AdministrationService adminService = Context.getAdministrationService(); 
    178                 String regex = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, ""); 
    179                  
    180                 // if the regex is empty, default to a simple "like" search 
    181                 if (regex.equals("")) { 
    182                         String prefix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_PREFIX, ""); 
    183                         String suffix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SUFFIX, ""); 
    184                         StringBuffer likeString = new StringBuffer(prefix).append(identifier).append(suffix); 
    185                         criteria.add(Expression.like("identifier", likeString.toString())); 
    186                 } 
    187                 // if the regex is present, search on that 
    188                 else { 
    189                         regex = regex.replace("@SEARCH@", identifier); 
    190                         criteria.add(Restrictions.sqlRestriction("identifier regexp '" + regex + "'")); 
    191                 } 
    192                  
    193                 if (includeVoided == false) { 
    194                         criteria.createAlias("patient", "pat"); 
    195                         criteria.add(Restrictions.eq("pat.voided", false)); 
    196                 } 
    197                  
    198                 criteria.setFirstResult(0); 
    199                 criteria.setMaxResults(getMaximumSearchResults()); 
    200                  
    201                 return criteria.list(); 
    202         } 
    203  
    204         @SuppressWarnings("unchecked") 
    205         public Collection<Patient> getPatientsByName(String name, boolean includeVoided) throws DAOException { 
    206                 //TODO simple name search to start testing, will need to make "real" name search 
    207                 //              i.e. split on whitespace, guess at first/last name, etc 
    208                 // TODO return the matched name instead of the primary name 
    209                 //   possible solution: "select new" org.openmrs.PatientListItem and return a list of those 
    210                  
    211                 name = name.replaceAll("  ", " "); 
    212                 name = name.replace(", ", " "); 
    213                 String[] names = name.split(" "); 
    214                  
    215                 if (log.isDebugEnabled()) 
    216                         log.debug("name: " + name); 
    217                  
    218                 Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class).createAlias("names", "name"); 
    219                 for (String n : names) { 
    220                         if (n != null && n.length() > 0) { 
    221                                 criteria.add(Expression.or( 
    222                                         Expression.like("name.familyName", n, MatchMode.START), 
    223                                         Expression.or( 
    224                                                 Expression.like("name.middleName", n, MatchMode.START), 
    225                                                 Expression.like("name.givenName", n, MatchMode.START) 
    226                                                 ) 
    227                                         ) 
    228                                 ); 
    229                         } 
    230                 } 
    231                  
    232                 if (includeVoided == false) 
    233                         criteria.add(Expression.eq("voided", new Boolean(false))); 
    234  
     141        /** 
     142     * @see org.openmrs.api.db.PatientDAO#getPatients(java.lang.String, java.lang.String, java.util.List) 
     143     */ 
     144        @SuppressWarnings("unchecked") 
     145    public List<Patient> getPatients(String name, String identifier, 
     146            List<PatientIdentifierType> identifierTypes) throws DAOException { 
     147             
     148        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class); 
     149         
     150        criteria.createAlias("names", "name"); 
     151                criteria.add(Expression.eq("name.voided", false)); 
    235152                criteria.addOrder(Order.asc("name.givenName")); 
    236153                criteria.addOrder(Order.asc("name.middleName")); 
    237154                criteria.addOrder(Order.asc("name.familyName")); 
    238                  
     155         
     156        if (name != null) { 
     157                // TODO simple name search to start testing, will need to make "real" 
     158                // name search 
     159                // i.e. split on whitespace, guess at first/last name, etc 
     160                // TODO return the matched name instead of the primary name 
     161                // possible solution: "select new" org.openmrs.PatientListItem and 
     162                // return a list of those 
     163 
     164                name = name.replaceAll("  ", " "); 
     165                name = name.replace(", ", " "); 
     166                String[] names = name.split(" "); 
     167                 
     168                        // TODO add junit test for searching on voided patient names 
     169                 
     170                String nameSoFar = names[0]; 
     171                for (int i=0 ; i < names.length ; i++) { 
     172                        String n = names[i]; 
     173                        if (n != null && n.length() > 0) { 
     174                                LogicalExpression oneNameSearch = getNameSearch(n); 
     175                                LogicalExpression searchExpression = oneNameSearch; 
     176                                if(i>0){ 
     177                                        nameSoFar += " " + n; 
     178                                        LogicalExpression fullNameSearch = getNameSearch(nameSoFar); 
     179                                        searchExpression = Expression.or(oneNameSearch, fullNameSearch); 
     180                                } 
     181                                criteria.add(searchExpression); 
     182                } 
     183                } 
     184                 
     185        } 
     186         
     187        // do the restriction on either identifier string or types 
     188        if (identifier != null || identifierTypes.size() > 0) { 
     189         
     190                // TODO add junit test for searching on voided identifiers 
     191 
     192                // add the join on the identifiers table 
     193                criteria.createAlias("identifiers", "ids"); 
     194                criteria.add(Expression.eq("ids.voided", false)); 
     195                 
     196                // do the identifier restriction 
     197                if (identifier != null) { 
     198                                AdministrationService adminService = Context.getAdministrationService(); 
     199                                String regex = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, ""); 
     200                 
     201                        // if the regex is empty, default to a simple "like" search or if  
     202                        // we're in hsql world, also only do the simple like search (because 
     203                        // hsql doesn't know how to deal with 'regexp' 
     204                        if (regex.equals("") || HibernateUtil.isHSQLDialect(sessionFactory)) { 
     205                                        String prefix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_PREFIX, ""); 
     206                                        String suffix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SUFFIX, ""); 
     207                                StringBuffer likeString = new StringBuffer(prefix).append(identifier) 
     208                                                                                  .append(suffix); 
     209                                criteria.add(Expression.like("ids.identifier", likeString.toString())); 
     210                                } 
     211                                // if the regex is present, search on that 
     212                                else { 
     213                                        regex = regex.replace("@SEARCH@", identifier); 
     214                                criteria.add(Restrictions.sqlRestriction("identifier regexp ?", regex, Hibernate.STRING)); 
     215                                } 
     216                        } 
     217                 
     218                // TODO add a junit test for patientIdentifierType restrictions 
     219                 
     220                // do the type restriction 
     221                if (identifierTypes.size() > 0) { 
     222                        criteria.add(Expression.in("ids.identifierType", identifierTypes)); 
     223                        } 
     224                } 
     225                 
     226                // TODO add junit test for searching on voided patients 
     227 
     228        // make sure the patient object isn't voided 
     229        criteria.add(Expression.eq("voided", false)); 
     230                 
     231        // restricting the search to the max search results value 
    239232                criteria.setFirstResult(0); 
    240233                criteria.setMaxResults(getMaximumSearchResults()); 
     
    244237 
    245238        /** 
    246          * @see org.openmrs.api.db.PatientService#deletePatient(org.openmrs.Patient) 
     239     * Auto generated method comment 
     240     *  
     241     * @param name 
     242     * @return 
     243         */ 
     244    private LogicalExpression getNameSearch(String name){ 
     245        // this criteria is essentially: 
     246                // where voided = false && name in [familyName, middleName, givenName] 
     247                return Expression.and(Expression.eq("name.voided", false), 
     248                                                             Expression.or(Expression.like("name.familyName", name, MatchMode.START), 
     249                                                                           Expression.or(Expression.like("name.middleName", name, MatchMode.START), 
     250                                                                                         Expression.like("name.givenName", name, MatchMode.START)))); 
     251        } 
     252 
     253        /** 
     254     * @see org.openmrs.api.db.PatientDAO#getAllPatients(boolean) 
     255         */ 
     256        @SuppressWarnings("unchecked") 
     257    public List<Patient> getAllPatients(boolean includeVoided) 
     258            throws DAOException { 
     259        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class); 
     260         
     261        if (includeVoided == false) 
     262                criteria.add(Expression.eq("voided", false)); 
     263         
     264        return criteria.list(); 
     265    } 
     266                 
     267        /** 
     268         * @see org.openmrs.api.PatientService#purgePatientIdentifierType(org.openmrs.PatientIdentifierType) 
     269     * @see org.openmrs.api.db.PatientDAO#deletePatientIdentifierType(org.openmrs.PatientIdentifierType) 
     270     */ 
     271    public void deletePatientIdentifierType( 
     272            PatientIdentifierType patientIdentifierType) throws DAOException { 
     273        sessionFactory.getCurrentSession().delete(patientIdentifierType); 
     274        } 
     275         
     276        /** 
     277     * @see org.openmrs.api.db.PatientDAO#getPatientIdentifiers(java.lang.String, java.util.List, java.util.List, java.util.List, java.lang.Boolean) 
     278     * @see org.openmrs.api.PatientService#getPatientIdentifiers(java.lang.String, java.util.List, java.util.List, java.util.List, java.lang.Boolean) 
     279         */ 
     280        @SuppressWarnings("unchecked") 
     281    public List<PatientIdentifier> getPatientIdentifiers(String identifier, 
     282            List<PatientIdentifierType> patientIdentifierTypes, 
     283            List<Location> locations, List<Patient> patients, 
     284            Boolean isPreferred) 
     285            throws DAOException { 
     286        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientIdentifier.class); 
     287         
     288        // TODO add junit test for not getting voided 
     289        // make sure the patient object isn't voided 
     290        criteria.add(Expression.eq("voided", false)); 
     291         
     292        // TODO add junit test for getting by identifier (and for not getting by partial here) 
     293        if (identifier != null) 
     294                criteria.add(Expression.eq("identifier", identifier)); 
     295         
     296        // TODO add junit test for getting by identifier type 
     297        if (patientIdentifierTypes.size() > 0) 
     298                        criteria.add(Expression.in("identifierType", patientIdentifierTypes)); 
     299         
     300        // TODO add junit test for getting by patients 
     301        if (patients.size() > 0) 
     302                        criteria.add(Expression.in("patient", patients)); 
     303         
     304        // TODO add junit test for getting by null/true/false isPreferred 
     305        if (isPreferred != null) 
     306                criteria.add(Expression.eq("preferred", isPreferred)); 
     307                 
     308        return criteria.list(); 
     309        } 
     310         
     311        /** 
     312     * @see org.openmrs.api.db.PatientDAO#savePatientIdentifierType(org.openmrs.PatientIdentifierType) 
     313         */ 
     314    public PatientIdentifierType savePatientIdentifierType( 
     315            PatientIdentifierType patientIdentifierType) throws DAOException { 
     316        sessionFactory.getCurrentSession().saveOrUpdate(patientIdentifierType); 
     317        return patientIdentifierType; 
     318        } 
     319         
     320        /** 
     321         * @see org.openmrs.api.PatientService#deletePatient(org.openmrs.Patient) 
    247322         */ 
    248323        public void deletePatient(Patient patient) throws DAOException { 
    249324                HibernatePersonDAO.deletePersonAndAttributes(sessionFactory, patient); 
    250325        } 
    251  
    252         /** 
    253          * @see org.openmrs.api.db.PatientService#getPatientIdentifiers(org.openmrs.PatientIdentifierType) 
    254          */ 
    255         @SuppressWarnings("unchecked") 
    256         public List<PatientIdentifier> getPatientIdentifiers(PatientIdentifierType pit) throws DAOException { 
    257                 List<PatientIdentifier> patientIdentifiers = sessionFactory.getCurrentSession().createQuery("from PatientIdentifier p where p.identifierType = :pit and p.voided = false") 
    258                                 .setParameter("pit", pit) 
    259                                 .list(); 
    260                  
    261                 return patientIdentifiers; 
    262         } 
    263          
    264         /** 
    265          * @see org.openmrs.api.db.PatientService#getPatientIdentifiers(java.lang.String,org.openmrs.PatientIdentifierType) 
    266          */ 
    267         @SuppressWarnings("unchecked") 
    268         public List<PatientIdentifier> getPatientIdentifiers(String identifier, PatientIdentifierType pit) throws DAOException { 
    269                 List<PatientIdentifier> ids; 
    270                 ids = sessionFactory.getCurrentSession().createQuery("from PatientIdentifier p where p.identifierType = :pit and p.identifier = :id and p.voided = false") 
    271                                 .setParameter("pit", pit) 
    272                                 .setString("id", identifier) 
    273                                 .list(); 
    274                  
    275                 return ids; 
    276         } 
    277          
    278         /** 
    279          * Only updates the identifier type at the moment 
    280          *  
    281          *  
    282          * @see org.openmrs.api.db.PatientService#updatePatientIdentifier(org.openmrs.PatientIdentifier) 
    283          */ 
    284         public void updatePatientIdentifier(PatientIdentifier pi) throws DAOException { 
    285                 log.debug("type: " + pi.getIdentifierType().getName()); 
    286                 sessionFactory.getCurrentSession().createQuery("update PatientIdentifier p set p.identifierType = :pit where p.patient = :pat and p.identifier = :id and p.location = :loc") 
    287                         .setParameter("pit", pi.getIdentifierType()) 
    288                         .setParameter("pat", pi.getPatient()) 
    289                         .setParameter("id", pi.getIdentifier()) 
    290                         .setParameter("loc", pi.getLocation()) 
    291                         .executeUpdate(); 
    292         } 
    293          
    294         /** 
    295          * @see org.openmrs.api.db.PatientService#getPatientIdentifierType(java.lang.Integer) 
    296          */ 
    297         public PatientIdentifierType getPatientIdentifierType(Integer patientIdentifierTypeId) throws DAOException { 
    298                 PatientIdentifierType patientIdentifierType = (PatientIdentifierType) sessionFactory.getCurrentSession().get(PatientIdentifierType.class, patientIdentifierTypeId); 
    299                  
    300                 return patientIdentifierType; 
    301         } 
    302          
    303         /** 
    304          * @see org.openmrs.api.db.PatientService#getPatientIdentifierType(java.lang.String) 
    305          */ 
    306         public PatientIdentifierType getPatientIdentifierType(String name) throws DAOException { 
    307                 PatientIdentifierType ret = (PatientIdentifierType) sessionFactory.getCurrentSession().createQuery("from PatientIdentifierType t where t.name = :name") 
    308                         .setString("name", name) 
    309                         .uniqueResult(); 
    310  
    311                 return ret; 
     326                 
     327        /** 
     328         * @see org.openmrs.api.PatientService#getPatientIdentifierType(java.lang.Integer) 
     329         */ 
     330        public PatientIdentifierType getPatientIdentifierType( 
     331                Integer patientIdentifierTypeId) throws DAOException { 
     332                return (PatientIdentifierType) sessionFactory.getCurrentSession() 
     333                                                             .get(PatientIdentifierType.class, 
     334                                                                  patientIdentifierTypeId); 
     335        } 
     336         
     337        /** 
     338         * @see org.openmrs.api.db.PatientDAO#getAllPatientIdentifierTypes(boolean) 
     339         */ 
     340        @SuppressWarnings("unchecked") 
     341    public List<PatientIdentifierType> getAllPatientIdentifierTypes(boolean includeRetired) 
     342                throws DAOException { 
     343                 
     344                // TODO test this method 
     345                 
     346                Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientIdentifierType.class); 
     347                criteria.addOrder(Order.asc("name")); 
     348                 
     349                if (includeRetired == false) 
     350                        criteria.add(Expression.eq("retired", false)); 
     351 
     352                return criteria.list(); 
    312353        }        
    313354 
    314355        /** 
    315          * @see org.openmrs.api.db.PatientService#getPatientIdentifierTypes() 
    316          */ 
    317         @SuppressWarnings("unchecked") 
    318         public List<PatientIdentifierType> getPatientIdentifierTypes() throws DAOException { 
    319                 List<PatientIdentifierType> patientIdentifierTypes = sessionFactory.getCurrentSession().createQuery("from PatientIdentifierType p order by p.name").list(); 
    320                  
    321                 return patientIdentifierTypes; 
    322         } 
    323  
    324         /** 
    325          * @see org.openmrs.api.db.PatientService#getTribe() 
     356     * @see org.openmrs.api.db.PatientDAO#getPatientIdentifierTypes(java.lang.String, java.lang.String, java.lang.Boolean, java.lang.Boolean) 
     357         */ 
     358        @SuppressWarnings("unchecked") 
     359    public List<PatientIdentifierType> getPatientIdentifierTypes(String name, 
     360            String format, Boolean required, Boolean hasCheckDigit) 
     361            throws DAOException { 
     362        // TODO test this method 
     363                 
     364                Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientIdentifierType.class); 
     365                criteria.addOrder(Order.asc("name")); 
     366                 
     367                if (name != null) 
     368                        criteria.add(Expression.eq("name", name)); 
     369                 
     370                if (format != null) 
     371                        criteria.add(Expression.eq("format", format)); 
     372                 
     373                if (required != null) 
     374                        criteria.add(Expression.eq("required", required)); 
     375                 
     376                if (hasCheckDigit!= null) 
     377                        criteria.add(Expression.eq("checkDigit", hasCheckDigit)); 
     378                 
     379                criteria.add(Expression.eq("retired", false)); 
     380                 
     381                return criteria.list(); 
     382        } 
     383 
     384        /** 
     385         * @see org.openmrs.api.PatientService#getTribe() 
     386         * @deprecated tribe will be moved to patient attribute 
    326387         */ 
    327388        public Tribe getTribe(Integer tribeId) throws DAOException { 
    328                 Tribe tribe = (Tribe)sessionFactory.getCurrentSession().get(Tribe.class, tribeId); 
     389                Tribe tribe = (Tribe) sessionFactory.getCurrentSession() 
     390                                                    .get(Tribe.class, tribeId); 
    329391                 
    330392                return tribe; 
     
    332394         
    333395        /** 
    334          * @see org.openmrs.api.db.PatientService#getTribes() 
     396         * @see org.openmrs.api.PatientService#getTribes() 
     397         * @deprecated tribe will be moved to patient attribute 
    335398         */ 
    336399        @SuppressWarnings("unchecked") 
    337400        public List<Tribe> getTribes() throws DAOException { 
    338                 List<Tribe> tribes = sessionFactory.getCurrentSession().createQuery("from Tribe t order by t.name asc").list(); 
     401                List<Tribe> tribes = sessionFactory.getCurrentSession() 
     402                                                   .createQuery("from Tribe t order by t.name asc") 
     403                                                   .list(); 
    339404                 
    340405                return tribes; 
     
    342407         
    343408        /** 
    344          * @see org.openmrs.api.db.PatientService#findTribes() 
     409         * @see org.openmrs.api.PatientService#findTribes() 
     410         * @deprecated tribe will be moved to patient attribute 
    345411         */ 
    346412        @SuppressWarnings("unchecked") 
    347413        public List<Tribe> findTribes(String s) throws DAOException { 
    348                 Criteria crit = sessionFactory.getCurrentSession().createCriteria(Tribe.class); 
     414                Criteria crit = sessionFactory.getCurrentSession() 
     415                                              .createCriteria(Tribe.class); 
    349416                crit.add(Expression.like("name", s, MatchMode.START)); 
    350417                crit.addOrder(Order.asc("name")); 
     
    353420        } 
    354421         
    355         /** @see org.openmrs.api.db.PatientService#findDuplicatePatients(java.util.Set<String>) */ 
    356         @SuppressWarnings("unchecked") 
    357         public List<Patient> findDuplicatePatients(Set<String> attributes) { 
     422        /** 
     423         * @see org.openmrs.api.db.PatientDAO#getDuplicatePatientsByAttributes(java.util.List) 
     424         */ 
     425        @SuppressWarnings("unchecked") 
     426        public List<Patient> getDuplicatePatientsByAttributes(List<String> attributes) { 
    358427                List<Patient> patients = new Vector<Patient>(); 
    359428                 
     
    398467                                        where += " and p1." + s + " = p2." + s; 
    399468                                        orderBy += "p1." + s + ", "; 
    400                                 } 
    401                                 else if (personFieldNames.contains(s)) { 
     469                                } else if (personFieldNames.contains(s)) { 
    402470                                        if (!select.contains("Person ")) { 
    403471                                                select += ", Person person1, Person person2"; 
     
    406474                                        where += " and person1." + s + " = person2." + s; 
    407475                                        orderBy += "person1." + s + ", "; 
    408                                 } 
    409                                 else if (personNameFieldNames.contains(s)) { 
     476                                } else if (personNameFieldNames.contains(s)) { 
    410477                                        if (!select.contains("PersonName")) { 
    411478                                                select += ", PersonName pn1, PersonName pn2"; 
     
    414481                                        where += " and pn1." + s + " = pn2." + s; 
    415482                                        orderBy += "pn1." + s + ", "; 
    416                                 } 
    417                                 else if (identifierFieldNames.contains(s)) { 
     483                                } else if (identifierFieldNames.contains(s)) { 
    418484                                        if (!select.contains("PatientIdentifier")) { 
    419485                                                select += ", PatientIdentifier pi1, PatientIdentifier pi2"; 
     
    422488                                        where += " and pi1." + s + " = pi2." + s; 
    423489                                        orderBy += "pi1." + s + ", "; 
    424                                 } 
    425                                 else 
     490                                } else 
    426491                                        log.warn("Unidentified attribute: " + s); 
    427492                        } 
     
    432497                        select = select + where + orderBy; 
    433498                         
    434                         Query query = sessionFactory.getCurrentSession().createQuery(select); 
     499                        Query query = sessionFactory.getCurrentSession() 
     500                                                    .createQuery(select); 
    435501                 
    436502                        patients = query.list(); 
     
    438504                 
    439505                /* 
    440                 if (attributes.size() > 0) { 
    441                         String select = "select p from Patient p"; 
    442                         String where  = " where 1=1 "; 
    443                         String groupBy= " group by "; 
    444                         String having = " having count(p.patientId) > 1"; 
    445                          
    446                         Class patient = Patient.class; 
    447                         Set<String> patientFieldNames = new HashSet<String>(patient.getDeclaredFields().length); 
    448                         for (Field f : patient.getDeclaredFields()){ 
    449                                 patientFieldNames.add(f.getName()); 
    450                                 log.debug(f.getName()); 
    451                         } 
    452                          
    453                         Class patientName = PersonName.class; 
    454                         Set<String> patientNameFieldNames = new HashSet<String>(patientName.getDeclaredFields().length); 
    455                         for (Field f : patientName.getDeclaredFields()){ 
    456                                 patientNameFieldNames.add(f.getName()); 
    457                                 log.debug(f.getName()); 
    458                         } 
    459                          
    460                         Class identifier = PatientIdentifier.class; 
    461                         Set<String> identifierFieldNames = new HashSet<String>(identifier.getDeclaredFields().length); 
    462                         for (Field f : identifier.getDeclaredFields()){ 
    463                                 identifierFieldNames.add(f.getName()); 
    464                                 log.debug(f.getName()); 
    465                         } 
    466                          
    467                         for (String s : attributes) { 
    468                                 if (patientFieldNames.contains(s)) { 
    469                                         groupBy += "p." + s + ", "; 
    470                                 } 
    471                                 else if (patientNameFieldNames.contains(s)) { 
    472                                         if (!select.contains("PersonName")) { 
    473                                                 select += ", PersonName pn"; 
    474                                                 where += "and p = pn.patient "; 
    475                                         } 
    476                                         groupBy += "pn." + s + ", "; 
    477                                 } 
    478                                 else if (identifierFieldNames.contains(s)) { 
    479                                         if (!select.contains("PatientIdentifier")) { 
    480                                                 select += ", PatientIdentifier pi"; 
    481                                                 where += "and p = pi.patient "; 
    482                                         } 
    483                                         groupBy += "pi." + s + ", "; 
    484                                 } 
    485                                 else 
    486                                         log.warn("Unidentified attribute: " + s); 
    487                         } 
    488                          
    489                         int index = groupBy.lastIndexOf(", "); 
    490                         groupBy = groupBy.substring(0, index); 
    491                          
    492                         select = select + where + groupBy + having; 
    493                          
    494                         Query query = session.createQuery(select); 
    495                  
    496                         patients = query.list(); 
    497                 } 
     506                 * if (attributes.size() > 0) { String select = "select p from Patient 
     507                 * p"; String where = " where 1=1 "; String groupBy= " group by "; 
     508                 * String having = " having count(p.patientId) > 1"; 
     509                 *  
     510                 * Class patient = Patient.class; Set<String> patientFieldNames = new 
     511                 * HashSet<String>(patient.getDeclaredFields().length); for (Field f : 
     512                 * patient.getDeclaredFields()){ patientFieldNames.add(f.getName()); 
     513                 * log.debug(f.getName()); } 
     514                 *  
     515                 * Class patientName = PersonName.class; Set<String> 
     516                 * patientNameFieldNames = new HashSet<String>(patientName.getDeclaredFields().length); 
     517                 * for (Field f : patientName.getDeclaredFields()){ 
     518                 * patientNameFieldNames.add(f.getName()); log.debug(f.getName()); } 
     519                 *  
     520                 * Class identifier = PatientIdentifier.class; Set<String> 
     521                 * identifierFieldNames = new HashSet<String>(identifier.getDeclaredFields().length); 
     522                 * for (Field f : identifier.getDeclaredFields()){ 
     523                 * identifierFieldNames.add(f.getName()); log.debug(f.getName()); } 
     524                 *  
     525                 * for (String s : attributes) { if (patientFieldNames.contains(s)) { 
     526                 * groupBy += "p." + s + ", "; } else if 
     527                 * (patientNameFieldNames.contains(s)) { if 
     528                 * (!select.contains("PersonName")) { select += ", PersonName pn"; where += 
     529                 * "and p = pn.patient "; } groupBy += "pn." + s + ", "; } else if 
     530                 * (identifierFieldNames.contains(s)) { if 
     531                 * (!select.contains("PatientIdentifier")) { select += ", 
     532                 * PatientIdentifier pi"; where += "and p = pi.patient "; } groupBy += 
     533                 * "pi." + s + ", "; } else log.warn("Unidentified attribute: " + s); } 
     534                 *  
     535                 * int index = groupBy.lastIndexOf(", "); groupBy = groupBy.substring(0, 
     536                 * index); 
     537                 *  
     538                 * select = select + where + groupBy + having; 
     539                 *  
     540                 * Query query = session.createQuery(select); 
     541                 *  
     542                 * patients = query.list(); } 
    498543                */ 
    499544                 
     
    508553        private Integer getMaximumSearchResults() { 
    509554                try { 
    510                         return Integer.valueOf( 
    511                                 Context.getAdministrationService().getGlobalProperty( 
    512                                                 OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MAX_RESULTS, 
    513                                                 "1000") 
    514                                         ); 
    515                 } 
    516                 catch (Exception e) { 
    517                         log.warn("Unable to convert the global property " + OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MAX_RESULTS + 
    518                                  "to a valid integer. Returning the default 1000"); 
     555                        return Integer.valueOf(Context.getAdministrationService() 
     556