All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ravendb.client.documents.session.operations.lazy.LazySessionOperations Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
package net.ravendb.client.documents.session.operations.lazy;

import net.ravendb.client.documents.Lazy;
import net.ravendb.client.documents.session.ConditionalLoadResult;
import net.ravendb.client.documents.session.DocumentSession;
import net.ravendb.client.documents.session.loaders.ILazyLoaderWithInclude;
import net.ravendb.client.documents.session.loaders.LazyMultiLoaderWithInclude;
import net.ravendb.client.documents.session.operations.LoadOperation;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.function.Consumer;

public class LazySessionOperations implements ILazySessionOperations {

    protected final DocumentSession delegate;

    public LazySessionOperations(DocumentSession delegate) {
        this.delegate = delegate;
    }

    @Override
    public ILazyLoaderWithInclude include(String path) {
        return new LazyMultiLoaderWithInclude(delegate).include(path);
    }

    @Override
    public  Lazy load(Class clazz, String id) {
        return load(clazz, id, null);
    }

    @Override
    public  Lazy load(Class clazz, String id, Consumer onEval) {
        if (delegate.isLoaded(id)) {
            return new Lazy<>(() -> delegate.load(clazz, id));
        }

        LazyLoadOperation lazyLoadOperation = new LazyLoadOperation<>(clazz, delegate, new LoadOperation(delegate).byId(id)).byId(id);
        return delegate.addLazyOperation(clazz, lazyLoadOperation, onEval);
    }

    @Override
    public  Lazy> loadStartingWith(Class clazz, String idPrefix) {
        return loadStartingWith(clazz, idPrefix, null, 0, 25, null, null);
    }

    @Override
    public  Lazy> loadStartingWith(Class clazz, String idPrefix, String matches) {
        return loadStartingWith(clazz, idPrefix, matches, 0, 25, null, null);
    }

    @Override
    public  Lazy> loadStartingWith(Class clazz, String idPrefix, String matches, int start) {
        return loadStartingWith(clazz, idPrefix, matches, start, 25, null, null);
    }

    @Override
    public  Lazy> loadStartingWith(Class clazz, String idPrefix, String matches, int start, int pageSize) {
        return loadStartingWith(clazz, idPrefix, matches, start, pageSize, null, null);
    }

    @Override
    public  Lazy> loadStartingWith(Class clazz, String idPrefix, String matches, int start, int pageSize, String exclude) {
        return loadStartingWith(clazz, idPrefix, matches, start, pageSize, exclude, null);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Lazy> loadStartingWith(Class clazz, String idPrefix, String matches, int start, int pageSize, String exclude, String startAfter) {
        LazyStartsWithOperation operation = new LazyStartsWithOperation<>(clazz, idPrefix, matches, exclude, start, pageSize, delegate, startAfter);

        return delegate.addLazyOperation((Class>)(Class)Map.class, operation, null);
    }

    @Override
    public  Lazy> load(Class clazz, Collection ids) {
        return load(clazz, ids, null);
    }

    @Override
    public  Lazy> load(Class clazz, Collection ids, Consumer> onEval) {
        return delegate.lazyLoadInternal(clazz, ids.toArray(new String[0]), new String[0], onEval);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Lazy> conditionalLoad(Class clazz, String id, String changeVector) {
        if (StringUtils.isEmpty(id)) {
            throw new IllegalArgumentException("Id cannot be null");
        }

        if (delegate.isLoaded(id)) {
            return new Lazy<>(() -> {
                TResult entity = delegate.load(clazz, id);
                if (entity == null) {
                    return ConditionalLoadResult.create((TResult) null, null);
                }
                String cv = delegate.advanced().getChangeVectorFor(entity);
                return ConditionalLoadResult.create(entity, cv);
            });
        }

        if (StringUtils.isEmpty(changeVector)) {
            throw new IllegalArgumentException("The requested document with id '"
                    + id + "' is not loaded into the session and could not conditional load when changeVector is null or empty.");
        }

        LazyConditionalLoadOperation lazyLoadOperation = new LazyConditionalLoadOperation<>(clazz, id, changeVector, delegate);
        return delegate.addLazyOperation((Class>)(Class)ConditionalLoadResult.class, lazyLoadOperation, null);
    }

    //TBD expr ILazyLoaderWithInclude ILazySessionOperations.Include(Expression> path)
    //TBD expr ILazyLoaderWithInclude ILazySessionOperations.Include(Expression>> path)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy