Changeset 4785 for openmrs/branches/complex-obs/src/api/org/openmrs/api/db/hibernate/HibernateConceptDAO.java
- Timestamp:
- 07/02/08 19:40:36 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs/branches/complex-obs/src/api/org/openmrs/api/db/hibernate/HibernateConceptDAO.java
r4517 r4785 14 14 package org.openmrs.api.db.hibernate; 15 15 16 import java.sql.Connection; 17 import java.sql.PreparedStatement; 16 18 import java.sql.SQLException; 17 19 import java.util.Collection; … … 43 45 import org.openmrs.ConceptComplex; 44 46 import org.openmrs.ConceptDatatype; 47 import org.openmrs.ConceptDerived; 45 48 import org.openmrs.ConceptName; 46 49 import org.openmrs.ConceptNumeric; … … 101 104 if (concept.getConceptId() == null || concept.getConceptId() < 1) 102 105 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 103 114 sessionFactory.getCurrentSession().saveOrUpdate(concept); 104 115 return concept; 105 116 } 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 } 106 163 107 164 /** … … 204 261 Criteria searchCriteria = sessionFactory.getCurrentSession().createCriteria(Drug.class, "drug"); 205 262 206 searchCriteria.add(Expression.eq("drug. voided", false));263 searchCriteria.add(Expression.eq("drug.retired", false)); 207 264 208 265 Iterator<String> word = words.iterator(); … … 263 320 */ 264 321 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();268 322 sessionFactory.getCurrentSession().delete(cc); 269 323 } … … 279 333 * @see org.openmrs.api.db.ConceptDAO#getAllConceptDatatypes(boolean) 280 334 */ 335 @SuppressWarnings("unchecked") 281 336 public List<ConceptDatatype> getAllConceptDatatypes(boolean includeRetired) 282 337 throws DAOException { … … 292 347 * @see org.openmrs.api.db.ConceptDAO#getConceptDatatypes(java.lang.String) 293 348 */ 349 @SuppressWarnings("unchecked") 294 350 public List<ConceptDatatype> getConceptDatatypes(String name) 295 351 throws DAOException {