List of usage examples for javax.persistence.criteria CriteriaBuilder createTupleQuery
CriteriaQuery<Tuple> createTupleQuery();
CriteriaQuery
object that returns a tuple of objects as its result. From source file:org.finra.herd.dao.impl.StorageFileDaoImpl.java
@Override public MultiValuedMap<Integer, String> getStorageFilePathsByStorageUnitIds(List<Integer> storageUnitIds) { // Create a map that can hold a collection of values against each key. MultiValuedMap<Integer, String> result = new ArrayListValuedHashMap<>(); // Retrieve the pagination size for the storage file paths query configured in the system. Integer paginationSize = configurationHelper .getProperty(ConfigurationValue.STORAGE_FILE_PATHS_QUERY_PAGINATION_SIZE, Integer.class); // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the storage file. Root<StorageFileEntity> storageFileEntity = criteria.from(StorageFileEntity.class); // Get the columns. Path<Integer> storageUnitIdColumn = storageFileEntity.get(StorageFileEntity_.storageUnitId); Path<String> storageFilePathColumn = storageFileEntity.get(StorageFileEntity_.path); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = getPredicateForInClause(builder, storageUnitIdColumn, storageUnitIds); // Add the select clause. criteria.multiselect(storageUnitIdColumn, storageFilePathColumn); // Add the where clause. criteria.where(queryRestriction);/*w w w . j ava2s .c o m*/ // Execute the query using pagination and populate the result map. int startPosition = 0; while (true) { // Run the query to get a list of tuples back. List<Tuple> tuples = entityManager.createQuery(criteria).setFirstResult(startPosition) .setMaxResults(paginationSize).getResultList(); // Populate the result map from the returned tuples (i.e. 1 tuple for each row). for (Tuple tuple : tuples) { // Extract the tuple values. Integer storageUnitId = tuple.get(storageUnitIdColumn); String storageFilePath = tuple.get(storageFilePathColumn); // Update the result map. result.put(storageUnitId, storageFilePath); } // Break out of the while loop if we got less results than the pagination size. if (tuples.size() < paginationSize) { break; } // Increment the start position. startPosition += paginationSize; } return result; }
From source file:org.finra.herd.dao.impl.StorageUnitDaoImpl.java
/** * Retrieves a list of storage unit availability DTOs per specified parameters. This method processes a sublist of partition filters specified by * partitionFilterSubListFromIndex and partitionFilterSubListSize parameters. * * @param businessObjectFormatKey the business object format key (case-insensitive). If a business object format version isn't specified, the latest * available format version for each partition value will be used * @param partitionFilters the list of partition filter to be used to select business object data instances. Each partition filter contains a list of * primary and sub-partition values in the right order up to the maximum partition levels allowed by business object data registration - with partition * values for the relative partitions not to be used for selection passed as nulls * @param businessObjectDataVersion the business object data version. If a business object data version isn't specified, the latest data version based on * the specified business object data status is returned * @param businessObjectDataStatus the business object data status. This parameter is ignored when the business object data version is specified. When * business object data version and business object data status both are not specified, the latest data version for each set of partition values will be * used regardless of the status//from ww w. j a va 2s .com * @param storageNames the list of storage names where the business object data storage units should be looked for (case-insensitive) * @param storagePlatformType the optional storage platform type, e.g. S3 for Hive DDL. It is ignored when the list of storage names is not empty * @param excludedStoragePlatformType the optional storage platform type to be excluded from search. It is ignored when the list of storage names is not * empty or the storage platform type is specified * @param partitionFilterSubListFromIndex the index of the first element in the partition filter sublist * @param partitionFilterSubListSize the size of the partition filter sublist * @param selectOnlyAvailableStorageUnits specifies if only available storage units will be selected or any storage units regardless of their status * * @return the list of storage unit availability DTOs sorted by partition values */ private List<StorageUnitAvailabilityDto> getStorageUnitsByPartitionFilters( BusinessObjectFormatKey businessObjectFormatKey, List<List<String>> partitionFilters, Integer businessObjectDataVersion, String businessObjectDataStatus, List<String> storageNames, String storagePlatformType, String excludedStoragePlatformType, boolean selectOnlyAvailableStorageUnits, int partitionFilterSubListFromIndex, int partitionFilterSubListSize) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the storage unit. Root<StorageUnitEntity> storageUnitEntity = criteria.from(StorageUnitEntity.class); // Join to the other tables we can filter on. Join<StorageUnitEntity, BusinessObjectDataEntity> businessObjectDataEntity = storageUnitEntity .join(StorageUnitEntity_.businessObjectData); Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage); Join<StorageEntity, StoragePlatformEntity> storagePlatformEntity = storageEntity .join(StorageEntity_.storagePlatform); Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntity = businessObjectDataEntity .join(BusinessObjectDataEntity_.businessObjectFormat); Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity .join(BusinessObjectDefinitionEntity_.namespace); Join<StorageUnitEntity, StorageUnitStatusEntity> storageUnitStatusEntity = storageUnitEntity .join(StorageUnitEntity_.status); // Create the standard restrictions (i.e. the standard where clauses). // Create a standard restriction based on the business object format key values. // Please note that we specify not to ignore the business object format version. Predicate mainQueryRestriction = getQueryRestriction(builder, businessObjectFormatEntity, fileTypeEntity, businessObjectDefinitionEntity, namespaceEntity, businessObjectFormatKey, false); // If a format version was not specified, use the latest available for this set of partition values. if (businessObjectFormatKey.getBusinessObjectFormatVersion() == null) { // Get the latest available format version for this set of partition values and per other restrictions. Subquery<Integer> subQuery = getMaximumBusinessObjectFormatVersionSubQuery(builder, criteria, businessObjectDefinitionEntity, businessObjectFormatEntity, fileTypeEntity, businessObjectDataEntity, businessObjectDataVersion, businessObjectDataStatus, storageNames, storagePlatformType, excludedStoragePlatformType, selectOnlyAvailableStorageUnits); mainQueryRestriction = builder.and(mainQueryRestriction, builder.in( businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)) .value(subQuery)); } // Add restriction as per specified primary and/or sub-partition values. mainQueryRestriction = builder.and(mainQueryRestriction, getQueryRestrictionOnPartitionValues(builder, businessObjectDataEntity, partitionFilters.subList(partitionFilterSubListFromIndex, partitionFilterSubListFromIndex + partitionFilterSubListSize))); // If a data version was specified, use it. Otherwise, use the latest one as per specified business object data status. if (businessObjectDataVersion != null) { mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal( businessObjectDataEntity.get(BusinessObjectDataEntity_.version), businessObjectDataVersion)); } else { // Business object data version is not specified, so get the latest one as per specified business object data status, if any. // Meaning, when both business object data version and business object data status are not specified, we just return // the latest business object data version in the specified storage. Subquery<Integer> subQuery = getMaximumBusinessObjectDataVersionSubQuery(builder, criteria, businessObjectDataEntity, businessObjectFormatEntity, businessObjectDataStatus, storageNames, storagePlatformType, excludedStoragePlatformType, selectOnlyAvailableStorageUnits); mainQueryRestriction = builder.and(mainQueryRestriction, builder.in(businessObjectDataEntity.get(BusinessObjectDataEntity_.version)).value(subQuery)); } // If specified, add restriction on storage. mainQueryRestriction = builder.and(mainQueryRestriction, getQueryRestrictionOnStorage(builder, storageEntity, storagePlatformEntity, storageNames, storagePlatformType, excludedStoragePlatformType)); // If specified, add a restriction on storage unit status availability flag. if (selectOnlyAvailableStorageUnits) { mainQueryRestriction = builder.and(mainQueryRestriction, builder.isTrue(storageUnitStatusEntity.get(StorageUnitStatusEntity_.available))); } // Order by partitions and storage names. List<Order> orderBy = new ArrayList<>(); for (SingularAttribute<BusinessObjectDataEntity, String> businessObjectDataPartition : BUSINESS_OBJECT_DATA_PARTITIONS) { orderBy.add(builder.asc(businessObjectDataEntity.get(businessObjectDataPartition))); } orderBy.add(builder.asc(storageEntity.get(StorageEntity_.name))); // Get the columns. Path<Integer> storageUnitIdColumn = storageUnitEntity.get(StorageUnitEntity_.id); Path<String> namespaceCodeColumn = namespaceEntity.get(NamespaceEntity_.code); Path<String> businessObjectDefinitionNameColumn = businessObjectDefinitionEntity .get(BusinessObjectDefinitionEntity_.name); Path<String> businessObjectFormatUsageColumn = businessObjectFormatEntity .get(BusinessObjectFormatEntity_.usage); Path<String> fileTypeColumn = fileTypeEntity.get(FileTypeEntity_.code); Path<Integer> businessObjectFormatVersionColumn = businessObjectFormatEntity .get(BusinessObjectFormatEntity_.businessObjectFormatVersion); Path<String> primaryPartitionValueColumn = businessObjectDataEntity .get(BusinessObjectDataEntity_.partitionValue); Path<String> subPartitionValue1Column = businessObjectDataEntity .get(BusinessObjectDataEntity_.partitionValue2); Path<String> subPartitionValue2Column = businessObjectDataEntity .get(BusinessObjectDataEntity_.partitionValue3); Path<String> subPartitionValue3Column = businessObjectDataEntity .get(BusinessObjectDataEntity_.partitionValue4); Path<String> subPartitionValue4Column = businessObjectDataEntity .get(BusinessObjectDataEntity_.partitionValue5); Path<Integer> businessObjectDataVersionColumn = businessObjectDataEntity .get(BusinessObjectDataEntity_.version); Path<String> storageNameColumn = storageEntity.get(StorageEntity_.name); Path<String> storageUnitDirectoryPathColumn = storageUnitEntity.get(StorageUnitEntity_.directoryPath); Path<String> businessObjectDataStatusColumn = businessObjectDataEntity .get(BusinessObjectDataEntity_.statusCode); Path<String> storageUnitStatusColumn = storageUnitEntity.get(StorageUnitEntity_.statusCode); Path<Boolean> storageUnitAvailableColumn = storageUnitStatusEntity.get(StorageUnitStatusEntity_.available); // Add the clauses for the query. criteria.multiselect(storageUnitIdColumn, namespaceCodeColumn, businessObjectDefinitionNameColumn, businessObjectFormatUsageColumn, fileTypeColumn, businessObjectFormatVersionColumn, primaryPartitionValueColumn, subPartitionValue1Column, subPartitionValue2Column, subPartitionValue3Column, subPartitionValue4Column, businessObjectDataVersionColumn, storageNameColumn, storageUnitDirectoryPathColumn, businessObjectDataStatusColumn, storageUnitStatusColumn, storageUnitAvailableColumn).where(mainQueryRestriction).orderBy(orderBy); // Run the query to get a list of tuples back. List<Tuple> tuples = entityManager.createQuery(criteria).getResultList(); // Build a list of storage unit availability DTOs to return. List<StorageUnitAvailabilityDto> storageUnitAvailabilityDtos = new ArrayList<>(); for (Tuple tuple : tuples) { storageUnitAvailabilityDtos.add(new StorageUnitAvailabilityDto(tuple.get(storageUnitIdColumn), new BusinessObjectDataKey(tuple.get(namespaceCodeColumn), tuple.get(businessObjectDefinitionNameColumn), tuple.get(businessObjectFormatUsageColumn), tuple.get(fileTypeColumn), tuple.get(businessObjectFormatVersionColumn), tuple.get(primaryPartitionValueColumn), getSubPartitionValuesFromRawSubPartitionValues(Arrays.asList( tuple.get(subPartitionValue1Column), tuple.get(subPartitionValue2Column), tuple.get(subPartitionValue3Column), tuple.get(subPartitionValue4Column))), tuple.get(businessObjectDataVersionColumn)), tuple.get(storageNameColumn), tuple.get(storageUnitDirectoryPathColumn), tuple.get(businessObjectDataStatusColumn), tuple.get(storageUnitStatusColumn), tuple.get(storageUnitAvailableColumn))); } return storageUnitAvailabilityDtos; }
From source file:org.finra.herd.dao.impl.StorageUnitNotificationRegistrationDaoImpl.java
@Override public List<NotificationRegistrationKey> getStorageUnitNotificationRegistrationKeysByNamespace( String namespace) {// w ww. j av a 2 s . c o m // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the storage unit notification registration. Root<StorageUnitNotificationRegistrationEntity> businessObjectDataNotificationEntityRoot = criteria .from(StorageUnitNotificationRegistrationEntity.class); // Join to the other tables we can filter on. Join<StorageUnitNotificationRegistrationEntity, NamespaceEntity> namespaceEntityJoin = businessObjectDataNotificationEntityRoot .join(StorageUnitNotificationRegistrationEntity_.namespace); // Get the columns. Path<String> notificationRegistrationNamespaceColumn = namespaceEntityJoin.get(NamespaceEntity_.code); Path<String> notificationRegistrationNameColumn = businessObjectDataNotificationEntityRoot .get(StorageUnitNotificationRegistrationEntity_.name); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(namespaceEntityJoin.get(NamespaceEntity_.code)), namespace.toUpperCase()); // Add the select clause. criteria.multiselect(notificationRegistrationNamespaceColumn, notificationRegistrationNameColumn); // Add the where clause. criteria.where(queryRestriction); // Add the order by clause. criteria.orderBy(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); }
From source file:org.finra.herd.dao.impl.StorageUnitNotificationRegistrationDaoImpl.java
@Override public List<NotificationRegistrationKey> getStorageUnitNotificationRegistrationKeysByNotificationFilter( StorageUnitNotificationFilter 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 storage unit notification registration. Root<StorageUnitNotificationRegistrationEntity> notificationRegistrationEntityRoot = criteria .from(StorageUnitNotificationRegistrationEntity.class); // Join to the other tables we can filter on. Join<StorageUnitNotificationRegistrationEntity, NamespaceEntity> notificationRegistrationNamespaceEntityJoin = notificationRegistrationEntityRoot .join(StorageUnitNotificationRegistrationEntity_.namespace); Join<StorageUnitNotificationRegistrationEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = notificationRegistrationEntityRoot .join(StorageUnitNotificationRegistrationEntity_.businessObjectDefinition); Join<BusinessObjectDefinitionEntity, NamespaceEntity> businessObjectDefinitionNamespaceEntity = businessObjectDefinitionEntity .join(BusinessObjectDefinitionEntity_.namespace); Join<StorageUnitNotificationRegistrationEntity, FileTypeEntity> fileTypeEntity = notificationRegistrationEntityRoot .join(StorageUnitNotificationRegistrationEntity_.fileType, JoinType.LEFT); // Get the columns. Path<String> notificationRegistrationNamespaceColumn = notificationRegistrationNamespaceEntityJoin .get(NamespaceEntity_.code); Path<String> notificationRegistrationNameColumn = notificationRegistrationEntityRoot .get(StorageUnitNotificationRegistrationEntity_.name); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(//from w w w . ja v a 2s . c o m 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(StorageUnitNotificationRegistrationEntity_.usage)), builder.equal( builder.upper(notificationRegistrationEntityRoot .get(StorageUnitNotificationRegistrationEntity_.usage)), businessObjectDataNotificationFilter.getBusinessObjectFormatUsage().toUpperCase()))); } if (StringUtils.isNotBlank(businessObjectDataNotificationFilter.getBusinessObjectFormatFileType())) { predicates.add(builder.or( builder.isNull(notificationRegistrationEntityRoot .get(StorageUnitNotificationRegistrationEntity_.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); }
From source file:org.niord.core.db.CriteriaHelper.java
/** * Initializes a CriteriaBuilder and CriteriaQuery with a Tuple query. * @param em the entity manager/*from www. j ava 2s. c o m*/ * @return the newly instantiated criteria builder */ public static CriteriaHelper<Tuple> initWithTupleQuery(EntityManager em) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Tuple> tupleQuery = builder.createTupleQuery(); return new CriteriaHelper<>(builder, tupleQuery); }
From source file:org.niord.core.message.MessageService.java
/** * Searches out the ID's of the paged result set of messages defined by the search parameters. * Also fills out the total result count of the message search result. * * @param param the search parameters// w w w .j a v a 2 s .c om * @param result the search result to update with the total result count * @return the paged list of message ID's */ @SuppressWarnings("all") List<Integer> searchPagedMessageIds(MessageSearchParams param, PagedSearchResultVo<Message> result) throws Exception { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Tuple> tupleQuery = builder.createTupleQuery(); // Select messages Root<Message> msgRoot = tupleQuery.from(Message.class); // Build the predicates based on the search parameters CriteriaHelper<Tuple> criteriaHelper = CriteriaHelper.initWithTupleQuery(em).between(msgRoot.get("updated"), param.getUpdatedFrom(), param.getUpdatedTo()); // Filter by dates if (param.getFrom() != null || param.getTo() != null) { DateType dateType = param.getDateType() != null ? param.getDateType() : DateType.PUBLISH_DATE; Date from = param.getFrom(); Date to = param.getTo(); switch (dateType) { case PUBLISH_DATE: criteriaHelper.overlaps(msgRoot.get("publishDateFrom"), msgRoot.get("publishDateTo"), from, to); break; case EVENT_DATE: criteriaHelper.overlaps(msgRoot.get("eventDateFrom"), msgRoot.get("eventDateTo"), from, to); break; case CREATED_DATE: criteriaHelper.between(msgRoot.get("created"), from, to); break; case UPDATED_DATE: criteriaHelper.between(msgRoot.get("updated"), from, to); break; case PUBLISH_FROM_DATE: criteriaHelper.between(msgRoot.get("publishDateFrom"), from, to); break; case PUBLISH_TO_DATE: criteriaHelper.between(msgRoot.get("publishDateTo"), from, to); break; } } // Main types and sub-types if (!param.getMainTypes().isEmpty()) { criteriaHelper.in(msgRoot.get("mainType"), param.getMainTypes()); } if (!param.getTypes().isEmpty()) { criteriaHelper.in(msgRoot.get("type"), param.getTypes()); } // Statuses if (!param.getStatuses().isEmpty()) { criteriaHelper.in(msgRoot.get("status"), param.getStatuses()); } // Search the Lucene index for free text search if (param.requiresLuceneSearch()) { List<Long> ids = null; try { ids = messageLuceneIndex.searchIndex(param.getQuery(), param.getLanguage(), Integer.MAX_VALUE); } catch (Exception e) { log.warn("Error searching lucene index for query " + param.getQuery()); ids = Collections.emptyList(); } criteriaHelper.in(msgRoot.get("id"), ids); } // Message series if (!param.getSeriesIds().isEmpty()) { criteriaHelper.in(msgRoot.get("messageSeries").get("seriesId"), param.getSeriesIds()); } // Filter by area, join over... if (!param.getAreaIds().isEmpty()) { Join<Message, Area> areas = msgRoot.join("areas", JoinType.LEFT); if (!param.getAreaIds().isEmpty()) { Predicate[] areaMatch = param.getAreaIds().stream().map(aid -> areaService.findByAreaId(aid)) .filter(Objects::nonNull).map(a -> builder.like(areas.get("lineage"), a.getLineage() + "%")) .toArray(Predicate[]::new); criteriaHelper.add(builder.or(areaMatch)); } } // Filter on categories if (!param.getCategoryIds().isEmpty()) { Join<Message, Category> categories = msgRoot.join("categories", JoinType.LEFT); Predicate[] categoryMatch = param.getCategoryIds().stream() .map(cid -> categoryService.findByCategoryId(cid)).filter(Objects::nonNull) .map(c -> builder.like(categories.get("lineage"), c.getLineage() + "%")) .toArray(Predicate[]::new); criteriaHelper.add(builder.or(categoryMatch)); } // Filter on charts if (!param.getChartNumbers().isEmpty()) { Join<Message, Chart> charts = msgRoot.join("charts", JoinType.LEFT); Predicate[] chartMatch = param.getChartNumbers().stream() .map(m -> builder.equal(charts.get("chartNumber"), m)).toArray(Predicate[]::new); criteriaHelper.add(builder.or(chartMatch)); } // Geometry if (param.getExtent() != null) { param.getExtent().setSRID(WGS84_SRID); Join<Message, MessagePart> partRoot = msgRoot.join("parts", JoinType.LEFT); Join<Message, FeatureCollection> fcRoot = partRoot.join("geometry", JoinType.LEFT); Join<FeatureCollection, Feature> fRoot = fcRoot.join("features", JoinType.LEFT); Predicate geomPredicate = new SpatialIntersectsPredicate(criteriaHelper.getCriteriaBuilder(), fRoot.get("geometry"), param.getExtent()); if (param.getIncludeNoPos() != null && param.getIncludeNoPos().booleanValue()) { // search for message with no geometry in addition to messages within extent criteriaHelper.add(builder.or(builder.equal(msgRoot.get("hasGeometry"), false), geomPredicate)); } else { // Only search for messages within extent criteriaHelper.add(geomPredicate); } } // Tags if (!param.getTags().isEmpty()) { Join<Message, MessageTag> tags = msgRoot.join("tags", JoinType.LEFT); String[] tagIds = param.getTags().toArray(new String[param.getTags().size()]); Predicate[] tagMatch = messageTagService.findTags(tagIds).stream() .map(t -> builder.equal(tags.get("id"), t.getId())).toArray(Predicate[]::new); criteriaHelper.add(builder.or(tagMatch)); } // User if (StringUtils.isNotBlank(param.getUsername())) { UserType userType = param.getUserType() == null ? UserType.UPDATED_BY : param.getUserType(); if (userType == UserType.CREATED_BY || userType == UserType.LAST_UPDATED_BY) { String joinCol = userType == UserType.CREATED_BY ? "createdBy" : "lastUpdatedBy"; Join<Message, User> userRoot = msgRoot.join(joinCol, JoinType.LEFT); criteriaHelper.equals(userRoot.get("username"), param.getUsername()); } else { Join<Message, MessageHistory> historyRoot = msgRoot.join("history", JoinType.LEFT); Join<MessageHistory, User> userRoot = historyRoot.join("user", JoinType.LEFT); criteriaHelper.equals(userRoot.get("username"), param.getUsername()); } } // Comments if (param.getCommentsType() != null) { Join<Message, Comment> comments = msgRoot.join("comments", JoinType.LEFT); User user = userService.currentUser(); Predicate own = user != null ? builder.equal(comments.get("user"), user) : null; Predicate exists = builder.isNotNull(comments.get("id")); Predicate unack = builder.isNull(comments.get("acknowledgedBy")); if (user != null && param.getCommentsType() == OWN) { criteriaHelper.add(own); } else if (user != null && param.getCommentsType() == OWN_UNACK) { criteriaHelper.add(builder.and(own, unack)); } else if (param.getCommentsType() == ANY_UNACK) { criteriaHelper.add(builder.and(exists, unack)); } else if (param.getCommentsType() == ANY) { criteriaHelper.add(exists); } } // Refenced messages if (StringUtils.isNotBlank(param.getMessageId())) { int levels = param.getReferenceLevels() == null ? 1 : param.getReferenceLevels(); // NB: This is expensive queries - limit the levels levels = Math.max(0, Math.min(5, levels)); // First, find messages referenced by the message ID Set<Integer> referencedIds = findReferencedMessageIds(new HashSet<>(), param.getMessageId(), levels); // Next, add messages referencing the message ID findReferencingMessageIds(referencedIds, param.getMessageId(), levels); criteriaHelper.in(msgRoot.get("id"), referencedIds); } // Determine the fields to fetch Join<Message, Area> areaRoot = null; Expression<?> treeSortOrder = null; List<Selection<?>> fields = new ArrayList<>(); fields.add(msgRoot.get("id")); if (param.sortByEventDate()) { fields.add(msgRoot.get("eventDateFrom")); fields.add(msgRoot.get("eventDateTo")); } else if (param.sortByPublishDate()) { fields.add(msgRoot.get("publishDateFrom")); fields.add(msgRoot.get("publishDateTo")); } else if (param.sortByFollowUpDate()) { fields.add(msgRoot.get("followUpDate")); } else if (param.sortById()) { fields.add(msgRoot.get("year")); fields.add(msgRoot.get("number")); fields.add(msgRoot.get("publishDateFrom")); } else if (param.sortByArea()) { areaRoot = msgRoot.join("area", JoinType.LEFT); // General messages (without an associated area) should be sorted last treeSortOrder = builder.selectCase().when(builder.isNull(areaRoot.get("treeSortOrder")), 999999) .otherwise(areaRoot.get("treeSortOrder")); fields.add(treeSortOrder); fields.add(msgRoot.get("areaSortOrder")); fields.add(msgRoot.get("year")); fields.add(msgRoot.get("number")); } Selection[] f = fields.toArray(new Selection<?>[fields.size()]); // Complete the query and fetch the message id's (and fields used for sorting) tupleQuery.multiselect(f).distinct(true).where(criteriaHelper.where()); // Sort the query if (param.sortByEventDate()) { if (param.getSortOrder() == SortOrder.ASC) { tupleQuery.orderBy(builder.asc(msgRoot.get("eventDateFrom")), builder.asc(msgRoot.get("eventDateTo")), builder.asc(msgRoot.get("id"))); } else { tupleQuery.orderBy(builder.desc(msgRoot.get("eventDateFrom")), builder.desc(msgRoot.get("eventDateTo")), builder.desc(msgRoot.get("id"))); } } else if (param.sortByPublishDate()) { if (param.getSortOrder() == SortOrder.ASC) { tupleQuery.orderBy(builder.asc(msgRoot.get("publishDateFrom")), builder.asc(msgRoot.get("publishDateTo")), builder.asc(msgRoot.get("id"))); } else { tupleQuery.orderBy(builder.desc(msgRoot.get("publishDateFrom")), builder.desc(msgRoot.get("publishDateTo")), builder.desc(msgRoot.get("id"))); } } else if (param.sortByFollowUpDate()) { if (param.getSortOrder() == SortOrder.ASC) { tupleQuery.orderBy(builder.asc(msgRoot.get("followUpDate")), builder.asc(msgRoot.get("id"))); } else { tupleQuery.orderBy(builder.desc(msgRoot.get("followUpDate")), builder.desc(msgRoot.get("id"))); } } else if (param.sortById()) { if (param.getSortOrder() == SortOrder.ASC) { tupleQuery.orderBy(builder.asc(msgRoot.get("year")), builder.asc(msgRoot.get("number")), builder.asc(msgRoot.get("publishDateFrom")), builder.asc(msgRoot.get("id"))); } else { tupleQuery.orderBy(builder.desc(msgRoot.get("year")), builder.desc(msgRoot.get("number")), builder.desc(msgRoot.get("publishDateFrom")), builder.desc(msgRoot.get("id"))); } } else if (param.sortByArea()) { if (param.getSortOrder() == SortOrder.ASC) { tupleQuery.orderBy(builder.asc(treeSortOrder), builder.asc(msgRoot.get("areaSortOrder")), builder.asc(msgRoot.get("year")), builder.asc(msgRoot.get("number")), builder.asc(msgRoot.get("id"))); } else { tupleQuery.orderBy(builder.desc(treeSortOrder), builder.desc(msgRoot.get("areaSortOrder")), builder.desc(msgRoot.get("year")), builder.desc(msgRoot.get("number")), builder.desc(msgRoot.get("id"))); } } // Execute the query List<Tuple> totalResult = em.createQuery(tupleQuery).getResultList(); // Register the total result result.setTotal(totalResult.size()); List<Integer> msgIds = totalResult.stream().map(t -> (Integer) t.get(0)).collect(Collectors.toList()); // Extract and return the paged sub-list int startIndex = Math.min(msgIds.size(), param.getPage() * param.getMaxSize()); int endIndex = Math.min(msgIds.size(), startIndex + param.getMaxSize()); return msgIds.subList(startIndex, endIndex); }
From source file:org.niord.core.publication.PublicationService.java
/** * Searches for a page of publication IDs matching the given search parameters * Will also update the search result with the total number of matches * * @param params the search parameters/*from ww w . j a v a2 s . c om*/ * @param result the result structure to update with the total number of matches * @return a page of publication IDs matching the given search parameters */ @SuppressWarnings("all") public List<Integer> searchPagedPublicationIds(PublicationSearchParams params, PagedSearchResultVo<Publication> result) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> tupleQuery = cb.createTupleQuery(); Root<Publication> publicationRoot = tupleQuery.from(Publication.class); // Build the predicate CriteriaHelper<Tuple> criteriaHelper = CriteriaHelper.initWithTupleQuery(em); // Match the main type criteriaHelper.equals(publicationRoot.get("mainType"), params.getMainType()); // Match the file type criteriaHelper.equals(publicationRoot.get("type"), params.getType()); // Match the status if (params.getStatuses() != null && !params.getStatuses().isEmpty()) { criteriaHelper.in(publicationRoot.get("status"), params.getStatuses()); } // Match the title if (StringUtils.isNotBlank(params.getTitle())) { Join<Publication, PublicationDesc> descs = publicationRoot.join("descs", JoinType.LEFT); criteriaHelper.like(descs.get("title"), params.getTitle()); // Optionally, match the language if (StringUtils.isNotBlank(params.getLanguage())) { criteriaHelper.equals(descs.get("lang"), params.getLanguage()); } } // Match the category Join<Publication, PublicationCategory> typeJoin = publicationRoot.join("category", JoinType.LEFT); if (StringUtils.isNotBlank(params.getCategory())) { criteriaHelper.equals(typeJoin.get("categoryId"), params.getCategory()); } // Match the publish flag of the category if (params.getPublished() != null) { criteriaHelper.equals(typeJoin.get("publish"), params.getPublished()); } // Match the domain if (StringUtils.isNotBlank(params.getDomain())) { Join<Publication, Domain> domainJoin = publicationRoot.join("domain", JoinType.LEFT); criteriaHelper.add(cb.or(cb.isNull(publicationRoot.get("domain")), cb.equal(domainJoin.get("domainId"), params.getDomain()))); } // Match the message publication category criteriaHelper.equals(publicationRoot.get("messagePublication"), params.getMessagePublication()); // Filter by dates if (params.getFrom() != null || params.getTo() != null) { Date from = params.getFrom(); //TimeUtils.resetTime(params.getFrom()); Date to = params.getTo(); //TimeUtils.endOfDay(params.getTo()); criteriaHelper.overlaps(publicationRoot.get("publishDateFrom"), publicationRoot.get("publishDateTo"), from, to); } // Compute the sort order List<Order> sortOrders = new ArrayList<>(); sortOrders.add(cb.asc(typeJoin.get("priority"))); sortOrders.add(cb.desc(publicationRoot.get("publishDateFrom"))); // Select ID field + fields used for sorting List<Selection<?>> fields = new ArrayList<>(); fields.add(publicationRoot.get("id")); fields.add(typeJoin.get("priority")); fields.add(publicationRoot.get("publishDateFrom")); Selection[] f = fields.toArray(new Selection<?>[fields.size()]); // Complete the query tupleQuery.multiselect(f).distinct(true).where(criteriaHelper.where()).orderBy(sortOrders); // Execute the query List<Tuple> totalResult = em.createQuery(tupleQuery).getResultList(); // Register the total result result.setTotal(totalResult.size()); List<Integer> publicationIds = totalResult.stream().map(t -> (Integer) t.get(0)) .collect(Collectors.toList()); // Extract and return the paged sub-list int startIndex = Math.min(publicationIds.size(), params.getPage() * params.getMaxSize()); int endIndex = Math.min(publicationIds.size(), startIndex + params.getMaxSize()); return publicationIds.subList(startIndex, endIndex); }
From source file:org.xlcloud.service.dao.JpaVirtualClusterDefinitionsDao.java
/** {@inheritDoc} */ @Override// ww w .j av a 2s . co m public List<VirtualClusterDefinitionModel> findByTypeAndTags(Long accountId, String type, Set<String> tags, boolean paged) { if (LOG.isDebugEnabled()) { LOG.debug("finding vc definitions with type id: " + type + " and following tag ids:" + ToStringBuilder.reflectionToString(tags)); } CriteriaBuilder criteriaBuilder = getCriteriaBuilder(); CriteriaQuery<Tuple> query = criteriaBuilder.createTupleQuery(); Root<VirtualClusterDefinitionModel> vcDefRoot = query.from(VirtualClusterDefinitionModel.class); Predicate typePredicate = criteriaBuilder.equal(vcDefRoot.get(VirtualClusterDefinitionModel_.type), type); Predicate tagsPredicate = vcDefRoot.get(VirtualClusterDefinitionModel_.tags).in(tags); boolean skipTagsJoin = tags.isEmpty(); Predicate predicate = skipTagsJoin ? typePredicate : criteriaBuilder.and(typePredicate, tagsPredicate); Predicate scopePredicate; if (accountId != null) { Predicate accountPredicate = criteriaBuilder .equal(vcDefRoot.get(VirtualClusterDefinitionModel_.accountId), accountId); Predicate privatePredicate = criteriaBuilder .equal(vcDefRoot.get(VirtualClusterDefinitionModel_.catalogScope), CatalogScope.PRIVATE); scopePredicate = criteriaBuilder.and(accountPredicate, privatePredicate); } else { scopePredicate = criteriaBuilder.equal(vcDefRoot.get(VirtualClusterDefinitionModel_.catalogScope), CatalogScope.PUBLIC); } predicate = criteriaBuilder.and(predicate, scopePredicate); query.multiselect(vcDefRoot, criteriaBuilder.count(vcDefRoot)); query.where(predicate); query.groupBy(vcDefRoot); if (!skipTagsJoin) { query.having(criteriaBuilder.equal(criteriaBuilder.count(vcDefRoot), tags.size())); } List<Tuple> list = getResultList(query, vcDefRoot, paged); List<VirtualClusterDefinitionModel> results = new LinkedList<VirtualClusterDefinitionModel>(); for (Tuple t : list) { results.add((VirtualClusterDefinitionModel) t.get(0)); } return results; }