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:com.order.erp.web.controller.SystemController.java

@RequestMapping(path = "staff", method = RequestMethod.GET)
public String staffIndex(HttpServletRequest request,
        @RequestParam(name = "keyword", required = false) String keyword,
        @PageableDefault(page = 0, size = 10, sort = "id", direction = Direction.ASC) Pageable pageable,
        ModelMap model) {// w w w .  ja va  2s .co m
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    User me = staffService.findByAccountUsername(((UserDetails) principal).getUsername());
    Company company = me.getCompany();
    //List<User> userList = userService.findUsersByAmoebaId(company.getId());

    final User meStaff = me;
    final String finalKeyword = keyword;
    final Company finalCompany = company;
    Page<User> page = staffService.findUsers(new Specification<User>() {
        public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            query.distinct(true);
            Predicate p1 = cb.equal(root.get("company").as(Company.class), finalCompany);
            Predicate p2 = cb.conjunction();
            if (StringUtils.isNotBlank(finalKeyword)) {
                Predicate p1_like = cb.like(root.get("account").get("username").as(String.class),
                        "%" + finalKeyword + "%");
                Predicate p2_like = cb.like(root.get("realname").as(String.class), "%" + finalKeyword + "%");
                p2 = cb.or(p1_like, p2_like);
            }
            Predicate p3 = cb.notEqual(root.as(User.class), meStaff);
            Predicate p_result = cb.and(p1, p2, p3);
            return p_result;
        }
    }, pageable);

    model.put("user", me);
    model.put("company", company);
    model.put("page", page);
    return "/system/staff_list";
}

From source file:eu.uqasar.service.ProcessService.java

/**
 * /*w w  w.j av a  2s.c  o  m*/
 * @param processId
 * @return
 */
public boolean processExists(Long processId) {
    logger.info(String.format("checking if process with ID %d exists ...", processId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Process> from = criteria.from(Process.class);
    criteria.where(cb.equal(from.get(Process_.id), processId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult() == 1);
}

From source file:net.przemkovv.sphinx.dao.impl.DefaultPermissionDAO.java

@Override
public List<Permission> getPermissionsForUser(User user) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Permission> cq = cb.createQuery(Permission.class);
    Root<Permission> permissions = cq.from(Permission.class);
    Join<Permission, Role> roles = permissions.join(Permission_.roles);
    Join<Role, User> users = roles.join(Role_.users);
    cq.where(cb.equal(users.get(User_.email), user.getEmail()));
    return em.createQuery(cq).getResultList();

}

From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java

/**
 * {@inheritDoc}/*w w  w .  j  a  v  a 2  s .com*/
 */
public List<User> getCustomers(int start, int limit, String sortField, String sortOrder,
        Map<String, Object> filters) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Predicate isCustomerRole = builder.equal(root.get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER);
    Predicate hasCreditCard = builder.equal(root.get(FIELD_CONSTANT_HAS_CREDIT_CARD), TRUE);
    query.where(builder.and(isCustomerRole, hasCreditCard));

    query.select(root);

    addSorting(sortField, sortOrder, query, builder, root);
    addFilterCriteria(filters, builder, root, query);
    return entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit).getResultList();
}

From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoElementtiDAOImpl.java

