/
HibernateCTLSchemaDao.java
50 lines (43 loc) · 1.82 KB
/
HibernateCTLSchemaDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package org.kaaproject.kaa.server.common.dao.impl.sql;
import org.hibernate.LockMode;
import org.hibernate.criterion.Restrictions;
import org.kaaproject.kaa.server.common.dao.impl.CTLSchemaDao;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import static org.apache.commons.lang.StringUtils.isNotBlank;
@Repository
public class HibernateCTLSchemaDao extends HibernateAbstractDao<CTLSchema> implements CTLSchemaDao<CTLSchema> {
private static final Logger LOG = LoggerFactory.getLogger(HibernateCTLSchemaDao.class);
@Override
public CTLSchema save(CTLSchema o) {
if (o.getTenant() == null) {
CTLSchema ctlSchema = findByFqnAndVersion(o.getFqn(), o.getVersion());
if (ctlSchema != null) {
throw new RuntimeException("Can't save system ctl schema with same fqn and version.");
}
}
return super.save(o);
}
@Override
protected Class<CTLSchema> getEntityClass() {
return CTLSchema.class;
}
@Override
public CTLSchema findByFqnAndVersion(String fqn, Integer version) {
CTLSchema ctlSchema = null;
LOG.debug("Searching ctl schema by fqn [{}] and version [{}]", fqn, version);
if (isNotBlank(fqn) && version != null) {
ctlSchema = findOneByCriterionWithLock(Restrictions.and(
Restrictions.eq("version", version),
Restrictions.eq("fqn", fqn)), LockMode.PESSIMISTIC_WRITE);
}
if (LOG.isTraceEnabled()) {
LOG.trace("[{},{}] Search result: {}.", fqn, version, ctlSchema);
} else {
LOG.debug("[{},{}] Search result: {}.", fqn, version, ctlSchema != null);
}
return ctlSchema;
}
}