Example usage for org.apache.commons.dbutils.handlers ScalarHandler ScalarHandler

List of usage examples for org.apache.commons.dbutils.handlers ScalarHandler ScalarHandler

Introduction

In this page you can find the example usage for org.apache.commons.dbutils.handlers ScalarHandler ScalarHandler.

Prototype

public ScalarHandler(String columnName) 

Source Link

Document

Creates a new instance of ScalarHandler.

Usage

From source file:com.che.software.testato.domain.dao.jdbc.impl.ProjectDAO.java

/**
 * Checks if a project with given name is already existing.
 * //from  ww w.  j a v a 2 s .  c o m
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param name the project name to check
 * @return true if this name is already used, else false.
 * @since July, 2011.
 * @throws ProjectSearchDAOException if an error occurs during the search.
 */
@Override
public boolean isProjectExists(String name) throws ProjectSearchDAOException {
    LOGGER.debug("isProjectExists().");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        return (Boolean) getQueryRunner().query(connection,
                "SELECT EXISTS( SELECT project_id FROM project WHERE name = ?) AS result ",
                new ScalarHandler("result"), new Object[] { name });
    } catch (SQLException e) {
        throw new ProjectSearchDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.ActionDAO.java

/**
 * Creates a map from an action plan id, a list of intentions and a list of
 * sections./*w ww. j  a v  a 2s .com*/
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param actionPlanId the action plan id to be linked to the created
 *        actions.
 * @param intentions the intentions to add in the map.
 * @param sections the sections to add in the map.
 * @since July, 2011.
 * @throws ActionCreationDAOException if an error occurs during the
 *         creation.
 */
@Override
public void createMap(int actionPlanId, List<Intention> intentions, List<Section> sections)
        throws ActionCreationDAOException {
    LOGGER.debug("createMap(" + actionPlanId + "," + intentions.size() + " intentions," + sections.size()
            + " sections).");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        connection.setAutoCommit(false);
        List<Integer> createdIntentionsId = new ArrayList<Integer>();
        for (Intention intention : intentions) {
            getQueryRunner().update(connection,
                    "INSERT INTO intention(intention_id, label) VALUES(nextval('intention_seq'), ?) ",
                    new Object[] { intention.getLabel() });
            createdIntentionsId.add((Integer) getQueryRunner().query(connection,
                    "SELECT MAX(intention_id)::int AS intentionId FROM intention ",
                    new ScalarHandler("intentionId")));
        }
        List<Intention> createdIntentions = getQueryRunner().query(connection,
                "SELECT intention_id AS intentionId, label FROM intention WHERE intention_id IN("
                        + getInClauseFromIntentionsIds(createdIntentionsId) + ") ",
                new BeanListHandler<Intention>(Intention.class));
        LOGGER.debug(createdIntentions.size() + " intentions created with success...");
        for (Section section : sections) {
            boolean source = false, target = false;
            for (Intention intention : createdIntentions) {
                if (!source && intention.getLabel().equalsIgnoreCase(section.getSourceIntention())) {
                    section.setSourceIntentionId(intention.getIntentionId());
                    source = true;
                }
                if (!target && intention.getLabel().equalsIgnoreCase(section.getTargetIntention())) {
                    section.setTargetIntentionId(intention.getIntentionId());
                    target = true;
                }
                if (target && source) {
                    break;
                }
            }
            Integer actionId = (Integer) getQueryRunner().query(connection,
                    "SELECT action_id::int AS actionId FROM action WHERE action_plan_id = ? AND source_intention = ? AND target_intention = ? ",
                    new ScalarHandler("actionId"), new Object[] { actionPlanId, section.getSourceIntentionId(),
                            section.getTargetIntentionId() });
            if (null == actionId) {
                LOGGER.debug("Action creation...");
                getQueryRunner().update(connection,
                        "INSERT INTO action(action_id, target_intention, source_intention, action_plan_id) VALUES(nextval('action_seq'),?,?,?) ",
                        new Object[] { section.getTargetIntentionId(), section.getSourceIntentionId(),
                                actionPlanId });
                actionId = (Integer) getQueryRunner().query(connection,
                        "SELECT action_id::int AS actionId FROM action WHERE action_plan_id = ? AND source_intention = ? AND target_intention = ? ",
                        new ScalarHandler("actionId"), new Object[] { actionPlanId,
                                section.getSourceIntentionId(), section.getTargetIntentionId() });
            }
            getQueryRunner().update(connection,
                    "INSERT INTO item(item_id, label, iteration_max_number) VALUES(nextval('item_seq'), ?, ?) ",
                    new Object[] { section.getStrategyLabel(), section.getMaxIterationNumber() });
            Integer createdItemId = (Integer) getQueryRunner().query(connection,
                    "SELECT MAX(item_id)::int AS itemId FROM item ", new ScalarHandler("itemId"));
            getQueryRunner().update(connection,
                    (section.isExclusive())
                            ? "INSERT INTO action_exclusive_item(action_id, item_id) VALUES(?,?) "
                            : "INSERT INTO action_inclusive_item(action_id, item_id) VALUES(?,?) ",
                    new Object[] { actionId, createdItemId });
        }
        connection.commit();
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (SQLException e1) {
            throw new ActionCreationDAOException(e1);
        }
        throw new ActionCreationDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.IterationDAO.java

/**
 * Creates the next iteration (both analytical and selective) for a given
 * prioritization.// w  ww.j  a  v  a  2  s  .  c om
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param prioritization the given prioritization.
 * @param scripts the scripts to use for this depth.
 * @since July, 2011.
 * @throws IterationCreationDAOException if an error occurs during the
 *         creation.
 */
@Override
public void createNextIteration(Prioritization prioritization, List<Script> scripts)
        throws IterationCreationDAOException {
    LOGGER.debug("createNextIteration(" + prioritization.getPrioritizationId() + ").");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        connection.setAutoCommit(false);
        getQueryRunner().update(connection,
                "INSERT INTO iteration(iteration_id, prioritization_id) VALUES(nextval('iteration_seq'), ?) ",
                new Object[] { prioritization.getPrioritizationId() });
        Integer createdIterationId = (Integer) getQueryRunner().query(connection,
                "SELECT MAX(iteration_id)::int AS iterationId FROM iteration ",
                new ScalarHandler("iterationId"));
        getQueryRunner().update(connection,
                "INSERT INTO iteration_assignment(iteration_assignment_id, criterion_type, iteration_id, analytical_prioritization_status) VALUES(nextval('iteration_assignment_seq'), ?, ?, ?) ",
                new Object[] { CriterionTypes.COST.name(), createdIterationId,
                        AssignmentStatus.NOT_ASSIGNED.name() });
        createComparisonMatrixItem(connection, scripts,
                (Integer) getQueryRunner().query(connection,
                        "SELECT MAX(iteration_assignment_id)::int AS result FROM iteration_assignment ",
                        new ScalarHandler("result")));
        getQueryRunner().update(connection,
                "INSERT INTO iteration_assignment(iteration_assignment_id, criterion_type, iteration_id, analytical_prioritization_status) VALUES(nextval('iteration_assignment_seq'), ?, ?, ?) ",
                new Object[] { CriterionTypes.FIT.name(), createdIterationId,
                        AssignmentStatus.NOT_ASSIGNED.name() });
        createComparisonMatrixItem(connection, scripts,
                (Integer) getQueryRunner().query(connection,
                        "SELECT MAX(iteration_assignment_id)::int AS result FROM iteration_assignment ",
                        new ScalarHandler("result")));
        getQueryRunner().update(connection,
                "INSERT INTO iteration_assignment(iteration_assignment_id, criterion_type, iteration_id, analytical_prioritization_status) VALUES(nextval('iteration_assignment_seq'), ?, ?, ?) ",
                new Object[] { CriterionTypes.RISK.name(), createdIterationId,
                        AssignmentStatus.NOT_ASSIGNED.name() });
        createComparisonMatrixItem(connection, scripts,
                (Integer) getQueryRunner().query(connection,
                        "SELECT MAX(iteration_assignment_id)::int AS result FROM iteration_assignment ",
                        new ScalarHandler("result")));
        getQueryRunner().update(connection,
                "INSERT INTO iteration_assignment(iteration_assignment_id, criterion_type, iteration_id, analytical_prioritization_status) VALUES(nextval('iteration_assignment_seq'), ?, ?, ?) ",
                new Object[] { CriterionTypes.VALUE.name(), createdIterationId,
                        AssignmentStatus.NOT_ASSIGNED.name() });
        createComparisonMatrixItem(connection, scripts,
                (Integer) getQueryRunner().query(connection,
                        "SELECT MAX(iteration_assignment_id)::int AS result FROM iteration_assignment ",
                        new ScalarHandler("result")));
        getQueryRunner().update(connection,
                "INSERT INTO iteration_assignment(iteration_assignment_id, iteration_id, selective_prioritization_status) VALUES(nextval('iteration_assignment_seq'), ?, ?) ",
                new Object[] { createdIterationId, AssignmentStatus.NOT_ASSIGNED.name() });
        connection.commit();
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (SQLException e1) {
            throw new IterationCreationDAOException(e1);
        }
        throw new IterationCreationDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.ServiceDAO.java

/**
 * Retrieves the id of the last user hired by a specific service.
 * /*www  .  jav a 2s  .com*/
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param serviceId the service id.
 * @return the resulting user id.
 * @since July, 2011.
 * @throws ServiceSearchDAOException if an error occurs during the search.
 */
@Override
public int getLastHiredUserIdFromServiceId(int serviceId) throws ServiceSearchDAOException {
    LOGGER.debug("getLastHiredUserIdFromServiceId(" + serviceId + ").");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        Object res = getQueryRunner().query(connection,
                "SELECT user_id::integer AS userId FROM user_service WHERE service_id = ? ORDER BY hired_date DESC ",
                new ScalarHandler("userId"), new Object[] { serviceId });
        return (null != res) ? ((Integer) res) : -1;
    } catch (SQLException e) {
        throw new ServiceSearchDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.ItemDAO.java

/**
 * Update an item to refine it./* w w w.  j av a  2 s .  c o  m*/
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param itemId the item id.
 * @return the created action plan id.
 * @since July, 2011.
 * @throws ItemUpdateDAOException if an error occurs during the update.
 */
@Override
public int refineItem(int itemId) throws ItemUpdateDAOException {
    LOGGER.debug("refineItem(" + itemId + ").");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        connection.setAutoCommit(false);
        getQueryRunner().update(connection,
                "INSERT INTO action_plan(action_plan_id) VALUES(nextval('action_plan_seq')) ");
        Integer createdActionPlan = (Integer) getQueryRunner().query(connection,
                "SELECT MAX(action_plan_id)::int AS actionPlanId FROM action_plan ",
                new ScalarHandler("actionPlanId"));
        getQueryRunner().update(connection, "UPDATE item SET action_plan_id = ? WHERE item_id = ? ",
                new Object[] { createdActionPlan, itemId });
        connection.commit();
        return createdActionPlan;
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (SQLException e1) {
            throw new ItemUpdateDAOException(e1);
        }
        throw new ItemUpdateDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:com.qcloud.component.snaker.access.mybatis.MybatisAccess.java

/**
 * /*from   w w  w.  jav a2  s  . c  o m*/
 * @param column ?
 * @param sql sql?
 * @param params ?
 * @return 
 */
public Object query(int column, String sql, Object... params) {

    SqlSession sqlSession = getSession();
    Object result;
    try {
        if (log.isDebugEnabled()) {
            log.debug("??=\n" + sql);
        }
        result = runner.query(sqlSession.getConnection(), sql, new ScalarHandler(column), params);
    } catch (SQLException e) {
        log.error(e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        SqlSessionUtils.closeSqlSession(sqlSession, getSqlSessionFactory());
    }
    return result;
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.VariantDAO.java

/**
 * Creates variants for a given test case. Is called after the variants
 * generation./*from   w w  w  . ja va 2 s .co m*/
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param testCaseId the test case id.
 * @param variants the list of generated variants.
 * @since July, 2011.
 * @throws VariantCreationDAOException if an error occurs during the
 *         creation.
 */
@Override
public void createVariantsFromTestCaseId(int testCaseId, List<VariantCreation> variants)
        throws VariantCreationDAOException {
    LOGGER.debug("createVariantsFromTestCaseId(" + testCaseId + "," + variants.size() + " variants)");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        connection.setAutoCommit(false);
        for (VariantCreation variant : variants) {
            getQueryRunner().update(connection,
                    "INSERT INTO variant(variant_id, variant_type, test_case_id, label, quantitative_criterion) VALUES(nextval('variant_id_seq'),?,?,NULL,NULL) ",
                    new Object[] { variant.getVariantType().name(), testCaseId });
            Integer createdVariantId = (Integer) getQueryRunner().query(connection,
                    "SELECT MAX(variant_id)::int AS result FROM variant ", new ScalarHandler("result"));
            for (Element element : variant.getElements()) {
                getQueryRunner().update(connection,
                        "INSERT INTO variant_element(element_id, variant_id) VALUES(?,?) ",
                        new Object[] { element.getElementId(), createdVariantId });
            }
            for (ProceduralArrow transition : variant.getTransitions()) {
                getQueryRunner().update(connection,
                        "INSERT INTO variant_transition(transition_id, variant_id) VALUES(?,?) ",
                        new Object[] { transition.getTransitionId(), createdVariantId });
            }
        }
        connection.commit();
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (SQLException e1) {
            throw new VariantCreationDAOException(e1);
        }
        throw new VariantCreationDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.ScriptDAO.java

/**
 * Checks if some script have been elicited for a given hierarchy.
 * //from w  w w  .  j  a  v a2 s . c  om
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param hierarchyId the hierarchy id.
 * @return true if the hierarchy has already been elicitate, else false.
 * @since July, 2011.
 * @throws ScriptSearchDAOException if an error occurs during the search.
 */
@Override
public boolean isScriptExistingFromHierarchyId(int hierarchyId) throws ScriptSearchDAOException {
    LOGGER.debug("isScriptExistingFromHierarchyId(" + hierarchyId + ").");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        return (Boolean) getQueryRunner().query(connection,
                "SELECT EXISTS ( SELECT script_id FROM script WHERE hierarchy_id = ? ) AS result ",
                new ScalarHandler("result"), new Object[] { hierarchyId });
    } catch (SQLException e) {
        throw new ScriptSearchDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.ServiceDAO.java

/**
 * Retrieves the users number of a specific service.
 * //from w  w w.jav a 2 s .com
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param serviceId the service id.
 * @return the resulting number.
 * @since July, 2011.
 * @throws ServiceSearchDAOException if an error occurs during the search.
 */
@Override
public long getUsersNumberFromServiceId(int serviceId) throws ServiceSearchDAOException {
    LOGGER.debug("getUsersNumberFromServiceId(" + serviceId + ").");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        return (Long) getQueryRunner().query(connection,
                "SELECT COUNT(*) AS usersNb FROM user_service WHERE service_id = ? ",
                new ScalarHandler("usersNb"), new Object[] { serviceId });
    } catch (SQLException e) {
        throw new ServiceSearchDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.VersionDAO.java

/**
 * Retrieves the versions number of a specific project.
 * //from   ww w  .  j a v a  2 s  .  co  m
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param projectId the project id.
 * @return the resulting number.
 * @since July, 2011.
 * @throws VersionSearchDAOException if an error occurs during the search.
 */
@Override
public long getVersionsNumberFromProjectId(int projectId) throws VersionSearchDAOException {
    LOGGER.debug("getVersionsNumberFromProjectId(" + projectId + ").");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        return (Long) getQueryRunner().query(connection,
                "SELECT COUNT(*) AS versionsNb FROM version WHERE project_id = ? ",
                new ScalarHandler("versionsNb"), new Object[] { projectId });
    } catch (SQLException e) {
        throw new VersionSearchDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}