Search in sources :

Example 71 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.

the class UserQueryDaoImpl method getUserQueryByPublicId.

@Override
public UserQuery getUserQueryByPublicId(String publicId) {
    String sql = sqlDictionary.getSQLQuery("read-user-query-by-pubid");
    MapSqlParameterSource namedParameters = new MapSqlParameterSource();
    namedParameters.addValue("public_id", publicId);
    UserQuery query = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource()).queryForObject(sql, namedParameters, new UserQueryRowMapper());
    long queryId = query.getUserQueryId();
    Map<Long, Set<String>> tags = getQueryTags(Arrays.asList(queryId));
    query.setTags(tags.get(queryId));
    return query;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ResultSet(java.sql.ResultSet) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) UserQuery(org.nextprot.api.user.domain.UserQuery)

Example 72 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.

the class UserQueryDaoImpl method queryList.

/**
 * Get user query list and extract tags
 *
 * @param sql the select from user_queries sql query
 * @param source
 * @return
 */
private List<UserQuery> queryList(String sql, SqlParameterSource source) {
    List<UserQuery> userQueryList = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource()).query(sql, source, new UserQueryRowMapper());
    if (!userQueryList.isEmpty()) {
        List<Long> queryIds = Lists.transform(userQueryList, UserQueryUtils.EXTRACT_QUERY_ID);
        Map<Long, Set<String>> tags = getQueryTags(queryIds);
        for (UserQuery query : userQueryList) {
            Set<String> tagSet = tags.get(query.getUserQueryId());
            // use hashset because google implementation is not serializable
            query.setTags(new HashSet<String>(tagSet));
        }
    }
    return userQueryList;
}
Also used : ResultSet(java.sql.ResultSet) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) UserQuery(org.nextprot.api.user.domain.UserQuery)

Example 73 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.

the class UserQueryDaoImpl method updateUserQuery.

@Override
public void updateUserQuery(final UserQuery src) {
    final String UPDATE_SQL = sqlDictionary.getSQLQuery("update-user-query");
    MapSqlParameterSource namedParameters = new MapSqlParameterSource();
    // key to identify query to update
    namedParameters.addValue("query_id", src.getUserQueryId());
    // values to update
    namedParameters.addValue("title", src.getTitle());
    namedParameters.addValue("description", src.getDescription());
    namedParameters.addValue("sparql", src.getSparql());
    namedParameters.addValue("published", src.getPublished() ? 'Y' : 'N');
    NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource());
    int affectedRows = jdbcTemplate.update(UPDATE_SQL, namedParameters);
    if (affectedRows != 1) {
        String msg = "oops something wrong occurred" + affectedRows + " rows were affected instead of only 1.";
        Logger.error(msg);
        throw new NextProtException(msg);
    }
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)

Example 74 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.

the class UserQueryDaoImpl method deleteUserQueryTags.

@Override
public int deleteUserQueryTags(long queryId, Set<String> tags) {
    final String DELETE_SQL = sqlDictionary.getSQLQuery("delete-user-query-tags");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("tags", tags);
    return new NamedParameterJdbcTemplate(dsLocator.getUserDataSource()).update(DELETE_SQL, params);
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)

Example 75 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.

the class UserQueryDaoImpl method getQueryTags.

@Override
public Map<Long, Set<String>> getQueryTags(Collection<Long> queryIds) {
    String sql = sqlDictionary.getSQLQuery("read-tags-by-user-query-ids");
    SqlParameterSource namedParameters = new MapSqlParameterSource("query_ids", queryIds);
    List<Tag> foundTags = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource()).query(sql, namedParameters, new UserQueryTagRowMapper());
    Map<Long, Set<String>> map = new HashMap<Long, Set<String>>();
    Set<Long> foundQueries = new HashSet<Long>();
    for (Tag tag : foundTags) {
        foundQueries.add(tag.getQueryId());
        if (!map.containsKey(tag.getQueryId())) {
            map.put(tag.getQueryId(), new HashSet<String>());
        }
        map.get(tag.getQueryId()).add(tag.getName());
    }
    for (long queryId : queryIds) {
        if (!foundQueries.contains(queryId))
            map.put(queryId, new HashSet<String>());
    }
    return map;
}
Also used : ResultSet(java.sql.ResultSet) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource)

Aggregations

NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)119 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)66 SqlParameterSource (org.springframework.jdbc.core.namedparam.SqlParameterSource)42 HashMap (java.util.HashMap)30 ResultSet (java.sql.ResultSet)16 SQLException (java.sql.SQLException)16 DbXref (org.nextprot.api.core.domain.DbXref)8 NextProtException (org.nextprot.api.commons.exception.NextProtException)7 PublicationDbXref (org.nextprot.api.core.domain.PublicationDbXref)7 ArrayList (java.util.ArrayList)5 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)5 List (java.util.List)4 Map (java.util.Map)4 CvTerm (org.nextprot.api.core.domain.CvTerm)4 GargoyleException (com.kyj.fx.voeditor.visual.exceptions.GargoyleException)3 NotSupportException (com.kyj.fx.voeditor.visual.exceptions.NotSupportException)3 DataSource (javax.sql.DataSource)3 UserProteinList (org.nextprot.api.user.domain.UserProteinList)3 ServiceException (com.netsteadfast.greenstep.base.exception.ServiceException)2 Date (java.util.Date)2