Java Code Examples for javax.persistence.criteria.CriteriaQuery#orderBy()

The following examples show how to use javax.persistence.criteria.CriteriaQuery#orderBy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MindVersionInfoFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public MindVersionInfo getLatestVersionWithMind(String mindId) throws Exception {
	if( StringUtils.isEmpty( mindId ) ){
		new Exception("脑图ID为空,无法查询版本信息!");
	}
	EntityManager em = this.entityManagerContainer().get(MindVersionInfo.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<MindVersionInfo> cq = cb.createQuery(MindVersionInfo.class);
	Root<MindVersionInfo> root = cq.from(MindVersionInfo.class);
	Predicate p = cb.equal( root.get(MindVersionInfo_.mindId) , mindId);
	cq.orderBy( cb.desc( root.get( MindVersionInfo_.updateTime ) ) );
	List<MindVersionInfo> versions = em.createQuery(cq.where(p)).setMaxResults(1).getResultList();
	if(ListTools.isNotEmpty( versions )) {
		 return versions.get(0);
	 }
	return null;
}
 
Example 2
Source File: BBSReplyInfoFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 *根据主题ID获取该主题所有的回复信息对象列表
 * @param subjectId
 * @param showSubReply 是否平级显示所有的的回复, 如果为false则只显示第一层
 * @param maxCount
 * @return
 * @throws Exception
 */
public List<BBSReplyInfo> listWithSubjectForPage(String subjectId, Boolean showSubReply, Integer maxCount, String orderType ) throws Exception {
	if( subjectId == null ){
		throw new Exception( "subjectId can not null." );
	}
	EntityManager em = this.entityManagerContainer().get( BBSReplyInfo.class );
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<BBSReplyInfo> cq = cb.createQuery( BBSReplyInfo.class );
	Root<BBSReplyInfo> root = cq.from( BBSReplyInfo.class );
	Predicate p = cb.equal( root.get( BBSReplyInfo_.subjectId ), subjectId );
	if( !showSubReply ){
		Predicate p_showSubReply = cb.isNull( root.get( BBSReplyInfo_.parentId ));
		p_showSubReply = cb.or( p_showSubReply, cb.equal( root.get( BBSReplyInfo_.parentId), ""));
		p = cb.and( p, p_showSubReply);
	}
	if( StringUtils.equalsIgnoreCase(orderType, "DESC")){
		cq.orderBy( cb.desc( root.get( BBSReplyInfo_.createTime ) ) );
	}else{
		cq.orderBy( cb.asc( root.get( BBSReplyInfo_.createTime ) ) );
	}
	if( maxCount == null ){
		return em.createQuery(cq.where(p)).setMaxResults( 2000 ).getResultList();
	}else{
		return em.createQuery(cq.where(p)).setMaxResults( maxCount ).getResultList();
	}
}
 
