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