Package eclc

Source Code of eclc.SaleJpaController

/*
* @(#)SaleJpaController.java   13/08/21
*
* Copyright (c) 2013 DieHard Development
*
* All rights reserved.
   Released under the BSD 3 clause license
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer. Redistributions in binary
    form must reproduce the above copyright notice, this list of conditions and
    the following disclaimer in the documentation and/or other materials
    provided with the distribution. Neither the name of the DieHard Development
    nor the names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*
*/


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package eclc;

//~--- non-JDK imports --------------------------------------------------------

import eclc.Exceptions.exceptions.NonexistentEntityException;
import eclc.Exceptions.exceptions.PreexistingEntityException;

//~--- JDK imports ------------------------------------------------------------

import java.io.Serializable;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
*
* @author TJ
*/
public class SaleJpaController implements Serializable {

    /**
     *   
     */
    private static final long serialVersionUID = 1L;
    private EntityManagerFactory emf = null;

    /**
     * Constructs ...
     *
     *
     * @param emf
     */
    public SaleJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    /**
     * Method description
     *
     *
     * @param Sale
     *
     * @throws Exception
     * @throws PreexistingEntityException
     */
    public void create(Sale Sale) throws PreexistingEntityException, Exception {
        EntityManager em = null;

        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(Sale);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findSale(Sale.getTransid()) != null) {
                throw new PreexistingEntityException("Sale " + Sale + " already exists.", ex);
            }

            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    /**
     * Method description
     *
     *
     * @param Sale
     *
     * @throws Exception
     * @throws NonexistentEntityException
     */
    public void edit(Sale Sale) throws NonexistentEntityException, Exception {
        EntityManager em = null;

        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Sale = em.merge(Sale);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();

            if ((msg == null) || (msg.length() == 0)) {
                Integer id = Sale.getTransid();

                if (findSale(id) == null) {
                    throw new NonexistentEntityException("The Sale with id " + id + " no longer exists.");
                }
            }

            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    /**
     * Method description
     *
     *
     * @param id
     *
     * @throws NonexistentEntityException
     */
    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;

        try {
            em = getEntityManager();
            em.getTransaction().begin();

            Sale Sale;

            try {
                Sale = em.getReference(Sale.class, id);
                Sale.getTransid();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The Sale with id " + id + " no longer exists.", enfe);
            }

            em.remove(Sale);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public List<Sale> findSaleEntities() {
        return findSaleEntities(true, -1, -1);
    }

    /**
     * Method description
     *
     *
     * @param maxResults
     * @param firstResult
     *
     * @return
     */
    public List<Sale> findSaleEntities(int maxResults, int firstResult) {
        return findSaleEntities(false, maxResults, firstResult);
    }

    @SuppressWarnings("unchecked")
    private List<Sale> findSaleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();

        try {
            CriteriaQuery<Object> cq = em.getCriteriaBuilder().createQuery();

            cq.select(cq.from(Sale.class));

            Query q = em.createQuery(cq);

            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }

            return q.getResultList();
        } finally {
            em.close();
        }
    }

    /**
     * Method description
     *
     *
     * @param id
     *
     * @return
     */
    public Sale findSale(Integer id) {
        EntityManager em = getEntityManager();

        try {
            return em.find(Sale.class, id);
        } finally {
            em.close();
        }
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public int getSaleCount() {
        EntityManager em = getEntityManager();

        try {
            CriteriaQuery<Object> cq = em.getCriteriaBuilder().createQuery();
            Root<Sale> rt = cq.from(Sale.class);

            cq.select(em.getCriteriaBuilder().count(rt));

            Query q = em.createQuery(cq);

            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}


//~ Formatted in DD Std on 13/08/21
TOP

Related Classes of eclc.SaleJpaController

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.