Example usage for javax.persistence EntityManager getCriteriaBuilder

List of usage examples for javax.persistence EntityManager getCriteriaBuilder

Introduction

In this page you can find the example usage for javax.persistence EntityManager getCriteriaBuilder.

Prototype

public CriteriaBuilder getCriteriaBuilder();

Source Link

Document

Return an instance of CriteriaBuilder for the creation of CriteriaQuery objects.

Usage

From source file:org.eclipse.jubula.client.core.persistence.TestDataCubePM.java

/**
 * @param tdc//  ww  w  .j a  va 2 s. c  o  m
 *            the test data cube to search for reusage
 * @param session
 *            The session into which the test cases will be loaded.
 * @return list of ITestDataCubePO
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<ITestDataCubePO> computeReuser(IParameterInterfacePO tdc, EntityManager session) {

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery();
    Root from = query.from(PoMaker.getTestDataCubeClass());
    query.select(from).where(builder.isNotNull(from.get("hbmReferencedDataCube"))); //$NON-NLS-1$

    List<ITestDataCubePO> queryResult = session.createQuery(query).getResultList();
    List<ITestDataCubePO> result = new ArrayList<ITestDataCubePO>();
    for (ITestDataCubePO pio : queryResult) {
        if (areEqual(pio.getReferencedDataCube(), tdc)) {
            result.add(pio);
        }
    }
    return result;
}

From source file:org.eclipse.jubula.client.core.persistence.TestDataCubePM.java

/**
 * @param pioToSearch/*w ww.  j a va2s .  c o m*/
 *            the test data cube to search for reusage
 * @param session
 *            The session into which the test cases will be loaded.
 * @param proj
 *            the project to search in
 * @return list of param node po; without any ITestDataCubePOs themselves as
 *         they don't have a parent project id
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<IParamNodePO> computeParamNodeReuser(IParameterInterfacePO pioToSearch,
        EntityManager session, IProjectPO proj) {

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery();
    Root from = query.from(PoMaker.getTestCasePOClass());
    query.select(from).where(builder.equal(from.get("hbmParentProjectId"), proj.getId())); //$NON-NLS-1$

    List<IParameterInterfacePO> queryResult = session.createQuery(query).getResultList();
    List<IParamNodePO> result = new ArrayList<IParamNodePO>();
    for (IParameterInterfacePO pio : queryResult) {
        if (pio instanceof IParamNodePO) {
            IParamNodePO pn = (IParamNodePO) pio;
            if (areEqual(pn.getReferencedDataCube(), pioToSearch)) {
                result.add(pn);
            }
        }
    }
    return result;
}

From source file:org.bubblecloud.ilves.cache.UserClientCertificateCache.java

/**
 * Get user by certificate./*  ww w  . j  a v  a 2 s .  com*/
 *
 * @param clientCertificate the client certificate
 * @param blackListNotFound whether certificate should be blacklisted if user is not found
 * @return the user or null if no matching user or more than one matching user was found.
 */
public static synchronized User getUserByCertificate(final Certificate clientCertificate,
        final boolean blackListNotFound) {
    if (blacklistCache.get(clientCertificate) != null) {
        LOGGER.debug(
                "Blacklisted TSL client certificate: " + ((X509Certificate) clientCertificate).getSubjectDN());
        return null;
    }
    final User cachedUser = certificateCache.get(clientCertificate);
    if (cachedUser != null) {
        LOGGER.debug("User matching TSL client certificate in cache: " + cachedUser.getUserId());
        return cachedUser;
    }
    final String encodedCertificateString;
    try {
        encodedCertificateString = Base64.encodeBase64String(clientCertificate.getEncoded());
    } catch (CertificateEncodingException e) {
        LOGGER.error("Error encoding TSL client certificate for finding user from database.");
        return null;
    }

    final EntityManager entityManager = entityManagerFactory.createEntityManager();
    final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    final CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class);
    final Root<User> root = criteria.from(User.class);
    criteria.where(criteriaBuilder.equal(root.get("certificate"), encodedCertificateString));
    final TypedQuery<User> query = entityManager.createQuery(criteria);
    final List<User> users = query.getResultList();
    if (users.size() == 1) {
        LOGGER.info("User found matching TSL client certificate: " + users.get(0).getUserId());
        certificateCache.put(clientCertificate, users.get(0));
        return users.get(0);
    } else if (users.size() > 1) {
        blacklistCache.put(clientCertificate, clientCertificate);
        LOGGER.error("Blacklisted TSL client certificate. More than one user had the certificate: "
                + clientCertificate);
        return null;
    } else {
        if (blackListNotFound) {
            blacklistCache.put(clientCertificate, clientCertificate);
            LOGGER.warn("Blacklisted TSL client certificate. User not found matching the certificate: "
                    + ((X509Certificate) clientCertificate).getSubjectDN());
        } else {
            LOGGER.warn("User not found matching the certificate: "
                    + ((X509Certificate) clientCertificate).getSubjectDN());
        }
        return null;
    }
}

