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/HibernatePersonDAO.java

    r4158 r4358  
    1414package org.openmrs.api.db.hibernate; 
    1515 
    16 import java.util.Date; 
    1716import java.util.LinkedHashSet; 
    1817import java.util.List; 
    1918import java.util.Set; 
    20 import java.util.Vector; 
    2119 
    2220import org.apache.commons.logging.Log; 
     
    2422import org.hibernate.Criteria; 
    2523import org.hibernate.Query; 
    26 import org.hibernate.Session; 
    2724import org.hibernate.SessionFactory; 
    2825import org.hibernate.criterion.Expression; 
    2926import org.hibernate.criterion.MatchMode; 
    3027import org.hibernate.criterion.Order; 
     28import org.hibernate.type.StringType; 
    3129import org.openmrs.Person; 
    3230import org.openmrs.PersonAddress; 
     
    3634import org.openmrs.Relationship; 
    3735import org.openmrs.RelationshipType; 
    38 import org.openmrs.api.context.Context; 
    3936import org.openmrs.api.db.DAOException; 
    4037import org.openmrs.api.db.PersonDAO; 
    4138 
     39/** 
     40 * Hibernate specific Person database methods 
     41 *  
     42 * This class should not be used directly.  All database calls 
     43 * should go through the Service layer.   
     44 *  
     45 * Use: 
     46 * <code> 
     47 *   PersonService ps = Context.getPersonService(); 
     48 *   ps.getPeople("name", false); 
     49 * </code> 
     50 *  
     51 * @see org.openmrs.api.db.PersonDAO 
     52 * @see org.openmrs.api.PersonService 
     53 * @see org.openmrs.api.context.Context 
     54 */ 
    4255public class HibernatePersonDAO implements PersonDAO { 
    4356 
     
    4861         */ 
    4962        private SessionFactory sessionFactory; 
    50          
    51         public HibernatePersonDAO() { 
    52         } 
    5363         
    5464        /** 
     
    6272         
    6373        /** 
    64          * @see org.openmrs.api.dao.PersonDAO#getSimilarPeople(java.lang.String,java.lang.Integer,java.lang.String) 
     74         * @see org.openmrs.api.PersonService#getSimilarPeople(java.lang.String,java.lang.Integer,java.lang.String) 
     75         * @see org.openmrs.api.db.PersonDAO#getSimilarPeople(java.lang.String,java.lang.Integer,java.lang.String) 
    6576         */ 
    6677        @SuppressWarnings("unchecked") 
     
    176187        } 
    177188         
     189        /** 
     190         * @see org.openmrs.api.db.PersonDAO#getPeople(java.lang.String, java.lang.Boolean) 
     191         */ 
    178192        @SuppressWarnings("unchecked") 
    179         public List<Person> findPeople(String name, boolean includeVoided) { 
     193        public List<Person> getPeople(String name, Boolean dead) { 
    180194                name = name.replace(", ", " "); 
    181195                String[] names = name.split(" "); 
    182                  
    183                 log.debug("name: " + name); 
    184196                 
    185197                Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class); 
     
    201213                } 
    202214                                 
    203                 if (includeVoided == false) 
    204                         criteria.add(Expression.eq("personVoided", false)); 
     215                criteria.add(Expression.eq("personVoided", false)); 
     216                 
     217                if (dead != null) 
     218                        criteria.add(Expression.eq("dead", dead)); 
    205219                 
    206220                criteria.addOrder(Order.asc("personId")); 
    207221                 
    208                 List returnList = new Vector(); 
    209                 returnList = criteria.list(); 
    210                  
    211                 return returnList; 
    212         } 
    213                  
    214                  
    215         /** 
    216          * @see org.openmrs.api.db.PersonService#getPerson(java.lang.Long) 
     222                return criteria.list(); 
     223        } 
     224                 
     225                 
     226        /** 
     227         * @see org.openmrs.api.PersonService#getPerson(java.lang.Integer) 
     228         * @see org.openmrs.api.db.PersonDAO#getPerson(java.lang.Integer) 
    217229         */ 
    218230        public Person getPerson(Integer personId) { 
     
    222234         
    223235        /** 
    224          * @see org.openmrs.api.db.PersonDAO#createPersonAttributeType(org.openmrs.PersonAttributeType) 
    225          */ 
    226         public void createPersonAttributeType(PersonAttributeType type) { 
    227                 sessionFactory.getCurrentSession().save(type); 
    228         } 
    229  
    230         /** 
     236         * @see org.openmrs.api.PersonService#deletePersonAttributeType(org.openmrs.PersonAttributeType) 
    231237         * @see org.openmrs.api.db.PersonDAO#deletePersonAttributeType(org.openmrs.PersonAttributeType) 
    232238         */ 
     
    236242 
    237243        /** 
    238          * @see org.openmrs.api.db.PersonDAO#getPersonAttributeTypes() 
    239          */ 
    240         @SuppressWarnings("unchecked") 
    241         public List<PersonAttributeType> getPersonAttributeTypes() { 
    242                 return sessionFactory.getCurrentSession() 
    243                                      .createQuery("from PersonAttributeType type order by type.name") 
    244                                      .list(); 
    245         } 
    246  
    247         /** 
    248          * @see org.openmrs.api.db.PersonDAO#updatePersonAttributeType(org.openmrs.PersonAttributeType) 
    249          */ 
    250         public void updatePersonAttributeType(PersonAttributeType type) { 
    251                 sessionFactory.getCurrentSession().merge(type); 
    252         } 
    253  
    254         /** 
     244         * @see org.openmrs.api.PersonService#savePersonAttributeType(org.openmrs.PersonAttributeType) 
     245         * @see org.openmrs.api.db.PersonDAO#savePersonAttributeType(org.openmrs.PersonAttributeType) 
     246         */ 
     247        public PersonAttributeType savePersonAttributeType(PersonAttributeType type) { 
     248                sessionFactory.getCurrentSession().saveOrUpdate(type); 
     249                return type; 
     250        } 
     251 
     252        /** 
     253         * @see org.openmrs.api.PersonService#getPersonAttributeType(java.lang.Integer) 
    255254         * @see org.openmrs.api.db.PersonDAO#getPersonAttributeType(java.lang.Integer) 
    256255         */ 
    257256        public PersonAttributeType getPersonAttributeType(Integer typeId) { 
    258                 return (PersonAttributeType) sessionFactory.getCurrentSession() 
    259                                                            .get(PersonAttributeType.class, 
    260                                                                 typeId); 
    261         } 
    262  
     257                return (PersonAttributeType) sessionFactory.getCurrentSession().get(PersonAttributeType.class, typeId); 
     258                } 
     259         
     260        /** 
     261         * @see org.openmrs.api.PersonService#getPersonAttribute(java.lang.Integer) 
     262         * @see org.openmrs.api.db.PersonDAO#getPersonAttribute(java.lang.Integer) 
     263         */ 
    263264        public PersonAttribute getPersonAttribute(Integer id) { 
    264                 return (PersonAttribute) sessionFactory.getCurrentSession() 
    265                                                        .get(PersonAttribute.class, id); 
    266         } 
    267  
    268         /** 
    269          * @see org.openmrs.api.db.PersonDAO#getPersonAttributeType(java.lang.String) 
    270          */ 
    271         public PersonAttributeType getPersonAttributeType(String typeName) { 
    272                 Session session = sessionFactory.getCurrentSession(); 
    273                 Query q = session.createQuery("from PersonAttributeType t where t.name = :name") 
    274                                                 .setString("name", typeName); 
    275                 return (PersonAttributeType)q.uniqueResult(); 
    276         } 
    277          
    278         /** 
    279          * @see org.openmrs.api.db.PatientService#getRelationship() 
     265                return (PersonAttribute)sessionFactory.getCurrentSession().get(PersonAttribute.class, id); 
     266        } 
     267         
     268        /** 
     269         * @see org.openmrs.api.PersonService#getAllPersonAttributeTypes(boolean) 
     270     * @see org.openmrs.api.db.PersonDAO#getAllPersonAttributeTypes(boolean) 
     271     */ 
     272    public List<PersonAttributeType> getAllPersonAttributeTypes( 
     273            boolean includeRetired) throws DAOException { 
     274        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PersonAttributeType.class, "r"); 
     275         
     276        if (!includeRetired) { 
     277                        criteria.add(Expression.eq("retired", false)); 
     278                } 
     279         
     280        criteria.addOrder(Order.asc("name")); 
     281         
     282        return criteria.list(); 
     283        } 
     284         
     285        /** 
     286     * @see org.openmrs.api.db.PersonDAO#getPersonAttributeTypes(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Boolean) 
     287         */ 
     288    public List<PersonAttributeType> getPersonAttributeTypes(String exactName, 
     289            String format, Integer foreignKey, Boolean searchable) 
     290            throws DAOException { 
     291        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PersonAttributeType.class, "r"); 
     292        criteria.add(Expression.eq("retired", false)); 
     293         
     294        if (exactName != null) 
     295                criteria.add(Expression.eq("name", exactName)); 
     296         
     297        if (format != null) 
     298                criteria.add(Expression.eq("format", format)); 
     299         
     300        if (foreignKey != null) 
     301                criteria.add(Expression.eq("foreignKey", foreignKey)); 
     302         
     303        if (searchable != null) 
     304                criteria.add(Expression.eq("searchable", format)); 
     305                                 
     306        return criteria.list(); 
     307        } 
     308         
     309        /** 
     310         * @see org.openmrs.api.PatientService#getRelationship() 
     311         * @see org.openmrs.api.db.PersonDAO#getRelationship(java.lang.Integer) 
    280312         */ 
    281313        public Relationship getRelationship(Integer relationshipId) 
     
    289321         
    290322        /** 
    291          * @see org.openmrs.api.db.PatientService#getRelationships() 
    292          */ 
    293         @SuppressWarnings("unchecked") 
    294         public List<Relationship> getRelationships() throws DAOException { 
    295                 List<Relationship> relationships = sessionFactory.getCurrentSession() 
    296                                                                  .createQuery("from Relationship r order by r.relationshipId asc") 
    297                                                                  .list(); 
    298                  
    299                 return relationships; 
    300         } 
    301          
    302         /** 
    303          * @see org.openmrs.api.db.PatientService#getRelationships(org.openmrs.Person) 
    304          */ 
    305         @SuppressWarnings("unchecked") 
    306         public List<Relationship> getRelationships(Person person, boolean showVoided) 
    307                         throws DAOException { 
    308                 Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Relationship.class, "r") 
    309                 .add(Expression.or(Expression.eq("personA", person), Expression.eq("personB", person))); 
    310                  
    311                 if (!showVoided) { 
    312                         criteria.add(Expression.eq("voided", showVoided)); 
    313                 } 
    314                  
    315                 return criteria.list(); 
    316         } 
    317  
    318         /** 
    319          * @see org.openmrs.api.db.PatientService#getRelationshipType(java.lang.Integer) 
     323         * @see org.openmrs.api.PersonService#getAllRelationships(boolean) 
     324     * @see org.openmrs.api.db.PersonDAO#getAllRelationships(boolean) 
     325         */ 
     326    public List<Relationship> getAllRelationships(boolean includeVoided) 
     327            throws DAOException { 
     328        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Relationship.class, "r"); 
     329         
     330        if (!includeVoided) { 
     331                        criteria.add(Expression.eq("voided", false)); 
     332                } 
     333         
     334        return criteria.list(); 
     335    } 
     336 
     337        /** 
     338         * @see org.openmrs.api.PersonService#getRelationships(org.openmrs.Person, org.openmrs.Person, org.openmrs.RelationshipType) 
     339     * @see org.openmrs.api.db.PersonDAO#getRelationships(org.openmrs.Person, org.openmrs.Person, org.openmrs.RelationshipType) 
     340     */ 
     341    public List<Relationship> getRelationships(Person fromPerson, 
     342            Person toPerson, RelationshipType relType) { 
     343        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Relationship.class, "r"); 
     344         
     345        if (fromPerson != null) 
     346                criteria.add(Expression.eq("personA", fromPerson)); 
     347        if (toPerson != null) 
     348                criteria.add(Expression.eq("personB", toPerson)); 
     349        if (relType != null) 
     350                criteria.add(Expression.eq("relationshipType", relType)); 
     351         
     352                criteria.add(Expression.eq("voided", false)); 
     353         
     354        return criteria.list(); 
     355    } 
     356 
     357        /** 
     358         * @see org.openmrs.api.PatientService#getRelationshipType(java.lang.Integer) 
     359         * @see org.openmrs.api.db.PatientDAO#getRelationshipType(java.lang.Integer) 
    320360         */ 
    321361        public RelationshipType getRelationshipType(Integer relationshipTypeId) 
     
    327367                 
    328368                return relationshipType; 
    329  
    330         } 
    331          
    332         public RelationshipType findRelationshipType(String relationshipTypeName) 
    333                 throws DAOException { 
    334                 RelationshipType ret = (RelationshipType) sessionFactory.getCurrentSession() 
    335                                                                         .createQuery("from RelationshipType t where CONCAT(t.aIsToB, CONCAT('/', t.bIsToA)) = :toString order by weight") 
    336                                                                         .setString("toString", 
    337                                                                                    relationshipTypeName) 
    338                                         .uniqueResult(); 
    339                  
    340                 return ret; 
    341         } 
    342  
    343         /** 
    344          * @see org.openmrs.api.db.PatientService#getRelationshipTypes() 
     369        } 
     370 
     371        /** 
     372         * @see org.openmrs.api.PersonService#getRelationshipTypes(java.lang.String, java.lang.Boolean) 
     373         * @see org.openmrs.api.db.PersonDAO#getRelationshipTypes(java.lang.String, java.lang.Boolean) 
    345374         */ 
    346375        @SuppressWarnings("unchecked") 
    347         public List<RelationshipType> getRelationshipTypes() throws DAOException { 
    348                 List<RelationshipType> relationshipTypes; 
    349                 relationshipTypes = sessionFactory.getCurrentSession() 
    350                                                   .createQuery("from RelationshipType t order by t.weight") 
    351                                                   .list(); 
    352                  
    353                 return relationshipTypes; 
    354         } 
    355          
    356         /** 
    357          * @see org.openmrs.api.db.PersonService#createRelationshipType(org.openmrs.RelationshipType) 
    358          */ 
    359         public void createRelationshipType(RelationshipType relationshipType) 
    360                 throws DAOException { 
    361                 relationshipType.setCreator(Context.getAuthenticatedUser()); 
    362                 relationshipType.setDateCreated(new Date()); 
    363                 sessionFactory.getCurrentSession().save(relationshipType); 
    364         } 
    365          
    366         /** 
    367          * @see org.openmrs.api.db.PersonService#updateRelationshipType(org.openmrs.RelationshipType) 
    368          */ 
    369         public void updateRelationshipType(RelationshipType relationshipType) 
    370                 throws DAOException { 
    371                 if (relationshipType.getRelationshipTypeId() == null) 
    372                         createRelationshipType(relationshipType); 
    373                 else 
     376    public List<RelationshipType> getRelationshipTypes(String relationshipTypeName, Boolean preferred) throws DAOException { 
     377                 
     378                Criteria criteria = sessionFactory.getCurrentSession().createCriteria(RelationshipType.class, "t"); 
     379                criteria.add(Expression.eq("retired", false)); 
     380                criteria.add(Expression.sql("CONCAT(t.aIsToB, CONCAT('/', t.bIsToA))", relationshipTypeName, new StringType())); 
     381         
     382                if (preferred != null) 
     383                        criteria.add(Expression.eq("preferred", preferred)); 
     384                 
     385                return criteria.list(); 
     386        } 
     387 
     388        /** 
     389         * @see org.openmrs.api.PatientService#getRelationshipTypes() 
     390         * @see org.openmrs.api.db.PersonDAO#getAllRelationshipTypes(boolean) 
     391         */ 
     392        @SuppressWarnings("unchecked") 
     393        public List<RelationshipType> getAllRelationshipTypes(boolean includeRetired) throws DAOException { 
     394                 
     395                Criteria criteria = sessionFactory.getCurrentSession().createCriteria(RelationshipType.class, "t"); 
     396                 
     397                if (!includeRetired) 
     398                        criteria.add(Expression.eq("retired", false)); 
     399                 
     400                criteria.addOrder(Order.asc("weight")); 
     401         
     402                return criteria.list(); 
     403        } 
     404         
     405        /** 
     406         * @see org.openmrs.api.PersonService#saveRelationshipType(org.openmrs.RelationshipType) 
     407         * @see org.openmrs.api.db.PersonDAO#saveRelationshipType(org.openmrs.RelationshipType) 
     408         */ 
     409        public RelationshipType saveRelationshipType(RelationshipType relationshipType) throws DAOException { 
    374410                        sessionFactory.getCurrentSession().saveOrUpdate(relationshipType); 
    375         } 
    376  
    377         /** 
    378          * @see org.openmrs.api.db.PersonService#deleteRelationshipType(org.openmrs.RelationshipType) 
     411                return relationshipType; 
     412        } 
     413 
     414        /** 
     415         * @see org.openmrs.api.PersonService#deleteRelationshipType(org.openmrs.RelationshipType) 
     416         * @see org.openmrs.api.db.PersonDAO#deleteRelationshipType(org.openmrs.RelationshipType) 
    379417         */ 
    380418        public void deleteRelationshipType(RelationshipType relationshipType) 
     
    384422         
    385423        /** 
    386          * @see org.openmrs.api.db.PersonDAO#createPerson(org.openmrs.Person) 
    387          * 
    388         public void createPerson(Person person) throws DAOException { 
    389                 sessionFactory.getCurrentSession().save(person); 
    390         } 
    391          */ 
    392          
    393         /** 
    394          * @see org.openmrs.api.db.PersonDAO#createPerson(org.openmrs.Person) 
    395          */ 
    396         public Person createPerson(Person person) throws DAOException { 
    397                 return (Person)sessionFactory.getCurrentSession().merge(person); 
    398         } 
    399  
    400         /** 
     424         * @see org.openmrs.api.PersonService#purgePerson(org.openmrs.Person) 
    401425         * @see org.openmrs.api.db.PersonDAO#deletePerson(org.openmrs.Person) 
    402426         */ 
     
    406430 
    407431        /** 
    408          * @see org.openmrs.api.db.PersonDAO#updatePerson(org.openmrs.Person) 
    409          */ 
    410         public void updatePerson(Person person) throws DAOException { 
    411                 if (person.getPersonId() == null) 
    412                         createPerson(person); 
    413                 else { 
     432         * @see org.openmrs.api.PersonService#savePerson(org.openmrs.Person) 
     433         * @see org.openmrs.api.db.PersonDAO#savePerson(org.openmrs.Person) 
     434         */ 
     435        public Person savePerson(Person person) throws DAOException { 
    414436                        sessionFactory.getCurrentSession().saveOrUpdate(person); 
    415                 } 
    416         } 
    417  
    418         /** 
    419          * @see org.openmrs.api.db.PersonService#createRelationship(org.openmrs.Relationship) 
    420          */ 
    421         public void createRelationship(Relationship relationship) 
    422                 throws DAOException { 
    423                 relationship.setCreator(Context.getAuthenticatedUser()); 
    424                 relationship.setDateCreated(new Date()); 
    425                 sessionFactory.getCurrentSession().save(relationship); 
    426         } 
    427          
    428         /** 
    429          * @see org.openmrs.api.db.PersonService#updateRelationship(org.openmrs.Relationship) 
    430          */ 
    431         public void updateRelationship(Relationship relationship) 
    432                 throws DAOException { 
    433                 if (relationship.getRelationshipId() == null) 
    434                         createRelationship(relationship); 
    435                 else 
     437                return person; 
     438                } 
     439 
     440        /** 
     441         * @see org.openmrs.api.PersonService#saveRelationship(org.openmrs.Relationship) 
     442         * @see org.openmrs.api.db.PersonDAO#saveRelationship(org.openmrs.Relationship) 
     443         */ 
     444        public Relationship saveRelationship(Relationship relationship) throws DAOException { 
    436445                        sessionFactory.getCurrentSession().saveOrUpdate(relationship); 
     446                return relationship; 
    437447        }        
    438448         
    439449        /** 
    440          * @see org.openmrs.api.db.PersonService#deleteRelationship(org.openmrs.Relationship) 
     450         * @see org.openmrs.api.PersonService#purgeRelationship(org.openmrs.Relationship) 
     451         * @see org.openmrs.api.db.PersonDAO#deleteRelationship(org.openmrs.Relationship) 
    441452         */ 
    442453        public void deleteRelationship(Relationship relationship) 
    443454                throws DAOException { 
    444455                sessionFactory.getCurrentSession().delete(relationship); 
    445         } 
    446          
    447         /** 
    448          * @see org.openmrs.api.db.PersonService#voidRelationship(org.openmrs.Relationship) 
    449          */ 
    450         public void voidRelationship(Relationship relationship) throws DAOException { 
    451                 relationship.setVoided(true); 
    452                 updateRelationship(relationship); 
    453         } 
    454  
    455         /** 
    456          * @see org.openmrs.api.db.PersonService#unvoidRelationship(org.openmrs.Relationship) 
    457          */ 
    458         public void unvoidRelationship(Relationship relationship) 
    459                 throws DAOException { 
    460                 relationship.setVoided(false); 
    461                 relationship.setVoidedBy(null); 
    462                 relationship.setDateVoided(null); 
    463                 relationship.setVoidReason(null); 
    464                 updateRelationship(relationship); 
    465456        } 
    466457