Package eclc

Source Code of eclc.ExpensJpaController

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

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. 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.

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 OWNER 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.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*
*
*
*/


/**
*
*/
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 DieHard
*
*/
public class ExpensJpaController implements Serializable {

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

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

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

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

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

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

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

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

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

                if (findExpens(id) == null) {
                    throw new NonexistentEntityException("The entry 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();

            Expens entry;

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

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

    /**
     * Method description
     *
     *
     * @return
     */
    public List<Expens> findEntryEntities() {
        return findExpensEntities(true, -1, -1);
    }

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

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

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

            cq.select(cq.from(Expens.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 Expens findExpens(Integer id) {
        EntityManager em = getEntityManager();

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

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

        try {
            CriteriaQuery<Object> cq = em.getCriteriaBuilder().createQuery();
            Root<Expens> rt = cq.from(Expens.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/23
TOP

Related Classes of eclc.ExpensJpaController

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.