List of usage examples for javax.persistence.criteria CriteriaBuilder and
Predicate and(Expression<Boolean> x, Expression<Boolean> y);
From source file:org.broadleafcommerce.admin.server.service.handler.SkuRestrictionFactoryImpl.java
protected Predicate buildCompositePredicate(CriteriaBuilder builder, Path targetPropertyPath, Path productPath, Predicate propertyExpression, Predicate defaultSkuExpression) { return builder.or( builder.or(builder.and(builder.isNotNull(targetPropertyPath), propertyExpression), builder.and(builder.and(builder.isNull(targetPropertyPath), builder.isNotNull(productPath)), defaultSkuExpression)), builder.and(builder.isNull(productPath), propertyExpression)); }
From source file:org.broadleafcommerce.cms.admin.server.handler.PageItemCriteriaCustomPersistenceHandler.java
@Override public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException { Entity entity = persistencePackage.getEntity(); removeHtmlEncoding(entity);// ww w . j a v a 2 s. com try { PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective(); Map<String, FieldMetadata> adminProperties = helper .getSimpleMergedProperties(PageItemCriteria.class.getName(), persistencePerspective); Object primaryKey = helper.getPrimaryKey(entity, adminProperties); PageItemCriteria adminInstance = (PageItemCriteria) dynamicEntityDao .retrieve(Class.forName(entity.getType()[0]), primaryKey); if (adminInstance.getPage().getLockedFlag()) { /* This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance */ CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder(); CriteriaQuery<Page> query = criteriaBuilder.createQuery(Page.class); Root<PageImpl> root = query.from(PageImpl.class); query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE), criteriaBuilder.equal(root.get("originalPageId"), adminInstance.getPage().getId()))); query.select(root); TypedQuery<Page> scQuery = dynamicEntityDao.getStandardEntityManager().createQuery(query); try { checkCriteria: { Page myContent = scQuery.getSingleResult(); for (PageItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) { if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule()) && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) { //manually set the values - otherwise unwanted properties will be set itemCriteria.setOrderItemMatchRule( entity.findProperty("orderItemMatchRule").getValue()); itemCriteria .setQuantity(Integer.parseInt(entity.findProperty("quantity").getValue())); adminInstance = itemCriteria; break checkCriteria; } } throw new RuntimeException("Unable to find an item criteria to update"); } } catch (Exception e) { throw new IllegalArgumentException("Unable to update a locked record"); } } else { adminInstance = (PageItemCriteria) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false); } adminInstance = (PageItemCriteria) dynamicEntityDao.merge(adminInstance); Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null); return adminEntity; } catch (Exception e) { LOG.error("Unable to execute persistence activity", e); throw new ServiceException("Unable to update entity for " + entity.getType()[0], e); } }
From source file:org.broadleafcommerce.cms.admin.server.handler.PageItemCriteriaCustomPersistenceHandler.java
@Override public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException { Entity entity = persistencePackage.getEntity(); try {/* w w w .j a v a 2 s . c o m*/ PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective(); Map<String, FieldMetadata> adminProperties = helper .getSimpleMergedProperties(PageItemCriteria.class.getName(), persistencePerspective); Object primaryKey = helper.getPrimaryKey(entity, adminProperties); PageItemCriteria adminInstance = (PageItemCriteria) dynamicEntityDao .retrieve(Class.forName(entity.getType()[0]), primaryKey); if (adminInstance.getPage().getLockedFlag()) { /* This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance */ CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder(); CriteriaQuery<Page> query = criteriaBuilder.createQuery(Page.class); Root<PageImpl> root = query.from(PageImpl.class); query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE), criteriaBuilder.equal(root.get("originalPageId"), adminInstance.getPage().getId()))); query.select(root); TypedQuery<Page> scQuery = dynamicEntityDao.getStandardEntityManager().createQuery(query); try { Page myContent = scQuery.getSingleResult(); for (PageItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) { if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule()) && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) { myContent.getQualifyingItemCriteria().remove(itemCriteria); return; } } throw new RuntimeException("Unable to find an item criteria to delete"); } catch (Exception e) { throw new IllegalArgumentException("Unable to update a locked record"); } } dynamicEntityDao.remove(adminInstance); } catch (Exception e) { LOG.error("Unable to execute persistence activity", e); throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e); } }
From source file:org.broadleafcommerce.cms.admin.server.handler.StructuredContentItemCriteriaCustomPersistenceHandler.java
@Override public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException { Entity entity = persistencePackage.getEntity(); removeHtmlEncoding(entity);//w ww . j a v a 2 s. c om try { PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective(); Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties( StructuredContentItemCriteria.class.getName(), persistencePerspective); Object primaryKey = helper.getPrimaryKey(entity, adminProperties); StructuredContentItemCriteria adminInstance = (StructuredContentItemCriteria) dynamicEntityDao .retrieve(Class.forName(entity.getType()[0]), primaryKey); if (adminInstance.getStructuredContent().getLockedFlag()) { /* This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance */ CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder(); CriteriaQuery<StructuredContent> query = criteriaBuilder.createQuery(StructuredContent.class); Root<StructuredContentImpl> root = query.from(StructuredContentImpl.class); query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE), criteriaBuilder.equal(root.get("originalItemId"), adminInstance.getStructuredContent().getId()))); query.select(root); TypedQuery<StructuredContent> scQuery = dynamicEntityDao.getStandardEntityManager() .createQuery(query); try { checkCriteria: { StructuredContent myContent = scQuery.getSingleResult(); for (StructuredContentItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) { if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule()) && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) { //manually set the values - otherwise unwanted properties will be set itemCriteria.setOrderItemMatchRule( entity.findProperty("orderItemMatchRule").getValue()); itemCriteria .setQuantity(Integer.parseInt(entity.findProperty("quantity").getValue())); adminInstance = itemCriteria; break checkCriteria; } } throw new RuntimeException("Unable to find an item criteria to update"); } } catch (Exception e) { throw new IllegalArgumentException("Unable to update a locked record"); } } else { adminInstance = (StructuredContentItemCriteria) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false); } adminInstance = (StructuredContentItemCriteria) dynamicEntityDao.merge(adminInstance); Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null); return adminEntity; } catch (Exception e) { LOG.error("Unable to execute persistence activity", e); throw new ServiceException("Unable to update entity for " + entity.getType()[0], e); } }
From source file:org.broadleafcommerce.cms.admin.server.handler.StructuredContentItemCriteriaCustomPersistenceHandler.java
@Override public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException { Entity entity = persistencePackage.getEntity(); try {/*from w w w .j ava2 s. c o m*/ PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective(); Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties( StructuredContentItemCriteria.class.getName(), persistencePerspective); Object primaryKey = helper.getPrimaryKey(entity, adminProperties); StructuredContentItemCriteria adminInstance = (StructuredContentItemCriteria) dynamicEntityDao .retrieve(Class.forName(entity.getType()[0]), primaryKey); if (adminInstance.getStructuredContent().getLockedFlag()) { /* This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance */ CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder(); CriteriaQuery<StructuredContent> query = criteriaBuilder.createQuery(StructuredContent.class); Root<StructuredContentImpl> root = query.from(StructuredContentImpl.class); query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE), criteriaBuilder.equal(root.get("originalItemId"), adminInstance.getStructuredContent().getId()))); query.select(root); TypedQuery<StructuredContent> scQuery = dynamicEntityDao.getStandardEntityManager() .createQuery(query); try { StructuredContent myContent = scQuery.getSingleResult(); for (StructuredContentItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) { if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule()) && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) { myContent.getQualifyingItemCriteria().remove(itemCriteria); return; } } throw new RuntimeException("Unable to find an item criteria to delete"); } catch (Exception e) { throw new IllegalArgumentException("Unable to update a locked record"); } } dynamicEntityDao.remove(adminInstance); } catch (Exception e) { LOG.error("Unable to execute persistence activity", e); throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e); } }
From source file:org.broadleafcommerce.core.catalog.dao.ProductDaoImpl.java
protected void attachSearchCriteria(SearchCriteria searchCriteria, From<?, ? extends Product> product, From<?, ? extends Sku> sku, List<Predicate> restrictions) { CriteriaBuilder builder = em.getCriteriaBuilder(); // Build out the filter criteria from the users request for (Entry<String, String[]> entry : searchCriteria.getFilterCriteria().entrySet()) { String key = entry.getKey(); List<String> eqValues = new ArrayList<String>(); List<String[]> rangeValues = new ArrayList<String[]>(); // Determine which path is the appropriate one to use Path<?> pathToUse;// w ww .j a v a 2 s . c om if (key.contains("defaultSku.")) { pathToUse = sku; key = key.substring("defaultSku.".length()); } else if (key.contains("productAttributes.")) { pathToUse = product.join("productAttributes"); key = key.substring("productAttributes.".length()); restrictions.add(builder.equal(pathToUse.get("name").as(String.class), key)); key = "value"; } else if (key.contains("product.")) { pathToUse = product; key = key.substring("product.".length()); } else { // We don't know which path this facet is built on - resolves previous bug that attempted // to attach search facet to any query parameter continue; } // Values can be equality checks (ie manufacturer=Dave's) or range checks, which take the form // key=range[minRange:maxRange]. Figure out what type of check this is for (String value : entry.getValue()) { if (value.contains("range[")) { String[] rangeValue = new String[] { value.substring(value.indexOf("[") + 1, value.indexOf(":")), value.substring(value.indexOf(":") + 1, value.indexOf("]")) }; rangeValues.add(rangeValue); } else { eqValues.add(value); } } // Add the equality range restriction with the "in" builder. That means that the query string // ?manufacturer=Dave&manufacturer=Bob would match either Dave or Bob if (eqValues.size() > 0) { restrictions.add(pathToUse.get(key).in(eqValues)); } // If we have any range restrictions, we need to build those too. Ranges are also "or"ed together, // such that specifying range[0:5] and range[10:null] for the same field would match items // that were valued between 0 and 5 OR over 10 for that field List<Predicate> rangeRestrictions = new ArrayList<Predicate>(); for (String[] range : rangeValues) { BigDecimal min = new BigDecimal(range[0]); BigDecimal max = null; if (range[1] != null && !range[1].equals("null")) { max = new BigDecimal(range[1]); } Predicate minRange = builder.greaterThan(pathToUse.get(key).as(BigDecimal.class), min); Predicate maxRange = null; if (max != null) { maxRange = builder.lessThan(pathToUse.get(key).as(BigDecimal.class), max); rangeRestrictions.add(builder.and(minRange, maxRange)); } else { rangeRestrictions.add(minRange); } } if (rangeRestrictions.size() > 0) { restrictions.add(builder.or(rangeRestrictions.toArray(new Predicate[rangeRestrictions.size()]))); } } }
From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java
/** * Builds the Typed Query/*from w w w .jav a 2 s . com*/ * @return */ public TypedQuery<Connection> buildSingleRelationship() { EntityManager em = EntityManagerHolder.get(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class); Root<Connection> connection = criteria.from(Connection.class); Predicate predicate = null; if (this.sender != null && this.receiver != null) { predicate = cb.equal(connection.get(Connection_.senderId), sender.getId()); predicate = cb.and(predicate, cb.equal(connection.get(Connection_.receiverId), receiver.getId())); } CriteriaQuery<Connection> select = criteria.select(connection).distinct(true); select.where(predicate); TypedQuery<Connection> typedQuery = em.createQuery(select); return typedQuery; }
From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java
/** * Builds the Typed Query// w w w. j a v a 2 s .c o m * @return */ public TypedQuery<Connection> build() { EntityManager em = EntityManagerHolder.get(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class); Root<Connection> connection = criteria.from(Connection.class); Predicate predicate = null; //owner if (this.owner != null) { predicate = cb.equal(connection.get(Connection_.senderId), owner.getId()); } //status if (this.status != null) { if (Relationship.Type.PENDING.equals(this.status)) { predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types)); } else { predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status)); } } CriteriaQuery<Connection> select = criteria.select(connection).distinct(true); select.where(predicate); TypedQuery<Connection> typedQuery = em.createQuery(select); if (this.limit > 0) { typedQuery.setFirstResult((int) offset); typedQuery.setMaxResults((int) limit); } return typedQuery; }
From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java
/** * Builds the Typed Query/*from w w w .ja v a 2s .co m*/ * @return */ public TypedQuery<Long> buildCount() { EntityManager em = EntityManagerHolder.get(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<Connection> connection = criteria.from(Connection.class); Predicate predicate = null; //owner if (this.owner != null) { predicate = cb.equal(connection.get(Connection_.senderId), owner.getId()); } //status if (this.status != null) { if (Relationship.Type.PENDING.equals(this.status)) { predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types)); } else { predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status)); } } CriteriaQuery<Long> select = criteria.select(cb.countDistinct(connection)); select.where(predicate); return em.createQuery(select); }
From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java
public TypedQuery<Connection> buildLastConnections() { EntityManager em = EntityManagerHolder.get(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class); Root<Connection> connection = criteria.from(Connection.class); Predicate predicate = null;/* w ww . ja v a2s . c o m*/ //owner if (this.owner != null) { predicate = cb.equal(connection.get(Connection_.senderId), owner.getId()); } //status if (this.status != null) { predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status)); } CriteriaQuery<Connection> select = criteria.select(connection).distinct(true); select.where(predicate); select.orderBy(cb.desc(connection.<Long>get(Connection_.id))); TypedQuery<Connection> typedQuery = em.createQuery(select); if (this.limit > 0) { typedQuery.setFirstResult((int) offset); typedQuery.setMaxResults((int) limit); } return typedQuery; }