Example usage for org.springframework.dao.support DataAccessUtils requiredSingleResult

List of usage examples for org.springframework.dao.support DataAccessUtils requiredSingleResult

Introduction

In this page you can find the example usage for org.springframework.dao.support DataAccessUtils requiredSingleResult.

Prototype

public static <T> T requiredSingleResult(@Nullable Collection<T> results)
        throws IncorrectResultSizeDataAccessException 

Source Link

Document

Return a single result object from the given Collection.

Usage

From source file:cherry.spring.common.foundation.impl.NumberingStoreImpl.java

@Override
public NumberingDefinition getDefinition(String numberName) {
    SQLQuery query = createBaseQuery(numberName);
    List<NumberingDefinition> list = queryDslJdbcOperations.query(query,
            new QBean<NumberingDefinition>(NumberingDefinition.class, nm.template, nm.minValue, nm.maxValue));
    return DataAccessUtils.requiredSingleResult(list);
}

From source file:cherry.spring.common.foundation.impl.NumberingStoreImpl.java

@Override
public long loadAndLock(String numberName) {
    SQLQuery query = createBaseQuery(numberName);
    query.forUpdate();// w  ww  . ja v a 2  s  .  co  m
    List<Long> list = queryDslJdbcOperations.query(query, nm.currentValue);
    return DataAccessUtils.requiredSingleResult(list).longValue();
}

From source file:net.paoding.rose.jade.provider.jdbctemplate.PreparedStatementCallbackReturnId.java

@Override
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {

    if (setter != null) {
        setter.setValues(ps);/*from www  .j  a v  a2s .c o  m*/
    }

    ps.executeUpdate();

    ResultSet keys = ps.getGeneratedKeys();
    if (keys != null) {

        try {
            RowMapperResultSetExtractor extractor = new RowMapperResultSetExtractor(
                    new SingleColumnRowMapper(Number.class), 1);
            return DataAccessUtils.requiredSingleResult((List<?>) extractor.extractData(keys));
        } finally {
            JdbcUtils.closeResultSet(keys);
        }
    }

    return null;
}

From source file:io.pivotal.security.controller.v1.health.DataSourceHealthIndicator.java

private void doDataSourceHealthCheck(Health.Builder builder) throws Exception {
    String product = getProduct();
    builder.up().withDetail("database", product);
    String validationQuery = getValidationQuery(product);
    try {/*from  w w w  .  j ava  2s  . c o  m*/
        // Avoid calling getObject as it breaks MySQL on Java 7
        List<Object> results = this.jdbcTemplate.query(validationQuery, new SingleColumnRowMapper());
        Object result = DataAccessUtils.requiredSingleResult(results);
        builder.withDetail("hello", result);
    } catch (Exception ex) {
        builder.down(ex);
    }
}

From source file:cherry.spring.common.foundation.impl.WorkdayStoreImpl.java

@Override
public LocalDate getNextWorkday(String name, LocalDate from, int numberOfWorkday) {
    ListSubQuery<Tuple> subquery = new SQLSubQuery().from(a, b).list(a.d.multiply(10).add(b.d).as("n"),
            dateOperation(LocalDate.class, DateTimeOps.ADD_DAYS, constant(from), a.d.multiply(10).add(b.d))
                    .as("dt"));

    SimplePath<Tuple> d = path(Tuple.class, "d");
    NumberPath<Long> dn = numberPath(Long.class, d, "n");
    DatePath<LocalDate> ddt = datePath(LocalDate.class, d, "dt");

    SQLQuery query = queryDslJdbcOperations.newSqlQuery();
    query.from(subquery, d).leftJoin(h0).on(h0.name.eq(name), h0.dt.between(constant(from), ddt),
            h0.deletedFlg.eq(NOT_DELETED.code()));
    query.where(new SQLSubQuery().from(h1)
            .where(h1.name.eq(name), h1.dt.eq(ddt), h1.deletedFlg.eq(NOT_DELETED.code())).notExists());
    query.groupBy(dn);//from   www. jav a 2 s . c o m
    query.having(h0.dt.count().eq(dn.subtract(numberOfWorkday).add(1)));
    List<LocalDate> list = queryDslJdbcOperations.query(query, ddt.min());

    return DataAccessUtils.requiredSingleResult(list);
}