@Override
public List<YhteystietoElementti> findAllKaytossa() {
    //        Query query = getEntityManager().createQuery("SELECT x FROM YhteystietoElementti x where x.kaytossa = true");
    //        return query.getResultList();

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<YhteystietoElementti> query = cb.createQuery(YhteystietoElementti.class);

    Root<YhteystietoElementti> root = query.from(YhteystietoElementti.class);
    query.select(root);/*from  ww w .  j  a  v  a  2 s  .c  o m*/

    Predicate whereClause = cb.equal(root.get("kaytossa"), true);
    query.where(whereClause);

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

From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoArvoDAOImpl.java

/**
 * Returns yhteystietoarvos for a given yhteystietojen tyyppi
 * @param yhteystietojenTyyppi the yhteystietojen tyyppi given
 * @return the yhteystietoarvo objects matching the given yhteystietojen tyyppi
 *///  ww w.  ja  v a 2 s . c  om
@Override
public List<YhteystietoArvo> findByYhteystietojenTyyppi(YhteystietojenTyyppi yhteystietojenTyyppi) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<YhteystietoArvo> query = cb.createQuery(YhteystietoArvo.class);

    Root<YhteystietoArvo> root = query.from(YhteystietoArvo.class);
    query.select(root);

    Predicate yhteystietojenTyyppiEquals = cb.equal(root.get("kentta").get("yhteystietojenTyyppi").get("oid"),
            yhteystietojenTyyppi.getOid());
    query.where(yhteystietojenTyyppiEquals);

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

From source file:com.samples.platform.core.SystemUserInitDao.java

/**
 * Get the {@link AuthenticationType}s out of the database.
 *
 * @param enabled//from w ww  . j a  v a  2 s.  co  m
 *            if not <code>null</code> and <code>true</code> only the
 *            enabled {@link AuthenticationType}s are replied.
 * @return the list of {@link AuthenticationType}s.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void enterSystemUser(final String contextName, final String userName, final String password,
        final String... roleNames) {
    AuthenticationType ac = this.of.createAuthenticationType();
    ac.setContext(contextName);
    ac.setEnabled(true);
    GrantedAuthorityType r;
    for (String roleName : roleNames) {
        r = this.of.createGrantedAuthorityType();
        r.setRoleName(roleName);
        ac.getGrantedAuthority().add(r);
    }
    ac.setPassword(password);
    ac.setUserName(userName);
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<AuthenticationType> q = cb.createQuery(AuthenticationType.class);
    Root<AuthenticationType> c = q.from(AuthenticationType.class);
    Predicate ands = cb.conjunction();
    ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.context), contextName));
    ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.userName), userName));
    q.where(ands);
    q.orderBy(cb.asc(c.<String>get(AuthenticationType_.userName)));
    TypedQuery<AuthenticationType> typedQuery = this.em.createQuery(q);
    try {
        AuthenticationType stored = typedQuery.getSingleResult();
        if (stored != null) {
            this.em.persist(ac);
        }
    } catch (NoResultException e) {
        this.em.persist(ac);
    }
}

From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoArvoDAOImpl.java

@Override
public YhteystietoArvo findByOrganisaatioAndNimi(String organisaatioOid, String nimi) {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<YhteystietoArvo> query = cb.createQuery(YhteystietoArvo.class);

    Root<YhteystietoArvo> root = query.from(YhteystietoArvo.class);
    query.select(root);//  w  w w  . j a v a2 s  .  c o  m

    Predicate organisaatioEquals = cb.equal(root.get("organisaatio").get("oid"), organisaatioOid);
    Predicate nameEquals = cb.equal(root.get("kentta").get("nimi"), nimi);

    Predicate whereClause = cb.and(organisaatioEquals, nameEquals);
    query.where(whereClause);

    return getEntityManager().createQuery(query).getSingleResult();

    //        Organisaatio org = organisaatioDAO.findBy("oid", organisaatioOid).get(0);
    //        Query query = getEntityManager().createQuery("SELECT x FROM YhteystietoArvo x " +
    //                "WHERE x.kentta.nimi = :nimi AND x.organisaatio.id = :organisaatioId");
    //        query.setParameter("nimi", nimi);
    //        query.setParameter("organisaatioId", org.getId());
    //        return (YhteystietoArvo) query.getSingleResult();
}

From source file:com.wms.studio.service.WallpaperServiveImpl.java

@Override
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public PageDto<WallpaperDto> findBy(final WallpaperEnum wallpaperType, final Date startDate, final Date endDate,
        PageSize pageSize) {//  w  ww. j  a v a2  s. co m

    if (pageSize == null) {
        pageSize = new PageSize();
    }

    Page<Wallpaper> pageWallpaper = this.wallpaperRepository.findAll(new Specification<Wallpaper>() {

        @Override
        public Predicate toPredicate(Root<Wallpaper> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

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

            if (wallpaperType != null) {
                pres.add(cb.equal(root.get("wallpaperType").as(WallpaperEnum.class), wallpaperType));
            }

            if (startDate != null) {
                pres.add(cb.greaterThanOrEqualTo(root.get("addDate").as(Date.class), startDate));
            }

            if (endDate != null) {
                pres.add(cb.lessThanOrEqualTo(root.get("addDate").as(Date.class), endDate));
            }

            Predicate[] p = new Predicate[pres.size()];
            return cb.and(pres.toArray(p));
        }
    }, new PageRequest(pageSize.getPage() - 1, pageSize.getLimit()));

    return this.wallpaperCovert.covertToDto(pageWallpaper);
}

From source file:com.aimdek.ccm.dao.impl.test.BasicAbstractGenericDaoImplTest.java

/**
 * Find by id./*  w  w w  .  ja  v  a  2  s . co m*/
 *
 * @param id
 *            the id
 * @param entityClass
 *            the entity class
 * @return the entity type
 */
public EntityType findById(IDType id, Class<EntityType> entityClass) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get("id"), id));

    return entityManager.createQuery(query).getSingleResult();
}