Java tutorial
/* BSD 3-Clause License Copyright (c) 2016, Universidad de Costa Rica All rights reserved. Elaborado por Ing. Adrin Alvarado Ramrez, a.alvarado.r@gmail.com Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package accesobd; import db.HibernateUtil; import entidades.AdmVista; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.Query; import utilitarios.Respuesta; import utilitarios.TipoMantenimiento; /** * * Clase que proporciona los mtodos para acceder a las vistas. * * @author Ing. Adrin Alvarado Ramrez <adrian.alvarado_r@ucr.ac.cr> - Facultad * de Educacin, Universidad de Costa Rica */ public class AccesoVistas { /** * Retorna la lista de vistas para un sistema particular. * @param idSistema El identificador del sistema. * @return */ public List<AdmVista> obtenerVistasSistema(int idSistema) { List<AdmVista> listaVistas = null; Session sesion; sesion = HibernateUtil.getSessionFactory().openSession(); try { Query q = sesion.createQuery("from AdmVista where admSistema.id = :idSistema"); q.setParameter("idSistema", idSistema); listaVistas = (List<AdmVista>) q.list(); } catch (Exception e) { System.out.println(e.toString()); } sesion.close(); return listaVistas; } /** * Obtiene una vista a partir de su identificador. * @param idVista * @return */ public AdmVista obtenerVista(int idVista) { AdmVista vista; Session sesion; sesion = HibernateUtil.getSessionFactory().openSession(); try { Query q = sesion.createQuery("from AdmVista where id = :id"); q.setParameter("id", idVista); vista = (AdmVista) q.uniqueResult(); } catch (Exception e) { vista = null; System.out.println(e.toString()); } finally { sesion.close(); } return vista; } /** * Mtodo para actualizar (agregar o modificar) una vista. * @param vista La estructura que representa el rol. * @param tipoMantenimiento El tipo de mantenimiento (AGREGAR, MODIFICAR, ELIMINAR). * @return */ public Respuesta actualizarVista(AdmVista vista, TipoMantenimiento tipoMantenimiento) { Respuesta res = new Respuesta(); boolean huboError = false; Session sesion; Transaction tx; sesion = HibernateUtil.getSessionFactory().openSession(); // Inicia la transaccin. tx = sesion.beginTransaction(); try { String operacionHecha = null; if (tipoMantenimiento == TipoMantenimiento.AGREGAR) { sesion.save(vista); operacionHecha = "agregada"; } else if (tipoMantenimiento == TipoMantenimiento.MODIFICAR) { sesion.update(vista); operacionHecha = "modificada"; } else if (tipoMantenimiento == TipoMantenimiento.ELIMINAR) { sesion.delete(vista); operacionHecha = "eliminada"; } tx.commit(); // Hace commit a la trasaccin. res.setResultado(true); res.setMensaje(String.format("Vista %s exitosamente", operacionHecha)); } catch (Exception ex) { huboError = true; res.setMensaje("Error al realizar la operacin en base de datos. No se pudo actualizar el sistema."); res.setMensajeError(ex.toString()); } finally { if (huboError) { tx.rollback(); res.setResultado(false); } sesion.close(); } return res; } }