Example usage for javax.persistence.criteria CriteriaQuery where

List of usage examples for javax.persistence.criteria CriteriaQuery where

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery where.

Prototype

CriteriaQuery<T> where(Predicate... restrictions);

Source Link

Document

Modify the query to restrict the query result according to the conjunction of the specified restriction predicates.

Usage

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

/**
 * Get user by certificate.//from  w w w . j av  a2  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.eclipse.jubula.client.core.persistence.TestResultPM.java

/**
 * @param session//  ww w. j  a  v  a  2s  . c o  m
 *            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();
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Customer> root,
        CustomerQueryCommand command) {/*w  w  w. j  av  a2  s.  c  om*/
    List<Predicate> ps = Lists.newArrayListWithCapacity(2);
    ps.add(cb.equal(root.get(Customer_.deleteFlag), command.isDeleteFlag()));
    if (!isBlank(command.getName())) {
        ps.add(cb.like(root.get(Customer_.name), command.getNameQuery()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Experience> root,
        ExperienceQueryCommand command) {
    List<Predicate> ps = Lists.newArrayListWithCapacity(2);
    if (command.getCustomers() != null) {
        ps.add(root.get(Experience_.customer).in(command.getCustomers()));
    }/*w ww  .  ja v a 2 s.  c  o m*/
    if (!isBlank(command.getName())) {
        ps.add(cb.like(root.get(Experience_.name), command.getNameQuery()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<ExperienceTag> root,
        ExperienceTagQueryCommand command) {
    List<Predicate> ps = Lists.newArrayListWithCapacity(2);
    if (command.getCustomers() != null) {
        ps.add(root.get(ExperienceTag_.customer).in(command.getCustomers()));
    }//from  w w w. jav  a  2 s  . c o m
    if (!isBlank(command.getName())) {
        ps.add(cb.like(root.get(ExperienceTag_.name), command.getNameQuery()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<TaskSmsTemplate> root,
        TaskSmsTemplateQueryCommand command) {
    List<Predicate> ps = Lists.newArrayListWithCapacity(2);
    if (command.getCustomers() != null) {
        ps.add(root.get(TaskSmsTemplate_.customer).in(command.getCustomers()));
    }//from   w  w w.j  ava  2  s  . c  om
    if (!isBlank(command.getName())) {
        ps.add(cb.like(root.get(TaskSmsTemplate_.name), command.getNameQuery()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Operator> root,
        OperatorQueryCommand command) {//from w w w . j  a  va  2 s  .  c om
    List<Predicate> ps = Lists.newArrayListWithCapacity(3);
    ps.add(cb.equal(root.get(Operator_.deleteFlag), command.isDeleteFlag()));
    if (command.getCustomer() != null) {
        ps.add(cb.isMember(command.getCustomer(), root.get(Operator_.customers)));
    }
    if (!isBlank(command.getName())) {
        ps.add(cb.like(root.get(Operator_.name), command.getNameQuery()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

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

/**
 * Copy Criteria without Selection//from   w  w w .j  a va2s .c o m
 * 
 * @param from
 *            source Criteria
 * @param to
 *            destination Criteria
 */
public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) {

    // Copy Roots
    for (Root<?> root : from.getRoots()) {
        Root<?> dest = to.from(root.getJavaType());
        dest.alias(getOrCreateAlias(root));
        copyJoins(root, dest);
    }

    to.groupBy(from.getGroupList());
    to.distinct(from.isDistinct());
    to.having(from.getGroupRestriction());
    to.where(from.getRestriction());
    to.orderBy(from.getOrderList());
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Create a query for fetching a single object
 * // w  w w  .  j ava 2  s.co m
 * @param entityManager
 *            the entity manager
 * @param entityClass
 *            the entity class
 * @param id
 *            ID of the object to return
 * @param fetchJoins
 *            fetch joins to include
 * @return
 */
public static <ID, T> CriteriaQuery<T> createFetchSingleObjectQuery(EntityManager entityManager,
        Class<T> entityClass, ID id, FetchJoinInformation[] fetchJoins) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> cq = builder.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    addFetchJoinInformation(root, fetchJoins);

    cq.where(builder.equal(root.get(DynamoConstants.ID), id));
    return cq;
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<PayOrder> root,
        PayOrderQueryCommand command) {/*from   w  w w.  j a  v a  2s  .c  o m*/
    List<Predicate> ps = Lists.newArrayListWithCapacity(3);
    if (command.getCustomer() != null) {
        ps.add(cb.equal(root.get(PayOrder_.customer), command.getCustomer()));
    }
    if (command.getPayDateStart() != null) {
        ps.add(cb.greaterThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart()));
    }
    if (command.getPayDateEnd() != null) {
        ps.add(cb.lessThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}