/*
* @(#)EntryJpaController.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.
*
*
*
*/
/*
* 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 EntryJpaController implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private EntityManagerFactory emf = null;
/**
* Constructs ...
*
*
* @param emf
*/
public EntryJpaController(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(Entry entry) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(entry);
em.getTransaction().commit();
} catch (Exception ex) {
if (findEntry(entry.getTruckid()) != null) {
throw new PreexistingEntityException("Entry " + entry + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
/**
* Method description
*
*
* @param entry
*
* @throws Exception
* @throws NonexistentEntityException
*/
public void edit(Entry 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.getTruckid();
if (findEntry(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();
Entry entry;
try {
entry = em.getReference(Entry.class, id);
entry.getTruckid();
} 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<Entry> findEntryEntities() {
return findEntryEntities(true, -1, -1);
}
/**
* Method description
*
*
* @param maxResults
* @param firstResult
*
* @return
*/
public List<Entry> findEntryEntities(int maxResults, int firstResult) {
return findEntryEntities(false, maxResults, firstResult);
}
@SuppressWarnings("unchecked")
private List<Entry> findEntryEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery<Object> cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Entry.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 Entry findEntry(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Entry.class, id);
} finally {
em.close();
}
}
/**
* Method description
*
*
* @return
*/
public int getEntryCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery<Object> cq = em.getCriteriaBuilder().createQuery();
Root<Entry> rt = cq.from(Entry.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