Search in sources :

Example 1 with Document

use of io.lumeer.api.model.Document in project engine by Lumeer.

the class DocumentFacade method getDocument.

public Document getDocument(String collectionId, String documentId) {
    Collection collection = collectionDao.getCollectionById(collectionId);
    permissionsChecker.checkRole(collection, Role.READ);
    Document document = documentDao.getDocumentById(documentId);
    DataDocument data = dataDao.getData(collection.getId(), documentId);
    document.setData(data);
    return document;
}
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)

Example 2 with Document

use of io.lumeer.api.model.Document in project engine by Lumeer.

the class DocumentFacade method createDocument.

public Document createDocument(String collectionId, Document document) {
    Collection collection = collectionDao.getCollectionById(collectionId);
    permissionsChecker.checkRole(collection, Role.WRITE);
    DataDocument data = DocumentUtils.checkDocumentKeysValidity(document.getData());
    Document storedDocument = createDocument(collection, document);
    DataDocument storedData = dataDao.createData(collection.getId(), storedDocument.getId(), data);
    storedDocument.setData(storedData);
    updateCollectionMetadata(collection, data.keySet(), Collections.emptySet(), 1);
    return storedDocument;
}
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)

Example 3 with Document

use of io.lumeer.api.model.Document in project engine by Lumeer.

the class DocumentFacade method updateDocument.

private Document updateDocument(Collection collection, String documentId) {
    Document document = documentDao.getDocumentById(documentId);
    document.setCollectionId(collection.getId());
    document.setUpdatedBy(authenticatedUser.getCurrentUsername());
    document.setUpdateDate(LocalDateTime.now());
    document.setDataVersion(document.getDataVersion() + 1);
    return documentDao.updateDocument(document.getId(), document);
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document)

Example 4 with Document

use of io.lumeer.api.model.Document in project engine by Lumeer.

the class DocumentFacade method patchDocumentData.

public Document patchDocumentData(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());
    updateCollectionMetadata(collection, attributesToAdd, Collections.emptySet(), 0);
    // TODO archive the old document
    DataDocument patchedData = dataDao.patchData(collection.getId(), documentId, data);
    Document updatedDocument = updateDocument(collection, documentId);
    updatedDocument.setData(patchedData);
    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 5 with Document

use of io.lumeer.api.model.Document in project engine by Lumeer.

the class ImportFacade method parseCSVFile.

private void parseCSVFile(Collection collection, String data) {
    if (data == null || data.trim().isEmpty()) {
        return;
    }
    CsvParserSettings settings = new CsvParserSettings();
    settings.detectFormatAutomatically();
    settings.setHeaderExtractionEnabled(true);
    CsvParser parser = new CsvParser(settings);
    parser.beginParsing(new StringReader(data));
    String[] headers = Arrays.stream(parser.getRecordMetadata().headers()).filter(Objects::nonNull).toArray(String[]::new);
    if (headers.length == 0) {
        return;
    }
    int[] counts = new int[headers.length];
    int documentsCount = 0;
    List<Document> documents = new ArrayList<>();
    String[] row;
    while ((row = parser.parseNext()) != null) {
        Document d = createDocumentFromRow(headers, row, counts);
        addDocumentMetadata(collection, d);
        documents.add(d);
        if (documents.size() >= MAX_PARSED_DOCUMENTS) {
            addDocumentsToDb(collection.getId(), documents);
            documents.clear();
        }
        documentsCount++;
    }
    if (!documents.isEmpty()) {
        addDocumentsToDb(collection.getId(), documents);
    }
    parser.stopParsing();
    addCollectionMetadata(collection, headers, counts, documentsCount);
}
Also used : CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) CsvParser(com.univocity.parsers.csv.CsvParser) DataDocument(io.lumeer.engine.api.data.DataDocument) JsonDocument(io.lumeer.api.dto.JsonDocument) Document(io.lumeer.api.model.Document)

Aggregations

Document (io.lumeer.api.model.Document)32 DataDocument (io.lumeer.engine.api.data.DataDocument)31 JsonDocument (io.lumeer.api.dto.JsonDocument)21 Test (org.junit.Test)18 SoftAssertions (org.assertj.core.api.SoftAssertions)10 Collection (io.lumeer.api.model.Collection)7 LocalDateTime (java.time.LocalDateTime)7 MorphiaDocument (io.lumeer.storage.mongodb.model.MorphiaDocument)6 JsonQuery (io.lumeer.api.dto.JsonQuery)5 HashSet (java.util.HashSet)4 ObjectId (org.bson.types.ObjectId)4 JsonCollection (io.lumeer.api.dto.JsonCollection)3 Entity (javax.ws.rs.client.Entity)3 Response (javax.ws.rs.core.Response)3 Pagination (io.lumeer.api.model.Pagination)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 WriteResult (com.mongodb.WriteResult)1 CsvParser (com.univocity.parsers.csv.CsvParser)1 CsvParserSettings (com.univocity.parsers.csv.CsvParserSettings)1