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

Java tutorial

Introduction

Here is the source code for it.geosdi.era.server.dao.hibernate.DAOServerHibernate.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.model.Server;
import it.geosdi.era.exception.DAOException;
import it.geosdi.era.server.dao.IDAOServer;

import java.util.List;

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;

public class DAOServerHibernate implements IDAOServer {

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    private Session getSession() {
        return this.sessionFactory.getCurrentSession();
    }

    @SuppressWarnings("unchecked")
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Server> findAll() throws DAOException {
        Query query = getSession().createQuery("from Server");
        return query.list();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Server> findAll(int offset, int limite) throws DAOException {
        return findByCriteria(offset, limite);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Server findById(long idServer) {
        List<Server> serverList = findByCriteria(Restrictions.eq("id", idServer));
        if (serverList.size() != 0)
            return serverList.get(0);
        return null;
    }

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

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

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

    @Transactional(propagation = Propagation.REQUIRED)
    public Server makePersistent(Server entity) throws DAOException {
        try {
            getSession().saveOrUpdate(entity);
        } catch (HibernateException ex) {
            throw new DAOException(ex);
        }
        return entity;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void makeTransient(Server entity) throws DAOException {
        try {
            getSession().delete(entity);
        } catch (HibernateException ex) {
            throw new DAOException(ex);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Server findByUrl(String urlServer) {
        List<Server> serverList = findByCriteria(Restrictions.eq("urlServer", urlServer));
        if (serverList.size() != 0)
            return serverList.get(0);
        return null;
    }
}