Java Code Examples for javax.persistence.EntityManager#getCriteriaBuilder()

The following examples show how to use javax.persistence.EntityManager#getCriteriaBuilder() . 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: LicenseCheckHook.java    From bouncr with Eclipse Public License 1.0 6 votes vote down vote up
private LicenseLastActivity findLastActivity(EntityManager em, UserLicense userLicense) {
    final CriteriaBuilder cb = em.getCriteriaBuilder();
    final CriteriaQuery<LicenseLastActivity> query = cb.createQuery(LicenseLastActivity.class);
    final Root<LicenseLastActivity> root = query.from(LicenseLastActivity.class);
    query.where(cb.equal(root.get("userLicense"), userLicense));
    return em.createQuery(query)
            .getResultStream()
            .findAny()
            .orElse(null);
}
 
Example 2
Source File: V2Count.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
private List<NameValueCountPair> groupByCreatorUnit(Business business, Predicate predicate) throws Exception {
	EntityManager em = business.entityManagerContainer().get(Task.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Tuple> cq = cb.createQuery(Tuple.class);
	Root<Task> root = cq.from(Task.class);
	Path<String> pathCreatorUnit = root.get(Task_.creatorUnit);
	cq.multiselect(pathCreatorUnit, cb.count(root)).where(predicate).groupBy(pathCreatorUnit);
	List<Tuple> os = em.createQuery(cq).getResultList();
	List<NameValueCountPair> list = new ArrayList<>();
	NameValueCountPair pair = null;
	for (Tuple o : os) {
		pair = new NameValueCountPair();
		pair.setName(o.get(pathCreatorUnit));
		pair.setValue(o.get(pathCreatorUnit));
		pair.setCount(o.get(1, Long.class));
		list.add(pair);
	}
	return list.stream().sorted(Comparator.comparing(NameValueCountPair::getCount).reversed())
			.collect(Collectors.toList());
}
 
Example 3
Source File: ItemFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public Integer getArrayLastIndexWithJobWithPath(String job, String... paths) throws Exception {
	EntityManager em = this.entityManagerContainer().get(Item.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Item> cq = cb.createQuery(Item.class);
	Root<Item> root = cq.from(Item.class);
	Predicate p = cb.equal(root.get(Item_.bundle), job);
	p = cb.and(p, cb.equal(root.get(Item_.itemCategory), ItemCategory.pp));
	for (int i = 0; ((i < paths.length) && (i < 8)); i++) {
		p = cb.and(p, cb.equal(root.get("path" + i), paths[i]));
	}
	for (int i = paths.length + 1; (i < 8); i++) {
		p = cb.and(p, cb.equal(root.get("path" + i), ""));
	}
	cq.select(root).where(p).orderBy(cb.desc(root.get("path" + paths.length + "Location")));
	List<Item> list = em.createQuery(cq).setMaxResults(1).getResultList();
	if (list.size() == 0) {
		return null;
	} else {
		return list.get(0).get("path" + paths.length + "Location", Integer.class);
	}
}
 
Example 4
Source File: BBSForumInfoFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public List<BBSForumInfo> listAllOpenForumInfo() throws Exception {
	EntityManager em = this.entityManagerContainer().get(BBSForumInfo.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<BBSForumInfo> cq = cb.createQuery(BBSForumInfo.class);
	Root<BBSForumInfo> root = cq.from(BBSForumInfo.class);
	Predicate p = cb.equal( root.get(BBSForumInfo_.forumStatus ), "启用" );
	cq.orderBy( cb.asc( root.get( BBSForumInfo_.orderNumber ) ) );
	return em.createQuery( cq.where(p) ).setMaxResults( 1000 ).getResultList();
}
 
Example 5
Source File: Calendar_SettingFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 根据配置编码查询配置信息对象
 * @param configCode
 * @return
 * @throws Exception
 */
public Calendar_Setting getWithConfigCode( String configCode ) throws Exception {
	if( configCode == null || configCode.isEmpty() ){
		return null;
	}
	Calendar_Setting attendanceSetting = null;
	List<Calendar_Setting> settingList = null;
	EntityManager em = this.entityManagerContainer().get(Calendar_Setting.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Calendar_Setting> cq = cb.createQuery(Calendar_Setting.class);
	Root<Calendar_Setting> root = cq.from(Calendar_Setting.class);
	Predicate p = cb.equal(root.get( Calendar_Setting_.configCode ),  configCode );
	settingList = em.createQuery(cq.where(p)).getResultList();
	if( settingList != null && settingList.size() > 0 ){
		attendanceSetting = settingList.get(0);
	}
	return attendanceSetting;
}
 
Example 6
Source File: ActionGetWithIdentityWithLevel.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private void referenceIdentity(Business business, WoUnitDuty woUnitDuty) throws Exception {
	EntityManager em = business.entityManagerContainer().get(Identity.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Identity> cq = cb.createQuery(Identity.class);
	Root<Identity> root = cq.from(Identity.class);
	Predicate p = root.get(Identity_.id).in(woUnitDuty.getIdentityList());
	List<Identity> os = em.createQuery(cq.select(root).where(p).distinct(true)).getResultList();
	List<WoIdentity> wos = WoIdentity.copier.copy(os);
	for (WoIdentity woIdentity : wos) {
		this.referencePerson(business, woIdentity);
	}
	wos = business.identity().sort(wos);
	woUnitDuty.setWoIdentityList(wos);
}
 
Example 7
Source File: ActionAppendWithUnitWithName.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private UnitAttribute get(Business business, Unit unit, String name) throws Exception {
	EntityManager em = business.entityManagerContainer().get(UnitAttribute.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<UnitAttribute> cq = cb.createQuery(UnitAttribute.class);
	Root<UnitAttribute> root = cq.from(UnitAttribute.class);
	Predicate p = cb.equal(root.get(UnitAttribute_.unit), unit.getId());
	p = cb.and(p, cb.equal(root.get(UnitAttribute_.name), name));
	List<UnitAttribute> os = em.createQuery(cq.select(root).where(p)).getResultList();
	if (!os.isEmpty()) {
		return os.get(0);
	}
	return null;
}
 
Example 8
Source File: TimerCategory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private Long countExpiredTaskCompleted(Business business, Date start, ActivityStub activityStub) throws Exception {
	EntityManager em = business.entityManagerContainer().get(TaskCompleted.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<TaskCompleted> root = cq.from(TaskCompleted.class);
	Predicate p = cb.greaterThan(root.get(TaskCompleted_.startTime), start);
	p = cb.and(p, cb.equal(root.get(TaskCompleted_.expired), true));
	p = cb.and(p, cb.equal(root.get(TaskCompleted_.activity), activityStub.getValue()));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 9
Source File: Folder2Factory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public List<String> listWithPersonWithSuperior(String person, String superior, String status) throws Exception {
	EntityManager em = this.entityManagerContainer().get(Folder2.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<Folder2> root = cq.from(Folder2.class);
	Predicate p = cb.equal(root.get(Folder2_.person), person);
	p = cb.and(p, cb.equal(root.get(Folder2_.superior), superior));
	if(StringUtils.isNotEmpty(status)){
		p = cb.and(p, cb.equal(root.get(Folder2_.status), status));
	}
	cq.select(root.get(Folder2_.id)).where(p);
	return em.createQuery(cq).getResultList();
}
 
Example 10
Source File: MCRUserManager.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a list of all users the given user is owner of.
 *
 * @param owner the user that owns other users
 */
public static List<MCRUser> listUsers(MCRUser owner) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRUser> query = cb.createQuery(MCRUser.class);
    Root<MCRUser> users = query.from(MCRUser.class);
    users.fetch(MCRUser_.owner);
    return em.createQuery(
        query
            .distinct(true)
            .where(cb.equal(users.get(MCRUser_.owner), owner)))
        .getResultList();
}
 
Example 11
Source File: TimerStartWorkUnitStubs.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private Collection<String> listUnitFromWorkCompleted(Business business, DateRange dateRange) throws Exception {
	EntityManagerContainer emc = business.entityManagerContainer();
	EntityManager em = emc.get(WorkCompleted.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<WorkCompleted> root = cq.from(WorkCompleted.class);
	Predicate p = cb.between(root.get(WorkCompleted_.startTime), dateRange.getStart(), dateRange.getEnd());
	cq.select(root.get(WorkCompleted_.creatorUnit)).distinct(true).where(p);
	List<String> list = em.createQuery(cq).getResultList();
	return list;
}
 
Example 12
Source File: OkrTaskFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 根据工作信息ID,列示所有的数据信息
 * @param workId
 * @return
 * @throws Exception 
 */
//@MethodDescribe( "根据工作信息ID,列示所有的数据信息" )
public List<String> listByWorkId(String workId) throws Exception {
	EntityManager em = this.entityManagerContainer().get(OkrTask.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<OkrTask> root = cq.from( OkrTask.class);
	Predicate p = cb.equal( root.get(OkrTask_.workId), workId );
	cq.select(root.get( OkrTask_.id) );
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 13
Source File: Calendar_EventRepeatMasterFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 列示指定Id的日历任务记录信息列表
 * @param ids
 * @return
 * @throws Exception
 */
public List<Calendar_EventRepeatMaster> list(List<String> ids) throws Exception {
	if( ListTools.isEmpty( ids ) ){
		return null;
	}
	EntityManager em = this.entityManagerContainer().get(Calendar_EventRepeatMaster.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Calendar_EventRepeatMaster> cq = cb.createQuery(Calendar_EventRepeatMaster.class);
	Root<Calendar_EventRepeatMaster> root = cq.from(Calendar_EventRepeatMaster.class);
	Predicate p = root.get( Calendar_EventRepeatMaster_.id).in(ids);
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 14
Source File: TaskListTemplateFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *  根据条件查询符合条件的项目信息ID
 * @param maxCount
 * @param orderField
 * @param orderType
 * @param personName
 * @param identityNames
 * @param unitNames
 * @param groupNames
 * @param queryFilter
 * @return
 * @throws Exception
 */
public List<Project> listWithFilter( Integer maxCount, 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 );

	Order orderWithField = CriteriaBuilderTools.getOrder(cb, root, Project_.class, orderField, orderType);
	if( orderWithField != null ){
		cq.orderBy( orderWithField );
	}
	return em.createQuery(cq.where(p)).setMaxResults( maxCount).getResultList();
}
 
Example 15
Source File: MCRJobQueue.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private int preFetch(int amount) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRJob> cq = cb.createQuery(MCRJob.class);
    Root<MCRJob> root = cq.from(MCRJob.class);

    List<Predicate> predicates = new ArrayList<>();
    predicates.add(cb.equal(root.get("status"), MCRJobStatus.NEW));
    if (action != null) {
        predicates.add(cb.equal(root.get("action"), action));
    }
    cq.where(cb.and(predicates.toArray(new Predicate[] {})));
    cq.orderBy(cb.asc(root.get("added")));
    cq.distinct(true);

    TypedQuery<MCRJob> query = em.createQuery(cq);
    query.setMaxResults(amount);

    List<MCRJob> jobs = query.getResultList();

    int i = 0;
    for (MCRJob job : jobs) {
        if (job.getParameters().isEmpty()) {
            continue;
        }

        i++;
        preFetch.add(job.clone());
        em.detach(job);
    }
    LOGGER.debug("prefetched {} jobs", i);
    return i;
}
 
Example 16
Source File: ActionListWithCurrentPersonDesc.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private List<Wo> list(Business business, Integer count, EffectivePerson effectivePerson) throws Exception {
	EntityManager em = business.entityManagerContainer().get(Instant.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Instant> cq = cb.createQuery(Instant.class);
	Root<Instant> root = cq.from(Instant.class);
	Predicate p = cb.equal(root.get(Instant_.person), effectivePerson.getDistinguishedName());
	List<Instant> os = em.createQuery(cq.select(root).where(p).orderBy(cb.desc(root.get(Instant_.createTime))))
			.setMaxResults(count).getResultList();
	return Wo.copier.copy(os);
}
 
Example 17
Source File: AttendanceDetailStatisticFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 根据员工,年月,统计请假天数
 * @param employeeNames
 * @param cycleYear
 * @param cycleMonth
 * @return
 * @throws Exception
 */
public Double sumOnSelfHolidayDaysByEmployeeYearAndMonth( List<String> employeeNames, String cycleYear, String cycleMonth) throws Exception{
	if( employeeNames == null || employeeNames.size() == 0 ){
		logger.error( new EmployeeNamesEmptyException() );
		return null;
	}		
	EntityManager em = this.entityManagerContainer().get( AttendanceDetail.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Double> cq = cb.createQuery(Double.class);
	Root<AttendanceDetail> root = cq.from( AttendanceDetail.class);
	// 不是周末,或者是周末但是调工作日了
	Predicate p1 = cb.isFalse(root.get( AttendanceDetail_.isWeekend ));
	p1 = cb.or( p1, cb.and(cb.isTrue(root.get( AttendanceDetail_.isWeekend )), cb.isTrue(root.get( AttendanceDetail_.isWorkday )) ));
							
	Predicate p = root.get( AttendanceDetail_.empName).in( employeeNames );
	p = cb.and( p, cb.equal( root.get( AttendanceDetail_.recordStatus ), 1));
	p = cb.and( p, cb.isFalse(root.get( AttendanceDetail_.isHoliday ))); //不是节假日
	p = cb.and( p, p1 ); //不是周末并且未调休工作晶
	if( cycleYear == null || cycleYear.isEmpty() ){
		logger.error( new CycleYearEmptyException() );
	}else{
		p = cb.and( p, cb.equal( root.get( AttendanceDetail_.cycleYear), cycleYear ));
	}
	if( cycleMonth == null || cycleMonth.isEmpty() ){
		logger.error( new CycleMonthEmptyException() );
	}else{
		p = cb.and( p, cb.equal( root.get(AttendanceDetail_.cycleMonth), cycleMonth ));
	}
	
	//查询总数
	cq.select( cb.sum( root.get( AttendanceDetail_.getSelfHolidayDays ) ) );	
			
	return em.createQuery(cq.where(p)).getSingleResult();
}
 
Example 18
Source File: ActionHasRole.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private Wo get(Business business, Wi wi) throws Exception {
	Wo wo = new Wo();
	wo.setValue(false);
	if (StringUtils.isEmpty(wi.getPerson()) || ListTools.isEmpty(wi.getRoleList())) {
		return wo;
	}
	Person person = business.person().pick(wi.getPerson());
	if (null == person) {
		return wo;
	}
	List<Role> roles = business.role().pick(wi.getRoleList());
	if (ListTools.isEmpty(roles)) {
		return wo;
	}
	List<String> groupIds = new ArrayList<>();
	groupIds.addAll(business.group().listSupNestedWithPerson(person.getId()));
	groupIds = ListTools.trim(groupIds, true, true);
	EntityManager em = business.entityManagerContainer().get(Role.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<Role> root = cq.from(Role.class);
	Predicate p = cb.isMember(person.getId(), root.get(Role_.personList));
	p = cb.or(p, root.get(Role_.groupList).in(groupIds));
	List<String> os = em.createQuery(cq.select(root.get(Role_.id)).where(p).distinct(true)).getResultList();
	boolean value = ListTools.containsAny(os,
			ListTools.extractProperty(roles, JpaObject.id_FIELDNAME, String.class, true, true));
	wo.setValue(value);
	return wo;
}
 
Example 19
Source File: VenueDTO.java    From monolith with Apache License 2.0 5 votes vote down vote up
public List<TicketPrice> findTicketPricesBySection(Section section, EntityManager em)
{
   CriteriaQuery<TicketPrice> criteria = em
          .getCriteriaBuilder().createQuery(TicketPrice.class);
   Root<TicketPrice> from = criteria.from(TicketPrice.class);
   CriteriaBuilder builder = em.getCriteriaBuilder();
   Predicate sectionIsSame = builder.equal(from.get("section"), section);
   return em.createQuery(
          criteria.select(from).where(sectionIsSame)).getResultList();
}
 
Example 20
Source File: TimerStartWorkApplicationStubs.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private String getApplicationNameFromWorkCompleted(Business business, DateRange dateRange, String applicationId)
		throws Exception {
	EntityManagerContainer emc = business.entityManagerContainer();
	EntityManager em = emc.get(WorkCompleted.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<WorkCompleted> root = cq.from(WorkCompleted.class);
	Predicate p = cb.between(root.get(WorkCompleted_.startTime), dateRange.getStart(), dateRange.getEnd());
	p = cb.and(p, cb.equal(root.get(WorkCompleted_.application), applicationId));
	cq.select(root.get(WorkCompleted_.applicationName)).where(p);
	List<String> list = em.createQuery(cq).setMaxResults(1).getResultList();
	return list.isEmpty() ? null : list.get(0);
}