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

Java tutorial

Introduction

Here is the source code for it.geosdi.era.server.dao.hibernate.DAOViewport.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.viewport.Viewport;
import it.geosdi.era.client.model.viewport.ViewportDTO;
import it.geosdi.era.client.model.viewport.ViewportType;
import it.geosdi.era.exception.DAOException;
import it.geosdi.era.server.dao.IDAOViewport;

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.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author giuseppe
 * 
 */
public class DAOViewport implements IDAOViewport {

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

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

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

    public List<Viewport> findAll() throws DAOException {
        // TODO Auto-generated method stub
        return null;
    }

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

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

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

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

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

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

    @SuppressWarnings("unchecked")
    @Transactional(propagation = Propagation.SUPPORTS)
    public ViewportDTO findByUserID(Long userId) throws DAOException {
        try {
            Query query = getSession().createQuery(
                    "select viewports " + "from Utente user join user.viewports viewports " + "where user.id = ?");
            query.setLong(0, userId);

            List<Viewport> viewports = query.list();

            Query query1 = getSession()
                    .createQuery("select viewports " + "from Utente user join user.viewports viewports "
                            + "where user.id = ? and viewports.type = ?");
            query1.setLong(0, userId);
            query1.setString(1, ViewportType.TYPE.getValue());
            Viewport v = (Viewport) query1.uniqueResult();

            return new ViewportDTO(v, viewports);
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Viewport makePersistent(Viewport viewport, Long idDefaultViewport, boolean checkChanged)
            throws DAOException {
        try {
            getSession().saveOrUpdate(viewport);

            if ((checkChanged) && (idDefaultViewport != null)) {
                Query query = getSession().createSQLQuery(
                        "update viewport " + "set viewport_type = '' " + "where id = ? and user_id = ?");
                query.setLong(0, idDefaultViewport);
                query.setLong(1, viewport.getUser().getId());
                query.executeUpdate();
            }
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
        return viewport;
    }

    @SuppressWarnings("unchecked")
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Viewport> loadViewportForUser(Long userId) throws DAOException {
        try {
            Query query = getSession().createQuery(
                    "select viewports " + "from Utente user join user.viewports viewports " + "where user.id = ?");
            query.setLong(0, userId);

            return query.list();
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
    }
}