Search in sources :

Example 1 with DaoException

use of gov.ca.cwds.data.DaoException in project api-core by ca-cwds.

the class SystemMetaDao method findAll.

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

Example 2 with DaoException

use of gov.ca.cwds.data.DaoException 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 DaoException

use of gov.ca.cwds.data.DaoException in project api-core by ca-cwds.

the class SsaName3Dao method callStoredProc.

/**
 * Call DB2 stored procedure SPSSANAME3 to insert soundex records for client search. Story
 * #146481759.
 *
 * @param parameterObject stored procedure parameters
 */
public void callStoredProc(SsaName3ParameterObject parameterObject) {
    Session session = sessionFactory.getCurrentSession();
    final String STORED_PROC_NAME = "SPSSANAME3";
    final String schema = (String) session.getSessionFactory().getProperties().get("hibernate.default_schema");
    String strdtts = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS").format(parameterObject.getUpdateTimeStamp());
    try {
        ProcedureCall q = session.createStoredProcedureCall(schema + "." + STORED_PROC_NAME);
        q.registerStoredProcedureParameter("TABLENAME", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("CRUDFUNCT", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("IDENTIFIER", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("NAMECODE", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("FIRSTNAME", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("MIDDLENAME", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("LASTNAME", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("STREETNUM", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("STREETNAME", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("GVRENTC", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("LASTUPDTM", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("LASTUPDID", String.class, ParameterMode.IN);
        q.registerStoredProcedureParameter("RETSTATUS", String.class, ParameterMode.OUT);
        q.registerStoredProcedureParameter("RETMESSAG", String.class, ParameterMode.OUT);
        q.setParameter("TABLENAME", parameterObject.getTableName());
        q.setParameter("CRUDFUNCT", parameterObject.getCrudOper());
        q.setParameter("IDENTIFIER", parameterObject.getIdentifier());
        q.setParameter("NAMECODE", parameterObject.getNameCd());
        q.setParameter("FIRSTNAME", parameterObject.getFirstName());
        q.setParameter("MIDDLENAME", parameterObject.getMiddleName());
        q.setParameter("LASTNAME", parameterObject.getLastName());
        q.setParameter("STREETNUM", parameterObject.getStreettNumber());
        q.setParameter("STREETNAME", parameterObject.getStreetName());
        q.setParameter("GVRENTC", String.valueOf(parameterObject.getGvrEntc()));
        q.setParameter("LASTUPDTM", strdtts);
        q.setParameter("LASTUPDID", parameterObject.getUpdateId());
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("TABLENAME = {}", parameterObject.getTableName());
            LOGGER.info("CRUDFUNCT = {}", parameterObject.getCrudOper());
            LOGGER.info("IDENTIFIER = {}", parameterObject.getIdentifier());
            LOGGER.info("NAMECODE = {}", parameterObject.getNameCd());
            LOGGER.info("FIRSTNAME = {}", parameterObject.getFirstName());
            LOGGER.info("MIDDLENAME = {}", parameterObject.getMiddleName());
            LOGGER.info("LASTNAME = {}", parameterObject.getLastName());
            LOGGER.info("STREETNUM = {}", parameterObject.getStreettNumber());
            LOGGER.info("STREETNAME = {}", parameterObject.getStreetName());
            LOGGER.info("GVRENTC = {}", String.valueOf(parameterObject.getGvrEntc()));
            LOGGER.info("LASTUPDTM = {}", strdtts);
            LOGGER.info("LASTUPDID = {}", parameterObject.getUpdateId());
        }
        q.execute();
        final String returnStatus = (String) q.getOutputParameterValue("RETSTATUS");
        final String returnMessage = (String) q.getOutputParameterValue("RETMESSAG");
        int returnCode = Integer.parseInt(returnStatus);
        LOGGER.info("storeProcReturnStatus: {}, storeProcreturnMessage: {}", returnStatus, returnMessage);
        /*
       * return code: 0=successful, 1=keys not generated, 2=Invalid parameters sent to stored
       * procedure 3=SQL failed, 4=Call to SSANAME3 DLL failed
       */
        if (returnCode != 0 && returnCode != 1) {
            LOGGER.error("Stored Procedure return message - {}", returnMessage);
            throw new DaoException("Stored Procedure returned with ERROR - {}" + returnMessage);
        }
    } catch (DaoException h) {
        throw new DaoException("Call to Stored Procedure failed - " + h, h);
    }
}
Also used : ProcedureCall(org.hibernate.procedure.ProcedureCall) DaoException(gov.ca.cwds.data.DaoException) SimpleDateFormat(java.text.SimpleDateFormat) Session(org.hibernate.Session)

Example 4 with DaoException

use of gov.ca.cwds.data.DaoException in project api-core by ca-cwds.

the class BatchBucketDao method bucketList.

/**
 * Retrieve all records for batch processing for a single bucket. PostgreSQL queries would likely
 * rely on the <a href="https://www.postgresql.org/docs/9.6/static/functions-window.html">NTILE
 * analytic function</a>, whereas DB2 10.5, lacking modern analytics, would likely rely on nested
 * or correlated queries. Note that DB2 doesn't even provide basic pseudo-columns, like ROWNUM,
 * without enabling <a href=
 * "http://www.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.apdv.porting.doc/doc/r0052867.html">
 * "compatibility vectors"</a> or writing a
 * <a href="http://hoteljavaopensource.blogspot.com/2011/06/ntile-and-db2.html">user-defined
 * function</a>.
 *
 * <p>
 * The following DB2 SQL demonstrates how to simulate the NTILE analytic without installing
 * additional packages or non-standard functions.
 * </p>
 *
 * <pre>
 * select z.identifier, z.bucket, z.rn, row_number() over (partition by z.bucket order by 1) as bucket_rn
 * from (
 *    select mod(y.rn,10) + 1 as bucket, y.rn, y.identifier
 *    from (
 *       select row_number() over (order by 1) as rn, x.identifier
 *       from ( select c.identifier from client_t c ) x
 *    ) y
 * ) z
 * where z.bucket = 3 FOR READ ONLY WITH UR;
 * </pre>
 *
 * <p>
 * Most batch bucket queries are defined Hibernate named queries in the persistence class itself.
 * </p>
 *
 * @param bucketNum current bucket for this batch
 * @param totalBuckets total buckets in batch run
 * @return ordered list of referral/client document records
 */
@SuppressWarnings("unchecked")
default List<T> bucketList(long bucketNum, long totalBuckets) {
    final String namedQueryName = getEntityClass().getName() + ".findAllByBucket";
    Session session = getSessionFactory().getCurrentSession();
    Transaction txn = session.beginTransaction();
    try {
        Query query = session.getNamedQuery(namedQueryName).setInteger("bucket_num", (int) bucketNum).setInteger("total_buckets", (int) totalBuckets);
        ImmutableList.Builder<T> results = new ImmutableList.Builder<>();
        results.addAll(query.list());
        txn.commit();
        return results.build();
    } catch (HibernateException h) {
        txn.rollback();
        throw new DaoException(h);
    }
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) ImmutableList(com.google.common.collect.ImmutableList) DaoException(gov.ca.cwds.data.DaoException) Session(org.hibernate.Session)

Example 5 with DaoException

use of gov.ca.cwds.data.DaoException in project api-core by ca-cwds.

the class BatchBucketDao method partitionedBucketList.

/**
 * Retrieve all records for batch processing for a single bucket. PostgreSQL queries would likely
 * rely on the <a href="https://www.postgresql.org/docs/9.6/static/functions-window.html">NTILE
 * analytic function</a>, whereas DB2 10.5, lacking modern analytics, would likely rely on nested
 * or correlated queries. Note that DB2 doesn't even provide basic pseudo-columns, like ROWNUM,
 * without enabling <a href=
 * "http://www.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.apdv.porting.doc/doc/r0052867.html">
 * "compatibility vectors"</a> or writing a
 * <a href="http://hoteljavaopensource.blogspot.com/2011/06/ntile-and-db2.html">user-defined
 * function</a>.
 *
 * <p>
 * The following DB2 SQL demonstrates how to simulate the NTILE analytic without installing
 * additional packages or non-standard functions.
 * </p>
 *
 * <pre>
 * {@code
 * select z.identifier, z.bucket, z.rn, row_number() over (partition by z.bucket order by 1) as bucket_rn
 * from (
 *    select mod(y.rn,10) + 1 as bucket, y.rn, y.identifier
 *    from (
 *       select row_number() over (order by 1) as rn, x.identifier
 *       from ( select c.identifier from client_t c
 *       WHERE x.identifier >= 'B3bMRWu8NW' and x.identifier < 'DW5GzxJ30A') x
 *    ) y
 * ) z
 * where z.bucket = 3 FOR READ ONLY WITH UR;
 * }
 * </pre>
 *
 * <p>
 * Most batch bucket queries are defined Hibernate named queries in the persistence class itself.
 * </p>
 *
 * @param bucketNum current bucket for this batch
 * @param totalBuckets total buckets for this batch run only, NOT the total for all batches
 * @param minId minimum key value, inclusive
 * @param maxId maximum key value, exclusive
 * @return ordered list of referral/client document records
 */
@SuppressWarnings("unchecked")
default List<T> partitionedBucketList(long bucketNum, long totalBuckets, String minId, String maxId) {
    final String namedQueryName = getEntityClass().getName() + ".findPartitionedBuckets";
    Session session = getSessionFactory().getCurrentSession();
    Transaction txn = session.beginTransaction();
    try {
        Query query = session.getNamedQuery(namedQueryName).setInteger("bucket_num", (int) bucketNum).setInteger("total_buckets", (int) totalBuckets).setString("min_id", minId).setString("max_id", maxId);
        ImmutableList.Builder<T> results = new ImmutableList.Builder<>();
        results.addAll(query.list());
        txn.commit();
        return results.build();
    } catch (HibernateException h) {
        txn.rollback();
        throw new DaoException(h);
    }
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) ImmutableList(com.google.common.collect.ImmutableList) DaoException(gov.ca.cwds.data.DaoException) Session(org.hibernate.Session)

Aggregations

DaoException (gov.ca.cwds.data.DaoException)6 Session (org.hibernate.Session)6 HibernateException (org.hibernate.HibernateException)5 Query (org.hibernate.Query)5 Transaction (org.hibernate.Transaction)5 ImmutableList (com.google.common.collect.ImmutableList)2 SystemCode (gov.ca.cwds.data.persistence.cms.SystemCode)2 SystemMeta (gov.ca.cwds.data.persistence.cms.SystemMeta)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ProcedureCall (org.hibernate.procedure.ProcedureCall)1