Example usage for org.hibernate StatelessSession getNamedQuery

List of usage examples for org.hibernate StatelessSession getNamedQuery

Introduction

In this page you can find the example usage for org.hibernate StatelessSession getNamedQuery.

Prototype

@Override
    org.hibernate.query.Query getNamedQuery(String queryName);

Source Link

Usage

From source file:com.ephesoft.dcma.performance.reporting.service.ReportDataServiceImpl.java

License:Open Source License

/**
 * Method to get System Level Statistics for a specified time.
 * // w  w w .  j  a va  2 s. c  o  m
 * @param endTime {@link Date} upto which the reporting data needs to be fetched
 * @param startTime {@link Date}Starting Date from which the reporting data needs to be fetched
 * @param batchClassIdList List<String>
 * @return List<{@link Integer}> list of integers specifying the system statistics
 * @throws DCMAException if error occurs in database creation
 */
public List<Integer> getSystemStatistics(Date endTime, Date startTime, List<String> batchClassIdList)
        throws DCMAException {
    LOGGER.info("Inside getSystemStatistics.. ");
    Connection connection = null;
    List<Integer> finalResult = new ArrayList<Integer>();
    try {
        connection = dynamicHibernateDao.getConnectionProvider().getConnection();

        StatelessSession session = dynamicHibernateDao.getStatelessSession(connection);
        Query qry = session.getNamedQuery(ReportingConstants.GET_SYSTEM_STATISTICS);

        // Adding 1 Day in the End Time to show all the records for that Day
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTime);
        calendar.add(Calendar.DATE, 1);
        LOGGER.info("batchClassIdList::" + batchClassIdList);

        qry.setParameter(ReportingConstants.END_TIME, new java.sql.Date(calendar.getTimeInMillis()));

        qry.setParameter(ReportingConstants.START_TIME, new java.sql.Date(startTime.getTime()));
        qry.setParameterList(ReportingConstants.GET_SYSTEM_STATISTICS_BATCH_CLASS_ID_LIST, batchClassIdList);

        List<?> results = qry.list();

        Object[] object = (Object[]) results.get(0);
        for (int i = 0; i < object.length; i++) {
            if (object[i] != null) {
                finalResult.add(Integer.parseInt(object[i].toString()));
            }
        }
    } catch (SQLException e) {
        LOGGER.error(ERROR_CREATING_DATABASE_CONNECTION + e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.error(ERROR_CLOSING_DATABASE_CONNECTION + e.getMessage(), e);
            }
        }
    }
    return finalResult;
}

From source file:com.ephesoft.dcma.performance.reporting.service.ReportDataServiceImpl.java

License:Open Source License

/**
 * Method to get Reports Per page for a WorkflowType for a specified time.
 * /*  w  w  w  .  j av a2  s  . co m*/
 * @param batchClassIds List<{@link String}> Batch Class Ids for which the report data needs to be fetched
 * @param workflowType {@link WorkflowType}, One of Module , Plugin or Workflow specifying the type of filter
 * @param endTime {@link Date} upto which the reporting data needs to be fetched
 * @param startTime {@link Date} Starting Date from which the reporting data needs to be fetched
 * @param StartIndex int, Start Index for pagination
 * @param range int Number of records per page
 * @param order {@link Order} By field
 * @return List<{@link ReportDisplayData}> List of RepoertDisplayData DTOs
 * @throws DCMAException if error occurs in database creation
 */
@SuppressWarnings("unchecked")
public List<ReportDisplayData> getReportByWorkflow(List<String> batchClassIds, WorkflowType workflowType,
        Date endTime, Date startTime, int StartIndex, int range, Order order) throws DCMAException {
    Connection connection = null;
    Query qry = null;
    List<ReportDisplayData> displayDatas = null;
    try {
        connection = dynamicHibernateDao.getConnectionProvider().getConnection();

        StatelessSession session = dynamicHibernateDao.getStatelessSession(connection);
        qry = session.getNamedQuery(ReportingConstants.GET_REPORT_BY_WORKFLOW);
        qry.setResultTransformer(Transformers.aliasToBean(ReportDisplayData.class));

        // Adding 1 Day in the End Time to show all the records for that Day
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTime);
        calendar.add(Calendar.DATE, 1);

        qry.setParameter(ReportingConstants.END_TIME, new java.sql.Date(calendar.getTimeInMillis()));

        qry.setParameter(ReportingConstants.START_TIME, new java.sql.Date(startTime.getTime()));
        qry.setParameter(ReportingConstants.START_INDEX, StartIndex);
        qry.setParameter(ReportingConstants.RANGE, range);
        qry.setParameter(ReportingConstants.WORK_FLOW_TYPE, workflowType.name());
        qry.setParameterList(ReportingConstants.BATCH_CLASS_ID_LIST, batchClassIds);
        displayDatas = qry.list();
    } catch (SQLException e) {
        LOGGER.error(ERROR_CREATING_DATABASE_CONNECTION + e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.error(ERROR_CLOSING_DATABASE_CONNECTION + e.getMessage(), e);
            }
        }
    }
    return displayDatas;
}

