From 78cf41d2a03a3e89c35589f9683f08e737368b8e Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Mon, 22 Sep 2014 12:39:11 +0200 Subject: [PATCH] =?UTF-8?q?Do=20AbstractService=20bylo=20p=C5=99id=C3=A1no?= =?UTF-8?q?=20DAO=20bez=20typov=C3=A9ho=20parametru=20pro=20obecn=C3=A9=20?= =?UTF-8?q?dotazov=C3=A1n=C3=AD=20datab=C3=A1ze.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/info/bukova/isspst/dao/BaseDao.java | 14 +++++---- .../java/info/bukova/isspst/dao/QueryDao.java | 29 ++++++++++++++++++ .../bukova/isspst/dao/jpa/QueryDaoJPA.java | 27 +++++++++++++++++ .../isspst/services/AbstractService.java | 30 +++++++++++++++++++ .../approved/ApprovedServiceImpl.java | 18 +---------- .../webapp/WEB-INF/spring/root-context.xml | 10 ++++--- 6 files changed, 101 insertions(+), 27 deletions(-) create mode 100644 src/main/java/info/bukova/isspst/dao/QueryDao.java create mode 100644 src/main/java/info/bukova/isspst/dao/jpa/QueryDaoJPA.java diff --git a/src/main/java/info/bukova/isspst/dao/BaseDao.java b/src/main/java/info/bukova/isspst/dao/BaseDao.java index bdbc7a42..c2d40da3 100644 --- a/src/main/java/info/bukova/isspst/dao/BaseDao.java +++ b/src/main/java/info/bukova/isspst/dao/BaseDao.java @@ -2,19 +2,21 @@ package info.bukova.isspst.dao; import java.util.List; -import org.hibernate.Query; -import org.hibernate.Session; - -public interface BaseDao { +/** + * Bázové rozhraní pro DAO pracující s konkrétními entitami. + * + * @author pepa + * + * @param - Třída datové entity. + */ +public interface BaseDao extends QueryDao { public void add(T entity); public void delete(T entity); public void modify(T entity); public T getById(int id); public List getAll(); - public Query getQuery(String query); public List execQuery(String query); public String getEntityName(); - public Session getSession(); } diff --git a/src/main/java/info/bukova/isspst/dao/QueryDao.java b/src/main/java/info/bukova/isspst/dao/QueryDao.java new file mode 100644 index 00000000..4b1a45f5 --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/QueryDao.java @@ -0,0 +1,29 @@ +package info.bukova.isspst.dao; + +import org.hibernate.Query; +import org.hibernate.Session; + +/** + * Rozhraní netypového DAO objektu pro obecné dotazování databáze. + * + * @author pepa + * + */ +public interface QueryDao { + + /** + * Vytvoří {@link Query} objekt na základě předaného stringu. + * + * @param query - string HQL dotazu + * @return - {@link Query} objekt + */ + public Query getQuery(String query); + + /** + * Vráti aktuální hibernat {@link Session}. + * + * @return {@link Session} + */ + public Session getSession(); + +} diff --git a/src/main/java/info/bukova/isspst/dao/jpa/QueryDaoJPA.java b/src/main/java/info/bukova/isspst/dao/jpa/QueryDaoJPA.java new file mode 100644 index 00000000..0786a333 --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/jpa/QueryDaoJPA.java @@ -0,0 +1,27 @@ +package info.bukova.isspst.dao.jpa; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; + +import info.bukova.isspst.dao.QueryDao; + +public class QueryDaoJPA implements QueryDao { + + private SessionFactory sessionFactory; + + public void setSessionFactory(SessionFactory factory) { + this.sessionFactory = factory; + } + + @Override + public Query getQuery(String query) { + return sessionFactory.getCurrentSession().createQuery(query); + } + + @Override + public Session getSession() { + return sessionFactory.getCurrentSession(); + } + +} diff --git a/src/main/java/info/bukova/isspst/services/AbstractService.java b/src/main/java/info/bukova/isspst/services/AbstractService.java index e01699e2..5289e9e2 100644 --- a/src/main/java/info/bukova/isspst/services/AbstractService.java +++ b/src/main/java/info/bukova/isspst/services/AbstractService.java @@ -5,6 +5,7 @@ import info.bukova.isspst.Constants; import info.bukova.isspst.Module; import info.bukova.isspst.SessionData; import info.bukova.isspst.dao.BaseDao; +import info.bukova.isspst.dao.QueryDao; import info.bukova.isspst.data.DataModel; import info.bukova.isspst.data.NumberSeries; import info.bukova.isspst.filters.Filter; @@ -34,6 +35,8 @@ public abstract class AbstractService implements Service private Validator validator; @Autowired protected SessionData sessionData; + @Autowired + protected QueryDao queryDao; private NumberSeriesService numberSeriesService; @@ -66,6 +69,10 @@ public abstract class AbstractService implements Service @Transactional @PreAuthorize("hasPermission(this, 'PERM_ADD')") public void add(T entity) { + if (dao == null) { + throw new IsspstException("DAO is null"); + } + validate(entity); entity.setCreated(new Date()); dao.add(entity); @@ -75,6 +82,10 @@ public abstract class AbstractService implements Service @Transactional @PreAuthorize("hasPermission(this, 'PERM_EDIT') or hasPermission(#entity, this.getUpdateEntityPermission())") public void update(T entity) { + if (dao == null) { + throw new IsspstException("DAO is null"); + } + validate(entity); entity.setModified(new Date()); dao.modify(entity); @@ -84,6 +95,10 @@ public abstract class AbstractService implements Service @Transactional @PreAuthorize("hasPermission(this, 'PERM_DELETE')") public void delete(T entity) { + if (dao == null) { + throw new IsspstException("DAO is null"); + } + dao.delete(entity); } @@ -118,6 +133,9 @@ public abstract class AbstractService implements Service @Transactional @PreAuthorize("hasPermission(this, 'PERM_READ')") public T getById(int id) { + if (dao == null) { + throw new IsspstException("DAO is null"); + } return dao.getById(id); } @@ -126,6 +144,10 @@ public abstract class AbstractService implements Service @Transactional @PreAuthorize("hasPermission(this, 'PERM_READ')") public List getAll() { + if (dao == null) { + throw new IsspstException("DAO is null"); + } + return dao.getAll(); } @@ -133,6 +155,10 @@ public abstract class AbstractService implements Service @Transactional @PreAuthorize("hasPermission(this, 'PERM_READ')") public List execQuery(String query) { + if (dao == null) { + throw new IsspstException("DAO is null"); + } + return dao.execQuery(query); } @@ -141,6 +167,10 @@ public abstract class AbstractService implements Service @Transactional @PreAuthorize("hasPermission(this, 'PERM_READ')") public T selectSingle(String query) { + if (dao == null) { + throw new IsspstException("DAO is null"); + } + try { Query q = dao.getQuery(query); return (T) q.uniqueResult(); diff --git a/src/main/java/info/bukova/isspst/services/approved/ApprovedServiceImpl.java b/src/main/java/info/bukova/isspst/services/approved/ApprovedServiceImpl.java index 67a78b2d..c6fef229 100644 --- a/src/main/java/info/bukova/isspst/services/approved/ApprovedServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/approved/ApprovedServiceImpl.java @@ -1,6 +1,5 @@ package info.bukova.isspst.services.approved; -import info.bukova.isspst.dao.BaseDao; import info.bukova.isspst.data.JoinedItem; import info.bukova.isspst.data.RequirementItem; import info.bukova.isspst.data.RequirementState; @@ -26,21 +25,6 @@ public class ApprovedServiceImpl extends AbstractService implements @Autowired UserService userService; - // V bázi je BaseDao, což nelze použít, protože třída JoinedItem nemá - // ekvivalent v databázi - // Bylo by dobré tuto funčnost vložit do báze... - BaseDao dao4Query; - - public BaseDao getDao4Query() - { - return dao4Query; - } - - public void setDao4Query(BaseDao dao4Query) - { - this.dao4Query = dao4Query; - } - @SuppressWarnings("unchecked") @Override @Transactional @@ -49,7 +33,7 @@ public class ApprovedServiceImpl extends AbstractService implements public List getAll() { List wgList = workgroupService.getUserCentres(userService.getCurrent()); - Query q = dao4Query.getQuery("select item from RequirementItem item left join item.requirement rq join rq.centre c where rq.kind is not null and rq.state = :state and c in (:wgList)"); + Query q = queryDao.getQuery("select item from RequirementItem item left join item.requirement rq join rq.centre c where rq.kind is not null and rq.state = :state and c in (:wgList)"); q.setParameterList("wgList", wgList); q.setParameter("state", RequirementState.APPROVED); List items = new ArrayList(); diff --git a/src/main/webapp/WEB-INF/spring/root-context.xml b/src/main/webapp/WEB-INF/spring/root-context.xml index 31eea4f4..89247358 100644 --- a/src/main/webapp/WEB-INF/spring/root-context.xml +++ b/src/main/webapp/WEB-INF/spring/root-context.xml @@ -179,6 +179,10 @@ + + + + @@ -305,10 +309,8 @@ - - - - + +