/*
* @(#)FleetJpaController.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 FleetJpaController implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private EntityManagerFactory emf = null;
/**
* Constructs ...
*
*
* @param emf
*/
public FleetJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
/**
* Method description
*
*
* @return
*/
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
/**
* Method description
*
*
* @param fleet
*
* @throws Exception
* @throws PreexistingEntityException
*/
public void create(Fleet fleet) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(fleet);
em.getTransaction().commit();
} catch (Exception ex) {
if (findFleet(fleet.getFleetid()) != null) {
throw new PreexistingEntityException("Fleet " + fleet + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
/**
* Method description
*
*
* @param fleet
*
* @throws Exception
* @throws NonexistentEntityException
*/
public void edit(Fleet fleet) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
fleet = em.merge(fleet);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if ((msg == null) || (msg.length() == 0)) {
Integer id = fleet.getFleetid();
if (findFleet(id) == null) {
throw new NonexistentEntityException("The fleet 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();
Fleet fleet;
try {
fleet = em.getReference(Fleet.class, id);
fleet.getFleetid();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The fleet with id " + id + " no longer exists.", enfe);
}
em.remove(fleet);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
/**
* Method description
*
*
* @return
*/
public List<Fleet> findFleetEntities() {
return findFleetEntities(true, -1, -1);
}
/**
* Method description
*
*
* @param maxResults
* @param firstResult
*
* @return
*/
public List<Fleet> findFleetEntities(int maxResults, int firstResult) {
return findFleetEntities(false, maxResults, firstResult);
}
@SuppressWarnings("unchecked")
private List<Fleet> findFleetEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery<Object> cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Fleet.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 Fleet findFleet(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Fleet.class, id);
} finally {
em.close();
}
}
/**
* Method description
*
*
* @return
*/
public int getFleetCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery<Object> cq = em.getCriteriaBuilder().createQuery();
Root<Fleet> rt = cq.from(Fleet.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