Search in sources :

Example 46 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class DocumentFacade method updateDocumentData.

public Document updateDocumentData(String collectionId, String documentId, DataDocument data) {
    Collection collection = collectionDao.getCollectionById(collectionId);
    permissionsChecker.checkRole(collection, Role.WRITE);
    DataDocument oldData = dataDao.getData(collectionId, documentId);
    Set<String> attributesToAdd = new HashSet<>(data.keySet());
    attributesToAdd.removeAll(oldData.keySet());
    Set<String> attributesToDec = new HashSet<>(oldData.keySet());
    attributesToDec.removeAll(data.keySet());
    updateCollectionMetadata(collection, attributesToAdd, attributesToDec, 0);
    // TODO archive the old document
    DataDocument updatedData = dataDao.updateData(collection.getId(), documentId, data);
    Document updatedDocument = updateDocument(collection, documentId);
    updatedDocument.setData(updatedData);
    return updatedDocument;
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Collection(io.lumeer.api.model.Collection) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 47 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class ImportFacade method createDocumentFromRow.

private Document createDocumentFromRow(String[] headers, String[] row, int[] counts) {
    final DataDocument d = new DataDocument();
    for (int i = 0; i < Math.min(headers.length, row.length); i++) {
        if (row[i] != null) {
            d.append(headers[i], row[i]);
            counts[i]++;
        }
    }
    return new JsonDocument(d);
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) JsonDocument(io.lumeer.api.dto.JsonDocument)

Example 48 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class MongoDbStorage method createOldDocument.

@Override
public void createOldDocument(final String collectionName, final DataDocument dataDocument, final String documentId, final int version) throws UnsuccessfulOperationException {
    Document doc = new Document(dataDocument);
    doc.put(DOCUMENT_ID, new BasicDBObject(DOCUMENT_ID, new ObjectId(documentId)));
    try {
        database.getCollection(collectionName).insertOne(doc);
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            throw new UnsuccessfulOperationException(e.getMessage(), e.getCause());
        } else {
            throw e;
        }
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) UnsuccessfulOperationException(io.lumeer.engine.api.exception.UnsuccessfulOperationException) MongoWriteException(com.mongodb.MongoWriteException) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) BsonDocument(org.bson.BsonDocument)

Example 49 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class MongoDbStorage method replaceDocument.

@Override
public void replaceDocument(final String collectionName, final DataDocument replaceDocument, final DataFilter filter) {
    DataDocument toReplace = new DataDocument(replaceDocument);
    if (toReplace.containsKey(DOCUMENT_ID)) {
        toReplace.remove(DOCUMENT_ID);
    }
    Document replaceDoc = new Document(toReplace);
    database.getCollection(collectionName).replaceOne(filter.<Bson>get(), replaceDoc, new UpdateOptions().upsert(true));
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) BsonDocument(org.bson.BsonDocument) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions)

Example 50 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class MongoDbStorage method aggregate.

@Override
public List<DataDocument> aggregate(final String collectionName, final DataDocument... stages) {
    if (stages == null || stages.length == 0) {
        return Collections.emptyList();
    }
    final List<DataDocument> result = new LinkedList<>();
    final List<Document> documents = new LinkedList<>();
    for (final DataDocument d : stages) {
        documents.add(MongoUtils.dataDocumentToDocument(d));
    }
    AggregateIterable<Document> resultDocuments = database.getCollection(collectionName).aggregate(documents);
    resultDocuments.into(new LinkedList<>()).forEach(d -> {
        result.add(MongoUtils.convertDocument(d));
    });
    return result;
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) BsonDocument(org.bson.BsonDocument) LinkedList(java.util.LinkedList)

Aggregations

DataDocument (io.lumeer.engine.api.data.DataDocument)75 Test (org.junit.Test)42 Collection (io.lumeer.api.model.Collection)16 Document (io.lumeer.api.model.Document)16 JsonDocument (io.lumeer.api.dto.JsonDocument)14 Document (org.bson.Document)13 SoftAssertions (org.assertj.core.api.SoftAssertions)10 ReturnDocument (com.mongodb.client.model.ReturnDocument)9 JsonCollection (io.lumeer.api.dto.JsonCollection)9 DataFilter (io.lumeer.engine.api.data.DataFilter)9 ImportedCollection (io.lumeer.api.model.ImportedCollection)6 SearchQuery (io.lumeer.storage.api.query.SearchQuery)6 LocalDateTime (java.time.LocalDateTime)6 BsonDocument (org.bson.BsonDocument)6 LinkedList (java.util.LinkedList)5 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Response (javax.ws.rs.core.Response)4 DataStorageStats (io.lumeer.engine.api.data.DataStorageStats)3