Do AbstractService bylo přidáno DAO bez typového parametru pro obecné
dotazování databáze.
This commit is contained in:
@@ -2,19 +2,21 @@ package info.bukova.isspst.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.Session;
|
||||
|
||||
public interface BaseDao<T> {
|
||||
/**
|
||||
* Bázové rozhraní pro DAO pracující s konkrétními entitami.
|
||||
*
|
||||
* @author pepa
|
||||
*
|
||||
* @param <T> - Třída datové entity.
|
||||
*/
|
||||
public interface BaseDao<T> extends QueryDao {
|
||||
|
||||
public void add(T entity);
|
||||
public void delete(T entity);
|
||||
public void modify(T entity);
|
||||
public T getById(int id);
|
||||
public List<T> getAll();
|
||||
public Query getQuery(String query);
|
||||
public List<T> execQuery(String query);
|
||||
public String getEntityName();
|
||||
public Session getSession();
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<T extends DataModel> implements Service<T>
|
||||
private Validator validator;
|
||||
@Autowired
|
||||
protected SessionData sessionData;
|
||||
@Autowired
|
||||
protected QueryDao queryDao;
|
||||
|
||||
private NumberSeriesService numberSeriesService;
|
||||
|
||||
@@ -66,6 +69,10 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
|
||||
@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<T extends DataModel> implements Service<T>
|
||||
@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<T extends DataModel> implements Service<T>
|
||||
@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<T extends DataModel> implements Service<T>
|
||||
@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<T extends DataModel> implements Service<T>
|
||||
@Transactional
|
||||
@PreAuthorize("hasPermission(this, 'PERM_READ')")
|
||||
public List<T> getAll() {
|
||||
if (dao == null) {
|
||||
throw new IsspstException("DAO is null");
|
||||
}
|
||||
|
||||
return dao.getAll();
|
||||
}
|
||||
|
||||
@@ -133,6 +155,10 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
|
||||
@Transactional
|
||||
@PreAuthorize("hasPermission(this, 'PERM_READ')")
|
||||
public List<T> execQuery(String query) {
|
||||
if (dao == null) {
|
||||
throw new IsspstException("DAO is null");
|
||||
}
|
||||
|
||||
return dao.execQuery(query);
|
||||
}
|
||||
|
||||
@@ -141,6 +167,10 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
|
||||
@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();
|
||||
|
||||
@@ -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<JoinedItem> implements
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
// V bázi je BaseDao<T>, 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<JoinedItem> implements
|
||||
public List<JoinedItem> getAll()
|
||||
{
|
||||
List<Workgroup> 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<JoinedItem> items = new ArrayList<JoinedItem>();
|
||||
|
||||
@@ -179,6 +179,10 @@
|
||||
</bean>
|
||||
|
||||
<!-- DAO -->
|
||||
<bean id="queryDao" class="info.bukova.isspst.dao.jpa.QueryDaoJPA">
|
||||
<property name="sessionFactory" ref="sessionFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="userDao" class="info.bukova.isspst.dao.jpa.UserDaoJPA">
|
||||
<property name="sessionFactory" ref="sessionFactory"/>
|
||||
</bean>
|
||||
@@ -305,10 +309,8 @@
|
||||
<property name="xmlContext" ref="xmlCtxAres" />
|
||||
</bean>
|
||||
|
||||
<bean id="approvedService" class="info.bukova.isspst.services.approved.ApprovedServiceImpl">
|
||||
<property name="dao4Query" ref="permissionDao"/>
|
||||
</bean>
|
||||
|
||||
<bean id="approvedService" class="info.bukova.isspst.services.approved.ApprovedServiceImpl"/>
|
||||
|
||||
<bean id="permissionService" class="info.bukova.isspst.services.users.PermissionServiceImpl">
|
||||
<property name="dao" ref="permissionDao"/>
|
||||
</bean>
|
||||
|
||||
Reference in New Issue
Block a user