com.gtcgroup.justify.jpa.helper.JstQueryUtilHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of justify-jpa Show documentation
Show all versions of justify-jpa Show documentation
JPA MODULE: PED Central is the home for the open-source �Justify� suite of software engineering modules. Justify seeks API alignment between Java 1.8+ application code and JUnit 5 test code.
/*
* [Licensed per the Open Source "MIT License".]
*
* Copyright (c) 2006 - 2017 by
* Global Technology Consulting Group, Inc. at
* http://gtcGroup.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.gtcgroup.justify.jpa.helper;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.persistence.CacheRetrieveMode;
import javax.persistence.CacheStoreMode;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.eclipse.persistence.config.CascadePolicy;
import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
import com.gtcgroup.justify.core.exception.internal.JustifyRuntimeException;
import com.gtcgroup.justify.jpa.po.JstCountAllJpaPO;
import com.gtcgroup.justify.jpa.po.internal.BaseJpaPO;
import com.gtcgroup.justify.jpa.po.internal.BaseQueryJpaPO;
/**
* This Helper class provides persistence {@link Query} support.
*
*
* Copyright (c) 2006 - 2017 by Global Technology Consulting Group, Inc. at
* gtcGroup.com .
*
*
* @author Marvin Toll
* @since v3.0
*/
public enum JstQueryUtilHelper {
@SuppressWarnings("javadoc")
INSTANCE;
/**
* This method returns the number of records in the table or view.
*
* @return long
*/
public static long count(final JstCountAllJpaPO queryPO) {
final CriteriaBuilder criteriaBuilder = queryPO.getEntityManager().getCriteriaBuilder();
final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Long.class);
criteriaQuery.select(criteriaBuilder.count(criteriaQuery.from(queryPO.getResultClass())));
final Query query = queryPO.getEntityManager().createQuery(criteriaQuery);
final Long countLong = (Long) query.getSingleResult();
final long count = countLong.longValue();
return count;
}
@SuppressWarnings("boxing")
static Query decorateQuery(final BaseQueryJpaPO queryPO, final Map stringParameterMap) {
final Query query = queryPO.getQuery();
if (null != stringParameterMap) {
for (final Entry stringEntry : stringParameterMap.entrySet()) {
query.setParameter(stringEntry.getKey(), stringEntry.getValue());
}
}
if (!queryPO.isSuppressForceDatabaseTrip()) {
query.setHint(QueryHints.CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS);
query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
query.setHint(QueryHints.REFRESH_CASCADE, CascadePolicy.CascadeByMapping);
}
if (!queryPO.isSuppressReadOnly()) {
query.setHint(QueryHints.READ_ONLY, HintValues.TRUE);
}
if (queryPO.isFirstResult()) {
query.setFirstResult(queryPO.getFirstResult());
}
if (queryPO.isMaxResults()) {
query.setMaxResults(queryPO.getMaxResults());
}
return query;
}
/**
* This method executes a query with parameters.
*
* @return {@link List}
*/
public static List queryResultList(final BaseQueryJpaPO queryPO,
final Map stringParameterMap) {
List entityList = null;
try {
final Query query = decorateQuery(queryPO, stringParameterMap);
entityList = queryList(query, queryPO);
} finally {
queryPO.closeEntityManagerIfCreatedWithPersistenceUnitName();
}
return entityList;
}
/**
* This method executes a query with parameters.
*
* @return {@link List}
*/
public static List queryResultList(final BaseQueryJpaPO queryPO) {
List entityList = null;
try {
final Query query = decorateQuery(queryPO, null);
entityList = queryList(query, queryPO);
} finally {
queryPO.closeEntityManagerIfCreatedWithPersistenceUnitName();
}
return entityList;
}
@SuppressWarnings("unchecked")
private static List queryList(final Query query, final BaseQueryJpaPO queryPO) {
List entityList;
try {
entityList = query.getResultList();
} catch (final Exception e) {
throw new JustifyRuntimeException(e);
}
if (entityList.isEmpty()) {
if (!queryPO.isSuppressExceptionForNull()) {
throw new JustifyRuntimeException("The list is empty.");
}
}
return entityList;
}
/**
* This method executes a query with parameters.
*
* @return ENTITY
*/
@SuppressWarnings("unchecked")
public static ENTITY querySingleResult(final BaseQueryJpaPO queryPO,
final Map parameterMap) {
ENTITY entity = null;
try {
final Query query = decorateQuery(queryPO, parameterMap);
entity = (ENTITY) query.getSingleResult();
JstQueryUtilHelper.throwExceptionForNull(queryPO, entity);
} finally {
queryPO.closeEntityManagerIfCreatedWithPersistenceUnitName();
}
return entity;
}
/**
* This method executes a query with parameters.
*
* @return ENTITY
*/
@SuppressWarnings("unchecked")
public static ENTITY querySingleResult(final BaseQueryJpaPO queryPO) {
ENTITY entity = null;
try {
final Query query = decorateQuery(queryPO, null);
entity = (ENTITY) query.getSingleResult();
JstQueryUtilHelper.throwExceptionForNull(queryPO, entity);
} catch (final NoResultException e) {
JstQueryUtilHelper.throwExceptionForNull(queryPO, entity);
} catch (final Exception e) {
throw new JustifyRuntimeException(e);
} finally {
queryPO.closeEntityManagerIfCreatedWithPersistenceUnitName();
}
return entity;
}
/**
* This method handles exception suppression.
*/
public static void throwExceptionForNull(final BaseJpaPO queryPO, final Object entity) {
if (null == entity) {
if (!queryPO.isSuppressExceptionForNull()) {
throw new JustifyRuntimeException("Unable to retrieve a result instance.");
}
}
}
}