/*
* VolunteerController.java
*
* Created on 4 May 2007, 19:56
*
*/
package org.spw.controller;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.spw.model.Program;
import org.spw.model.Volunteer;
import org.spw.model.VolunteerApplication;
/**
* Manage the Volunteer database persistence
* @author PSe
*/
public class VolunteerController extends PersonController {
/** Creates a new instance of VolunteerController */
public VolunteerController() {
super();
}
/**
* Get all volunteers
* @return List of all volunteers
*/
public List<Volunteer> getVolunteers() {
EntityManager em = emf.createEntityManager();
List<Volunteer> result = null;
try {
Query query = em.createNamedQuery("Volunteer.findAll");
result = query.getResultList();
} catch (Exception e) {
Logger.getLogger(VolunteerController.class.getName()).log(Level.SEVERE, "Error Query all Volunteer", e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
/**
* Get only recent volunteers
* @param year The year (included) from which the volunteer is recent
* @return List of interested volunteers from the year
*/
public List<Volunteer> getRecentVolunteers(int year) {
EntityManager em = emf.createEntityManager();
List<Volunteer> result = null;
try {
Query query = em.createNamedQuery("Volunteer.findRecents");
query.setParameter("year", year);
result = query.getResultList();
} catch (Exception e) {
Logger.getLogger(VolunteerController.class.getName()).log(Level.SEVERE, "Error Query all Volunteer", e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
/**
* Get only volunteers for a year
* @param year The year (included) of application
* @return List of interested volunteers from the year
*/
public List<Volunteer> getVolunteersForYear(int year) {
EntityManager em = emf.createEntityManager();
List<Volunteer> result = null;
try {
Query query = em.createNamedQuery("Volunteer.findForYear");
query.setParameter("year", year);
result = query.getResultList();
} catch (Exception e) {
Logger.getLogger(VolunteerController.class.getName()).log(Level.SEVERE, "Error Query all Volunteer", e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
/**
* Get all volunteers with criterias
* @return List of all volunteers
* @param criterias criterias to apply zhen extracting the Volunteers
* @throws org.spw.controller.CriteriaException Incorrect criterias
*/
public List<Volunteer> getSelectionVolunteers(Criteria[] criterias) throws CriteriaException {
QueryBuilder qb = new QueryBuilder(Volunteer.class);
qb.applyCriterias(criterias);
EntityManager em = emf.createEntityManager();
List<Volunteer> result = null;
try {
Query query = em.createQuery(qb.getStatement());
result = query.getResultList();
} catch (Exception e) {
Logger.getLogger(VolunteerController.class.getName()).log(Level.SEVERE,
"Error Query Selection of Volunteers (" + qb.getStatement() + ")",
e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
//TODO: generate an exception in all query methods to have a better control
throw new CriteriaException(e.getMessage());
} finally {
em.close();
}
return result;
}
/**
* Create a new volunteer
* @param object volunteer to create
*/
public void create(Volunteer object) {
super.create(object);
}
/**
* Obtain the volunteer instance
* @param key unique Id of the volunteer
* @return the volunteer instance, null if not found
*/
public Volunteer read(Long key) {
return (Volunteer) super.read(key);
}
/**
* Update the volunteer or add it if he doesn't exist
* @param object The volunteer instance to update
* @return a new persited object
*/
public Volunteer update(Volunteer object) {
return (Volunteer) super.update(object);
}
/**
* Remove the instance
* @param object The instance to delete
*/
public void delete(Volunteer object) {
super.delete(object);
}
/**
* Parse the string representation to obtain an instance of volunteer
* @param volunteer The string representation (end with [id])
* @return instance of volunteer, null if not found
*/
public Volunteer parse(String volunteer) {
return (Volunteer) super.parse(volunteer);
}
/**
* Add (or upddate) the VolunteerApplication of the volunteer
* @param volunteer the applying Volunteer
* @param application the volunteer application to add or update
* @param previousProgram if the program has changed, the program before the change
* @return A new version of the volunteer after the state has been persisted
*/
public Volunteer addApplication(Volunteer volunteer, VolunteerApplication application, Program previousProgram) {
Volunteer result = null;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
//Persist the application
application.setVolunteer(volunteer);
application = em.merge(application);
// assign a new type depending of the application state
String newType = application.getNewTypeVolunteer();
if (newType != null) {
volunteer.setTypeVolunteer(newType);
}
// persist the volunteer
volunteer.setApplication(application);
result = em.merge(volunteer);
// Add the application to the current programm if needed
if (application.getProgram() != null) {
List<VolunteerApplication> applications =
application.getProgram().getApplications();
if (! applications.contains(application)) {
application.getProgram().getApplications().add(application);
em.merge(application.getProgram());
}
}
//Remove from the previous program to maintain a correct state
if (previousProgram != null && !previousProgram.equals(application.getProgram())
&& previousProgram.getApplications().contains(application)) {
previousProgram.getApplications().remove(application);
em.merge(previousProgram);
}
// Commit
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(VolunteerController.class.getName()).log(Level.SEVERE, "Error setting Application for " + volunteer, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
}