/*
* CountryController.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.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.spw.model.Country;
/**
* Manage Country objects.
* Implement CRUD operations.
* @author PSe
*/
public class CountryController {
private final EntityManagerFactory emf;
/** Creates a new instance of CountryController */
public CountryController() {
Properties props = PropertiesLoader.loadProperties("database.properties");
if (props != null)
{
emf = Persistence.createEntityManagerFactory("VolunteerPU", props);
}
else
{
emf = Persistence.createEntityManagerFactory("VolunteerPU");
}
}
/**
* Retrieve all the countries.
* @return List of all countries
*/
public List<Country> getCountries() {
EntityManager em = emf.createEntityManager();
List<Country> result = null;
try {
Query query = em.createNamedQuery("Country.findCountry");
result = query.getResultList();
} catch (Exception e) {
Logger.getLogger(CountryController.class.getName()).log(Level.SEVERE, "Error Query all Country", e);
} finally {
em.close();
}
return result;
}
/**
* Find the country by its name.
* @param key The name of the country. It is assumed that this name is unique.
* @return The corresponding country object.
*/
public Country getCountryByName(String key) {
EntityManager em = emf.createEntityManager();
Country retValue = null;
try {
Query query =
em.createNamedQuery("Country.findByCountryName");
query.setParameter("countryName", key);
retValue = (Country) query.getSingleResult();
} catch (NoResultException e) {
Logger.getLogger(CountryController.class.getName()).
log(Level.FINEST, "Cannot find country name " + key, e.getMessage());
} catch (Exception e) {
Logger.getLogger(CountryController.class.getName()).
log(Level.SEVERE, "Error reading " + key, e);
} finally {
em.close();
}
return retValue;
}
public void create(Country object) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
em.persist(object);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(CountryController.class.getName()).
log(Level.SEVERE, "Error creating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
}
public Country read(String key) {
EntityManager em = emf.createEntityManager();
Country retValue = null;
try {
retValue = em.find(Country.class, key);
} catch (Exception e) {
Logger.getLogger(CountryController.class.getName()).
log(Level.SEVERE, "Error reading " + key, e);
} finally {
em.close();
}
return retValue;
}
public Country update(Country object) {
Country result = null;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
result = em.merge(object);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(CountryController.class.getName()).
log(Level.SEVERE, "Error updating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
public void delete(Country object) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
Country entity = em.find(Country.class, object.getCountryCode());
em.remove(entity);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(CountryController.class.getName()).
log(Level.SEVERE, "Error deleting " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
}
}