Search in sources :

Example 1 with BeforeDeleteEvent

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;
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) Document(org.bson.Document) BeforeDeleteEvent(org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent) Collation(org.springframework.data.mongodb.core.query.Collation) AfterDeleteEvent(org.springframework.data.mongodb.core.mapping.event.AfterDeleteEvent) WriteConcern(com.mongodb.WriteConcern) MongoCursor(com.mongodb.client.MongoCursor) DeleteResult(com.mongodb.client.result.DeleteResult) DataAccessException(org.springframework.dao.DataAccessException)

Example 2 with BeforeDeleteEvent

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);
        }
    }
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) BasicDBObject(com.mongodb.BasicDBObject) Autowired(org.springframework.beans.factory.annotation.Autowired) LogRepository(com.epam.ta.reportportal.database.dao.LogRepository) BeforeDeleteEvent(org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) AbstractMongoEventListener(org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) DBObject(com.mongodb.DBObject) TestItem(com.epam.ta.reportportal.database.entity.item.TestItem) Service(org.springframework.stereotype.Service) ObjectId(org.bson.types.ObjectId) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) TestItem(com.epam.ta.reportportal.database.entity.item.TestItem)

Example 3 with BeforeDeleteEvent

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);
}
Also used : TestItemRepository(com.epam.ta.reportportal.database.dao.TestItemRepository) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) BasicDBObject(com.mongodb.BasicDBObject) Autowired(org.springframework.beans.factory.annotation.Autowired) BeforeDeleteEvent(org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent) AbstractMongoEventListener(org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) StreamSupport.stream(java.util.stream.StreamSupport.stream) DBObject(com.mongodb.DBObject) Service(org.springframework.stereotype.Service) Launch(com.epam.ta.reportportal.database.entity.Launch) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Launch(com.epam.ta.reportportal.database.entity.Launch)

Aggregations

BeforeDeleteEvent (org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)2 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)2 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)2 AbstractMongoEventListener (org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener)2 Service (org.springframework.stereotype.Service)2 LogRepository (com.epam.ta.reportportal.database.dao.LogRepository)1 TestItemRepository (com.epam.ta.reportportal.database.dao.TestItemRepository)1 Launch (com.epam.ta.reportportal.database.entity.Launch)1 TestItem (com.epam.ta.reportportal.database.entity.item.TestItem)1 MongoException (com.mongodb.MongoException)1 WriteConcern (com.mongodb.WriteConcern)1 MongoCursor (com.mongodb.client.MongoCursor)1 DeleteResult (com.mongodb.client.result.DeleteResult)1 ArrayList (java.util.ArrayList)1