Project

General

Profile

1 35440 antonis.le
package eu.dnetlib.goldoa.service.dao;
2
3 46777 panagiotis
import eu.dnetlib.goldoa.domain.BankAccount;
4 39076 antonis.le
import eu.dnetlib.goldoa.domain.Publisher;
5 46738 panagiotis
import org.apache.log4j.Logger;
6 47525 antonis.le
import org.hibernate.criterion.MatchMode;
7 45460 panagiotis
import org.hibernate.criterion.Restrictions;
8
import org.springframework.stereotype.Repository;
9 46633 panagiotis
10 46707 panagiotis
import java.math.BigInteger;
11 35440 antonis.le
import java.util.List;
12
13
/**
14
 * Created by antleb on 3/13/15.
15
 */
16 45460 panagiotis
@Repository
17
public class PublisherDAO extends AbstractDao<String,Publisher>{
18 46738 panagiotis
	private static Logger logger = Logger.getLogger(PublisherDAO.class);
19 46463 panagiotis
	@SuppressWarnings("unchecked")
20 46422 panagiotis
	public List<Object> search(String term) {
21 46463 panagiotis
		return createEntityCriteria().add(Restrictions.
22 47525 antonis.le
				ilike("name",term, MatchMode.ANYWHERE)).list();
23 35440 antonis.le
	}
24 35468 antonis.le
25 46777 panagiotis
	@SuppressWarnings("unchecked")
26 39076 antonis.le
	public Publisher getPublisher(String id) {
27 46777 panagiotis
		List<Publisher> rs =  createEntityCriteria()
28
				.add(Restrictions.ilike("id", id)).list();
29
		if(rs.size() == 0)
30
			return null;
31
		return rs.get(0);
32 39076 antonis.le
	}
33 35468 antonis.le
34 39076 antonis.le
	public Publisher savePublisher(Publisher publisher) {
35 46738 panagiotis
36 47245 panagiotis
		if(publisher.getBankAccount() == null) {
37
			BigInteger id = getBankId();
38
			publisher.setBankAccount(new BankAccount(id,null, getBankAddress().toString(), null, null, null));
39
		}
40 46738 panagiotis
41
		Publisher p = getPublisher(publisher.getId());
42
		Publisher publisher_merged = null;
43
44
		if(p!=null)
45
			publisher_merged = (Publisher) getSession().merge(publisher);
46
		else
47
			persist(publisher);
48
49 45460 panagiotis
		return publisher;
50 39076 antonis.le
	}
51 35521 antonis.le
52 46707 panagiotis
    public BigInteger getBankAddress() {
53
		return (BigInteger) getSession().createSQLQuery("select nextval('publisher_bank_address_seq') as id").list().get(0);
54
55 46633 panagiotis
    }
56 46738 panagiotis
57 47245 panagiotis
	public BigInteger getBankId() {
58
		return (BigInteger) getSession().createSQLQuery("select nextval('bank_id_seq') as id").list().get(0);
59
60
	}
61
62 46738 panagiotis
    @SuppressWarnings("unchecked")
63
    public Publisher getPublisherByName(String name) {
64
		List<Publisher> rs = (List<Publisher>) createEntityCriteria()
65
				.add(Restrictions.ilike("name", name)).list();
66
		if(rs.size() > 0 )
67
			return rs.get(0);
68
		return null;
69
    }
70 38690 antonis.le
}