Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAA-600: Update CTL dao implementation.
- Loading branch information
Igor Khanenko
committed
Nov 11, 2015
1 parent
b98e402
commit de63ebb
Showing
7 changed files
with
113 additions
and
27 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/CTLSchemaDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
package org.kaaproject.kaa.server.common.dao.impl; | ||
|
||
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema; | ||
|
||
public interface CTLSchemaDao<T> extends SqlDao<T> { | ||
CTLSchema findByFqnAndVersion(String fqn, Integer version); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 30 additions & 7 deletions
37
...ao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/sql/HibernateCTLSchemaDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,50 @@ | ||
package org.kaaproject.kaa.server.common.dao.impl.sql; | ||
|
||
import org.hibernate.LockMode; | ||
import org.hibernate.LockOptions; | ||
import org.hibernate.Session; | ||
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> { | ||
|
||
LockOptions lockOptions = new LockOptions(LockMode.PESSIMISTIC_WRITE); | ||
private static final Logger LOG = LoggerFactory.getLogger(HibernateCTLSchemaDao.class); | ||
|
||
@Override | ||
public CTLSchema save(CTLSchema o) { | ||
Session session = getSession(); | ||
CTLSchema t = (CTLSchema) session.merge(o); | ||
session.flush(); | ||
return t; | ||
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
.../dao/src/main/java/org/kaaproject/kaa/server/common/dao/service/CTLSchemaServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.kaaproject.kaa.server.common.dao.service; | ||
|
||
|
||
import org.kaaproject.kaa.server.common.dao.impl.CTLSchemaDao; | ||
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
public class CTLSchemaServiceImpl { | ||
|
||
@Autowired | ||
private CTLSchemaDao<CTLSchema> ctlSchemaDao; | ||
|
||
public CTLSchema save(CTLSchema ctlSchema) { | ||
return ctlSchemaDao.save(ctlSchema); | ||
} | ||
} |
62 changes: 43 additions & 19 deletions
62
...rc/test/java/org/kaaproject/kaa/server/common/dao/impl/sql/HibernateCTLSchemaDaoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
.../src/test/java/org/kaaproject/kaa/server/common/dao/service/CTLSchemaServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.kaaproject.kaa.server.common.dao.service; | ||
|
||
|
||
|
||
public class CTLSchemaServiceImplTest { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters