Do AbstractService bylo přidáno DAO bez typového parametru pro obecné

dotazování databáze.
multitenant
Josef Rokos 10 years ago
parent 6835dd2f7c
commit 78cf41d2a0

@ -2,19 +2,21 @@ package info.bukova.isspst.dao;
import java.util.List; import java.util.List;
import org.hibernate.Query; /**
import org.hibernate.Session; * Bázové rozhraní pro DAO pracující s konkrétními entitami.
*
public interface BaseDao<T> { * @author pepa
*
* @param <T> - Třída datové entity.
*/
public interface BaseDao<T> extends QueryDao {
public void add(T entity); public void add(T entity);
public void delete(T entity); public void delete(T entity);
public void modify(T entity); public void modify(T entity);
public T getById(int id); public T getById(int id);
public List<T> getAll(); public List<T> getAll();
public Query getQuery(String query);
public List<T> execQuery(String query); public List<T> execQuery(String query);
public String getEntityName(); 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.Module;
import info.bukova.isspst.SessionData; import info.bukova.isspst.SessionData;
import info.bukova.isspst.dao.BaseDao; import info.bukova.isspst.dao.BaseDao;
import info.bukova.isspst.dao.QueryDao;
import info.bukova.isspst.data.DataModel; import info.bukova.isspst.data.DataModel;
import info.bukova.isspst.data.NumberSeries; import info.bukova.isspst.data.NumberSeries;
import info.bukova.isspst.filters.Filter; import info.bukova.isspst.filters.Filter;
@ -34,6 +35,8 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
private Validator validator; private Validator validator;
@Autowired @Autowired
protected SessionData sessionData; protected SessionData sessionData;
@Autowired
protected QueryDao queryDao;
private NumberSeriesService numberSeriesService; private NumberSeriesService numberSeriesService;
@ -66,6 +69,10 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
@Transactional @Transactional
@PreAuthorize("hasPermission(this, 'PERM_ADD')") @PreAuthorize("hasPermission(this, 'PERM_ADD')")
public void add(T entity) { public void add(T entity) {
if (dao == null) {
throw new IsspstException("DAO is null");
}
validate(entity); validate(entity);
entity.setCreated(new Date()); entity.setCreated(new Date());
dao.add(entity); dao.add(entity);
@ -75,6 +82,10 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
@Transactional @Transactional
@PreAuthorize("hasPermission(this, 'PERM_EDIT') or hasPermission(#entity, this.getUpdateEntityPermission())") @PreAuthorize("hasPermission(this, 'PERM_EDIT') or hasPermission(#entity, this.getUpdateEntityPermission())")
public void update(T entity) { public void update(T entity) {
if (dao == null) {
throw new IsspstException("DAO is null");
}
validate(entity); validate(entity);
entity.setModified(new Date()); entity.setModified(new Date());
dao.modify(entity); dao.modify(entity);
@ -84,6 +95,10 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
@Transactional @Transactional
@PreAuthorize("hasPermission(this, 'PERM_DELETE')") @PreAuthorize("hasPermission(this, 'PERM_DELETE')")
public void delete(T entity) { public void delete(T entity) {
if (dao == null) {
throw new IsspstException("DAO is null");
}
dao.delete(entity); dao.delete(entity);
} }
@ -118,6 +133,9 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
@Transactional @Transactional
@PreAuthorize("hasPermission(this, 'PERM_READ')") @PreAuthorize("hasPermission(this, 'PERM_READ')")
public T getById(int id) { public T getById(int id) {
if (dao == null) {
throw new IsspstException("DAO is null");
}
return dao.getById(id); return dao.getById(id);
} }
@ -126,6 +144,10 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
@Transactional @Transactional
@PreAuthorize("hasPermission(this, 'PERM_READ')") @PreAuthorize("hasPermission(this, 'PERM_READ')")
public List<T> getAll() { public List<T> getAll() {
if (dao == null) {
throw new IsspstException("DAO is null");
}
return dao.getAll(); return dao.getAll();
} }
@ -133,6 +155,10 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
@Transactional @Transactional
@PreAuthorize("hasPermission(this, 'PERM_READ')") @PreAuthorize("hasPermission(this, 'PERM_READ')")
public List<T> execQuery(String query) { public List<T> execQuery(String query) {
if (dao == null) {
throw new IsspstException("DAO is null");
}
return dao.execQuery(query); return dao.execQuery(query);
} }
@ -141,6 +167,10 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
@Transactional @Transactional
@PreAuthorize("hasPermission(this, 'PERM_READ')") @PreAuthorize("hasPermission(this, 'PERM_READ')")
public T selectSingle(String query) { public T selectSingle(String query) {
if (dao == null) {
throw new IsspstException("DAO is null");
}
try { try {
Query q = dao.getQuery(query); Query q = dao.getQuery(query);
return (T) q.uniqueResult(); return (T) q.uniqueResult();

@ -1,6 +1,5 @@
package info.bukova.isspst.services.approved; package info.bukova.isspst.services.approved;
import info.bukova.isspst.dao.BaseDao;
import info.bukova.isspst.data.JoinedItem; import info.bukova.isspst.data.JoinedItem;
import info.bukova.isspst.data.RequirementItem; import info.bukova.isspst.data.RequirementItem;
import info.bukova.isspst.data.RequirementState; import info.bukova.isspst.data.RequirementState;
@ -26,21 +25,6 @@ public class ApprovedServiceImpl extends AbstractService<JoinedItem> implements
@Autowired @Autowired
UserService userService; 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") @SuppressWarnings("unchecked")
@Override @Override
@Transactional @Transactional
@ -49,7 +33,7 @@ public class ApprovedServiceImpl extends AbstractService<JoinedItem> implements
public List<JoinedItem> getAll() public List<JoinedItem> getAll()
{ {
List<Workgroup> wgList = workgroupService.getUserCentres(userService.getCurrent()); 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.setParameterList("wgList", wgList);
q.setParameter("state", RequirementState.APPROVED); q.setParameter("state", RequirementState.APPROVED);
List<JoinedItem> items = new ArrayList<JoinedItem>(); List<JoinedItem> items = new ArrayList<JoinedItem>();

@ -179,6 +179,10 @@
</bean> </bean>
<!-- DAO --> <!-- 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"> <bean id="userDao" class="info.bukova.isspst.dao.jpa.UserDaoJPA">
<property name="sessionFactory" ref="sessionFactory"/> <property name="sessionFactory" ref="sessionFactory"/>
</bean> </bean>
@ -305,10 +309,8 @@
<property name="xmlContext" ref="xmlCtxAres" /> <property name="xmlContext" ref="xmlCtxAres" />
</bean> </bean>
<bean id="approvedService" class="info.bukova.isspst.services.approved.ApprovedServiceImpl"> <bean id="approvedService" class="info.bukova.isspst.services.approved.ApprovedServiceImpl"/>
<property name="dao4Query" ref="permissionDao"/>
</bean>
<bean id="permissionService" class="info.bukova.isspst.services.users.PermissionServiceImpl"> <bean id="permissionService" class="info.bukova.isspst.services.users.PermissionServiceImpl">
<property name="dao" ref="permissionDao"/> <property name="dao" ref="permissionDao"/>
</bean> </bean>

Loading…
Cancel
Save