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;
}
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;
}
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);
}
}
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);
}
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;
}
Aggregations