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

io.bootique.agrest.v5.AgrestModuleExtender Maven / Gradle / Ivy

There is a newer version: 3.0.M2
Show newest version
/*
 * Licensed to ObjectStyle LLC under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ObjectStyle LLC licenses
 * this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.bootique.agrest.v5;

import io.agrest.AgModuleProvider;
import io.agrest.meta.AgEntityOverlay;
import io.bootique.ModuleExtender;
import io.bootique.di.Binder;
import io.bootique.di.SetBuilder;

/**
 * @since 3.0
 */
public class AgrestModuleExtender extends ModuleExtender {

    private SetBuilder moduleProviders;
    private SetBuilder builderCallbacks;
    private SetBuilder entityOverlays;

    public AgrestModuleExtender(Binder binder) {
        super(binder);
    }

    @Override
    public AgrestModuleExtender initAllExtensions() {
        contributeModuleProviders();
        contributeBuilderCallbacks();
        contributeEntityOverlays();
        return this;
    }

    /**
     * Sets the policy for the maximum depth of relationship paths in Agrest, such as includes. Depth is counted from
     * the root of the request. Only non-negative depths are allowed. Zero depth blocks all relationships, "1" -
     * blocks anything beyond direct relationships, and so on. Attribute paths are not counted towards depth
     * (either root or nested).
     *
     * @since 3.0
     */
    public AgrestModuleExtender maxPathDepth(int maxPathDepth) {
        return addBuilderCallback(b -> b.maxPathDepth(maxPathDepth));
    }

    /**
     * Configures Agrest runtime to exclude properties with null values from the JSON responses. If this method is not
     * called, nulls will be rendered.
     *
     * @since 3.0
     */
    public AgrestModuleExtender skipNullProperties() {
        return addBuilderCallback(b -> b.skipNullProperties());
    }

    /**
     * Adds an entity overlay to the Agrest model.
     *
     * @since 3.0
     */
    public AgrestModuleExtender addEntityOverlay(Class> entityOverlayType) {
        contributeEntityOverlays().add(entityOverlayType);
        return this;
    }

    /**
     * Adds an entity overlay to the Agrest model.
     *
     * @since 3.0
     */
    public AgrestModuleExtender addEntityOverlay(AgEntityOverlay entityOverlay) {
        contributeEntityOverlays().addInstance(entityOverlay);
        return this;
    }

    /**
     * @return this extender instance.
     * @since 1.1
     */
    public AgrestModuleExtender addBuilderCallback(AgBuilderCallback callback) {
        contributeBuilderCallbacks().addInstance(callback);
        return this;
    }

    /**
     * @return this extender instance.
     * @since 1.1
     */
    public AgrestModuleExtender addBuilderCallback(Class callbackType) {
        contributeBuilderCallbacks().add(callbackType);
        return this;
    }

    /**
     * @return this extender instance.
     */
    public AgrestModuleExtender addModuleProvider(AgModuleProvider moduleProvider) {
        contributeModuleProviders().addInstance(moduleProvider);
        return this;
    }

    /**
     * @return this extender instance.
     */
    public AgrestModuleExtender addModuleProvider(Class moduleProviderType) {
        contributeModuleProviders().add(moduleProviderType);
        return this;
    }

    private SetBuilder contributeModuleProviders() {
        return moduleProviders != null ? moduleProviders : (moduleProviders = newSet(AgModuleProvider.class));
    }

    private SetBuilder contributeBuilderCallbacks() {
        return builderCallbacks != null ? builderCallbacks : (builderCallbacks = newSet(AgBuilderCallback.class));
    }

    private SetBuilder contributeEntityOverlays() {
        return entityOverlays != null ? entityOverlays : (entityOverlays = newSet(AgEntityOverlay.class));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy