Package org.spw.controller

Source Code of org.spw.controller.OptionValueController

/*
* OptionValueController.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.OptionValue;
import org.spw.model.OptionValuePK;

/**
* Manage the generic options.
* Implement a CRUD controller.
* @author PSe
*/
public class OptionValueController {
   
    private final EntityManagerFactory emf;
    public static final String OFFICES = "Offices";
    public static final String PARTNERSHIPS = "Partnerships";
    public static final String WEBSITE_TYPES = "Website types";
    public static final String EVENT_TYPES = "Event types";
    public static final String EXPO_STALL = "Expo/Stall";
    public static final String HOW_HEARD = "How heard";
    public static final String TITLES = "Titles";
    public static final String CONTRACT_TYPES = "Contract types";

    /** Creates a new instance of OptionValueController */
    public OptionValueController() {
        Properties props = PropertiesLoader.loadProperties("database.properties");
        if (props != null)
        {
            emf = Persistence.createEntityManagerFactory("VolunteerPU", props);
        }
        else
        {
            emf = Persistence.createEntityManagerFactory("VolunteerPU");
        }
    }
   
    /**
     * Retrieve all the options
     * @return List of all option value objects
     */
    public List<OptionValue> getOptionValues() {
        EntityManager em = emf.createEntityManager();
        List<OptionValue> result = null;
        try {
            Query query = em.createNamedQuery("OptionValue.findAll");
            result = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(OptionValueController.class.getName()).log(Level.SEVERE, "Error Query all OptionValue", e);
        } finally {
            em.close();
        }
        return result;
    }
   
    /**
     * Retrieve only the values of one option, find by name.
     * @param option The option selected
     * @return List of all values for this option.
     */
    public List<OptionValue> getOptionValueByName(String option) {
        EntityManager em = emf.createEntityManager();
        List<OptionValue>  retValue = null;
        try {
            Query query = em.createNamedQuery("OptionValue.findByName");
            query.setParameter("name", option);
            retValue = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(OptionValueController.class.getName()).log(Level.SEVERE, "Error reading " + option, e);
        } finally {
            em.close();
        }
        return retValue;
    }
   
    /**
     * Retrieve the exact value option with its key.
     * @param option The coption
     * @param key key of the option
     * @return The corresponding object
     */
    public OptionValue getOptionValueByLabel(String option, String key) {
        EntityManager em = emf.createEntityManager();
        OptionValue retValue = null;
        try {
            Query query = em.createNamedQuery("OptionValue.findByLabel");
            query.setParameter("name", option);
            query.setParameter("label", key);
            retValue = (OptionValue) query.getSingleResult();
        } catch (NoResultException e) {
            Logger.getLogger(OptionValueController.class.getName()).log(Level.FINEST, "Not found " + key, e);
        } catch (Exception e) {
            Logger.getLogger(OptionValueController.class.getName()).log(Level.SEVERE, "Error reading " + key, e);
        } finally {
            em.close();
        }
        return retValue;
    }
   
    public void create(OptionValue object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(OptionValueController.class.getName()).log(Level.SEVERE, "Error creating " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
   
    public OptionValue read(OptionValuePK key) {
        EntityManager em = emf.createEntityManager();
        OptionValue retValue = null;
        try {
            retValue = em.find(OptionValue.class, key);
        } catch (Exception e) {
            Logger.getLogger(OptionValueController.class.getName()).log(Level.SEVERE, "Error reading " + key, e);
        } finally {
            em.close();
        }
        return retValue;
    }
   
    public OptionValue update(OptionValue object) {
        OptionValue result = null;
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            result = em.merge(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(OptionValueController.class.getName()).log(Level.SEVERE, "Error updating " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
   
    public void delete(OptionValue object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            OptionValuePK pk = new OptionValuePK(object.getOptionName(),
                    object.getOptionOrder());
            OptionValue entity = em.find(OptionValue.class, pk);
            em.remove(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(OptionValueController.class.getName()).log(Level.SEVERE, "Error deleting " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
   
}
TOP

Related Classes of org.spw.controller.OptionValueController

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.