/*
* VolunteerApplicationController.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.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.spw.model.Contribution;
import org.spw.model.VolunteerApplication;
/**
* Manage the Applications of a volunteer.
* Implement CRUD operations and parse the string representation.
* @author PSe
*/
public class VolunteerApplicationController {
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("VolunteerPU");
/**
* Creates a new instance of VolunteerApplicationController
*/
public VolunteerApplicationController() {
}
/**
* Retrieve the applications
* @return List of all applications
*/
public List<VolunteerApplication> getApplications() {
EntityManager em = emf.createEntityManager();
List<VolunteerApplication> result = null;
try {
Query query = em.createNamedQuery("VolunteerApplication.findAll");
result = query.getResultList();
} catch (Exception e) {
Logger.getLogger(VolunteerApplicationController.class.getName()).log(Level.SEVERE, "Error Query all Application", e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
public void create(VolunteerApplication object) throws EntityExistsException {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
em.persist(object);
em.getTransaction().commit();
} catch (EntityExistsException eee) {
Logger.getLogger(VolunteerApplicationController.class.getName()).log(Level.INFO, "Entity already exist " + object);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
throw eee;
} catch (Exception e) {
Logger.getLogger(VolunteerApplicationController.class.getName()).log(Level.SEVERE, "Error creating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
}
public VolunteerApplication read(Long key) {
EntityManager em = emf.createEntityManager();
VolunteerApplication retValue = null;
try {
retValue = em.find(VolunteerApplication.class, key);
} catch (Exception e) {
Logger.getLogger(VolunteerApplicationController.class.getName()).log(Level.SEVERE, "Error reading " + key, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return retValue;
}
public VolunteerApplication update(VolunteerApplication object) {
VolunteerApplication result = null;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
result = em.merge(object);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(VolunteerApplicationController.class.getName()).log(Level.SEVERE, "Error updating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
public VolunteerApplication addContribution(VolunteerApplication object, Contribution contribution) {
VolunteerApplication result = null;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
object.addContribution(contribution);
result = em.merge(object);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(VolunteerApplicationController.class.getName()).log(Level.SEVERE, "Error updating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
public VolunteerApplication removeContribution(VolunteerApplication object, Contribution contribution) {
VolunteerApplication result = null;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
contribution = em.find(Contribution.class, contribution.getIdContribution());
object.removeContribution(contribution);
em.remove(contribution);
result = em.merge(object);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(VolunteerApplicationController.class.getName()).log(Level.SEVERE, "Error updating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
public void delete(VolunteerApplication object) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
VolunteerApplication entity = em.find(VolunteerApplication.class, object.getIdApplication());
em.remove(entity);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(VolunteerApplicationController.class.getName()).log(Level.SEVERE, "Error deleting " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
}
/**
* From the string representation, give the corresponding application
* @param applicationString The application as a string representation
* @return The corresponding application object.
*/
public VolunteerApplication parse(String applicationString) {
if (applicationString == null) return null;
int begin = applicationString.lastIndexOf('[') + 1;
int last = applicationString.lastIndexOf(']');
if (begin < 0 || last < 0 || begin > last) return null;
String number = applicationString.substring(begin, last);
VolunteerApplication retValue = null;
try {
retValue = read(Long.parseLong(number));
} catch (NumberFormatException ex) {
//incorrect number, could not appen
Logger.getLogger(VolunteerApplicationController.class.getName()).severe(applicationString +
" is incorrect as a string version of a Application to parse. [" +
ex.getLocalizedMessage() + "]");
}
return retValue;
}
}