/*
* ContactController.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.Contact;
import org.spw.model.Document;
import org.spw.model.Donation;
/**
* Manage the persistence of Contact
* <p>This class is not intended to create new entities. New entities must
* be any subclass. It's why the create is protected</p>
* @author PSe
*/
public class ContactController {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("VolunteerPU");
/**
* Creates a new instance of ContactController
*/
public ContactController() {
}
/**
* Retrieve all the contacts.
* @return List of all the contacts
*/
public List<Contact> getContacts() {
EntityManager em = emf.createEntityManager();
List<Contact> result = null;
try {
Query query = em.createNamedQuery("Contact.findAll");
result = query.getResultList();
} catch (Exception e) {
Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE, "Error Query all Contact", e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
/**
* Retrieve the contacts whose first name begin with some characters.
* The result can be limited to keep the list small.
* @param beginning The characters to match. Only the contacts begining with these characters are returned.
* @param maxResult If 0, no limit;
* Other value = only maxResult occurences maximum.
* @return List of the contacts matching the beginning character,
* in the limit of maxResult occurences.
*/
public List<Contact> getContactsBeginningBy(String beginning, int maxResult) {
EntityManager em = emf.createEntityManager();
List<Contact> result = null;
try {
Query query = em.createNamedQuery("Contact.findBeginningBy");
if (maxResult > 0) {
query.setMaxResults(maxResult);
}
query.setParameter("beginning", beginning + "%");
result = query.getResultList();
} catch (Exception e) {
Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE,
"Error Query Contacts beginning by " + beginning, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
public void create(Contact object) throws EntityExistsException {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
em.persist(object);
em.getTransaction().commit();
} catch (EntityExistsException eee) {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
throw eee;
} catch (Exception e) {
Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE, "Error creating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
}
public Contact read(Long key) {
EntityManager em = emf.createEntityManager();
Contact retValue = null;
try {
retValue = em.find(Contact.class, key);
} catch (Exception e) {
Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE, "Error reading " + key, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return retValue;
}
public Contact update(Contact object) {
Contact result = null;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
result = em.merge(object);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE, "Error updating " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
return result;
}
public void delete(Contact object) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
Contact entity = em.find(Contact.class, object.getIdContact());
em.remove(entity);
em.getTransaction().commit();
} catch (Exception e) {
Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE, "Error deleting " + object, e);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
} finally {
em.close();
}
}
/**
* Retrieve a Contact from its string representation.
* @param contact Contact as a string representation
* @return The contact object corresponding to the string rep.
*/
public Contact parse(String contact) {
if (contact == null) return null;
int begin = contact.lastIndexOf('[');
if (begin < 0) return null;
int last = contact.lastIndexOf(']');
if (last < 0) return null;
String number = contact.substring(begin+1, last);
Contact retValue = null;
try {
retValue = read(Long.parseLong(number));
} catch (NumberFormatException ex) {
//incorrect number, could not appen
Logger.getLogger(ContactController.class.getName()).severe(contact +
" is incorrect as a string version of a contact to parse [" +
ex.getLocalizedMessage() + "]");
}
return retValue;
}
/**
* Add or update the donation of the contact.
* This method manage the consistence of the operation in one transaction
* @param donation the donation to persist. The donor must be set
* @return the new version of the donation after persisted (the donor is
* updated with the lastest version of the contact)
*/
public Donation addDonation(Donation donation) {
Contact contact = donation.getDonor();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// remove the donation if exist
if (donation.getIdDonation() != null) {
contact.getDonations().remove(donation);
}
// merge the donation, add it and merge the contact
donation = em.merge(donation);
contact.getDonations().add(donation);
contact = em.merge(contact);
em.getTransaction().commit();
em.close();
// store the donor in the donation as a return value
donation.setDonor(contact);
return donation;
}
/**
* Remove the donation of the contact.
* This method manage the consistence of the operation in one transaction
* @param donation the donation to remove
* @return the new version of the donor after persisted
*/
public Contact removeDonation(Donation donation) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
donation = em.find(Donation.class, donation.getIdDonation());
Contact contact = donation.getDonor();
// remove the donation if exist
if (donation.getIdDonation() != null) {
contact.getDonations().remove(donation);
}
em.remove(donation);
contact = em.merge(contact);
em.getTransaction().commit();
em.close();
return contact;
}
/**
* Add or update the document of the contact.
* This method manage the consistence of the operation in one transaction
* @param document the document to persist. The contact must be set
* @return the new version of the document after persisted (the contact is
* updated with the lastest version of the contact)
*/
public Document addDocument(Document document) {
Contact contact = document.getContact();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// remove the document if exist
if (document.getIdDocument() != null) {
contact.getDocumentLinks().remove(document);
}
// merge the document, add it and merge the contact
document = em.merge(document);
contact.getDocumentLinks().add(document);
contact = em.merge(contact);
em.getTransaction().commit();
em.close();
// store the contact in the document as a return value
document.setContact(contact);
return document;
}
/**
* Remove the document of the contact.
* This method manage the consistence of the operation in one transaction
* @param document the document to remove
* @return the new version of the contact after persisted
*/
public Contact removeDocument(Document document) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
document = em.find(Document.class, document.getIdDocument());
Contact contact = document.getContact();
// remove the donation if exist
if (document.getIdDocument() != null) {
contact.getDocumentLinks().remove(document);
}
em.remove(document);
contact = em.merge(contact);
em.getTransaction().commit();
em.close();
return contact;
}
}