From source file:com.snowstore.mercury.indicator.DataSourceHealthIndicator.java

private void doDataSourceHealthCheck(Health.Builder builder) throws Exception {
    String[] product = getProduct().split(";");

    builder.up().withDetail("database", product[0]);
    String validationQuery = getValidationQuery(product[0]);
    if (StringUtils.hasText(validationQuery)) {
        try {//from  w ww  .  ja  v a 2  s .c  o  m
            // Avoid calling getObject as it breaks MySQL on Java 7
            List<Object> results = this.jdbcTemplate.query(validationQuery, new SingleColumnRowMapper());
            Object result = DataAccessUtils.requiredSingleResult(results);
            builder.withDetail("hello", result);
            builder.withDetail(SERVER, product[1]);
        } catch (Exception ex) {
            builder.down(ex);
        }
    }
}

From source file:org.jasig.portlet.blackboardvcportlet.service.impl.ConferenceUserServiceImpl.java

@Override
public ConferenceUser getOrCreateConferenceUser(BasicUser basicUser) {
    //If the uniqueId is specified try to find an existing user by uniqueId
    //if no existing user is found create a new internal user
    final String uniqueId = basicUser.getUniqueId();
    if (uniqueId != null) {
        ConferenceUser user = this.conferenceUserDao.getUserByUniqueId(uniqueId);
        if (user != null) {
            return user;
        }/*from   w w w.j av a  2 s.c  o  m*/

        user = this.conferenceUserDao.createInternalUser(uniqueId);
        user.setDisplayName(basicUser.getDisplayName());
        user.setEmail(basicUser.getEmail());
        user.getAdditionalEmails().addAll(basicUser.getAdditionalEmails());

        return user;
    }

    final String email = basicUser.getEmail();

    //Try to find a single user by email, it potentially possible to have more than one
    //user entry with the same primary email address in the case of a user being able to
    //chose their address
    Set<ConferenceUser> users = this.conferenceUserDao.getUsersByPrimaryEmail(email);
    if (users.size() == 1) {
        return DataAccessUtils.requiredSingleResult(users);
    }

    //If no users were found with that primary address try to find users that have the email
    //listed in their "additional" email address set
    if (users.isEmpty()) {
        users = this.conferenceUserDao.getUsersByAnyEmail(email);
        if (users.size() == 1) {
            return DataAccessUtils.requiredSingleResult(users);
        }
    }

    //Fall back to treating the user as external
    final ConferenceUser user = this.conferenceUserDao.getExternalUserByEmail(email);
    if (user != null) {
        return user;
    }

    return this.conferenceUserDao.createExternalUser(basicUser.getDisplayName(), email);
}

From source file:lib.JdbcTemplate.java

@Override
public <T> T queryForObject(String sql, RowMapper<T> rowMapper) throws DataAccessException {
    List<T> results = query(sql, rowMapper);
    return DataAccessUtils.requiredSingleResult(results);
}

From source file:architecture.common.spring.jdbc.core.ExtendedJdbcTemplate.java

public <T> T queryForObject(String sql, List<ParameterMapping> parameterMappings,
        Map<String, Object> parameters, RowMapper<T> rowMapper) throws DataAccessException {
    List<T> results = query(sql, newMappedPreparedStatementSetter(parameters, parameterMappings),
            new RowMapperResultSetExtractor<T>(rowMapper, 1));
    return DataAccessUtils.requiredSingleResult(results);
}

From source file:cc.tooyoung.common.db.JdbcTemplate.java

@SuppressWarnings("unchecked")
public Object queryForObject(String sql, RowMapper rowMapper) throws DataAccessException {
    List results = query(sql, rowMapper);
    return DataAccessUtils.requiredSingleResult(results);
}