Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
07/02/08 19:40:36 (5 months ago)
Author:
bmckown
Message:

Merging trunk to complex-obs [4379][4764]

Files:

Legend:

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

    r4517 r4785  
    1414package org.openmrs.api.db.hibernate; 
    1515 
     16import java.sql.Connection; 
     17import java.sql.PreparedStatement; 
    1618import java.sql.SQLException; 
    1719import java.util.Collection; 
     
    4345import org.openmrs.ConceptComplex; 
    4446import org.openmrs.ConceptDatatype; 
     47import org.openmrs.ConceptDerived; 
    4548import org.openmrs.ConceptName; 
    4649import org.openmrs.ConceptNumeric; 
     
    101104                if (concept.getConceptId() == null || concept.getConceptId() < 1) 
    102105                        concept.setConceptId(this.getNextAvailableId()); 
     106                else { 
     107                        // this method checks the concept_numeric, concept_derived, etc tables 
     108                        // to see if a row exists there or not.  This is needed because hibernate 
     109                        // doesn't like to insert into concept_numeric but update concept in the  
     110                        // same go.  It assumes that its either in both tables or no tables 
     111                        insertRowIntoSubclassIfNecessary(concept); 
     112                } 
     113                 
    103114                sessionFactory.getCurrentSession().saveOrUpdate(concept); 
    104115                return concept; 
    105116        } 
     117 
     118        /** 
     119     * Convenience method that will check this concept for subtype values (ConceptNumeric, ConceptDerived, etc) 
     120     * and insert a line into that subtable if needed. 
     121     *  
     122     * This prevents a hibernate ConstraintViolationException 
     123     *  
     124     * @param concept the concept that will be inserted 
     125     */ 
     126    private void insertRowIntoSubclassIfNecessary(Concept concept) { 
     127        Connection connection = sessionFactory.getCurrentSession().connection(); 
     128                 
     129        // check the concept_numeric table 
     130        if (concept instanceof ConceptNumeric) { 
     131                 
     132                try { 
     133                                PreparedStatement ps = connection.prepareStatement("SELECT * FROM concept WHERE concept_id = ? and not exists (select * from concept_numeric WHERE concept_id = ?)"); 
     134                                ps.setInt(1, concept.getConceptId()); 
     135                                ps.setInt(2, concept.getConceptId()); 
     136                                ps.execute(); 
     137                 
     138                                if (ps.getResultSet().next()) { 
     139                                        // we have to evict the current concept out of the session because 
     140                                        // the user probably had to change the class of this object to get it  
     141                                        // to now be a numeric 
     142                                        // (must be done before the "insert into...") 
     143                                        sessionFactory.getCurrentSession().clear(); 
     144                                         
     145                                        ps = connection.prepareStatement("INSERT INTO concept_numeric (concept_id, precise) VALUES (?, false)"); 
     146                                        ps.setInt(1, concept.getConceptId()); 
     147                                        ps.executeUpdate(); 
     148                                } 
     149                                else { 
     150                                        // no stub insert is needed because either a concept row  
     151                                        // doesn't exist or a concept_numeric row does exist 
     152                                } 
     153                                 
     154                        } 
     155                        catch (SQLException e) { 
     156                                log.error("Error while trying to see if this ConceptNumeric is in the concept_numeric table already", e); 
     157                        } 
     158        } 
     159        else if (concept instanceof ConceptDerived) { 
     160                // check the concept_derived table 
     161        } 
     162    } 
    106163 
    107164        /** 
     
    204261                        Criteria searchCriteria = sessionFactory.getCurrentSession().createCriteria(Drug.class, "drug"); 
    205262                         
    206                         searchCriteria.add(Expression.eq("drug.voided", false)); 
     263                        searchCriteria.add(Expression.eq("drug.retired", false)); 
    207264                         
    208265                        Iterator<String> word = words.iterator(); 
     
    263320         */ 
    264321        public void purgeConceptClass(ConceptClass cc) throws DAOException  { 
    265                 sessionFactory.getCurrentSession().createQuery("delete from ConceptClass where concept_class_id = :c") 
    266                                         .setInteger("c", cc.getConceptClassId()) 
    267                                         .executeUpdate();                        
    268322                sessionFactory.getCurrentSession().delete(cc); 
    269323        } 
     
    279333     * @see org.openmrs.api.db.ConceptDAO#getAllConceptDatatypes(boolean) 
    280334     */ 
     335    @SuppressWarnings("unchecked") 
    281336    public List<ConceptDatatype> getAllConceptDatatypes(boolean includeRetired) 
    282337            throws DAOException { 
     
    292347     * @see org.openmrs.api.db.ConceptDAO#getConceptDatatypes(java.lang.String) 
    293348     */ 
     349    @SuppressWarnings("unchecked") 
    294350    public List<ConceptDatatype> getConceptDatatypes(String name) 
    295351            throws DAOException {