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.openmeetings.app.data.basic.Sessionmanagement.java

/**
 * update the session every time a user makes a request
 * /*from w  w  w . j a  v  a  2  s .  co m*/
 * @param SID
 */
private void updatesession(String SID) {
    try {
        // log.debug("****** updatesession: "+SID);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);

        List<Sessiondata> fullList = q.getResultList();
        if (fullList.size() == 0) {
            log.error("Found NO session to updateSession: ");

        } else {
            // log.debug("Found session to updateSession: ");
            Sessiondata sd = fullList.iterator().next();
            // log.debug("Found session to updateSession sd "+sd.getUser_id()+" "+sd.getSession_id());
            sd.setRefresh_time(new Date());

            if (sd.getId() == null) {
                em.persist(sd);
            } else {
                if (!em.contains(sd)) {
                    em.merge(sd);
                }
            }
        }

    } catch (Exception ex2) {
        log.error("[updatesession]: ", ex2);
    }
}

From source file:org.openmeetings.app.data.basic.Sessionmanagement.java

public Boolean updateUserRemoteSession(String SID, String sessionXml) {
    try {/*from   w w  w  .j  a  v  a 2  s  . co  m*/
        log.debug("updateUser User SID: " + SID);

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);
        List<Sessiondata> fullList = q.getResultList();

        if (fullList.size() == 0) {
            log.error("Could not find session to update: " + SID);
            return false;
        } else {
            // log.error("Found session to update: "+SID);
        }
        Sessiondata sd = fullList.get(0);
        // log.debug("Found session to update: "+sd.getSession_id()+
        // " userId: "+USER_ID);

        sd.setRefresh_time(new Date());
        sd.setSessionXml(sessionXml);

        if (sd.getId() == null) {
            em.persist(sd);
        } else {
            if (!em.contains(sd)) {
                em.merge(sd);
            }
        }
        // log.debug("session updated User: "+USER_ID);
        return true;
    } catch (Exception ex2) {
        log.error("[updateUserRemoteSession]: ", ex2);
    }
    return null;
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the list of serviceIds available.
 *
 * @param modelVersion// w w w.  j ava2s  .  c  o m
 *            the model version.
 * @return the list of serviceIds available.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<String> getServiceIds(final String modelVersion) {
    final List<String> value = new ArrayList<String>();
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<ServiceType> q = cb.createQuery(ServiceType.class);
    final Root<ServiceType> f = q.from(ServiceType.class);
    q.where(cb.equal(f.<String>get(ServiceType_.modelVersion), modelVersion));
    q.orderBy(cb.asc(f.<String>get(ServiceType_.serviceId)), cb.asc(f.<String>get(ServiceType_.namespace)));
    final TypedQuery<ServiceType> typedQuery = this.em.createQuery(q);
    final List<ServiceType> services = typedQuery.getResultList();
    services.stream().filter(s -> Objects.nonNull(s.getServiceId())).forEach(s -> value.add(s.getServiceId()));
    return value;
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the list of target name spaces available.
 *
 * @param modelVersion/*from w  ww. j a va2 s  .  c  o m*/
 *            the model version.
 * @return the list of target name spaces available.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<String> getTargetNamespaces(final String modelVersion) {
    final List<String> value = new ArrayList<String>();
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<ClusterType> q = cb.createQuery(ClusterType.class);
    final Root<ClusterType> f = q.from(ClusterType.class);
    q.where(cb.equal(f.<String>get(ClusterType_.modelVersion), modelVersion));
    q.orderBy(cb.asc(f.<String>get(ClusterType_.name)), cb.asc(f.<String>get(ClusterType_.id)));
    final TypedQuery<ClusterType> typedQuery = this.em.createQuery(q);
    final List<ClusterType> cluster = typedQuery.getResultList();
    cluster.stream().filter(c -> Objects.nonNull(c.getName())).forEach(c -> value.add(c.getName()));
    return value;
}

From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

/**
 * @param length/*from   ww w.ja va  2 s .co  m*/
 *            maxResult
 * @param start
 *            pagination starting point
 * @param search
 *            search string like ' name: toto , dupont' to search for %toto% on name + %dupont% in all properties
 * @param searchProperties
 *            properties to search or all if null
 * @param orders
 *            sorted result by properties
 * @return
 */
public List<ENTITY> findFiltered(Integer length, Integer start, String search, List<String> searchProperties,
        List<Order> orders) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ENTITY> query = builder.createQuery(entityClass);
    Root<ENTITY> root = query.from(entityClass);

    if (!Strings.isNullOrEmpty(search)) {
        Predicate[] buildFilterPredicates = BuildFilterPredicates(root, search, searchProperties);
        if (buildFilterPredicates.length > 0) {
            query.where(builder.or(buildFilterPredicates));
        }
    }

    if (orders != null) {
        query.orderBy(OrderToOrder.toJpa(builder, root, orders));
    }

    TypedQuery<ENTITY> q = entityManager.createQuery(query);
    if (start != null) {
        q.setFirstResult(start);
    }
    if (length != null) {
        q.setMaxResults(length);
    }
    return findList(q);
}

From source file:edu.umm.radonc.ca_dash.model.TxInstanceFacade.java

public List<TxInstance> itemsDateRange(Date startDate, Date endDate, int[] range) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(TxInstance.class);
    Root<TxInstance> rt = cq.from(TxInstance.class);
    cq.where(cb.and(rt.get(TxInstance_.completed).isNotNull(),
            cb.between(rt.get(TxInstance_.completed), startDate, endDate)));
    cq.orderBy(cb.asc(rt.get(TxInstance_.completed)));
    Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);//from   w  ww. ja  v  a 2  s .c  om
    return q.getResultList();
}

From source file:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * Count the measurement results belonging to a specific adapter
 * @param adapter/* w  ww. j  a  v  a 2s  .  c  om*/
 * @return
 */
public int countAllByAdapter(AdapterSettings adapter) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.adapter), adapter);
    query.where(condition);
    return em.createQuery(query).getResultList().size();
}

From source file:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * /*from   w w w . j a v a  2 s. c  om*/
 * @param adapter
 * @return
 */
public List<CubesMetricMeasurement> getAllByAdapter(AdapterSettings adapter) {
    logger.info("Get measurements for adapter: " + adapter);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.adapter), adapter);
    query.where(condition);
    return em.createQuery(query).getResultList();
}

From source file:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * @param project/*from w  w w .j a  va2 s.  com*/
 * @return List with all the measurements of a project
 */
public List<CubesMetricMeasurement> getMeasurementByProject(Project project) {
    logger.info("Obtaining measurements for the project: " + project);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.project), project);
    query.where(condition);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();

}

From source file:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * //from  www  .  ja va  2 s .  co  m
 * @param metric
 * @return
 * @throws uQasarException
 */
public List<CubesMetricMeasurement> getMeasurementsForMetric(String metric) throws uQasarException {
    logger.info("Get measurements for metric: " + metric);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.cubesMetric), metric);
    query.where(condition);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();
}