it.geosdi.era.server.dao.hibernate.DAOStratoProgettoHibernate.java Source code

Java tutorial

Introduction

Here is the source code for it.geosdi.era.server.dao.hibernate.DAOStratoProgettoHibernate.java

Source

/*
 *  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.StratoProgetto;
import it.geosdi.era.exception.DAOException;
import it.geosdi.era.server.dao.IDAOStratoProgetto;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
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;

/**
 * @author Giuseppe La Scaleia
 * @author Francesco Izzi
 */

public class DAOStratoProgettoHibernate implements IDAOStratoProgetto {

    /** LOGGER **/
    private static Log logger = LogFactory.getLog(DAOStratoProgettoHibernate.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 StratoProgetto salvaStratoProgetto(StratoProgetto stratoProgetto) {
        return this.makePersistent(stratoProgetto);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void eliminaStratoProgetto(StratoProgetto stratoProgetto) {
        this.makeTransient(stratoProgetto);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public StratoProgetto update(StratoProgetto stratoProgetto) {
        return makePersistent(stratoProgetto);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public StratoProgetto findById(long idStratoProgetto) {
        try {
            List<StratoProgetto> listaStratiProgetto = findByCriteria(Restrictions.eq("id", idStratoProgetto));
            if (listaStratiProgetto.size() != 0)
                return listaStratiProgetto.get(0);
        } catch (HibernateException e) {
            logger.error(e);
            throw new DAOException(e.getMessage());
        }
        return null;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public StratoProgetto findByProjectIdAndLayerName(long projectId, String layerName) {
        Session session = getSession();
        Query query = session.createQuery(
                "select stratiProgetto from Progetto progetto join progetto.stratiProgetto stratiProgetto where progetto.id = ? and stratiProgetto.testoDaVisualizzare = ?");
        query.setParameter(0, projectId);
        query.setParameter(1, layerName);
        return (StratoProgetto) query.uniqueResult();
    }

    public List<StratoProgetto> findAll() throws DAOException {
        return findByCriteria();
    }

    public List<StratoProgetto> findAll(int offset, int limite) throws DAOException {
        return findByCriteria(offset, limite);
    }

    @SuppressWarnings("unchecked")
    public List<StratoProgetto> findByCriteria(Criterion... criterion) throws DAOException {
        try {
            Criteria crit = getSession().createCriteria(StratoProgetto.class);
            for (Criterion c : criterion) {
                crit.add(c);
            }
            return crit.list();
        } catch (HibernateException ex) {
            throw new DAOException(ex);
        }
    }

    @SuppressWarnings("unchecked")
    public List<StratoProgetto> findByCriteria(int offset, int limite, Criterion... criterion) throws DAOException {
        try {
            Criteria crit = getSession().createCriteria(StratoProgetto.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 StratoProgetto findById(Long id, boolean lock) throws DAOException {
        StratoProgetto entity;
        try {
            if (lock) {
                entity = (StratoProgetto) getSession().load(StratoProgetto.class, id, LockMode.UPGRADE);
            } else {
                entity = (StratoProgetto) getSession().load(StratoProgetto.class, id);
            }
        } catch (HibernateException ex) {
            throw new DAOException(ex);
        }
        return entity;
    }

    public void lock(StratoProgetto entity) throws DAOException {
        try {
            getSession().lock(entity, LockMode.UPGRADE);
        } catch (HibernateException ex) {
            throw new DAOException(ex);
        }
    }

    public StratoProgetto makePersistent(StratoProgetto entity) throws DAOException {
        try {
            getSession().saveOrUpdate(entity);
        } catch (HibernateException ex) {
            throw new DAOException(ex);
        }
        return entity;
    }

    public void makeTransient(StratoProgetto entity) throws DAOException {
        try {
            getSession().delete(entity);
        } catch (HibernateException ex) {
            logger.error(Costanti.EXCEPTION_ERROR + ex.getMessage());
            throw new DAOException(ex.getMessage());
        }
    }
}