From source file:org.niord.core.db.CriteriaHelper.java

/**
 * Initializes a CriteriaBuilder and CriteriaQuery with a Tuple query.
 * @param em the entity manager//  w  w w  .  j  ava2 s.c o m
 * @return the newly instantiated criteria builder
 */
public static CriteriaHelper<Tuple> initWithTupleQuery(EntityManager em) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> tupleQuery = builder.createTupleQuery();
    return new CriteriaHelper<>(builder, tupleQuery);
}

From source file:org.niord.core.db.CriteriaHelper.java

/**
 * Initializes a CriteriaBuilder and CriteriaQuery with a the given result class.
 * @param em the entity manager//from  www . ja  va 2 s. c  o  m
 * @param resultClass the result class
 * @return the newly instantiated criteria builder
 */
public static <T> CriteriaHelper<T> initWithQuery(EntityManager em, Class<T> resultClass) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(resultClass);
    return new CriteriaHelper<>(builder, query);
}

From source file:org.eclipse.jubula.client.core.persistence.TestResultPM.java

/**
 * @param session/* w  ww .  j av a 2s. co m*/
 *            The session in which to execute the Persistence (JPA / EclipseLink) query.
 * @return a list of test result ids that have test result details
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List<Number> computeTestresultIdsWithDetails(EntityManager session) {

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery();
    Path from = query.from(PoMaker.getTestResultClass()).get("internalTestResultSummaryID"); //$NON-NLS-1$
    query.select(from).distinct(true);

    return session.createQuery(query).getResultList();
}

From source file:org.eclipse.jubula.client.core.persistence.TestResultPM.java

/**
 * @param session The session in which to execute the Persistence (JPA / EclipseLink) query.
 * @param summaryId The database ID of the summary for which to compute the
 *                  corresponding Test Result nodes.
 * @return the Test Result nodes associated with the given Test Result 
 *         Summary, sorted by sequence (ascending).
 *//*w  ww.j  a  v a  2 s  .co m*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List<ITestResultPO> computeTestResultListForSummary(EntityManager session, Long summaryId) {

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery();
    Root from = query.from(PoMaker.getTestResultClass());
    query.orderBy(builder.asc(from.get("keywordSequence"))) //$NON-NLS-1$
            .select(from).where(builder.equal(from.get("internalTestResultSummaryID"), summaryId)); //$NON-NLS-1$

    return session.createQuery(query).getResultList();
}

From source file:com.github.jinahya.persistence.ShadowTest.java

protected static List<Morton> MORTONS(final EntityManager manager, final int firstResult,
        final int maxResults) {

    final CriteriaBuilder builder = manager.getCriteriaBuilder();
    final CriteriaQuery<Morton> query = builder.createQuery(Morton.class);
    final Root<Morton> morton = query.from(Morton.class);

    query.select(morton).orderBy(builder.desc(morton.get(Morton_.id)));

    return manager.createQuery(query).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
}

From source file:org.agric.oxm.utils.JpaUtils.java

/**
 * Create a row count CriteriaQuery from a CriteriaQuery
 * //from  ww  w .  j  a  v  a 2  s  .c  om
 * @param em
 *            entity manager
 * @param criteria
 *            source criteria
 * @return row coutnt CriteriaQuery
 */
public static <T> CriteriaQuery<Long> countCriteria(EntityManager em, CriteriaQuery<T> criteria) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);
    copyCriteriaNoSelection(criteria, countCriteria);
    countCriteria.select(builder.count(findRoot(countCriteria, criteria.getResultType())));

    return countCriteria;
}

From source file:org.eclipse.jubula.client.core.persistence.TestResultPM.java

/**
 * @param session/* www . j  a v a  2s.com*/
 *            The session in which to execute the Persistence (JPA /
 *            EclipseLink) query.
 * @param summaryId
 *            The database ID of the summary for which to compute the
 *            corresponding Test Result nodes.
 * @return the Test Result nodes associated with the given Test Result
 *         Summary, sorted by sequence (ascending).
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ITestResultSummaryPO getTestResultSummary(EntityManager session, Long summaryId) {
    if (session == null) {
        return null;
    }

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery();
    Root from = query.from(PoMaker.getTestResultSummaryClass());
    query.where(builder.equal(from.get("id"), summaryId)); //$NON-NLS-1$

    return (ITestResultSummaryPO) session.createQuery(query).getSingleResult();
}