Project

General

Profile

1
package eu.dnetlib.goldoa.service;
2

    
3
import eu.dnetlib.goldoa.domain.Funder;
4
import eu.dnetlib.goldoa.domain.Vocabulary;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.dao.EmptyResultDataAccessException;
7
import org.springframework.jdbc.core.JdbcTemplate;
8
import org.springframework.jdbc.core.RowMapper;
9

    
10
import javax.sql.DataSource;
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.sql.Types;
14
import java.util.List;
15

    
16
/**
17
 * Created by antleb on 5/6/15.
18
 */
19
public class FunderManagerImpl implements FunderManager {
20

    
21
	@Autowired
22
	private DataSource dataSource;
23

    
24
	private final static String SEARCH_FUNDERS = "select id, name from funder where lower(name) like lower(?)";
25
	private final static String GET_FUNDER = "select id, name, url, source from funder where id=?";
26

    
27
	@Override
28
	public List<Vocabulary> search(String term) {
29
		return new JdbcTemplate(dataSource).query(SEARCH_FUNDERS, new String[]{"%" + term + "%"}, new int[]{Types.VARCHAR}, new RowMapper<Vocabulary>() {
30
			@Override
31
			public Vocabulary mapRow(ResultSet rs, int rowNum) throws SQLException {
32
				return new Vocabulary(rs.getString("id"), rs.getString("name"));
33
			}
34
		});
35
	}
36

    
37
	@Override
38
	public Funder getById(String id) {
39
		try {
40
			return new JdbcTemplate(dataSource).queryForObject(GET_FUNDER, new String[]{id}, new int[]{Types.VARCHAR}, new RowMapper<Funder>() {
41
				@Override
42
				public Funder mapRow(ResultSet rs, int rowNum) throws SQLException {
43
					return new Funder(rs.getString("id"), rs.getString("name"), rs.getString("url"), rs.getString("source"));
44
				}
45
			});
46
		} catch (EmptyResultDataAccessException e) {
47
			return null;
48
		}
49
	}
50
}
(8-8/29)