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

ioinformarics.oss.jackson.module.jsonld.HydraCollectionBuilder Maven / Gradle / Ivy

The newest version!
package ioinformarics.oss.jackson.module.jsonld;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldProperty;

import java.util.Optional;

/**
 * @author Alexander De Leon
 */
public class HydraCollectionBuilder extends JsonldGraphBuilder {

    private  Long totalItems;
    private  Integer itemsPerPage;
    private  String firstPage;
    private  String nextPage;
    private  String previousPage;
    private  String lastPage;
    private  boolean isPaged = false;


    public HydraCollectionBuilder totalItems(Long totalItems) {
        this.totalItems = totalItems;
        isPaged = true;
        return this;
    }

    public HydraCollectionBuilder itemsPerPage(Integer itemsPerPage) {
        this.itemsPerPage = itemsPerPage;
        isPaged = true;
        return this;
    }

    public HydraCollectionBuilder firstPage(String firstPage) {
        this.firstPage = firstPage;
        isPaged = true;
        return this;
    }

    public HydraCollectionBuilder nextPage(String nextPage) {
        this.nextPage = nextPage;
        isPaged = true;
        return this;
    }

    public HydraCollectionBuilder previousPage(String previousPage) {
        this.previousPage = previousPage;
        isPaged = true;
        return this;
    }

    public HydraCollectionBuilder lastPage(String lastPage) {
        this.lastPage = lastPage;
        isPaged = true;
        return this;
    }

    public JsonldResource build(Iterable elements) {
        return new HydraCollection(elements, buildContext(elements).orElse(null),
                isPaged? "hydra:PagedCollection": "hydra:Collection", graphId, totalItems, itemsPerPage, firstPage, nextPage, previousPage, lastPage);
    }

    protected Optional buildContext(Iterable elements) {
        Optional hydraContext = JsonldContextFactory.fromAnnotations(HydraCollection.CollectionContainer.class);
        Optional mergedContext = hydraContext.map(it -> (ObjectNode)it.setAll(JsonldContextFactory.fromAnnotations(elements).orElse(emptyNode())));
        return JsonldContextFactory.multiContext(Optional.ofNullable(context), mergedContext);
    }

    private ObjectNode emptyNode() {
        return JsonNodeFactory.withExactBigDecimals(true).objectNode();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy