Package org.spw.controller

Source Code of org.spw.controller.CommunicationController

/*
* CommunicationController.java
*
* Created on 3 July 2007, 19:26
*
*/

package org.spw.controller;

import java.util.Collection;
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.Communication;
import org.spw.model.Contact;

/**
* This class manage the Communication entities.
* It's a basic CRUD controller
* @author PSe
*/
public class CommunicationController {
   
    private static EntityManagerFactory emf;
   
    /** Creates a new instance of CommunicationController */
    public CommunicationController() {
        Properties props = PropertiesLoader.loadProperties("database.properties");
        if (props != null)
        {
            emf = Persistence.createEntityManagerFactory("VolunteerPU", props);
        }
        else
        {
            emf = Persistence.createEntityManagerFactory("VolunteerPU");
        }
    }
   
    /**
     * Retreive all the communications
     * @return all the communications
     */
    public List<Communication> getCommunications() {
        EntityManager em = emf.createEntityManager();
        List<Communication> result = null;
        try {
            Query query = em.createNamedQuery("Communication.findAll");
            result = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(CommunicationController.class.getName()).log(Level.SEVERE, "Error Query all Communication", e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
    /**
     * Createa new communication
     * @param object the communication to create
     */
    public void create(Communication object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(CommunicationController.class.getName()).log(Level.SEVERE, "Error creating " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
   
    /**
     * Find a new communication
     * @param key The unique ID of the object
     * @return a communication if found, null otherwise
     */
    public Communication read(Long key) {
        EntityManager em = emf.createEntityManager();
        Communication retValue = null;
        try {
            retValue = em.find(Communication.class, key);
        } catch (Exception e) {
            Logger.getLogger(CommunicationController.class.getName()).log(Level.SEVERE, "Error reading " + key, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return retValue;
    }
   
    /**
     * Update an existing communication. If it's a new Communication, create it.
     * @param object The communication to update
     * @return the new object communication after the persistence operation
     */
    public Communication update(Communication object) {
        Communication result = null;
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            result = em.merge(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(CommunicationController.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 Communication
     * @param object The communication to delete
     */
    public void delete(Communication object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            Communication entity = em.find(Communication.class, object.getIdCommunication());
            // remove communication from contact removed from the list
            for(Contact contact : entity.getParticipants()) {
                contact.getParticipate().remove(entity);
                em.merge(contact);
            }
           
            em.remove(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(CommunicationController.class.getName()).log(Level.SEVERE, "Error deleting " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
   
    /**
     * Retreive all the communications with action non completed
     * @return all the communications
     */
    public List<Communication> getActionsNotCompleted() {
        EntityManager em = emf.createEntityManager();
        List<Communication> result = null;
        try {
            Query query = em.createNamedQuery("Communication.findActionsNotCompleted");
            result = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(CommunicationController.class.getName()).log(Level.SEVERE, "Error Query ActionsNotCompleted", e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
    /**
     * Update an existing communication with the list of participants involved
     * Takes care of both side of the relation. The participant list must be
     * set in the property participants before.
     * @param communication the communication (can be a new one)
     * @param newParticipants new list of participants (the existing list must not change)
     * Could be null if no previous participants.
     * @return the new communication object after being persisted
     */
    public Communication updateParticipants(Communication communication,
            List<Contact> newParticipants) {
        // First persist the communication with all the contacts
        Communication result = null;
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            // remove communication from contact removed from the list
            for(Contact contact : communication.getParticipants()) {
                if (!newParticipants.contains(contact)) {
                    contact.getParticipate().remove(communication);
                    em.merge(contact);
                }
            }
            // update communication
            communication.setParticipants(newParticipants);
            result = em.merge(communication);
            // add communication for new ones
            for(Contact contact : newParticipants) {
                Collection communications = contact.getParticipate();
                if (! communications.contains(result)) {
                    contact.getParticipate().add(communication);
                    em.merge(contact);
                }
            }
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(CommunicationController.class.getName()).log(Level.SEVERE, "Error updating " + communication, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
}
TOP

Related Classes of org.spw.controller.CommunicationController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.