Example 3
Source File: MindVersionInfoFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public MindVersionInfo getLatestVersionWithMind(String mindId) throws Exception {
	if( StringUtils.isEmpty( mindId ) ){
		new Exception("脑图ID为空,无法查询版本信息!");
	}
	EntityManager em = this.entityManagerContainer().get(MindVersionInfo.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<MindVersionInfo> cq = cb.createQuery(MindVersionInfo.class);
	Root<MindVersionInfo> root = cq.from(MindVersionInfo.class);
	Predicate p = cb.equal( root.get(MindVersionInfo_.mindId) , mindId);
	cq.orderBy( cb.desc( root.get( MindVersionInfo_.updateTime ) ) );
	List<MindVersionInfo> versions = em.createQuery(cq.where(p)).setMaxResults(1).getResultList();
	if(ListTools.isNotEmpty( versions )) {
		 return versions.get(0);
	 }
	return null;
}
 
Example 4
Source File: CmsPermissionService.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 根据条件获取用户有权限访问的所有文档ID列表
 * @param emc
 * @param queryFilter
 * @param maxResultCount
 * @return
 * @throws Exception
 */
public List<String> lisViewableDocIdsWithFilter(EntityManagerContainer emc, QueryFilter queryFilter,
		Integer maxResultCount) throws Exception {
	if (maxResultCount == null || maxResultCount == 0) {
		maxResultCount = 500;
	}
	List<String> ids = new ArrayList<>();
	List<Review> reviews = null;
	EntityManager em = emc.get(Review.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Review> cq = cb.createQuery(Review.class);
	Root<Review> root = cq.from(Review.class);
	// Predicate p=null;
	Predicate p = CriteriaBuilderTools.composePredicateWithQueryFilter(Review_.class, cb, null, root, queryFilter);
	cq.orderBy(cb.desc(root.get(Review.publishTime_FIELDNAME)));
	reviews = em.createQuery(cq.where(p)).setMaxResults(maxResultCount).getResultList();
	if ( ListTools.isNotEmpty( reviews )) {
		for (Review review : reviews) {
			if (!ids.contains(review.getDocId())) {
				ids.add(review.getDocId());
			}
		}
	}
	return ids;
}
 
Example 5
Source File: HibernateTrackedEntityDataValueAuditStore.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<TrackedEntityDataValueAudit> getTrackedEntityDataValueAudits( List<DataElement> dataElements,
    List<ProgramStageInstance> programStageInstances, AuditType auditType, int first, int max )
{
    CriteriaBuilder builder = sessionFactory.getCurrentSession().getCriteriaBuilder();
    CriteriaQuery<TrackedEntityDataValueAudit> query = builder.createQuery( TrackedEntityDataValueAudit.class );
    Root<TrackedEntityDataValueAudit> root = query.from( TrackedEntityDataValueAudit.class );
    query.select( root );

    List<Predicate> predicates = getTrackedEntityDataValueAuditCriteria( dataElements, programStageInstances, auditType, builder, root );
    query.where( predicates.toArray( new Predicate[ predicates.size() ] ) );
    query.orderBy( builder.desc( root.get( "created" ) ) );

    return sessionFactory.getCurrentSession().createQuery( query )
            .setFirstResult( first )
            .setMaxResults( max )
            .getResultList();
}
 
Example 6
Source File: MindVersionInfoFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 根据脑图ID获取一个最早的版本信息ID
 * @param mindId
 * @return
 * @throws Exception 
 */
public MindVersionInfo getEarliestVersionInfoId(String mindId) throws Exception {
	if( StringUtils.isEmpty( mindId ) ){
		new Exception("脑图ID为空,无法查询版本信息!");
	}
	EntityManager em = this.entityManagerContainer().get(MindVersionInfo.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<MindVersionInfo> cq = cb.createQuery(MindVersionInfo.class);
	Root<MindVersionInfo> root = cq.from(MindVersionInfo.class);
	Predicate p = cb.equal( root.get(MindVersionInfo_.mindId) , mindId);
	cq.orderBy( cb.asc( root.get( MindVersionInfo_.updateTime ) ) );
	List<MindVersionInfo> versions = em.createQuery(cq.where(p)).setMaxResults(1).getResultList();
	if(ListTools.isNotEmpty( versions )) {
		 return versions.get(0);
	}
	return null;
}
 
Example 7
Source File: AbstractGenericDAOImpl.java    From cia with Apache License 2.0 6 votes vote down vote up
private List<T> getPageOrderBy(final Integer pageNr, final Integer resultPerPage,
		final SingularAttribute<T, ? extends Object> orderBy) {
	final CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(getPersistentClass());
	final Root<T> root = criteriaQuery.from(getPersistentClass());

	criteriaQuery.select(root);

	if (orderBy != null) {
		criteriaQuery.orderBy(criteriaBuilder.desc(root.get(orderBy)));
	}

	final TypedQuery<T> typedQuery = getEntityManager().createQuery(criteriaQuery);
	addCacheHints(typedQuery, "getAll");

	if (pageNr != null && resultPerPage != null) {
		typedQuery.setFirstResult((pageNr - 1) * resultPerPage);
		typedQuery.setMaxResults(resultPerPage);

	}

	return typedQuery.getResultList();

}
 
Example 8
Source File: BBSVoteRecordFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public List<BBSVoteRecord> listVoteRecordForPage(String subjectId, String voteOptionId, Integer maxRecordCount) throws Exception {
	if( maxRecordCount == null ){
		throw new Exception( "maxRecordCount is null." );
	}
	EntityManager em = this.entityManagerContainer().get(BBSVoteRecord.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<BBSVoteRecord> cq = cb.createQuery(BBSVoteRecord.class);
	Root<BBSVoteRecord> root = cq.from(BBSVoteRecord.class);
	Predicate p = cb.isNotNull( root.get( BBSVoteRecord_.id ) );
	p = cb.and( p, cb.equal( root.get( BBSVoteRecord_.optionId ), voteOptionId ));
	cq.orderBy( cb.desc( root.get( BBSVoteRecord_.createTime ) ) );
	return em.createQuery(cq.where(p)).setMaxResults( maxRecordCount ).getResultList();
}
 
Example 9
Source File: TaskFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 列示指定Id的Task实体信息列表
 * @param ids
 * @return
 * @throws Exception
 */
public List<Task> list( List<String> ids ) throws Exception {
	if( ids == null || ids.size() == 0 ){
		return new ArrayList<Task>();
	}
	EntityManager em = this.entityManagerContainer().get(Task.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Task> cq = cb.createQuery(Task.class);
	Root<Task> root = cq.from(Task.class);
	Predicate p = root.get(Task_.id).in(ids);
	cq.orderBy( cb.asc( root.get( Task_.createTime ) ) );
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 10
Source File: HibernateTrackedEntityDataValueAuditStore.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<TrackedEntityDataValueAudit> getTrackedEntityDataValueAudits( List<DataElement> dataElements,
    List<ProgramStageInstance> programStageInstances, AuditType auditType )
{
    CriteriaBuilder builder = sessionFactory.getCurrentSession().getCriteriaBuilder();
    CriteriaQuery<TrackedEntityDataValueAudit> query = builder.createQuery( TrackedEntityDataValueAudit.class );
    Root<TrackedEntityDataValueAudit> root = query.from( TrackedEntityDataValueAudit.class );
    query.select( root );

    List<Predicate> predicates = getTrackedEntityDataValueAuditCriteria( dataElements, programStageInstances, auditType, builder, root );
    query.where( predicates.toArray( new Predicate[ predicates.size() ] ) );
    query.orderBy( builder.desc( root.get( "created" ) ) );

    return sessionFactory.getCurrentSession().createQuery( query ).getResultList();
}
 
Example 11
Source File: FooServiceSortingIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
    final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
    final Root<Foo> from = criteriaQuery.from(Foo.class);
    final CriteriaQuery<Foo> select = criteriaQuery.select(from);
    criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id")));
    final TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
    final List<Foo> fooList = typedQuery.getResultList();
    for (final Foo foo : fooList) {
        System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
    }
}
 
Example 12
Source File: FooServiceSortingIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
    final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
    final Root<Foo> from = criteriaQuery.from(Foo.class);
    final CriteriaQuery<Foo> select = criteriaQuery.select(from);
    criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")));
    final TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
    final List<Foo> fooList = typedQuery.getResultList();
    for (final Foo foo : fooList) {
        System.out.println("Name:" + foo.getName() + "--------Id:" + foo.getId());
    }
}
 
Example 13
Source File: JpaQueryUtils.java    From we-cmdb with Apache License 2.0 5 votes vote down vote up
public static void applySorting(Sorting sorting, CriteriaBuilder cb, CriteriaQuery query, Map<String, Expression> selectionMap) {
    if (sorting != null && sorting.getField() != null && selectionMap.get(sorting.getField()) == null) {
        throw new InvalidArgumentException(String.format("Sorting field name [%s] is invalid.", sorting.getField()));
    }

    if (sorting != null && (!Strings.isNullOrEmpty(sorting.getField()))) {
        boolean asc = sorting.getAsc();
        String sortField = sorting.getField();
        if (asc) {
            query.orderBy(cb.asc(selectionMap.get(sortField)));
        } else {
            query.orderBy(cb.desc(selectionMap.get(sortField)));
        }
    }
}
 
Example 14
Source File: AbstractProgrammingSubmissionHibernateDao.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List<M> getByContainerJidAndUserJidAndProblemJid(String containerJid, String userJid, String problemJid) {
    CriteriaBuilder cb = currentSession().getCriteriaBuilder();
    CriteriaQuery<M> query = cb.createQuery(getEntityClass());
    Root<M> root = query.from(getEntityClass());

    query.where(cb.and(cb.equal(root.get(AbstractProgrammingSubmissionModel_.containerJid), containerJid), cb.equal(root.get(AbstractProgrammingSubmissionModel_.createdBy), userJid), cb.equal(root.get(AbstractProgrammingSubmissionModel_.problemJid), problemJid)));
    query.orderBy(cb.asc(root.get(AbstractProgrammingSubmissionModel_.createdAt)));

    return currentSession().createQuery(query).getResultList();
}
 
Example 15
Source File: RolePermissionsResource.java    From bouncr with Eclipse Public License 1.0 5 votes vote down vote up
@Decision(HANDLE_OK)
public List<Permission> list(Role role, EntityManager em) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Permission> query = cb.createQuery(Permission.class);
    Root<Permission> userRoot = query.from(Permission.class);
    Join<Role, User> rolesJoin = userRoot.join("roles");
    query.where(cb.equal(rolesJoin.get("id"), role.getId()));
    query.orderBy(cb.asc(userRoot.get("id")));
    return em.createQuery(query)
            .setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH)
            .getResultList();
}
 
Example 16
Source File: MindBaseInfoFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 列示全部的脑图实体信息列表
 * @return
 * @throws Exception
 */
public List<String> listAll() throws Exception {
	EntityManager em = this.entityManagerContainer().get(MindBaseInfo.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<MindBaseInfo> root = cq.from(MindBaseInfo.class);
	cq.orderBy( cb.desc( root.get( MindBaseInfo_.updateTime ) ) );
	cq.select( root.get(MindBaseInfo_.id ) );
	return em.createQuery( cq ).setMaxResults( 10000 ).getResultList();
}
 
Example 17
Source File: AttendanceDetailFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public String getMaxRecordDate() throws Exception {
	EntityManager em = this.entityManagerContainer().get(AttendanceDetail.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();		
	CriteriaQuery<AttendanceDetail> cq = cb.createQuery( AttendanceDetail.class );
	Root<AttendanceDetail> root = cq.from( AttendanceDetail.class);		
	cq.orderBy( cb.desc( root.get( AttendanceDetail_.recordDateString) ) );	
	List<AttendanceDetail> resultList = em.createQuery(cq).setMaxResults(1).getResultList();
	if( resultList == null || resultList.size() == 0 ){
		return null;
	}else{
		return resultList.get(0).getRecordDateString();
	}
}
 
Example 18
Source File: BusinessObjectFormatDaoImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@Override
public List<BusinessObjectFormatKey> getBusinessObjectFormatsWithFilters(BusinessObjectDefinitionKey businessObjectDefinitionKey,
    String businessObjectFormatUsage, boolean latestBusinessObjectFormatVersion)
{
    // Create the criteria builder and a tuple style criteria query.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();

    // The criteria root is the business object format.
    Root<BusinessObjectFormatEntity> businessObjectFormatEntity = criteria.from(BusinessObjectFormatEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity =
        businessObjectFormatEntity.join(BusinessObjectFormatEntity_.businessObjectDefinition);
    Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity.join(BusinessObjectFormatEntity_.fileType);
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity.join(BusinessObjectDefinitionEntity_.namespace);

    // Get the columns.
    Path<String> namespaceCodeColumn = namespaceEntity.get(NamespaceEntity_.code);
    Path<String> businessObjectDefinitionNameColumn = businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name);
    Path<String> businessObjectFormatUsageColumn = businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage);
    Path<String> fileTypeCodeColumn = fileTypeEntity.get(FileTypeEntity_.code);
    Path<Integer> businessObjectFormatVersionColumn = businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion);
    Expression<Integer> maxBusinessObjectFormatVersionExpression =
        builder.max(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion));

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction =
        builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), businessObjectDefinitionKey.getNamespace().toUpperCase());
    queryRestriction = builder.and(queryRestriction, builder.equal(builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)),
        businessObjectDefinitionKey.getBusinessObjectDefinitionName().toUpperCase()));

    // Add the business object format usage where parameter is not empty
    if (StringUtils.isNotEmpty(businessObjectFormatUsage))
    {
        queryRestriction = builder.and(queryRestriction,
            builder.equal(builder.upper(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage)), businessObjectFormatUsage.toUpperCase()));
    }

    // Add the select clause.
    criteria.multiselect(namespaceCodeColumn, businessObjectDefinitionNameColumn, businessObjectFormatUsageColumn, fileTypeCodeColumn,
        latestBusinessObjectFormatVersion ? maxBusinessObjectFormatVersionExpression : businessObjectFormatVersionColumn);

    // Add the where clause.
    criteria.where(queryRestriction);

    // If only the latest (maximum) business object format versions to be returned, create and apply the group by clause.
    if (latestBusinessObjectFormatVersion)
    {
        List<Expression<?>> grouping = new ArrayList<>();
        grouping.add(namespaceCodeColumn);
        grouping.add(businessObjectDefinitionNameColumn);
        grouping.add(businessObjectFormatUsageColumn);
        grouping.add(fileTypeCodeColumn);
        criteria.groupBy(grouping);
    }

    // Add the order by clause.
    List<Order> orderBy = new ArrayList<>();
    orderBy.add(builder.asc(businessObjectFormatUsageColumn));
    orderBy.add(builder.asc(fileTypeCodeColumn));
    if (!latestBusinessObjectFormatVersion)
    {
        orderBy.add(builder.asc(businessObjectFormatVersionColumn));
    }
    criteria.orderBy(orderBy);

    // Run the query to get a list of tuples back.
    List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();

    // Populate the "keys" objects from the returned tuples (i.e. 1 tuple for each row).
    List<BusinessObjectFormatKey> businessObjectFormatKeys = new ArrayList<>();
    for (Tuple tuple : tuples)
    {
        BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey();
        businessObjectFormatKeys.add(businessObjectFormatKey);
        businessObjectFormatKey.setNamespace(tuple.get(namespaceCodeColumn));
        businessObjectFormatKey.setBusinessObjectDefinitionName(tuple.get(businessObjectDefinitionNameColumn));
        businessObjectFormatKey.setBusinessObjectFormatUsage(tuple.get(businessObjectFormatUsageColumn));
        businessObjectFormatKey.setBusinessObjectFormatFileType(tuple.get(fileTypeCodeColumn));
        businessObjectFormatKey.setBusinessObjectFormatVersion(
            tuple.get(latestBusinessObjectFormatVersion ? maxBusinessObjectFormatVersionExpression : businessObjectFormatVersionColumn));
    }

    return businessObjectFormatKeys;
}
 
