use of org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent in project spring-data-mongodb by spring-projects.
the class MongoTemplate method doRemove.
protected <T> DeleteResult doRemove(final String collectionName, final Query query, @Nullable final Class<T> entityClass) {
Assert.notNull(query, "Query must not be null!");
Assert.hasText(collectionName, "Collection name must not be null or empty!");
final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
final Document queryObject = queryMapper.getMappedObject(query.getQueryObject(), entity);
return execute(collectionName, new CollectionCallback<DeleteResult>() {
public DeleteResult doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
maybeEmitEvent(new BeforeDeleteEvent<T>(queryObject, entityClass, collectionName));
Document removeQuery = queryObject;
DeleteOptions options = new DeleteOptions();
query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName, entityClass, null, queryObject);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
DeleteResult dr = null;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Remove using query: {} in collection: {}.", new Object[] { serializeToJsonSafely(removeQuery), collectionName });
}
if (query.getLimit() > 0 || query.getSkip() > 0) {
MongoCursor<Document> cursor = new QueryCursorPreparer(query, entityClass).prepare(collection.find(removeQuery).projection(new Document(ID_FIELD, 1))).iterator();
Set<Object> ids = new LinkedHashSet<>();
while (cursor.hasNext()) {
ids.add(cursor.next().get(ID_FIELD));
}
removeQuery = new Document(ID_FIELD, new Document("$in", ids));
}
if (writeConcernToUse == null) {
dr = collection.deleteMany(removeQuery, options);
} else {
dr = collection.withWriteConcern(writeConcernToUse).deleteMany(removeQuery, options);
}
maybeEmitEvent(new AfterDeleteEvent<T>(queryObject, entityClass, collectionName));
return dr;
}
});
}
use of org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent in project commons-dao by reportportal.
the class DeleteItemsListener method onBeforeDelete.
@Override
public void onBeforeDelete(BeforeDeleteEvent<TestItem> event) {
DBObject dbqo = queryMapper.getMappedObject(event.getDBObject(), mappingContext.getPersistentEntity(TestItem.class));
for (DBObject dbObject : mongoTemplate.getCollection(event.getCollectionName()).find(dbqo)) {
Boolean isRetryProcessed = (Boolean) dbObject.get("retryProcessed");
if (isRetryProcessed == null || isRetryProcessed) {
String objectId = dbObject.get("_id").toString();
List<TestItem> itemsForDelete = mongoTemplate.find(queryItems(objectId), TestItem.class);
List<ObjectId> objectIds = itemsForDelete.stream().map(it -> new ObjectId(it.getId())).collect(toList());
BasicDBObject query = new BasicDBObject("_id", new BasicDBObject("$in", objectIds));
mongoTemplate.getCollection(event.getCollectionName()).remove(query);
List<String> itemRefs = getLogItemReferences(itemsForDelete);
logRepository.deleteByItemRef(itemRefs);
}
}
}
use of org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent in project commons-dao by reportportal.
the class DeleteLaunchesListener method onBeforeDelete.
@Override
public void onBeforeDelete(BeforeDeleteEvent<Launch> event) {
DBObject dboq = queryMapper.getMappedObject(event.getDBObject(), mappingContext.getPersistentEntity(Launch.class));
List<String> ids = stream(mongoTemplate.getCollection(event.getCollectionName()).find(dboq).spliterator(), false).map(dbObject -> dbObject.get("_id").toString()).distinct().collect(toList());
final BasicDBObject itemsQuery = new BasicDBObject("parent", new BasicDBObject("$exists", false));
itemsQuery.put("launchRef", new BasicDBObject("$in", ids));
final List<String> itemIds = stream(mongoTemplate.getCollection("testItem").find(itemsQuery).spliterator(), false).map(dbObject -> dbObject.get("_id").toString()).collect(toList());
testItemRepository.delete(itemIds);
}
Aggregations