Package org.spw.controller

Source Code of org.spw.controller.PartnershipController

/*
* PartnershipController.java
*
* Created on 8 June 2007, 20:34
*
*/

package org.spw.controller;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.spw.model.Advertisement;
import org.spw.model.Partnership;

/**
* Manage persistence of Partnership
* @author PSe
*/
public class PartnershipController extends ContactController {
   
    /**
     * Creates a new instance of PartnershipController
     */
    public PartnershipController() {
        super();
    }
   
    /**
     * Return all the partnerships
     * @return list of all partnersips
     */
    public List<Partnership> getPartnerships() {
        EntityManager em = emf.createEntityManager();
        List<Partnership> result = null;
        try {
            Query query = em.createNamedQuery("Partnership.findAll");
            result = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(PartnershipController.class.getName()).log(Level.SEVERE, "Error Query all Partnership", e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
    /**
     * Retrieve the institutions matching the beginning
     *
     * @param beginning Beginning characters to match
     * @param maxReturn if 0, no limit in the result; if not nul, limit
     * the number of returned items.
     * @return List of the Institution with matching beginning
     * limited to maxValue items
     */
    public List<Partnership> getInstitutionsBeginningBy(String beginning, int maxReturn) {
        EntityManager em = emf.createEntityManager();
        List<Partnership> result = null;
        try {
            Query query = em.createNamedQuery("Partnership.findInstitutions");
            query.setParameter("beginning", beginning + "%");
            if (maxReturn > 0) {
                query.setMaxResults(maxReturn);
            }
            result = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(PartnershipController.class.getName()).log(Level.SEVERE, "Error Query Institutions", e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }

    public void create(Partnership object) {
        super.create(object);
    }
   
    public Partnership read(Long key) {
        return (Partnership) super.read(key);
    }
   
    public Partnership update(Partnership object) {
        return (Partnership) super.update(object);
    }
   
   
    public void delete(Partnership object) {
        super.delete(object);
    }
   
    public Partnership parse(String partnership) {
        return (Partnership) super.parse(partnership);
    }

    /**
     * Add (or update) a new Advertisement of a partnership
     * @param partnership The partnership to update (must exist)
     * @param advertisement The advertisement to add (can be a new one)
     * @return The new partnership object.
     */
    public Partnership addAdvertisement(Partnership partnership,
            Advertisement advertisement) {
        Partnership result;
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            advertisement = em.merge(advertisement);
            if (!partnership.getAdvertisements().contains(advertisement)) {
                partnership.getAdvertisements().add(advertisement);
            }
            result = em.merge(partnership);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(PartnershipController.class.getName()).log(Level.SEVERE, "Error updating " + partnership, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return partnership;      
    }
   
    /**
     * Remove an Advertisement of a partnership
     * @param partnership The partnership to update (must exist)
     * @param advertisement The advertisement to remove
     * @return The new partnership object.
     */
    public Partnership removeAdvertisement(Partnership partnership,
            Advertisement advertisement) {
        Partnership result = null;
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            if (partnership.getAdvertisements().contains(advertisement)) {
                partnership.getAdvertisements().remove(advertisement);
            }
            result = em.merge(partnership);
            advertisement = em.find(Advertisement.class, advertisement.getIdAdvertisement());
            em.remove(advertisement);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(PartnershipController.class.getName()).log(Level.SEVERE, "Error updating " + partnership, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;      
    }
}
TOP

Related Classes of org.spw.controller.PartnershipController

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.