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

com.redhat.lightblue.assoc.ep.Search Maven / Gradle / Ivy

There is a newer version: 2.18.0
Show newest version
/*
 Copyright 2013 Red Hat, Inc. and/or its affiliates.

 This file is part of lightblue.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see .
 */
package com.redhat.lightblue.assoc.ep;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redhat.lightblue.mediator.OperationContext;
import com.redhat.lightblue.mediator.SimpleFindImpl;

import com.redhat.lightblue.crud.CRUDFindRequest;
import com.redhat.lightblue.crud.DocCtx;
import com.redhat.lightblue.crud.DocumentStream;
import com.redhat.lightblue.crud.ListDocumentStream;

import com.redhat.lightblue.util.JsonDoc;

/**
 * Performs search
 *
 * Input: n/a Output: ResultDocument
 */
public class Search extends AbstractSearchStep {

    private static final Logger LOGGER = LoggerFactory.getLogger(Search.class);

    public Search(ExecutionBlock block) {
        super(block);
    }
    
    protected DocumentStream postProcess(OperationContext result, ExecutionContext ctx) {
        return DocumentStream.map(result.getDocumentStream(),x->new ResultDocument(block,x.getOutputDocument()));
    }

    public OperationContext search(ExecutionContext ctx) {
        CRUDFindRequest req = buildFindRequest(ctx);
        if (req != null) {
            return search(ctx, req);
        } else {
            return null;
        }
    }

    protected CRUDFindRequest buildFindRequest(ExecutionContext ctx) {
        CRUDFindRequest findRequest = new CRUDFindRequest();
        findRequest.setQuery(query);
        findRequest.setProjection(projection);
        findRequest.setSort(sort);
        findRequest.setFrom(from);
        findRequest.setTo(to);
        return findRequest;
    }

    @Override
    protected DocumentStream getSearchResults(ExecutionContext ctx) {
        OperationContext result = search(ctx);
        if (result != null) {
            return postProcess(result, ctx);
        } else {
            return new ListDocumentStream(new ArrayList<>());
        }
    }

    @Override
    public JsonNode explain(ExecutionContext ctx) {
        ObjectNode node=(ObjectNode)toJson();
        CRUDFindRequest req=buildFindRequest(ctx);
        OperationContext searchCtx = ctx.getOperationContext().
            getDerivedOperationContext(block.getMetadata().getName(), req);
        new SimpleFindImpl(block.getMetadata(), searchCtx.getFactory()).explain(searchCtx,req);
        DocumentStream itr=searchCtx.getDocumentStream();
        List docs=new ArrayList<>();
        while(itr.hasNext()) {
            DocCtx doc=itr.next();
            if(!doc.hasErrors())
                docs.add(doc.getOutputDocument());
        }
        itr.close();
        if(!docs.isEmpty()) {
            if(docs.size()==1) {
                node.set("implementation",docs.get(0).getRoot());
            } else {
                ArrayNode arr=JsonNodeFactory.instance.arrayNode();
                for(JsonDoc doc:docs) {
                    arr.add(doc.getRoot());
                }
                node.set("implementation",arr);
            }
        }
        return node;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy