use of org.openmrs.ConceptComplex in project openmrs-core by openmrs.
the class HibernateConceptDAO method getConceptComplex.
/**
* @see org.openmrs.api.db.ConceptDAO#getConceptComplex(java.lang.Integer)
*/
@Override
public ConceptComplex getConceptComplex(Integer conceptId) {
ConceptComplex cc;
Object obj = sessionFactory.getCurrentSession().get(ConceptComplex.class, conceptId);
// and re-fetch it as a ConceptComplex
if (obj != null && !obj.getClass().equals(ConceptComplex.class)) {
// remove from cache
sessionFactory.getCurrentSession().evict(obj);
// session.get() did not work here, we need to perform a query to get a ConceptComplex
Query query = sessionFactory.getCurrentSession().createQuery("from ConceptComplex where conceptId = :conceptId").setParameter("conceptId", conceptId);
obj = query.uniqueResult();
}
cc = (ConceptComplex) obj;
return cc;
}
use of org.openmrs.ConceptComplex in project openmrs-core by openmrs.
the class ConceptServiceTest method getConceptComplex_shouldReturnAConceptComplexObject.
/**
* @see ConceptService#getConceptComplex(Integer)
*/
@Test
public void getConceptComplex_shouldReturnAConceptComplexObject() {
executeDataSet("org/openmrs/api/include/ObsServiceTest-complex.xml");
ConceptComplex concept = Context.getConceptService().getConceptComplex(8473);
Assert.assertNotNull(concept);
}
use of org.openmrs.ConceptComplex in project openmrs-core by openmrs.
the class HibernateConceptDAO method insertRowIntoSubclassIfNecessary.
/**
* Convenience method that will check this concept for subtype values (ConceptNumeric,
* ConceptDerived, etc) and insert a line into that subtable if needed. This prevents a
* hibernate ConstraintViolationException
*
* @param concept the concept that will be inserted
*/
private void insertRowIntoSubclassIfNecessary(Concept concept) {
// check the concept_numeric table
if (concept instanceof ConceptNumeric) {
String select = "SELECT 1 from concept_numeric WHERE concept_id = :conceptId";
Query query = sessionFactory.getCurrentSession().createSQLQuery(select);
query.setInteger("conceptId", concept.getConceptId());
// Converting to concept numeric: A single concept row exists, but concept numeric has not been populated yet.
if (query.uniqueResult() == null) {
// we have to evict the current concept out of the session because
// the user probably had to change the class of this object to get it
// to now be a numeric
// (must be done before the "insert into...")
sessionFactory.getCurrentSession().clear();
// Just in case this was changed from concept_complex to numeric
// We need to add a delete line for each concept sub class that is not concept_numeric
deleteSubclassConcept("concept_complex", concept.getConceptId());
String insert = "INSERT INTO concept_numeric (concept_id, precise) VALUES (:conceptId, false)";
query = sessionFactory.getCurrentSession().createSQLQuery(insert);
query.setInteger("conceptId", concept.getConceptId());
query.executeUpdate();
} else {
// hence row should be deleted from the concept_numeric
if (!concept.isNumeric()) {
deleteSubclassConcept("concept_numeric", concept.getConceptId());
}
}
} else // check the concept complex table
if (concept instanceof ConceptComplex) {
String select = "SELECT 1 FROM concept_complex WHERE concept_id = :conceptId";
Query query = sessionFactory.getCurrentSession().createSQLQuery(select);
query.setInteger("conceptId", concept.getConceptId());
// Converting to concept complex: A single concept row exists, but concept complex has not been populated yet.
if (query.uniqueResult() == null) {
// we have to evict the current concept out of the session because
// the user probably had to change the class of this object to get it
// to now be a ConceptComplex
// (must be done before the "insert into...")
sessionFactory.getCurrentSession().clear();
// Just in case this was changed from concept_numeric to complex
// We need to add a delete line for each concept sub class that is not concept_complex
deleteSubclassConcept("concept_numeric", concept.getConceptId());
// Add an empty row into the concept_complex table
String insert = "INSERT INTO concept_complex (concept_id) VALUES (:conceptId)";
query = sessionFactory.getCurrentSession().createSQLQuery(insert);
query.setInteger("conceptId", concept.getConceptId());
query.executeUpdate();
} else {
// hence row should be deleted from the concept_complex
if (!concept.isComplex()) {
deleteSubclassConcept("concept_complex", concept.getConceptId());
}
}
}
}
use of org.openmrs.ConceptComplex in project openmrs-core by openmrs.
the class ConceptServiceTest method saveConcept_shouldSaveChangesBetweenConceptNumericAndComplex.
/**
* @see ConceptService#saveConcept(Concept)
*/
@Test
public void saveConcept_shouldSaveChangesBetweenConceptNumericAndComplex() {
executeDataSet(INITIAL_CONCEPTS_XML);
// save a concept numeric
ConceptNumeric cn = new ConceptNumeric(1);
cn.setDatatype(new ConceptDatatype(1));
cn.setConceptClass(new ConceptClass(1));
cn.addName(new ConceptName("a new conceptnumeric", Locale.US));
cn.addDescription(new ConceptDescription("some description", null));
cn.setHiAbsolute(20.0);
conceptService.saveConcept(cn);
// confirm that we saved a concept numeric
Concept firstConcept = conceptService.getConceptNumeric(1);
assertEquals("a new conceptnumeric", firstConcept.getName(Locale.US).getName());
assertTrue(firstConcept instanceof ConceptNumeric);
ConceptNumeric firstConceptNumeric = (ConceptNumeric) firstConcept;
assertEquals(20.0, firstConceptNumeric.getHiAbsolute(), 0);
// change to concept complex
ConceptComplex cn2 = new ConceptComplex(1);
cn2.setDatatype(new ConceptDatatype(13));
cn2.setConceptClass(new ConceptClass(1));
cn2.addName(new ConceptName("a new conceptComplex", Locale.US));
cn2.addDescription(new ConceptDescription("some description", null));
cn2.setHandler("SomeHandler");
conceptService.saveConcept(cn2);
// confirm that we saved a concept complex
firstConcept = conceptService.getConceptComplex(1);
assertEquals("a new conceptComplex", firstConcept.getName(Locale.US).getName());
assertTrue(firstConcept instanceof ConceptComplex);
ConceptComplex firstConceptComplex = (ConceptComplex) firstConcept;
assertEquals("SomeHandler", firstConceptComplex.getHandler());
// change to concept numeric
cn = new ConceptNumeric(1);
ConceptDatatype dt = new ConceptDatatype(1);
dt.setName("Numeric");
cn.setDatatype(dt);
cn.setConceptClass(new ConceptClass(1));
cn.addName(new ConceptName("a new conceptnumeric", Locale.US));
cn.addDescription(new ConceptDescription("some description", null));
cn.setHiAbsolute(20.0);
conceptService.saveConcept(cn);
// confirm that we saved a concept numeric
firstConcept = conceptService.getConceptNumeric(1);
assertEquals("a new conceptnumeric", firstConcept.getName(Locale.US).getName());
assertTrue(firstConcept instanceof ConceptNumeric);
firstConceptNumeric = (ConceptNumeric) firstConcept;
assertEquals(20.0, firstConceptNumeric.getHiAbsolute(), 0);
// change to concept complex
cn2 = new ConceptComplex(1);
cn2.setDatatype(new ConceptDatatype(13));
cn2.setConceptClass(new ConceptClass(1));
cn2.addName(new ConceptName("a new conceptComplex", Locale.US));
cn2.addDescription(new ConceptDescription("some description", null));
cn2.setHandler("SomeHandler");
conceptService.saveConcept(cn2);
// confirm we saved a concept complex
firstConcept = conceptService.getConceptComplex(1);
assertEquals("a new conceptComplex", firstConcept.getName(Locale.US).getName());
assertTrue(firstConcept instanceof ConceptComplex);
firstConceptComplex = (ConceptComplex) firstConcept;
assertEquals("SomeHandler", firstConceptComplex.getHandler());
}
use of org.openmrs.ConceptComplex in project openmrs-core by openmrs.
the class ConceptServiceTest method saveConcept_shouldSaveNonConceptComplexObjectAsConceptComplex.
/**
* @see ConceptService#saveConcept(Concept)
*/
@Test
public void saveConcept_shouldSaveNonConceptComplexObjectAsConceptComplex() {
executeDataSet(INITIAL_CONCEPTS_XML);
// this tests saving a current concept as a newly changed conceptComplex
// assumes there is already a concept in the database
// with a concept id of #1
ConceptComplex cn = new ConceptComplex(1);
cn.setDatatype(new ConceptDatatype(13));
cn.setConceptClass(new ConceptClass(1));
cn.addName(new ConceptName("a new conceptComplex", Locale.US));
cn.addDescription(new ConceptDescription("some description", null));
cn.setHandler("SomeHandler");
conceptService.saveConcept(cn);
Concept firstConcept = conceptService.getConceptComplex(1);
assertEquals("a new conceptComplex", firstConcept.getName(Locale.US).getName());
assertTrue(firstConcept instanceof ConceptComplex);
ConceptComplex firstConceptComplex = (ConceptComplex) firstConcept;
assertEquals("SomeHandler", firstConceptComplex.getHandler());
}
Aggregations