Search in sources :

Example 6 with UpdateResource

use of io.lumeer.engine.api.event.UpdateResource in project engine by Lumeer.

the class MongoCollectionDao method updateCollection.

@Override
public Collection updateCollection(final String id, final Collection collection, final Collection originalCollection, final boolean pushNotification) {
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);
    try {
        Bson update = new Document("$set", collection).append("$inc", new Document(CollectionCodec.VERSION, 1L));
        final Collection updatedCollection = databaseCollection().findOneAndUpdate(idFilter(id), update, options);
        if (updatedCollection == null) {
            throw new StorageException("Collection '" + id + "' has not been updated.");
        }
        if (pushNotification && updateResourceEvent != null) {
            updateResourceEvent.fire(new UpdateResource(updatedCollection, originalCollection));
        }
        return updatedCollection;
    } catch (MongoException ex) {
        throw new StorageException("Cannot update collection: " + collection, ex);
    }
}
Also used : MongoException(com.mongodb.MongoException) UpdateResource(io.lumeer.engine.api.event.UpdateResource) MongoCollection(com.mongodb.client.MongoCollection) Collection(io.lumeer.api.model.Collection) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) StorageException(io.lumeer.storage.api.exception.StorageException) Bson(org.bson.conversions.Bson)

Aggregations

UpdateResource (io.lumeer.engine.api.event.UpdateResource)6 MongoException (com.mongodb.MongoException)4 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)4 StorageException (io.lumeer.storage.api.exception.StorageException)4 Document (org.bson.Document)4 Bson (org.bson.conversions.Bson)4 Collection (io.lumeer.api.model.Collection)3 MongoCollection (com.mongodb.client.MongoCollection)1 Organization (io.lumeer.api.model.Organization)1 Project (io.lumeer.api.model.Project)1 View (io.lumeer.api.model.View)1 RemoveResource (io.lumeer.engine.api.event.RemoveResource)1