Package org.spw.controller

Source Code of org.spw.controller.DonationController

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

package org.spw.controller;

import java.util.List;
import java.util.Properties;
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.Donation;

/**
* This class manage the Donation entities.
* It's a basic CRUD controller
* @author PSe
*/
public class DonationController {
   
    private final EntityManagerFactory emf;
   
    /** Creates a new instance of DonationController */
    public DonationController() {
        Properties props = PropertiesLoader.loadProperties("database.properties");
        if (props != null)
        {
            emf = Persistence.createEntityManagerFactory("VolunteerPU", props);
        }
        else
        {
            emf = Persistence.createEntityManagerFactory("VolunteerPU");
        }
    }
   
    /**
     * Retreive all the donations
     * @return all the donations
     */
    public List<Donation> getDonations() {
        EntityManager em = emf.createEntityManager();
        List<Donation> result = null;
        try {
            Query query = em.createNamedQuery("Donation.findAll");
            result = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(DonationController.class.getName()).log(Level.SEVERE, "Error Query all Donation", e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
    /**
     * Createa new donation
     * @param object the donation to create
     */
    public void create(Donation object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(DonationController.class.getName()).log(Level.SEVERE, "Error creating " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
   
    /**
     * Find a new donation
     * @param key The unique ID of the object
     * @return a donation if found, null otherwise
     */
    public Donation read(Long key) {
        EntityManager em = emf.createEntityManager();
        Donation retValue = null;
        try {
            retValue = em.find(Donation.class, key);
        } catch (Exception e) {
            Logger.getLogger(DonationController.class.getName()).log(Level.SEVERE, "Error reading " + key, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return retValue;
    }
   
    /**
     * Update an existing donation. If it's a new Donation, create it.
     * @param object The donation to update
     * @return the new object donation after the persistence operation
     */
    public Donation update(Donation object) {
        Donation result = null;
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            result = em.merge(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(DonationController.class.getName()).log(Level.SEVERE, "Error updating " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
   
    /**
     * Delete an instance of Donation
     * @param object The donation to delete
     */
    public void delete(Donation object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            Donation entity = em.find(Donation.class, object.getIdDonation());
            em.remove(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(DonationController.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.DonationController

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.