Java tutorial
/* * GeoSDI ERA - The new era of webGIS * http://code.google.com/p/geosdiera/ * ==================================================================== * * Copyright (C) 2008-2009 GeoSDI Group (CNR IMAA). * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. * * ==================================================================== * * This software consists of voluntary contributions made by developers * of GeoSDI Group. For more information on GeoSDI, please see * <http://www.geosdi.org/>. * */ package it.geosdi.era.server.dao.hibernate; import it.geosdi.era.client.Costanti; import it.geosdi.era.client.model.Progetto; import it.geosdi.era.client.model.Utente; import it.geosdi.era.exception.DAOException; import it.geosdi.era.server.dao.IDAOProgetto; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Criteria; import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.LockMode; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Restrictions; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; public class DAOProgettoHibernate implements IDAOProgetto { private static Log logger = LogFactory.getLog(DAOProgettoHibernate.class); private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } private Session getSession() { return this.sessionFactory.getCurrentSession(); } @Transactional(propagation = Propagation.REQUIRED) public void deleteProgetto(Progetto progetto) { makeTransient(progetto); } public List<Utente> findUtentiByIdProgetto(long id) { // TODO Auto-generated method stub return null; } @Transactional(propagation = Propagation.REQUIRED) public Progetto saveProgetto(Progetto progetto) { // TODO Auto-generated method stub return makePersistent(progetto); } @Transactional(propagation = Propagation.REQUIRED) public void updateProgetto(Progetto progetto) { makePersistent(progetto); } @Transactional(propagation = Propagation.REQUIRED) public void inizializzaStratiProgetto(Progetto progetto) { Hibernate.initialize(progetto); } @Transactional(propagation = Propagation.SUPPORTS) public Progetto findLayerByIdProgetto(long id) { Query query = getSession().createQuery( "select progetto from Progetto progetto join fetch progetto.layers layer where progetto.id = ?"); query.setParameter(1, id); return (Progetto) query.uniqueResult(); } @Transactional(propagation = Propagation.SUPPORTS) public Progetto findByNomeAndIdUtente(long idUtente, String nomeProgetto) throws DAOException { Session session = getSession(); Query query = (Query) session.createQuery( "select progetto from Progetto progetto join progetto.utentiProgetto utentiProgetto join utentiProgetto.utente utente where utente.id = ? and progetto.nomeProgetto = ?"); query.setParameter(0, idUtente); query.setParameter(1, nomeProgetto); return (Progetto) query.uniqueResult(); } @Transactional(propagation = Propagation.SUPPORTS) public Progetto findById(long idProgetto) throws DAOException { List<Progetto> listaProgetti = findByCriteria(Restrictions.eq("id", idProgetto)); if (listaProgetti.size() != 0) return listaProgetti.get(0); return null; } @Transactional(propagation = Propagation.SUPPORTS) public void flush(Progetto progetto) { getSession().evict(progetto); } public List<Progetto> findAll() throws DAOException { return findByCriteria(); } public List<Progetto> findAll(int offset, int limite) throws DAOException { return findByCriteria(offset, limite); } @SuppressWarnings("unchecked") public List<Progetto> findByCriteria(Criterion... criterion) throws DAOException { try { Criteria crit = getSession().createCriteria(Progetto.class); for (Criterion c : criterion) { crit.add(c); } return crit.list(); } catch (HibernateException ex) { throw new DAOException(ex); } } @SuppressWarnings("unchecked") public List<Progetto> findByCriteria(int offset, int limite, Criterion... criterion) throws DAOException { try { Criteria crit = getSession().createCriteria(Progetto.class); for (Criterion c : criterion) { crit.add(c); } crit.setFirstResult(offset); crit.setMaxResults(limite); return crit.list(); } catch (HibernateException ex) { throw new DAOException(ex); } } public Progetto findById(Long id, boolean lock) throws DAOException { Progetto entity; try { if (lock) { entity = (Progetto) getSession().load(Progetto.class, id, LockMode.UPGRADE); } else { entity = (Progetto) getSession().load(Progetto.class, id); } } catch (HibernateException ex) { throw new DAOException(ex); } return entity; } public void lock(Progetto entity) throws DAOException { try { getSession().lock(entity, LockMode.UPGRADE); } catch (HibernateException ex) { throw new DAOException(ex); } } public Progetto makePersistent(Progetto entity) throws DAOException { try { getSession().saveOrUpdate(entity); } catch (HibernateException ex) { throw new DAOException(ex); } return entity; } @Transactional(propagation = Propagation.REQUIRED) public void makeTransient(Progetto entity) throws DAOException { try { getSession().delete(entity); } catch (HibernateException ex) { logger.error(Costanti.EXCEPTION_ERROR + ex.getMessage()); throw new DAOException(ex.getMessage()); } } }