Example 19
Source File: ProjectTemplateFactory.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 根据条件查询符合条件的项目信息ID,根据上一条的sequnce查询指定数量的信息
 * @param maxCount
 * @param sequenceFieldValue
 * @param orderField
 * @param orderType
 * @param personName
 * @param identityNames
 * @param unitNames
 * @param groupNames
 * @param queryFilter
 * @return
 * @throws Exception
 */
public List<Project> listWithFilter( Integer maxCount, Object sequenceFieldValue, String orderField, String orderType, String personName, List<String> identityNames, List<String> unitNames, List<String> groupNames, QueryFilter queryFilter) throws Exception {
	EntityManager em = this.entityManagerContainer().get( Project.class );
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Project> cq = cb.createQuery(Project.class);
	Root<Project> root = cq.from(Project.class);
	Predicate p_permission = null;
	
	if( StringUtils.isNotEmpty( personName )) {
		//可以管理的栏目,肯定可以发布信息
		p_permission = CriteriaBuilderTools.predicate_or( cb, p_permission, cb.isMember( personName, root.get( Project_.participantPersonList )) );
		p_permission = CriteriaBuilderTools.predicate_or( cb, p_permission, cb.isMember( personName, root.get( Project_.manageablePersonList )) );
		p_permission = CriteriaBuilderTools.predicate_or( cb, p_permission, cb.equal( root.get( Project_.creatorPerson ), personName ) );
		p_permission = CriteriaBuilderTools.predicate_or( cb, p_permission, cb.equal( root.get( Project_.executor ), personName ) );
	}
	
	if( ListTools.isNotEmpty( identityNames )) {
		p_permission = CriteriaBuilderTools.predicate_or( cb, p_permission,  root.get( Project_.participantIdentityList).in(identityNames));
	}
	if( ListTools.isNotEmpty( unitNames )) {
		p_permission = CriteriaBuilderTools.predicate_or( cb, p_permission,  root.get( Project_.participantUnitList).in(unitNames));
	}
	if( ListTools.isNotEmpty( groupNames )) {
		p_permission = CriteriaBuilderTools.predicate_or( cb, p_permission,  root.get( Project_.participantGroupList).in(groupNames));
	}
	
	Predicate p = CriteriaBuilderTools.composePredicateWithQueryFilter( Project_.class, cb, p_permission, root, queryFilter );
	
	if( sequenceFieldValue != null && StringUtils.isNotEmpty( sequenceFieldValue.toString() )) {
		Predicate p_seq = cb.isNotNull( root.get( Dynamic_.sequence ) );
		if( "desc".equalsIgnoreCase( orderType )){
			p_seq = cb.and( p_seq, cb.lessThan( root.get( Project_.sequence ), sequenceFieldValue.toString() ));
		}else{
			p_seq = cb.and( p_seq, cb.greaterThan( root.get( Project_.sequence ), sequenceFieldValue.toString() ));
		}
		p = cb.and( p, p_seq);
	}		
	
	Order orderWithField = CriteriaBuilderTools.getOrder( cb, root, Project_.class, orderField, orderType );
	if( orderWithField != null ){
		cq.orderBy( orderWithField );
	}
	System.out.println(">>>SQL:" + em.createQuery(cq.where(p)).setMaxResults( maxCount).toString() );
	return em.createQuery(cq.where(p)).setMaxResults( maxCount).getResultList();
}
 
