Package org.spw.controller

Source Code of org.spw.controller.ProgramController

/*
* ProgramController.java
*
* Created on 4 May 2007, 19:56
*
*/

package org.spw.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.spw.model.Program;

/**
* Manage the Programs.
* Implement CRUD operations.
* @author PSe
*/
public class ProgramController {
   
    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("VolunteerPU");
   
    /** Creates a new instance of ProgramController */
    public ProgramController() {
    }
   
    /**
     * Retrieve the programs as a list
     * @return List of programs
     */
    public List<Program> getPrograms() {
        EntityManager em = emf.createEntityManager();
        List<Program> result = null;
        try {
            Query query = em.createNamedQuery("Program.findAll");
            result = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(ProgramController.class.getName()).log(Level.SEVERE, "Error Query all Program", e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
    /**
     * Retrieve the programs of a year as a list
     * @return List of programs
     */
    public List<Program> getProgramsYear(int year) {
        EntityManager em = emf.createEntityManager();
        List<Program> result = null;
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, 0, 1, 0, 0);
        Date startPeriod = cal.getTime();
        cal.set(year, 11, 31, 23, 59);
        Date endPeriod = cal.getTime();
        try {
            Query query = em.createNamedQuery("Program.findInPeriod");
            query.setParameter("startPeriod", startPeriod);
            query.setParameter("endPeriod", endPeriod);
            result = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(ProgramController.class.getName()).log(Level.SEVERE, "Error Query all Program", e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
    public void create(Program object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(ProgramController.class.getName()).log(Level.SEVERE, "Error creating " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
   
    public Program read(Long key) {
        EntityManager em = emf.createEntityManager();
        Program retValue = null;
        try {
            retValue = em.find(Program.class, key);
        } catch (Exception e) {
            Logger.getLogger(ProgramController.class.getName()).log(Level.SEVERE, "Error reading " + key, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return retValue;
    }
   
    public Program update(Program object) {
        Program result = null;
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            result = em.merge(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(ProgramController.class.getName()).log(Level.SEVERE, "Error updating " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
   
    public void delete(Program object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            Program entity = em.find(Program.class, object.getIdProgram());
            em.remove(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(ProgramController.class.getName()).log(Level.SEVERE, "Error deleting " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
   
}
TOP

Related Classes of org.spw.controller.ProgramController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.