/*
* FundraisingController.java
*
* Created on 29 Sept 2007, 09:31
*
*/
package org.spw.controller;
import java.util.ArrayList;
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.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.spw.model.Fundraising;
import org.spw.model.FundraisingPlan;
/**
* Manage the Fundraisings.
* Implement CRUD operations.
* @author PSe
*/
public class FundraisingController {
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("VolunteerPU");
/** Creates a new instance of FundraisingController */
public FundraisingController() {
}
/**
* Retrieve the fundraisings as a list
* @return List of fundraisings
*/
public List<Fundraising> getFundraisings() {
EntityManager em = emf.createEntityManager();
List<Fundraising> result = null;
try {
Query query = em.createNamedQuery("Fundraising.findAll");
result = query.getResultList();
} catch (Exception e) {
Logger.getLogger(FundraisingController.class.getName()).log(Level.SEVERE, "Error Query all Fundraising", e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
public void create(Fundraising object) throws EntityExistsException {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
em.persist(object);
em.getTransaction().commit();
} catch (EntityExistsException eee) {
Logger.getLogger(FundraisingController.class.getName()).log(Level.INFO, "EntityExistsException for " + object);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
throw eee;
} catch (Exception e) {
Logger.getLogger(FundraisingController.class.getName()).log(Level.SEVERE, "Error creating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
}
public Fundraising read(Integer key) {
EntityManager em = emf.createEntityManager();
Fundraising retValue = null;
try {
retValue = em.find(Fundraising.class, key);
} catch (Exception e) {
Logger.getLogger(FundraisingController.class.getName()).log(Level.SEVERE, "Error reading " + key, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return retValue;
}
public Fundraising update(Fundraising object) {
Fundraising result = null;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
result = em.merge(object);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(FundraisingController.class.getName()).log(Level.SEVERE, "Error updating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
public void delete(Fundraising object) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
Fundraising entity = em.find(Fundraising.class, object.getYearFunding());
em.remove(entity);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(FundraisingController.class.getName()).log(Level.SEVERE, "Error deleting " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
}
/**
* Add/Update the fundraisings plan of a monthSelected
* @param year the fundraising year
* @param monthSelected the month of selection of the volunteer (1-12)
* @param plan the set of plan to add (an empty array clear the content)
* @return the new instance after being persisted
*/
public Fundraising addFundraisingsPlan(int year, int monthSelected, FundraisingPlan[] plan) {
if (monthSelected < 1 || monthSelected > 12)
throw new IllegalArgumentException("Month selected in fundraising plan should be from 1 to 12, found " + monthSelected);
EntityManager em = emf.createEntityManager();
for (int i = 0; i < plan.length; i++) {
if (plan[i].getMonthFunding() < 1 || plan[i].getMonthFunding() > 12)
throw new IllegalArgumentException("Month funding in fundraising plan should be from 1 to 12, found " + plan[i]);
}
Fundraising fundraising = null;
em.getTransaction().begin();
try {
fundraising = em.find(Fundraising.class, year);
if (fundraising == null)
throw new IllegalArgumentException("The fundraising does not exist for this year " + year);
List<FundraisingPlan> previous = new ArrayList<FundraisingPlan>(fundraising.getFundraisingPlans());
//remove the previous instances
for (FundraisingPlan p : previous) {
if (p.getMonthSelected() == monthSelected) {
fundraising.getFundraisingPlans().remove(p);
p.setFundraising(null);
em.remove(p);
}
}
// add the new ones
for (int i = 0; i < plan.length; i++) {
FundraisingPlan p = new FundraisingPlan();
p.setFundraising(fundraising);
p.setMonthSelected(monthSelected);
p.setMonthFunding(plan[i].getMonthFunding());
p.setAmount(plan[i].getAmount());
fundraising.getFundraisingPlans().add(p);
plan[i] = p;
}
//update
fundraising = em.merge(fundraising);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(FundraisingController.class.getName()).log(Level.SEVERE, "Error reading " + year, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return fundraising;
}
/**
* Retrieve the fundraisings plan of a monthSelected
* @param year the fundraising year
* @param monthSelected the month of selection of the volunteer (1-12)
* @param plan the set of plan to add (an empty array clear the content)
* @return all the needed instances for a month including empty
*/
public FundraisingPlan[] getFundraisingsPlan(int year, int monthSelected) {
if (monthSelected < 1 || monthSelected > 12)
throw new IllegalArgumentException("Month selected in fundraising plan should be from 1 to 12, found " + monthSelected);
EntityManager em = emf.createEntityManager();
Fundraising fundraising = null;
FundraisingPlan[] monthPlan = new FundraisingPlan[12];
try {
fundraising = em.find(Fundraising.class, year);
if (fundraising == null)
throw new IllegalArgumentException("The fundraising does not exist for this year " + year);
for (FundraisingPlan p : fundraising.getFundraisingPlans()) {
if (p.getMonthSelected() == monthSelected) {
monthPlan[p.getMonthFunding()-1] = p;
}
}
// add the new ones (begin from the selected month, can't be before
for (int i = (monthSelected -1) ; i < monthPlan.length; i++) {
if (monthPlan[i] == null) {
FundraisingPlan p = new FundraisingPlan();
p.setFundraising(fundraising);
p.setMonthSelected(monthSelected);
p.setMonthFunding(i+1);
monthPlan[i] = p;
}
}
} catch (Exception e) {
Logger.getLogger(FundraisingController.class.getName()).log(Level.SEVERE, "Error reading " + year, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return monthPlan;
}
}