net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java Source code

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java

Source

/*******************************************************************************
 * Copyright 2014 Juan Diego Navarre Gonzalez
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package net.navasoft.madcoin.backend.model.controller.helper.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import net.navasoft.madcoin.backend.model.controller.helper.ComplexId;
import net.navasoft.madcoin.backend.model.controller.helper.IJPAHelper;
import net.navasoft.madcoin.backend.model.entities.NormalizedEntity;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * net.navasoft.madcoin.backend.model.controller Class class JPAControllerImpl.
 * Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (${authorMail})
 * @version 1.0
 * @param <Entity>
 *            the generic type
 * @since 28/08/2014 11:20:27 PM
 */

public abstract class JPAHelper<Entity extends NormalizedEntity> implements IJPAHelper<Entity> {

    /**
     * Gets the quantity.
     * 
     * @param dbAccess
     *            the db access
     * @param target
     *            the target
     * @return the quantity
     * @since 28/08/2014, 11:20:27 PM
     */
    public int getQuantity(EntityManager dbAccess, Class<Entity> target) {
        CriteriaQuery<Long> cq = dbAccess.getCriteriaBuilder().createQuery(Long.class);
        Root<Entity> rt = cq.from(target);
        cq.select(dbAccess.getCriteriaBuilder().count(rt));
        Query q = dbAccess.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    /**
     * Find by simple id.
     * 
     * @param dbAccess
     *            the db access
     * @param id
     *            the id
     * @param target
     *            the target
     * @return the entity
     * @since 28/08/2014, 11:20:27 PM
     */
    @Override
    public Entity findBySimpleId(EntityManager dbAccess, Number id, Class<Entity> target) {
        return dbAccess.find(target, id);
    }

    /**
     * Find by complex id.
     * 
     * @param dbAccess
     *            the db access
     * @param id
     *            the id
     * @param target
     *            the target
     * @return the entity
     * @since 28/08/2014, 11:20:27 PM
     */
    @Override
    public Entity findByComplexId(EntityManager dbAccess, ComplexId id, Class<Entity> target) {
        return dbAccess.find(target, id);
    }

    // /**
    // * New record.
    // *
    // * @param target
    // * the target
    // * @param newRecord
    // * the new record
    // * @return the entity
    // * @throws PreexistingEntityException
    // * the preexisting entity exception
    // * @throws NonexistentEntityException
    // * the nonexistent entity exception
    // * @since 12/08/2014, 02:05:48 AM
    // */
    // @Override
    // public Entity newRecord(EntityManager dbAccess, Class<Entity> target,
    // Entity newRecord) throws PreexistingEntityException,
    // NonexistentEntityException {
    // try {
    // dbAccess.persist(newRecord);
    // dbAccess.flush();
    // } catch (ConstraintViolationException duplicate) {
    // throw new PreexistingEntityException(duplicate.getMessage(),
    // duplicate.getCause());
    // }
    // if (newRecord.getIdClass().equals(Long.class)
    // || newRecord.getIdClass().equals(Integer.class)) {
    // return findLast(dbAccess, target, newRecord);
    // } else {
    // return findByComplexId(dbAccess,
    // (ComplexId) newRecord.getNormalizedId(), target);
    // }
    // }
    //
    /**
     * Find last.
     * 
     * @param dbAccess
     *            the db access
     * @param target
     *            the target
     * @param newRecord
     *            the new record
     * @return the entity
     * @since 28/08/2014, 11:20:27 PM
     */
    @Transactional(propagation = Propagation.REQUIRED)
    protected Entity findLast(EntityManager dbAccess, Class<Entity> target, Entity newRecord) {
        TypedQuery<Entity> query = dbAccess.createNamedQuery(newRecord.getNamedQueryId(), target);
        return query.getSingleResult();
    }

    /**
     * Find entities set.
     * 
     * @param em
     *            the em
     * @param target
     *            the target
     * @param maxResults
     *            the max results
     * @param firstResult
     *            the first result
     * @return the list
     * @since 28/08/2014, 11:20:27 PM
     */
    protected List<Entity> findEntitiesSet(EntityManager em, Class<Entity> target, int maxResults,
            int firstResult) {
        return findEntities(em, target, false, maxResults, firstResult);
    }

    /**
     * Find entities.
     * 
     * @param em
     *            the em
     * @param target
     *            the target
     * @param all
     *            the all
     * @param maxResults
     *            the max results
     * @param firstResult
     *            the first result
     * @return the list
     * @since 28/08/2014, 11:20:27 PM
     */
    private List<Entity> findEntities(EntityManager em, Class<Entity> target, boolean all, int maxResults,
            int firstResult) {
        CriteriaQuery<Entity> cq = em.getCriteriaBuilder().createQuery(target);
        cq.select(cq.from(target));
        TypedQuery<Entity> q = em.createQuery(cq);
        if (!all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        return q.getResultList();
    }

    /**
     * Find all entities.
     * 
     * @param em
     *            the em
     * @param target
     *            the target
     * @return the list
     * @since 28/08/2014, 11:20:27 PM
     */
    protected List<Entity> findAllEntities(EntityManager em, Class<Entity> target) {
        return findEntities(em, target, true, -1, -1);
    }

    /**
     * Gets the allby query.
     * 
     * @param em
     *            the em
     * @param target
     *            the target
     * @return the allby query
     * @since 28/08/2014, 11:20:27 PM
     */
    protected List<Entity> getAllbyQuery(EntityManager em, Class<Entity> target) {
        TypedQuery<Entity> query = em.createNamedQuery(target.getSimpleName() + ".findAll", target);
        return query.getResultList();
    }

}