Search in sources :

Example 1 with SystemCode

use of gov.ca.cwds.data.persistence.cms.SystemCode in project api-core by ca-cwds.

the class ApiHibernateInterceptorTest method testForSynchronizedPreFlush.

@SuppressWarnings("unused")
@Test
public void testForSynchronizedPreFlush() throws Exception {
    ExecutorService service = Executors.newFixedThreadPool(4);
    ApiHibernateInterceptor interceptor = new ApiHibernateInterceptor();
    final HashMap<Integer, Object> map = new HashMap<>();
    SystemCode code1 = new SystemCode();
    SystemCode code2 = new SystemCode();
    SystemCode code3 = new SystemCode();
    SystemCode code4 = new SystemCode();
    map.put(1, code1);
    map.put(2, code2);
    map.put(3, code3);
    map.put(4, code4);
    Iterator<?> iter = map.entrySet().iterator();
    Future<Date> future1 = execCallable(service, iter, interceptor);
    Future<Date> future2 = execCallable(service, iter, interceptor);
    Future<Date> future3 = execCallable(service, iter, interceptor);
    Future<Date> future4 = execCallable(service, iter, interceptor);
}
Also used : HashMap(java.util.HashMap) SystemCode(gov.ca.cwds.data.persistence.cms.SystemCode) ExecutorService(java.util.concurrent.ExecutorService) PersistentObject(gov.ca.cwds.data.persistence.PersistentObject) Date(java.util.Date) Test(org.junit.Test)

Example 2 with SystemCode

use of gov.ca.cwds.data.persistence.cms.SystemCode in project api-core by ca-cwds.

the class SystemCodeDao method findBySystemCodeId.

@SuppressWarnings("unchecked")
public SystemCode findBySystemCodeId(Number systemCodeId) {
    final String namedQueryName = SystemCode.class.getName() + ".findBySystemCodeId";
    Session session = getSessionFactory().getCurrentSession();
    Transaction txn = session.getTransaction();
    boolean transactionExists = txn != null && txn.isActive();
    txn = transactionExists ? txn : session.beginTransaction();
    try {
        final Query query = session.getNamedQuery(namedQueryName).setShort("systemId", systemCodeId.shortValue());
        final SystemCode systemCode = (SystemCode) query.getSingleResult();
        if (!transactionExists)
            txn.commit();
        return systemCode;
    } catch (HibernateException h) {
        txn.rollback();
        throw new DaoException(h);
    }
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) SystemCode(gov.ca.cwds.data.persistence.cms.SystemCode) DaoException(gov.ca.cwds.data.DaoException) Session(org.hibernate.Session)

Example 3 with SystemCode

use of gov.ca.cwds.data.persistence.cms.SystemCode in project api-core by ca-cwds.

the class SystemCodeDaoTest method findAll_Args__.

@Test
public void findAll_Args__() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    Transaction tx = mock(Transaction.class);
    Query query = mock(Query.class);
    when(sessionFactory.getCurrentSession()).thenReturn(session);
    when(session.getNamedQuery(any(String.class))).thenReturn(query);
    when(session.beginTransaction()).thenReturn(tx);
    when(query.list()).thenReturn(new ArrayList<SystemCode>());
    when(query.setString(any(String.class), any(String.class))).thenReturn(query);
    SystemCodeDao target = new SystemCodeDao(sessionFactory);
    SystemCode[] actual = target.findByForeignKeyMetaTable("asdf");
    SystemCode[] expected = new SystemCode[0];
    assertThat(actual, is(equalTo(expected)));
}
Also used : SessionFactory(org.hibernate.SessionFactory) Transaction(org.hibernate.Transaction) Query(org.hibernate.query.Query) SystemCode(gov.ca.cwds.data.persistence.cms.SystemCode) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with SystemCode

use of gov.ca.cwds.data.persistence.cms.SystemCode in project api-core by ca-cwds.

the class SystemCodeDao method findByForeignKeyMetaTable.

/**
 * @param foreignKeyMetaTable meta group
 * @return all keys by meta table
 */
@SuppressWarnings("unchecked")
public SystemCode[] findByForeignKeyMetaTable(String foreignKeyMetaTable) {
    final String namedQueryName = SystemCode.class.getName() + ".findByForeignKeyMetaTable";
    final Session session = getCurrentSession();
    Transaction txn = session.getTransaction();
    boolean transactionExists = txn != null && txn.isActive();
    txn = transactionExists ? txn : session.beginTransaction();
    try {
        final Query query = session.getNamedQuery(namedQueryName).setString("foreignKeyMetaTable", foreignKeyMetaTable);
        final SystemCode[] systemCodes = (SystemCode[]) query.list().toArray(new SystemCode[0]);
        if (!transactionExists)
            txn.commit();
        return systemCodes;
    } catch (HibernateException h) {
        txn.rollback();
        throw new DaoException(h);
    }
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) SystemCode(gov.ca.cwds.data.persistence.cms.SystemCode) DaoException(gov.ca.cwds.data.DaoException) Session(org.hibernate.Session)

Aggregations

SystemCode (gov.ca.cwds.data.persistence.cms.SystemCode)4 Session (org.hibernate.Session)3 Transaction (org.hibernate.Transaction)3 DaoException (gov.ca.cwds.data.DaoException)2 HibernateException (org.hibernate.HibernateException)2 Query (org.hibernate.Query)2 Test (org.junit.Test)2 PersistentObject (gov.ca.cwds.data.persistence.PersistentObject)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 SessionFactory (org.hibernate.SessionFactory)1 Query (org.hibernate.query.Query)1