/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import domain.Client;
import domain.Collaborator;
import domain.District;
import domain.EntityWithId;
import domain.Lpu;
import domain.Speciality;
import domain.UserClient;
import domain.UserRequest;
import domain.address.AddressDistrict;
import domain.address.AddressObject;
import domain.shedule.SheduleReception;
import domain.shedule.WorkType;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;
import utils.DAOState;
import utils.LoadHelper;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import web.operator.request.ClientData;
/**
*
* @author axe
*/
public class DAOImpl extends HibernateDaoSupport implements DAO {
protected static final Logger log = Logger.getLogger(DAOImpl.class);
public DAOImpl() {
}
@Override
public <T> T getById(Class<T> clazz, int id) {
return (T) getHibernateTemplate().get(clazz.getName(), id);
}
@Override
public <T> List<T> getList(Class<T> clazz) {
return getList(clazz, null, null);
}
@Override
public <T> List<T> getList(Class<T> clazz, Integer limit) {
return getList(clazz, null, limit);
}
@Override
public <T> List<T> getList(Class<T> clazz, Integer offset, Integer limit) {
return getList(DetachedCriteria.forClass(clazz), offset, limit);
}
@Override
public List getList(DetachedCriteria criteria, Integer offset, Integer limit) {
if (offset == null && limit == null) {
return getHibernateTemplate().findByCriteria(criteria);
}
int iOffset = (offset == null) ? 0 : offset.intValue();
int iLimit = (limit == null) ? Integer.MAX_VALUE : limit.intValue();
return getHibernateTemplate().findByCriteria(criteria, iOffset, iLimit);
}
@Override
public <T> List getList(final Class<T> clazz, Integer offset, Integer limit, final LoadHelper<T> helper) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
List items = session.createCriteria(clazz).list();
for (Object com : items) {
helper.load((T) com);
}
return items;
}
});
}
@Override
public Object getValue(final DetachedCriteria criteria) {
return getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return criteria.getExecutableCriteria(session).list().get(0);
}
});
}
@Override
public int getCount(final Class clazz) {
return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Integer doInHibernate(Session session) throws HibernateException, SQLException {
return (Integer) (session.createCriteria(clazz)
.setProjection(Projections.rowCount()).list().get(0));
}
});
}
@Override
public void save(Object obj) {
if(obj instanceof DAOState) {
log.debug("SAVEEX: " + obj.toString());
DAOState ds = (DAOState) obj;
ds.save(getHibernateTemplate());
} else {
log.debug("SAVE: " + obj.toString());
if (obj instanceof EntityWithId) {
if (((EntityWithId)obj).getId() == 0) {
getHibernateTemplate().save(obj);
} else {
getHibernateTemplate().update(obj);
}
} else {
getHibernateTemplate().saveOrUpdate(obj);
}
}
}
@Override
public void saveAndRefresh(Object fac) {
fac = getHibernateTemplate().save(fac);
//getHibernateTemplate().refresh(fac);
}
@Override
public void save(Collection objects) {
for(Object o:objects) {
save(o);
}
}
@Override
public void save(Object[] objects) {
for(Object o:objects) {
save(o);
}
}
@Override
public void delete(Object obj) {
if(obj instanceof DAOState) {
log.debug("DELETEEX: " + obj.toString());
DAOState ds = (DAOState) obj;
ds.delete(getHibernateTemplate());
} else {
log.debug("DELETE: " + obj.toString());
getHibernateTemplate().delete(obj);
}
}
@Override
public void delete(Collection objects) {
for(Object o:objects) {
delete(o);
}
}
@Override
public void delete(Object[] objects) {
for(Object o:objects) {
delete(o);
}
}
@Override
public List<UserRequest> getUserRequests(String login) {
DetachedCriteria criteria = DetachedCriteria.forClass(UserRequest.class)
.add(Property.forName("requesterLogin").eq(login));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<UserRequest> getAllUsersToRegister() {
HibernateTemplate template = getHibernateTemplate();
template.setMaxResults(10);
return getHibernateTemplate().loadAll(UserRequest.class);
}
@Override
public List<Client> getConfirmClients(final String login) {
/*List executeFind = getHibernateTemplate().executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(Session sn) throws HibernateException, SQLException {
SQLQuery createSQLQuery = sn.createSQLQuery("select uc.user_client_id from user_client uc where uc.user_login=:login");
createSQLQuery.setParameter("login", login);
return createSQLQuery.list();
}
});
if(executeFind.size() == 0) {
return new LinkedList<Client>();
}*/
DetachedCriteria criteria = DetachedCriteria.forClass(UserClient.class)
.add(Property.forName("userLogin").eq(login));
List<UserClient> founded = getHibernateTemplate().findByCriteria(criteria);
ArrayList<Client> target = new ArrayList<Client>();
for (UserClient userClient : founded) {
target.add(userClient.getClient());
}
return target;
}
@Override
public List<WorkType> getAviableWorks() {
DetachedCriteria criteria = DetachedCriteria.forClass(WorkType.class)
.add(Property.forName("available").eq(true))
.add(Property.forName("trash").eq(false))
.add(Property.forName("sheduleIndividualCollection").isNotEmpty());
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<Lpu> getLpuByWorkType(WorkType workType) {
DetachedCriteria criteria = DetachedCriteria.forClass(Lpu.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.createCriteria("collaboratorCollection")
.add(Property.forName("sheduled").eq(true))
.createCriteria("sheduleIndividualCollection")
.add(Property.forName("workType").eq(workType));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<Lpu> getAviableLpu() {
DetachedCriteria criteria = DetachedCriteria.forClass(Lpu.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.createCriteria("collaboratorCollection")
.add(Property.forName("sheduled").eq(true))
.createCriteria("sheduleIndividualCollection")
.add(Property.forName("workType").isNotNull());
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<Collaborator> getCollaboratorsByLpuAndWorkType(Lpu lpu, WorkType workType) {
DetachedCriteria criteria = DetachedCriteria.forClass(Collaborator.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.add(Property.forName("lpu").eq(lpu))
.createCriteria("sheduleIndividualCollection")
.add(Property.forName("workType").eq(workType));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<WorkType> getWorksByLpu(Lpu lpu) {
DetachedCriteria criteria = DetachedCriteria.forClass(WorkType.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.add(Property.forName("available").eq(true))
.add(Property.forName("trash").eq(false))
.createCriteria("sheduleIndividualCollection")
.createCriteria("collaborator")
.add(Property.forName("lpu").eq(lpu));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<SheduleReception> getSeduledClients(String login, Date date) {//
DetachedCriteria criteria = DetachedCriteria.forClass(SheduleReception.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.add(Property.forName("begin").gt(date))
.createCriteria("client")
.createCriteria("userClientCollection")
.add(Property.forName("userLogin").eq(login));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<Speciality> getCollaboratorSpecialities(Collaborator collaborator) {
DetachedCriteria criteria = DetachedCriteria.forClass(Speciality.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.createCriteria("functions")
.add(Property.forName("collaborator").eq(collaborator))
.add(Property.forName("trash").eq(false));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<Client> findOutClientsByData(ClientData data) {
DetachedCriteria criteria = DetachedCriteria.forClass(Client.class);
if(data.surname != null) {
criteria.createCriteria("surname")
.add(Property.forName("title").eq(data.surname));
}
if(data.name != null) {
criteria.createCriteria("name")
.add(Property.forName("title").eq(data.name));
}
if(data.patron != null) {
criteria.createCriteria("patronymic")
.add(Property.forName("title").eq(data.patron));
}
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<AddressObject> getChildList(AddressObject ao) {
DetachedCriteria criteria = DetachedCriteria.forClass(AddressObject.class)
.add(Property.forName("parent").eq(ao))
.addOrder(Order.asc("title"))
.createCriteria("type").add(Property.forName("id").ne(241));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<AddressObject> getChildList(AddressObject ao, int level) {
DetachedCriteria criteria = DetachedCriteria.forClass(AddressObject.class)
.add(Property.forName("parent").eq(ao))
.addOrder(Order.asc("title"))
.createCriteria("type")
.add(Property.forName("level").eq(level))
.add(Property.forName("id").ne(241));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<District> getDistrictList(Lpu lpu) {
DetachedCriteria criteria = DetachedCriteria.forClass(District.class)
.add(Property.forName("lpu").eq(lpu))
.addOrder(Order.asc("title"));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<AddressDistrict> AddressDistrictList(AddressObject object) {
DetachedCriteria criteria = DetachedCriteria.forClass(AddressDistrict.class)
.add(Property.forName("addressObject").eq(object));
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List<WorkType> getWorksByCollaborator(Collaborator collaborator) {
DetachedCriteria criteria = DetachedCriteria.forClass(WorkType.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.add(Property.forName("available").eq(true))
.add(Property.forName("trash").eq(false))
.createCriteria("sheduleIndividualCollection")
.add(Property.forName("collaborator").eq(collaborator));
return getHibernateTemplate().findByCriteria(criteria);
}
}