Package com.intel.mtwilson.as.controller

Source Code of com.intel.mtwilson.as.controller.TblOemJpaController

/*
* Copyright (c) 2013, Intel Corporation.
* All rights reserved.
*
* The contents of this file are released under the BSD license, you may not use this file except in compliance with the 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 Intel Corporation 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 com.intel.mtwilson.as.controller;

import com.intel.mtwilson.as.controller.exceptions.ASDataException;
import com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException;
import com.intel.mtwilson.as.data.TblOem;
import java.io.Serializable;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
*
* @author dsmagadx
*/
public class TblOemJpaController implements Serializable {
    private Logger log = LoggerFactory.getLogger(getClass());

    public TblOemJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TblOem tblOem) {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(tblOem);
            em.getTransaction().commit();
        } finally {
                em.close();
        }
    }

    public void edit(TblOem tblOem) throws NonexistentEntityException, ASDataException {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            em.merge(tblOem);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tblOem.getId();
                if (findTblOem(id) == null) {
                    throw new NonexistentEntityException("The tblOem with id " + id + " no longer exists.");
                }
            }
            throw new ASDataException(ex);
        } finally {
                em.close();
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            TblOem tblOem;
            try {
                tblOem = em.getReference(TblOem.class, id);
                tblOem.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tblOem with id " + id + " no longer exists.", enfe);
            }
            em.remove(tblOem);
            em.getTransaction().commit();
        } finally {
                em.close();
        }
    }

    public List<TblOem> findTblOemEntities() {
        return findTblOemEntities(true, -1, -1);
    }

    public List<TblOem> findTblOemEntities(int maxResults, int firstResult) {
        return findTblOemEntities(false, maxResults, firstResult);
    }

    private List<TblOem> findTblOemEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(TblOem.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TblOem findTblOem(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TblOem.class, id);
        } finally {
            em.close();
        }
    }

    public int getTblOemCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<TblOem> rt = cq.from(TblOem.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public TblOem findTblOemByName(String name) {
        EntityManager em = getEntityManager();
        try {
            Query query = em.createNamedQuery("TblOem.findByName");
            query.setParameter("name", name);
            TblOem tblOem = (TblOem) query.getSingleResult();
            return tblOem;

        } catch(NoResultException e){
            log.info( "NoResultException : OEM [{}] not found", name);
            return null;
        }finally {
            em.close();
        }
    }
   
}
TOP

Related Classes of com.intel.mtwilson.as.controller.TblOemJpaController

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.