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

net.ravendb.client.documents.operations.compareExchange.GetCompareExchangeValuesOperation Maven / Gradle / Ivy

package net.ravendb.client.documents.operations.compareExchange;

import net.ravendb.client.documents.IDocumentStore;
import net.ravendb.client.documents.conventions.DocumentConventions;
import net.ravendb.client.documents.operations.IOperation;
import net.ravendb.client.http.HttpCache;
import net.ravendb.client.http.RavenCommand;
import net.ravendb.client.http.ServerNode;
import net.ravendb.client.primitives.Reference;
import net.ravendb.client.util.UrlUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;

import java.io.IOException;
import java.util.Map;

public class GetCompareExchangeValuesOperation implements IOperation>> {

    private final Class _clazz;
    private final String[] _keys;

    private final String _startWith;
    private final Integer _start;
    private final Integer _pageSize;

    private final boolean _materializeMetadata;

    public GetCompareExchangeValuesOperation(Class clazz, String[] keys) {
        this(clazz, keys, true);
    }

    public GetCompareExchangeValuesOperation(Class clazz, String[] keys, boolean materializeMetadata) {
        if (keys == null || keys.length == 0) {
            throw new IllegalArgumentException("Keys cannot be null or empty array");
        }
        _keys = keys;
        _materializeMetadata = materializeMetadata;
        _clazz = clazz;

        _start = null;
        _pageSize = null;
        _startWith = null;
    }

    public GetCompareExchangeValuesOperation(Class clazz, String startWith) {
        this(clazz, startWith, null, null);
    }

    public GetCompareExchangeValuesOperation(Class clazz, String startWith, Integer start) {
        this(clazz, startWith, start, null);
    }

    public GetCompareExchangeValuesOperation(Class clazz, String startWith, Integer start, Integer pageSize) {
        _startWith = startWith;
        _start = start;
        _pageSize = pageSize;
        _clazz = clazz;
        _materializeMetadata = true;

        _keys = null;

    }

    @SuppressWarnings("unchecked")
    @Override
    public RavenCommand>> getCommand(IDocumentStore store, DocumentConventions conventions, HttpCache cache) {
        return new GetCompareExchangeValuesCommand(this, _materializeMetadata, conventions);
    }

    private static class GetCompareExchangeValuesCommand extends RavenCommand>> {
        private final GetCompareExchangeValuesOperation _operation;
        private final boolean _materializeMetadata;
        private final DocumentConventions _conventions;

        @SuppressWarnings("unchecked")
        public GetCompareExchangeValuesCommand(GetCompareExchangeValuesOperation operation, boolean materializeMetadata, DocumentConventions conventions) {
            super((Class>>) (Class)Map.class);
            _operation = operation;
            _materializeMetadata = materializeMetadata;
            _conventions = conventions;
        }

        @Override
        public boolean isReadRequest() {
            return true;
        }

        @Override
        public HttpRequestBase createRequest(ServerNode node, Reference url) {
            StringBuilder pathBuilder = new StringBuilder(node.getUrl());

            pathBuilder.append("/databases/")
                    .append(node.getDatabase())
                    .append("/cmpxchg?");

            if (_operation._keys != null) {
                for (String key : _operation._keys) {
                    pathBuilder.append("&key=").append(UrlUtils.escapeDataString(key));
                }
            } else {
                if (StringUtils.isNotEmpty(_operation._startWith)) {
                    pathBuilder.append("&startsWith=")
                            .append(UrlUtils.escapeDataString(_operation._startWith));
                }

                if (_operation._start != null) {
                    pathBuilder.append("&start=")
                            .append(_operation._start);
                }

                if (_operation._pageSize != null) {
                    pathBuilder.append("&pageSize=")
                            .append(_operation._pageSize);
                }
            }

            url.value = pathBuilder.toString();

            return new HttpGet();
        }

        @Override
        public void setResponse(String response, boolean fromCache) throws IOException {
            result = CompareExchangeValueResultParser.getValues(_operation._clazz, response, _materializeMetadata, _conventions);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy