Example usage for javax.persistence.criteria CriteriaBuilder equal

List of usage examples for javax.persistence.criteria CriteriaBuilder equal

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder equal.

Prototype

Predicate equal(Expression<?> x, Object y);

Source Link

Document

Create a predicate for testing the arguments for equality.

Usage

From source file:me.ineson.demo.service.utils.RestUtilsTest.java

@SuppressWarnings("unchecked")
@Test//  w  w w. j  a v a 2 s  .co  m
public void testParseWhereClause() {
    CriteriaBuilder criteriaBuilderMock = mock(CriteriaBuilder.class);
    CriteriaQuery<SolarBody> criteriaQueryMock = mock(CriteriaQuery.class);
    Root<SolarBody> rootMock = mock(Root.class);
    log.debug("mock root {}, builder {}", rootMock, criteriaBuilderMock);

    Join<Object, Object> idFieldPath = mock(Join.class);
    when(rootMock.get("id")).thenReturn(idFieldPath);

    Predicate firstPredicate = mock(Predicate.class);
    when(criteriaBuilderMock.equal(idFieldPath, "22")).thenReturn(firstPredicate);

    Predicate predicate = RestUtils.parseWhereClause("id=22", rootMock, criteriaQueryMock, criteriaBuilderMock,
            null);

    verify(rootMock, times(1)).get("id");
    verifyNoMoreInteractions(rootMock);

    verify(criteriaBuilderMock, times(1)).equal(idFieldPath, "22");
    verifyNoMoreInteractions(criteriaBuilderMock);

    Assert.assertEquals(firstPredicate, predicate);
}

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

/**
 * /* w  w w  . j ava 2  s .  c o m*/
 * @param metric
 * @param date
 * @return
 * @throws uQasarException
 */
public int countMeasurementsByMetricAndDate(String metric, Date date) throws uQasarException {
    logger.info("Count measurements for metric: " + metric + "and date: " + date);

    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);
    Predicate condition2 = cb.equal(root.get(CubesMetricMeasurement_.timeStamp), date);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList().size();
}

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

/**
 * update the session every time a user makes a request
 * /*from  w w w. ja  v a 2 s .  c om*/
 * @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:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * /*from  w ww .ja va2s  .  c o m*/
 * @param metric
 * @return
 * @throws uQasarException
 */
private List<CubesMetricMeasurement> getMeasurementsByMetricAndDate(String metric, Date date)
        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);
    Predicate condition2 = cb.equal(root.get(CubesMetricMeasurement_.timeStamp), date);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();
}

From source file:se.inera.intyg.intygstjanst.persistence.model.dao.impl.CertificateDaoImpl.java

@Override
public List<Certificate> findCertificate(Personnummer civicRegistrationNumber, List<String> types,
        LocalDate fromDate, LocalDate toDate, List<String> careUnits) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Certificate> query = criteriaBuilder.createQuery(Certificate.class);
    Root<Certificate> root = query.from(Certificate.class);

    root.fetch("states", JoinType.LEFT);

    if (civicRegistrationNumber == null) {
        return Collections.emptyList();
    }/*from  w  w w .j  a v  a 2s .  c  o  m*/

    List<Predicate> predicates = new ArrayList<>();

    // meta data has to match civic registration number
    predicates.add(criteriaBuilder.equal(root.get("civicRegistrationNumber"),
            DaoUtil.formatPnrForPersistence(civicRegistrationNumber)));

    // filter by certificate types
    if (types != null && !types.isEmpty()) {
        predicates.add(criteriaBuilder.lower(root.<String>get("type")).in(toLowerCase(types)));
    }

    // filter by care unit
    if (careUnits != null && !careUnits.isEmpty()) {
        predicates.add(root.<String>get("careUnitId").in(careUnits));
    }

    query.where(predicates.toArray(new Predicate[predicates.size()]));

    // order by signed date
    query.orderBy(criteriaBuilder.asc(root.get("signedDate")));

    List<Certificate> tmpResult = entityManager.createQuery(query).getResultList();
    List<Certificate> result = filterDuplicates(tmpResult);

    // expect a small number, so lets filter in memory
    return new DateFilter(result).filter(fromDate, toDate);
}

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

/**
 * @param project//from   w w  w  .  j av a 2 s .c  om
 * @param metric
 * @return
 */
public CubesMetricMeasurement getLatestMeasurementByProjectAndMetric(String project, String metric) {
    logger.info("Obtaining measurements for the project: " + project + " and 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_.cubeName), project);
    Predicate condition2 = cb.equal(root.get(CubesMetricMeasurement_.cubesMetric), metric);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    List<CubesMetricMeasurement> measurements = em.createQuery(query).getResultList();
    CubesMetricMeasurement measurement = null;
    if (measurements != null && measurements.size() > 0 && measurements.get(0) != null) {
        measurement = measurements.get(0); // Get the most "fresh" result 
    }
    return measurement;
}

From source file:ch.sdi.plugins.oxwall.job.OxSqlJob.java

/**
 * @see ch.sdi.core.intf.SqlJob#isAlreadyPresent(ch.sdi.core.impl.data.Person)
 *//*ww  w  .j  av  a2 s . c  o m*/
@Override
public boolean isAlreadyPresent(Person<?> aPerson) throws SdiException {
    if (myDryRun && !myCheckDuplicateOnDryRun) {
        myLog.debug("DryRun is active. Not checking for duplicate person");
        return false;
    } // if myDryRun

    CriteriaBuilder cb = myEntityManager.getCriteriaBuilder();

    CriteriaQuery<OxUser> criteria = cb.createQuery(OxUser.class);
    Root<OxUser> root = criteria.from(OxUser.class);
    ParameterExpression<String> mailParam = cb.parameter(String.class);
    criteria.select(root).where(cb.equal(root.get("email"), mailParam));

    TypedQuery<OxUser> queryEMail = myEntityManager.createQuery(criteria);

    queryEMail.setParameter(mailParam, aPerson.getEMail());
    List<OxUser> results = queryEMail.getResultList();

    if (results.size() > 0) {
        myLog.debug("given Person is already present: " + results.get(0));
        return true;
    } // if results.size() > 0

    return false;
}

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

public Boolean updateUserRemoteSession(String SID, String sessionXml) {
    try {// w  ww.  ja  v a 2s .  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:org.jasig.portlet.blackboardvcportlet.dao.impl.ConferenceUserDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.emailParameter = this.createParameterExpression(String.class, "email");

    this.getUsersByPrimaryEmailQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<ConferenceUserImpl>>() {
                @Override/*from   w w w  . j a  va  2 s. c o m*/
                public CriteriaQuery<ConferenceUserImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<ConferenceUserImpl> criteriaQuery = cb
                            .createQuery(ConferenceUserImpl.class);
                    final Root<ConferenceUserImpl> definitionRoot = criteriaQuery
                            .from(ConferenceUserImpl.class);
                    criteriaQuery.select(definitionRoot);
                    criteriaQuery
                            .where(cb.equal(definitionRoot.get(ConferenceUserImpl_.email), emailParameter));

                    return criteriaQuery;
                }
            });

    this.getUsersByAnyEmailQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<ConferenceUserImpl>>() {
                @Override
                public CriteriaQuery<ConferenceUserImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<ConferenceUserImpl> criteriaQuery = cb
                            .createQuery(ConferenceUserImpl.class);
                    final Root<ConferenceUserImpl> definitionRoot = criteriaQuery
                            .from(ConferenceUserImpl.class);
                    criteriaQuery.select(definitionRoot);
                    criteriaQuery.where(
                            cb.or(cb.equal(definitionRoot.get(ConferenceUserImpl_.email), emailParameter),
                                    cb.isMember(emailParameter,
                                            definitionRoot.get(ConferenceUserImpl_.additionalEmails))));

                    return criteriaQuery;
                }
            });
}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * find tests associated with this artifact
 *///from   w  w  w.jav  a2  s.  c o  m
private List<SpecificationTest> findSpecificationTests(Specification specification) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SpecificationTest> c = cb.createQuery(SpecificationTest.class);
    Root<SpecificationTest> obj = c.from(SpecificationTest.class);
    c.select(obj).where(cb.equal(obj.get("specification"), specification));
    return em.createQuery(c).getResultList();
}