private static Criteria andOperatorSafe(Criteria criteria1, Criteria criteria2) { if(criteria1 == null && criteria2 == null) { return null; } if(criteria1 == null) { return criteria2; } if(criteria2 == null) { return criteria1; } return criteria1.andOperator(criteria2); }
private Criteria andSafeCriterion(Criteria crit_1, Criteria crit_2) { if(crit_1 == null) { return crit_2; } else if(crit_2 == null) { return crit_1; } else { return new Criteria().andOperator(crit_1, crit_2); } }
@Override protected Criteria visit(AndNode node) { Criteria criteria = new Criteria(); List<Criteria> children = node.getChildren().stream() .map(this::visitAny).collect(Collectors.toList()); return criteria.andOperator(children.toArray(new Criteria[children.size()])); }
@Override public Iterable<RuleEntity> search(String search) { Collection<String> terms = new HashSet<>(Arrays.asList(search.split(" "))); Criteria[] criterias = terms .stream() .map(s -> Criteria.where("searchIndex").regex(s.toLowerCase(), "iu")) .toArray(Criteria[]::new); return mongoTemplate.find(Query.query(new Criteria().andOperator(criterias)).with(Sort.by("order")), RuleEntity.class); } }
@Override public List<String> findBinaryIdsByLogRefs(List<String> ids) { Aggregation aggregation = newAggregation(match(where("id").in(ids).andOperator(where("binaryContent").exists(true))), group("binaryContent.binaryDataId", "binaryContent.thumbnailId")); AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, Log.class, Map.class); return results.getMappedResults().stream().flatMap(it -> (Stream<String>) it.values().stream()).collect(toList()); }
@Override public List<String> findBinaryIdsByItemRefs(List<String> ids) { Aggregation aggregation = newAggregation(match(where("testItemRef").in(ids).andOperator(where("binaryContent").exists(true))), group("binaryContent.binaryDataId", "binaryContent.thumbnailId")); AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, Log.class, Map.class); return results.getMappedResults().stream().flatMap(it -> (Stream<String>) it.values().stream()).collect(toList()); }
protected Query buildQuery(final String paramName, final Collection<String> values, final Map<String, Object> filterParams) { Criteria queryCriteria = Criteria.where(paramName).in(values); if (!CollectionUtils.isEmpty(filterParams)) { final Criteria[] aCriteria = buildAndParamsCriteria(filterParams); queryCriteria = queryCriteria.andOperator(aCriteria); } return new Query(queryCriteria); }
private List<InternalAlert> getInternalAlerts() { LOGGER.debug("Searching active internal alerts ...."); final Criteria typeCriteria = Criteria.where("type").is(AlertType.INTERNAL.name()); final Criteria activeCriteria = Criteria.where("active").is(Boolean.TRUE); final Query query = new Query(typeCriteria.andOperator(activeCriteria)); final List<InternalAlert> content = mongoOps.find(query, InternalAlert.class, "alert"); LOGGER.debug("... and found {} alerts to monitor.", content.size()); return content; }
@Override public long countNewNotifications(String userId) { if (userId == null) { throw new ObservationException("User identifier cannot be null."); } Criteria criteria = Criteria.where(QUERY_PARAM_NOTIFICATION_USER_ID).is(userId); criteria.andOperator(Criteria.where(QUERY_PARAM_NOTIFICATION_IS_NEW).is(true)); Query query = new Query(criteria); long count = mongoTemplate.count(query, mongoCollectionName); return count; }
/** * Create criteria for loading owned entities and shared to specified project entities * * @param projectName * @param userName * @return */ private Criteria getAllEntitiesCriteria(String projectName, String userName) { return new Criteria().orOperator(new Criteria().andOperator(Criteria.where("acl.entries.projectId").is(projectName), Criteria.where("acl.entries.permissions").is(AclPermissions.READ.name()) ), new Criteria().andOperator(Criteria.where("acl.ownerUserId").is(userName), Criteria.where("projectName").is(projectName))); } }
@Override public List<TestItem> findTestItemWithIssues(String launchId) { Criteria externalIssues = new Criteria().andOperator(where(LAUNCH_REFERENCE).is(launchId), where(ISSUE).exists(true)); Query query = query(externalIssues); return mongoTemplate.find(query, TestItem.class); }
private ElementCollection getElementCollectionForUser(String collectionId, String userId) { checkElementCollection(collectionId); Criteria criteriaForPublic = Criteria.where(F_COLLECTION_ID).is(collectionId) .andOperator(Criteria.where(F_COLLECTION_VISIBILITY).is(CollectionVisibility.PUBLIC.name())); Query query = new Query(criteriaForPublic); ElementCollection item = mongoTemplate.findOne(query, ElementCollection.class, mongoCollectionName); if (item == null && userId != null) { Criteria criteriaForUser = Criteria.where(F_COLLECTION_ID).is(collectionId) .andOperator(Criteria.where(F_USERS_ID).is(userId)); query = new Query(criteriaForUser); item = mongoTemplate.findOne(query, ElementCollection.class, mongoCollectionName); } return item; }
@Override @RequiresServiceRole(roleName="READ") public CollectionData fetchSpecialCollection(String userId, String type) { Query query = new Query(Criteria.where(F_USERS_ID).is(userId).andOperator(Criteria.where(F_COLLECTION_TYPE).is(type))); ElementCollection elementCollection = mongoTemplate.findOne(query, ElementCollection.class, mongoCollectionName); return convertToCollectionData(elementCollection); }
@Override public void removeCriteriaByObjectId(String userId, String objectId) { if (userId == null) { throw new ObservationException("User identifier cannot be null."); } if (objectId == null) { throw new ObservationException("Object identifier cannot be null."); } Criteria criteria = Criteria.where(QUERY_PARAM_USER_ID).is(userId); criteria.andOperator(Criteria.where(QUERY_PARAM_OBJECT_ID).is(objectId)); Query query = new Query(criteria); mongoTemplate.remove(query, mongoCollectionName); }
private Criteria precedingByModificationDateAndIdCriterion(Date date, String id) { Criteria criteria = new Criteria() .orOperator( Criteria.where(PROPERTY_MODIFICATION_DATE).lt(date), new Criteria().andOperator( Criteria.where(PROPERTY_MODIFICATION_DATE).is(date), Criteria.where(F_ID).lt(id) ) ); return criteria; }
@Override @RequiresServiceRole(roleName="READ") public List<CollectionWithDocumentData> listPublicCollectionsWithElement(String elementId) { Criteria criteria = Criteria.where(F_ATTACHED_DOCUMENT_ID).is(elementId).andOperator( Criteria.where(F_COLLECTION_VISIBILITY).is(CollectionVisibility.PUBLIC.name())); Query query = new Query(criteria); List<ElementCollection> items = mongoTemplate.find(query, ElementCollection.class, mongoCollectionName); return convertWithDocument(items, elementId); }
private Criteria followingByModificationDateAndIdCriterion(Date date, String id) { Criteria criteria = new Criteria() .orOperator( Criteria.where(PROPERTY_MODIFICATION_DATE).gt(date), new Criteria().andOperator( Criteria.where(PROPERTY_MODIFICATION_DATE).is(date), Criteria.where(F_ID).gt(id) ) ); return criteria; }
@Override public List<UserFilter> findAvailableFilters(String projectName, String[] ids, String userName) { //where ID from provided array AND it's shared on project Query q = Query.query(where(ID).in(Arrays.asList(ids)) .andOperator(new Criteria() .orOperator( where(OWNER).is(userName), where(PROJECT).is(projectName), where(ENTRIES).elemMatch(where("projectId").is(projectName))))); return mongoTemplate.find(q, UserFilter.class); } }
private ElementCollection getElementCollectionForEditor(String collectionId, String userId) { checkElementCollection(collectionId); Query query = new Query(Criteria .where(F_COLLECTION_ID) .is(collectionId) .andOperator(Criteria.where(F_USERS_ID).is(userId), Criteria.where(F_USERS_ROLE).in(CollectionRole.EDITOR.name(), CollectionRole.CREATOR.name()))); ElementCollection collection = mongoTemplate.findOne(query, ElementCollection.class, mongoCollectionName); return collection; }
public String getSensorType(final String provider, final String sensor) { final String cacheKey = provider + "#" + sensor; if (sensorTypesCache.get(cacheKey) == null) { final Criteria queryCriteria = Criteria.where("sensorId").is(sensor); queryCriteria.andOperator(Criteria.where("providerId").is(provider)); final Query query = new Query(queryCriteria); query.fields().include("type"); sensorTypesCache.put(cacheKey, mongoOperations.findOne(query, CatalogAdditionalFields.class, "sensor").getType()); } return sensorTypesCache.get(cacheKey); }