Search in sources :

Example 1 with ConceptComplex

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;
}
Also used : SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) LuceneQuery(org.openmrs.api.db.hibernate.search.LuceneQuery) OpenmrsObject(org.openmrs.OpenmrsObject) ConceptComplex(org.openmrs.ConceptComplex)

Example 2 with ConceptComplex

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);
}
Also used : ConceptComplex(org.openmrs.ConceptComplex) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 3 with ConceptComplex

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());
            }
        }
    }
}
Also used : SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) LuceneQuery(org.openmrs.api.db.hibernate.search.LuceneQuery) ConceptNumeric(org.openmrs.ConceptNumeric) ConceptComplex(org.openmrs.ConceptComplex)

Example 4 with ConceptComplex

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());
}
Also used : OpenmrsMatchers.hasConcept(org.openmrs.test.OpenmrsMatchers.hasConcept) Concept(org.openmrs.Concept) ConceptClass(org.openmrs.ConceptClass) ConceptNumeric(org.openmrs.ConceptNumeric) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) ConceptComplex(org.openmrs.ConceptComplex) ConceptDatatype(org.openmrs.ConceptDatatype) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 5 with ConceptComplex

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());
}
Also used : OpenmrsMatchers.hasConcept(org.openmrs.test.OpenmrsMatchers.hasConcept) Concept(org.openmrs.Concept) ConceptClass(org.openmrs.ConceptClass) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) ConceptComplex(org.openmrs.ConceptComplex) ConceptDatatype(org.openmrs.ConceptDatatype) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Aggregations

ConceptComplex (org.openmrs.ConceptComplex)5 Test (org.junit.Test)3 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)3 Query (org.hibernate.Query)2 SQLQuery (org.hibernate.SQLQuery)2 Concept (org.openmrs.Concept)2 ConceptClass (org.openmrs.ConceptClass)2 ConceptDatatype (org.openmrs.ConceptDatatype)2 ConceptDescription (org.openmrs.ConceptDescription)2 ConceptName (org.openmrs.ConceptName)2 ConceptNumeric (org.openmrs.ConceptNumeric)2 LuceneQuery (org.openmrs.api.db.hibernate.search.LuceneQuery)2 OpenmrsMatchers.hasConcept (org.openmrs.test.OpenmrsMatchers.hasConcept)2 OpenmrsObject (org.openmrs.OpenmrsObject)1