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

com.github.davidmoten.odata.client.CollectionPageEntityRequest Maven / Gradle / Ivy

package com.github.davidmoten.odata.client;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.github.davidmoten.odata.client.internal.RequestHelper;

@JsonIgnoreType
public class CollectionPageEntityRequest> implements Iterable {

    private final ContextPath contextPath;
    private final Class cls;
    private final EntityRequestFactory entityRequestFactory;
    private final SchemaInfo schemaInfo;

    // should not be public api
    public CollectionPageEntityRequest(ContextPath contextPath, Class cls,
            EntityRequestFactory entityRequestFactory, SchemaInfo schemaInfo) {
        this.contextPath = contextPath;
        this.entityRequestFactory = entityRequestFactory;
        this.cls = cls;
        this.schemaInfo = schemaInfo;
    }

    CollectionPage get(CollectionRequestOptions options) {
        ContextPath cp = contextPath.addQueries(options.getQueries());
        List h = RequestHelper.cleanAndSupplementRequestHeaders(options, "minimal",
                false);
        HttpResponse r = cp.context().service().get(options.getUrlOverride().orElse(cp.toUrl()), h);
        RequestHelper.checkResponseCode(cp, r, 200, 299);
        return cp.context().serializer().deserializeCollectionPage(r.getText(), cls, cp,
                schemaInfo, h);
    }

    T post(CollectionRequestOptions options, T entity) {
        return RequestHelper.post(entity, contextPath, cls, options, schemaInfo);
    }

    public R id(String id) {
        return entityRequestFactory.create(contextPath.addKeys(new NameValue(id)));
    }

    public CollectionPage get() {
        return new CollectionEntityRequestOptionsBuilder(this).get();
    }
    
    @Override
    public Iterator iterator() {
        return get().iterator();
    }
    
    public Stream stream() {
        return get().stream();
    }
    
    public List toList() {
        return get().toList();
    }

    public T post(T entity) {
        return new CollectionEntityRequestOptionsBuilder(this).post(entity);
    }
    
    /**
     * Returns a request for only those members of the collection that are of the
     * requested type. This is referred to in the OData
     * 4.01 specification as a "restriction to instances of the derived type".
     * 
     * @param 
     *            the type ("derived type") to be restricting to
     * @param cls
     *            the Class of the type to restrict to
     * @return a request for a collection of instances with the given type
     */
    @SuppressWarnings("unchecked")
    public  CollectionPageEntityRequest> filter(Class cls) {
        return new CollectionPageEntityRequest>(contextPath.addSegment(odataTypeName(cls)), cls,
                (EntityRequestFactory>) entityRequestFactory, schemaInfo);
    }
    
    private static  String odataTypeName(Class cls) {
        try {
            Constructor constructor = cls.getDeclaredConstructor();
            constructor.setAccessible(true);
            T o = constructor.newInstance();
            return o.odataTypeName();
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
                | NoSuchMethodException | SecurityException e) {
            throw new ClientException(e);
        }
    }

    public CollectionEntityRequestOptionsBuilder requestHeader(String key, String value) {
        return new CollectionEntityRequestOptionsBuilder(this).requestHeader(key, value);
    }
    
    public CollectionEntityRequestOptionsBuilder requestHeader(RequestHeader header) {
        return new CollectionEntityRequestOptionsBuilder(this).requestHeader(header);
    }

    public CollectionEntityRequestOptionsBuilder maxPageSize(int size) {
        return new CollectionEntityRequestOptionsBuilder(this).maxPageSize(size);
    }

    public CollectionEntityRequestOptionsBuilder search(String clause) {
        return new CollectionEntityRequestOptionsBuilder(this).search(clause);
    }

    public CollectionEntityRequestOptionsBuilder expand(String clause) {
        return new CollectionEntityRequestOptionsBuilder(this).expand(clause);
    }

    public CollectionEntityRequestOptionsBuilder filter(String clause) {
        return new CollectionEntityRequestOptionsBuilder(this).filter(clause);
    }

    public CollectionEntityRequestOptionsBuilder orderBy(String clause) {
        return new CollectionEntityRequestOptionsBuilder(this).orderBy(clause);
    }

    public CollectionEntityRequestOptionsBuilder skip(long n) {
        return new CollectionEntityRequestOptionsBuilder(this).skip(n);
    }

    public CollectionEntityRequestOptionsBuilder top(long n) {
        return new CollectionEntityRequestOptionsBuilder(this).top(n);
    }

    public CollectionEntityRequestOptionsBuilder select(String clause) {
        return new CollectionEntityRequestOptionsBuilder(this).select(clause);
    }

    public CollectionEntityRequestOptionsBuilder metadataFull() {
        return new CollectionEntityRequestOptionsBuilder(this).metadataFull();
    }

    public CollectionEntityRequestOptionsBuilder metadataMinimal() {
        return new CollectionEntityRequestOptionsBuilder(this).metadataMinimal();
    }

    public CollectionEntityRequestOptionsBuilder metadataNone() {
        return new CollectionEntityRequestOptionsBuilder(this).metadataNone();
    }
    
    public CollectionEntityRequestOptionsBuilder urlOverride(String urlOverride) {
        return new CollectionEntityRequestOptionsBuilder(this).urlOverride(urlOverride);
    }

}