From source file:com.ephesoft.dcma.performance.reporting.service.ReportDataServiceImpl.java

License:Open Source License

/**
 * Method to get Reports Per page for a User for a specified time.
 * //w  w w  . j  ava  2  s.  c  o m
 * @param batchClassIds List<{@link String}>, Batch Class Ids for which the report data needs to be fetched
 * @param userName {@link String}, User name for which the report are to be fetched
 * @param endTime {@link Date}, Date upto which the reporting data needs to be fetched
 * @param startTime {@link Date}, Starting Date from which the reporting data needs to be fetched
 * @param StartIndex int, Start Index for pagination
 * @param range int, Number of records per page
 * @param order {@link Order}, By field
 * @return List<{@link ReportDisplayData}>, List of RepoertDisplayData DTOs
 * @throws DCMAException if error occurs in database creation
 */
@SuppressWarnings("unchecked")
public List<ReportDisplayData> getReportByUser(List<String> batchClassIds, String userName, Date endTime,
        Date startTime, int StartIndex, int range, Order order) throws DCMAException {
    Connection connection = null;
    Query qry = null;
    List<ReportDisplayData> resultList = null;
    try {
        connection = dynamicHibernateDao.getConnectionProvider().getConnection();
        StatelessSession session = dynamicHibernateDao.getStatelessSession(connection);

        if (userName.equalsIgnoreCase(ReportingConstants.ALL)) {
            qry = session.getNamedQuery(ReportingConstants.GET_REPORT_FOR_ALL_USERS);
        } else {
            qry = session.getNamedQuery(ReportingConstants.GET_REPORT_BY_USER_NAME);
            qry.setParameter(ReportingConstants.USER_NAME, userName);
        }
        qry.setResultTransformer(Transformers.aliasToBean(ReportDisplayData.class));
        qry.setParameterList(ReportingConstants.BATCH_CLASS_ID_LIST, batchClassIds);

        // Adding 1 Day in the End Time to show all the records for that Day
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTime);
        calendar.add(Calendar.DATE, 1);

        qry.setParameter(ReportingConstants.END_TIME, new java.sql.Date(calendar.getTimeInMillis()));
        qry.setParameter(ReportingConstants.START_TIME, new java.sql.Date(startTime.getTime()));
        qry.setParameter(ReportingConstants.START_INDEX, StartIndex);
        qry.setParameter(ReportingConstants.RANGE, range);
        resultList = qry.list();
    } catch (SQLException e) {
        LOGGER.error(ERROR_CREATING_DATABASE_CONNECTION + e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.error(ERROR_CLOSING_DATABASE_CONNECTION + e.getMessage(), e);
            }
        }
    }

    return resultList;
}

From source file:com.ephesoft.dcma.performance.reporting.service.ReportDataServiceImpl.java

License:Open Source License

/**
 * Method to get total records for a WorkflowType for a specified time.
 * //ww w  .  j a  v  a2  s  .  c om
 * @param batchClassIds List<{@link String}>, Batch Class Ids for which the report data needs to be fetched
 * @param workflowType {@link WorkflowType}, One of Module , Plugin or Workflow specifying the type of filter
 * @param endTime {@link Date}, Date upto which the reporting data needs to be fetched
 * @param startTime {@link Date}, Starting Date from which the reporting data needs to be fetched
 * @return Total {@link Integer}, Record count for the crtieria parameters
 * @throws DCMAException if error occurs in database creation
 */
public Integer getReportTotalRowCountByWorkflow(List<String> batchClassIds, WorkflowType workflowType,
        Date endTime, Date startTime) throws DCMAException {
    Connection connection = null;
    Integer finalResult = 0;
    try {
        connection = dynamicHibernateDao.getConnectionProvider().getConnection();
        StatelessSession session = dynamicHibernateDao.getStatelessSession(connection);

        Query qry = session.getNamedQuery(ReportingConstants.GET_TOTAL_ROW_COUNT_BY_WORKFLOW);
        qry.setParameterList(ReportingConstants.BATCH_CLASS_ID_LIST, batchClassIds);

        // Adding 1 Day in the End Time to show all the records for that Day
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTime);
        calendar.add(Calendar.DATE, 1);

        qry.setParameter(ReportingConstants.END_TIME, new java.sql.Date(calendar.getTimeInMillis()));
        qry.setParameter(ReportingConstants.START_TIME, new java.sql.Date(startTime.getTime()));
        qry.setParameter(ReportingConstants.WORK_FLOW_TYPE, workflowType.name());
        List<?> results = qry.list();

        if (results != null) {
            finalResult = (Integer) results.get(0);
        }
    } catch (SQLException e) {
        LOGGER.error(ERROR_CREATING_DATABASE_CONNECTION + e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.error(ERROR_CLOSING_DATABASE_CONNECTION + e.getMessage(), e);
            }
        }
    }
    return finalResult;
}

From source file:com.ephesoft.dcma.performance.reporting.service.ReportDataServiceImpl.java

License:Open Source License

/**
 * Method to get total records for a User for a specified time.
 * /* w  w w.j a va2s  .c  om*/
 * @param batchClassIds List<{@link String}>, Batch Class Ids for which the report data needs to be fetched
 * @param userName {@link String}, User name for which the report are to be fetched
 * @param endTime {@link Date}, Date upto which the reporting data needs to be fetched
 * @param startTime {@link Date}, Starting Date from which the reporting data needs to be fetched
 * @return Total {@link Integer}, Record count for the crtieria parameters
 * @throws DCMAException if error occurs in database creation
 */
public Integer getReportTotalRowCountByUser(List<String> batchClassIds, String userName, Date endTime,
        Date startTime) throws DCMAException {
    Connection connection = null;
    Integer finalResult = 0;
    try {
        connection = dynamicHibernateDao.getConnectionProvider().getConnection();
        StatelessSession session = dynamicHibernateDao.getStatelessSession(connection);
        Query qry;
        if (userName.equalsIgnoreCase(ReportingConstants.ALL)) {
            qry = session.getNamedQuery(ReportingConstants.GET_TOTAL_ROW_COUNT_BY_ALL_USERS);
        } else {
            qry = session.getNamedQuery(ReportingConstants.GET_TOTAL_ROW_COUNT_BY_USER_NAME);
            qry.setParameter(ReportingConstants.USER_NAME, userName);
        }
        qry.setParameterList(ReportingConstants.BATCH_CLASS_ID_LIST, batchClassIds);
        // Adding 1 Day in the End Time to show all the records for that Day
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTime);
        calendar.add(Calendar.DATE, 1);

        qry.setParameter(ReportingConstants.END_TIME, new java.sql.Date(calendar.getTimeInMillis()));
        qry.setParameter(ReportingConstants.START_TIME, new java.sql.Date(startTime.getTime()));
        List<?> results = qry.list();
        finalResult = 0;
        if (results != null && (!results.isEmpty())) {
            finalResult = (Integer) results.get(0);
        }
    } catch (SQLException e) {
        LOGGER.error(ERROR_CREATING_DATABASE_CONNECTION + e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.error(ERROR_CLOSING_DATABASE_CONNECTION + e.getMessage(), e);
            }
        }
    }
    return finalResult;
}

From source file:com.ephesoft.dcma.performance.reporting.service.ReportDataServiceImpl.java

License:Open Source License

/**
 * Method to get whether another user is connected to the reporting DB.
 * /* w  ww.jav  a 2  s. c  o  m*/
 * @return Boolean , true if another user is connected to the report database
 * @throws DCMAException if error occurs in database creation
 */
public Boolean isAnotherUserConnected() throws DCMAException {
    LOGGER.info("Entering is already user connected method.");
    Connection connection = null;
    Boolean isAnotherUserAlreadyConnected = false;
    try {
        connection = dynamicHibernateDao.getConnectionProvider().getConnection();
        StatelessSession session = dynamicHibernateDao.getStatelessSession(connection);
        List<?> results = null;
        try {
            Query qry = session.getNamedQuery(ReportingConstants.GET_IS_ALREADY_USER_CONNECTED);
            results = qry.list();
        } catch (MappingException e) {
            String errorMesg = "Unable to get the named query:\"getIsAlreadyUserConnected\" from mapping file.";
            LOGGER.error(errorMesg + "Exception thrown is:", e);
            throw new DCMAException(errorMesg, e);
        } catch (Exception e) {
            String errorMesg = "An error occurred with the reporting query. Please check the logs for further details.";
            LOGGER.error(errorMesg + "Exception thrown is:", e);
            throw new DCMAException(errorMesg, e);
        }
        isAnotherUserAlreadyConnected = false;
        if (results != null && (!results.isEmpty())) {
            isAnotherUserAlreadyConnected = (Boolean) results.get(0);
        }
    } catch (SQLException e) {
        LOGGER.error(ERROR_CREATING_DATABASE_CONNECTION + e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.error(ERROR_CLOSING_DATABASE_CONNECTION + e.getMessage(), e);
            }
        }
    }
    LOGGER.info("Value of flag variable \"is_already_user connected\" is: " + isAnotherUserAlreadyConnected);
    return isAnotherUserAlreadyConnected;
}

From source file:eu.europa.ec.fisheries.uvms.spatial.service.dao.AbstractAreaDao.java

License:Open Source License

public List<Serializable> bulkInsert(Map<String, List<Property>> features, List<UploadMappingProperty> mapping)
        throws ServiceException {
    List<Serializable> invalidGeometryList = new ArrayList<>();
    StatelessSession session = (getEntityManager().unwrap(Session.class)).getSessionFactory()
            .openStatelessSession();//  ww  w  .ja va  2s  .  co  m
    Transaction tx = session.beginTransaction();
    try {
        Query query = session.getNamedQuery(getDisableAreaNamedQuery());
        query.executeUpdate();
        for (List<Property> properties : features.values()) {
            Map<String, Object> values = BaseAreaEntity.createAttributesMap(properties);
            BaseAreaEntity entity = createEntity(values, mapping);
            if (entity.getName() == null || entity.getCode() == null) {
                throw new ServiceException("NAME AND CODE FIELD ARE MANDATORY");
            }
            Serializable identifier = session.insert(entity);
            if (!entity.getGeom().isValid()) {
                invalidGeometryList.add(identifier);
            }
        }
        log.debug("Commit transaction");
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        throw new ServiceException("Rollback transaction", e);
    } finally {
        log.debug("Closing session");
        session.close();
    }
    return invalidGeometryList;
}

From source file:org.jboss.seam.wiki.core.nestedset.listener.InsertNestedSetOperation.java

License:LGPL

protected void executeOnDatabase(StatelessSession ss) {
    log.trace("executing nested set insert on database");

    Query updateLeft = ss.createQuery(
            "update " + nodeEntityName + " n set " + " n.nodeInfo.nsLeft = n.nodeInfo.nsLeft + :spaceNeeded "
                    + " where n.nodeInfo.nsThread = :thread and n.nodeInfo.nsLeft > :right");
    updateLeft.setParameter("spaceNeeded", spaceNeeded);
    updateLeft.setParameter("thread", parentThread);
    updateLeft.setParameter("right", newLeft);
    int updateLeftCount = updateLeft.executeUpdate();
    log.trace("updated left values of nested set nodes: " + updateLeftCount);

    Query updateRight = ss.createQuery(
            "update " + nodeEntityName + " n set " + " n.nodeInfo.nsRight = n.nodeInfo.nsRight + :spaceNeeded "
                    + " where n.nodeInfo.nsThread = :thread and n.nodeInfo.nsRight >= :right");
    updateRight.setParameter("spaceNeeded", spaceNeeded);
    updateRight.setParameter("thread", parentThread);
    updateRight.setParameter("right", newLeft);
    int updateRightCount = updateRight.executeUpdate();
    log.trace("updated right values of nested set nodes: " + updateRightCount);

    log.trace("updating the newly inserted row with thread, left, and right values");
    /*//from w  w w.  j a  v a 2s  .  com
    TODO: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1657
    Query updateNode =
        ss.createQuery("update " + nodeEntityName + " n set " +
                       " n.nodeInfo.nsLeft = :left, n.nodeInfo.nsRight = :right, n.nodeInfo.nsThread = :thread " +
                       " where n.id = :id");
     */
    Query updateNode = ss.getNamedQuery("updateNestedSet." + nodeEntityName);

    updateNode.setParameter("thread", parentThread);
    updateNode.setParameter("left", newLeft);
    updateNode.setParameter("right", newRight);
    updateNode.setParameter("id", node.getId());
    updateNode.executeUpdate();
}