Search in sources :

Example 66 with DocumentEntity

use of org.jnosql.diana.api.document.DocumentEntity in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerTest method createSubdocumentList.

private DocumentEntity createSubdocumentList() {
    DocumentEntity entity = DocumentEntity.of("AppointmentBook");
    entity.add(Document.of("_id", new Random().nextInt()));
    List<List<Document>> documents = new ArrayList<>();
    documents.add(asList(Document.of("name", "Ada"), Document.of("type", ContactType.EMAIL), Document.of("information", "ada@lovelace.com")));
    documents.add(asList(Document.of("name", "Ada"), Document.of("type", ContactType.MOBILE), Document.of("information", "11 1231231 123")));
    documents.add(asList(Document.of("name", "Ada"), Document.of("type", ContactType.PHONE), Document.of("information", "phone")));
    entity.add(Document.of("contacts", documents));
    return entity;
}
Also used : DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Random(java.util.Random) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List)

Example 67 with DocumentEntity

use of org.jnosql.diana.api.document.DocumentEntity in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerTest method shouldSaveSubDocument.

@Test
public void shouldSaveSubDocument() {
    DocumentEntity entity = getEntity();
    entity.add(Document.of("phones", Document.of("mobile", "1231231")));
    DocumentEntity entitySaved = entityManager.insert(entity);
    Document id = entitySaved.find("_id").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where("_id").eq(id.get()).build();
    DocumentEntity entityFound = entityManager.select(query).get(0);
    Document subDocument = entityFound.find("phones").get();
    List<Document> documents = subDocument.get(new TypeReference<List<Document>>() {
    });
    assertThat(documents, contains(Document.of("mobile", "1231231")));
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Example 68 with DocumentEntity

use of org.jnosql.diana.api.document.DocumentEntity in project jnosql-diana-driver by eclipse.

the class LiveQueryLIstener method onLiveResult.

@Override
public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
    ODocument oDocument = (ODocument) iOp.getRecord();
    DocumentEntity entity = OrientDBConverter.convert(oDocument);
    switch(iOp.type) {
        case CREATED:
            callbacks.getCreateCallback().ifPresent(callback -> callback.accept(entity));
            return;
        case UPDATED:
            callbacks.getUpdateCallback().ifPresent(callback -> callback.accept(entity));
            return;
        case DELETED:
            callbacks.getDeleteCallback().ifPresent(callback -> callback.accept(entity));
            return;
        default:
    }
}
Also used : DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 69 with DocumentEntity

use of org.jnosql.diana.api.document.DocumentEntity in project jnosql-diana-driver by eclipse.

the class OrientDBConverter method convert.

static List<DocumentEntity> convert(List<ODocument> result) {
    List<DocumentEntity> entities = new ArrayList<>();
    for (ODocument document : result) {
        DocumentEntity entity = convert(document);
        entities.add(entity);
    }
    return entities;
}
Also used : DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ArrayList(java.util.ArrayList) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 70 with DocumentEntity

use of org.jnosql.diana.api.document.DocumentEntity in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerAsync method select.

@Override
public void select(DocumentQuery query, Consumer<List<DocumentEntity>> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException {
    String collectionName = query.getDocumentCollection();
    MongoCollection<Document> collection = asyncMongoDatabase.getCollection(collectionName);
    Bson mongoDBQuery = query.getCondition().map(DocumentQueryConversor::convert).orElse(EMPTY);
    List<DocumentEntity> entities = new CopyOnWriteArrayList<>();
    FindIterable<Document> result = collection.find(mongoDBQuery);
    Block<Document> documentBlock = d -> entities.add(createEntity(collectionName, d));
    SingleResultCallback<Void> voidSingleResultCallback = (v, e) -> callBack.accept(entities);
    result.forEach(documentBlock, voidSingleResultCallback);
}
Also used : FindIterable(com.mongodb.async.client.FindIterable) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) DocumentCollectionManagerAsync(org.jnosql.diana.api.document.DocumentCollectionManagerAsync) MongoCollection(com.mongodb.async.client.MongoCollection) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) BsonDocument(org.bson.BsonDocument) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Bson(org.bson.conversions.Bson) Consumer(java.util.function.Consumer) List(java.util.List) Documents(org.jnosql.diana.api.document.Documents) Block(com.mongodb.Block) MongoDatabase(com.mongodb.async.client.MongoDatabase) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) DeleteResult(com.mongodb.client.result.DeleteResult) ID_FIELD(org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD) SingleResultCallback(com.mongodb.async.SingleResultCallback) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)205 Test (org.junit.jupiter.api.Test)167 Document (org.jnosql.diana.api.document.Document)107 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)101 List (java.util.List)63 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)47 Arrays.asList (java.util.Arrays.asList)38 ArrayList (java.util.ArrayList)36 AtomicReference (java.util.concurrent.atomic.AtomicReference)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 Duration (java.time.Duration)22 HashMap (java.util.HashMap)21 Consumer (java.util.function.Consumer)19 Arrays (java.util.Arrays)17 Collectors (java.util.stream.Collectors)17 DocumentQueryBuilder.select (org.jnosql.diana.api.document.query.DocumentQueryBuilder.select)17 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)16 Map (java.util.Map)15 Optional (java.util.Optional)15 BeforeEach (org.junit.jupiter.api.BeforeEach)15