1
|
package eu.dnetlib.enabling.is.dao;
|
2
|
|
3
|
import java.io.Serializable;
|
4
|
import java.util.List;
|
5
|
|
6
|
import javax.persistence.Table;
|
7
|
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.hibernate.Query;
|
11
|
import org.hibernate.Session;
|
12
|
import org.hibernate.SessionFactory;
|
13
|
import org.hibernate.Transaction;
|
14
|
|
15
|
import eu.dnetlib.enabling.is.hib.objects.HibDnetObject;
|
16
|
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
|
17
|
|
18
|
public class DnetSimpleDaoImpl<T extends HibDnetObject> implements DnetSimpleDao<T> {
|
19
|
|
20
|
private SessionFactory sessionFactory;
|
21
|
private Class<T> clazz;
|
22
|
private String table;
|
23
|
|
24
|
private static final Log log = LogFactory.getLog(DnetSimpleDaoImpl.class);
|
25
|
|
26
|
public DnetSimpleDaoImpl(final Class<T> clazz, final SessionFactory sessionFactory) {
|
27
|
this.clazz = clazz;
|
28
|
this.sessionFactory = sessionFactory;
|
29
|
this.table = clazz.getAnnotation(Table.class).name();
|
30
|
}
|
31
|
|
32
|
@Override
|
33
|
public void save(final T hib) throws InformationServiceException {
|
34
|
final Session session = sessionFactory.openSession();
|
35
|
try {
|
36
|
final Transaction tx = session.beginTransaction();
|
37
|
hib.save(session);
|
38
|
tx.commit();
|
39
|
} catch (Throwable e) {
|
40
|
log.error("Error saving resource, class: " + clazz, e);
|
41
|
throw new InformationServiceException("Error saving resource, class: " + clazz, e);
|
42
|
} finally {
|
43
|
session.close();
|
44
|
}
|
45
|
}
|
46
|
|
47
|
@SuppressWarnings("unchecked")
|
48
|
@Override
|
49
|
public List<T> list() throws InformationServiceException {
|
50
|
final Session session = sessionFactory.openSession();
|
51
|
try {
|
52
|
return session.createQuery("from " + table).list();
|
53
|
} catch (Throwable e) {
|
54
|
log.error("Error listing resources, class: " + clazz, e);
|
55
|
throw new InformationServiceException("Error listing resources, class: " + clazz, e);
|
56
|
} finally {
|
57
|
session.close();
|
58
|
}
|
59
|
|
60
|
}
|
61
|
|
62
|
@SuppressWarnings("unchecked")
|
63
|
@Override
|
64
|
public List<T> list(final String col, final Object value) throws InformationServiceException {
|
65
|
final Session session = sessionFactory.openSession();
|
66
|
try {
|
67
|
final Query query = session.createQuery("from " + table + " where " + col + " = :value");
|
68
|
query.setParameter("value", value);
|
69
|
return query.list();
|
70
|
} catch (Throwable e) {
|
71
|
log.error("Error listing resources, class: " + clazz, e);
|
72
|
throw new InformationServiceException("Error listing resources, class: " + clazz, e);
|
73
|
} finally {
|
74
|
session.close();
|
75
|
}
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public T find(final Serializable id) throws InformationServiceException {
|
80
|
final Session session = sessionFactory.openSession();
|
81
|
try {
|
82
|
return clazz.cast(session.get(clazz, id));
|
83
|
} catch (Throwable e) {
|
84
|
log.error("Error obtaining resource, class: " + clazz + ", id: " + id, e);
|
85
|
throw new InformationServiceException("Error obtaining resource, class: " + clazz + ", id: " + id, e);
|
86
|
} finally {
|
87
|
session.close();
|
88
|
}
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public void delete(final String id) throws InformationServiceException {
|
93
|
final Session session = sessionFactory.openSession();
|
94
|
try {
|
95
|
final Transaction tx = session.beginTransaction();
|
96
|
final T hib = clazz.cast(session.get(clazz, id));
|
97
|
hib.delete(session);
|
98
|
tx.commit();
|
99
|
} catch (Throwable e) {
|
100
|
log.error("Error deleting resource, class: " + clazz + ", id: " + id, e);
|
101
|
throw new InformationServiceException("Error deleting resource, class: " + clazz + ", id: " + id, e);
|
102
|
} finally {
|
103
|
session.close();
|
104
|
}
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public long count() throws InformationServiceException {
|
109
|
final Session session = sessionFactory.openSession();
|
110
|
try {
|
111
|
return ((Number) session.createQuery("select count(*) from " + table).uniqueResult()).longValue();
|
112
|
} catch (Throwable e) {
|
113
|
log.error("Error listing resources, class: " + clazz, e);
|
114
|
throw new InformationServiceException("Error listing resources, class: " + clazz, e);
|
115
|
} finally {
|
116
|
session.close();
|
117
|
}
|
118
|
}
|
119
|
|
120
|
public SessionFactory getSessionFactory() {
|
121
|
return sessionFactory;
|
122
|
}
|
123
|
|
124
|
public void setSessionFactory(final SessionFactory sessionFactory) {
|
125
|
this.sessionFactory = sessionFactory;
|
126
|
}
|
127
|
|
128
|
public Class<T> getClazz() {
|
129
|
return clazz;
|
130
|
}
|
131
|
|
132
|
public void setClazz(final Class<T> clazz) {
|
133
|
this.clazz = clazz;
|
134
|
}
|
135
|
|
136
|
}
|