Example 20
Source File: BusinessObjectDataNotificationRegistrationDaoImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@Override
public List<NotificationRegistrationKey> getBusinessObjectDataNotificationRegistrationKeysByNotificationFilter(
    BusinessObjectDataNotificationFilter businessObjectDataNotificationFilter)
{
    // Create the criteria builder and a tuple style criteria query.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();

    // The criteria root is the business object data notification registration.
    Root<BusinessObjectDataNotificationRegistrationEntity> notificationRegistrationEntityRoot =
        criteria.from(BusinessObjectDataNotificationRegistrationEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataNotificationRegistrationEntity, NamespaceEntity> notificationRegistrationNamespaceEntityJoin =
        notificationRegistrationEntityRoot.join(BusinessObjectDataNotificationRegistrationEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity =
        notificationRegistrationEntityRoot.join(BusinessObjectDataNotificationRegistrationEntity_.businessObjectDefinition);
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> businessObjectDefinitionNamespaceEntity =
        businessObjectDefinitionEntity.join(BusinessObjectDefinitionEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, FileTypeEntity> fileTypeEntity =
        notificationRegistrationEntityRoot.join(BusinessObjectDataNotificationRegistrationEntity_.fileType, JoinType.LEFT);

    // Get the columns.
    Path<String> notificationRegistrationNamespaceColumn = notificationRegistrationNamespaceEntityJoin.get(NamespaceEntity_.code);
    Path<String> notificationRegistrationNameColumn = notificationRegistrationEntityRoot.get(BusinessObjectDataNotificationRegistrationEntity_.name);

    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(builder.upper(businessObjectDefinitionNamespaceEntity.get(NamespaceEntity_.code)),
        businessObjectDataNotificationFilter.getNamespace().toUpperCase()));
    predicates.add(builder.equal(builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)),
        businessObjectDataNotificationFilter.getBusinessObjectDefinitionName().toUpperCase()));
    if (StringUtils.isNotBlank(businessObjectDataNotificationFilter.getBusinessObjectFormatUsage()))
    {
        predicates.add(builder.or(builder.isNull(notificationRegistrationEntityRoot.get(BusinessObjectDataNotificationRegistrationEntity_.usage)), builder
            .equal(builder.upper(notificationRegistrationEntityRoot.get(BusinessObjectDataNotificationRegistrationEntity_.usage)),
                businessObjectDataNotificationFilter.getBusinessObjectFormatUsage().toUpperCase())));
    }
    if (StringUtils.isNotBlank(businessObjectDataNotificationFilter.getBusinessObjectFormatFileType()))
    {
        predicates.add(builder.or(builder.isNull(notificationRegistrationEntityRoot.get(BusinessObjectDataNotificationRegistrationEntity_.fileType)),
            builder.equal(builder.upper(fileTypeEntity.get(FileTypeEntity_.code)),
                businessObjectDataNotificationFilter.getBusinessObjectFormatFileType().toUpperCase())));
    }

    // Add the select and where clauses to the query.
    criteria.multiselect(notificationRegistrationNamespaceColumn, notificationRegistrationNameColumn)
        .where(builder.and(predicates.toArray(new Predicate[predicates.size()])));

    // Add the order by clause to the query.
    criteria.orderBy(builder.asc(notificationRegistrationNamespaceColumn), builder.asc(notificationRegistrationNameColumn));

    // Run the query to get a list of tuples back.
    List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();

    // Populate the list of keys from the returned tuples.
    return getNotificationRegistrationKeys(tuples, notificationRegistrationNamespaceColumn, notificationRegistrationNameColumn);
}