Search in sources :

Example 1 with IDocumentSession

use of net.ravendb.client.documents.session.IDocumentSession in project jnosql-diana-driver by eclipse.

the class RavenDBDocumentCollectionManager method insert.

private void insert(DocumentEntity entity, LocalDateTime time, IDocumentSession session) {
    String collection = entity.getName();
    Map<String, Object> entityMap = EntityConverter.getMap(entity);
    String id = entity.find(EntityConverter.ID_FIELD).map(d -> d.get(String.class)).orElse(collection + '/');
    session.store(entityMap, id);
    IMetadataDictionary metadata = session.advanced().getMetadataFor(entityMap);
    metadata.put(COLLECTION, collection);
    if (Objects.nonNull(time)) {
        metadata.put(EXPIRES, time.toString());
    }
    session.saveChanges();
    entity.add(EntityConverter.ID_FIELD, session.advanced().getDocumentId(entityMap));
}
Also used : LocalDateTime(java.time.LocalDateTime) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) HashMap(java.util.HashMap) RavenException(net.ravendb.client.exceptions.RavenException) DocumentQuery(jakarta.nosql.document.DocumentQuery) Duration(java.time.Duration) Map(java.util.Map) IDocumentSession(net.ravendb.client.documents.session.IDocumentSession) StreamSupport(java.util.stream.StreamSupport) DocumentCollectionManager(jakarta.nosql.document.DocumentCollectionManager) IMetadataDictionary(net.ravendb.client.documents.session.IMetadataDictionary) IEnumerableQuery(net.ravendb.client.documents.session.IEnumerableQuery) DocumentStore(net.ravendb.client.documents.DocumentStore) Document(jakarta.nosql.document.Document) DocumentEntity(jakarta.nosql.document.DocumentEntity) Query(net.ravendb.client.documents.queries.Query) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) IDocumentQuery(net.ravendb.client.documents.session.IDocumentQuery) List(java.util.List) Stream(java.util.stream.Stream) Clock(java.time.Clock) EXPIRES(net.ravendb.client.Constants.Documents.Metadata.EXPIRES) Collections(java.util.Collections) COLLECTION(net.ravendb.client.Constants.Documents.Metadata.COLLECTION) IMetadataDictionary(net.ravendb.client.documents.session.IMetadataDictionary)

Example 2 with IDocumentSession

use of net.ravendb.client.documents.session.IDocumentSession in project jnosql-diana-driver by eclipse.

the class RavenDBDocumentCollectionManager method update.

@Override
public DocumentEntity update(DocumentEntity entity) {
    Objects.requireNonNull(entity, "entity is required");
    try (IDocumentSession session = store.openSession()) {
        Document id = entity.find(EntityConverter.ID_FIELD).orElseThrow(() -> new RavenException("Id is required to Raven Update operation"));
        HashMap<String, Object> map = session.load(HashMap.class, id.get(String.class));
        map.putAll(EntityConverter.getMap(entity));
        session.saveChanges();
    }
    return entity;
}
Also used : RavenException(net.ravendb.client.exceptions.RavenException) IDocumentSession(net.ravendb.client.documents.session.IDocumentSession) Document(jakarta.nosql.document.Document)

Example 3 with IDocumentSession

use of net.ravendb.client.documents.session.IDocumentSession in project jnosql-diana-driver by eclipse.

the class RavenDBDocumentCollectionManager method delete.

@Override
public void delete(DocumentDeleteQuery query) {
    Objects.requireNonNull(query, "query is required");
    try (IDocumentSession session = store.openSession()) {
        Stream<Map> entities = getQueryMaps(new RavenDeleteQuery(query), session);
        entities.map(EntityConverter::getId).forEach(session::delete);
        session.saveChanges();
    }
}
Also used : IDocumentSession(net.ravendb.client.documents.session.IDocumentSession) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with IDocumentSession

use of net.ravendb.client.documents.session.IDocumentSession in project jnosql-diana-driver by eclipse.

the class RavenDBDocumentCollectionManager method insert.

@Override
public DocumentEntity insert(DocumentEntity entity, Duration ttl) {
    Objects.requireNonNull(entity, "entity is required");
    Objects.requireNonNull(ttl, "ttl is required");
    LocalDateTime time = LocalDateTime.now(Clock.systemUTC()).plus(ttl);
    try (IDocumentSession session = store.openSession()) {
        insert(entity, time, session);
    }
    return entity;
}
Also used : LocalDateTime(java.time.LocalDateTime) IDocumentSession(net.ravendb.client.documents.session.IDocumentSession)

Aggregations

IDocumentSession (net.ravendb.client.documents.session.IDocumentSession)4 Document (jakarta.nosql.document.Document)2 LocalDateTime (java.time.LocalDateTime)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RavenException (net.ravendb.client.exceptions.RavenException)2 DocumentCollectionManager (jakarta.nosql.document.DocumentCollectionManager)1 DocumentDeleteQuery (jakarta.nosql.document.DocumentDeleteQuery)1 DocumentEntity (jakarta.nosql.document.DocumentEntity)1 DocumentQuery (jakarta.nosql.document.DocumentQuery)1 Clock (java.time.Clock)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 COLLECTION (net.ravendb.client.Constants.Documents.Metadata.COLLECTION)1 EXPIRES (net.ravendb.client.Constants.Documents.Metadata.EXPIRES)1