package com.altamiracorp.lumify.web.routes.graph;

import static com.altamiracorp.lumify.core.model.ontology.OntologyLumifyProperties.CONCEPT_TYPE;

import com.altamiracorp.lumify.core.model.ontology.Concept;
import com.altamiracorp.lumify.core.model.ontology.OntologyRepository;
import com.altamiracorp.lumify.core.user.User;
import com.altamiracorp.lumify.core.util.GraphUtil;
import com.altamiracorp.lumify.web.BaseRequestHandler;
import com.altamiracorp.miniweb.HandlerChain;
import com.altamiracorp.securegraph.Direction;
import com.altamiracorp.securegraph.Graph;
import com.altamiracorp.securegraph.Vertex;
import com.google.inject.Inject;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;

public class GraphRelatedVertices extends BaseRequestHandler {
    private final Graph graph;
    private final OntologyRepository ontologyRepository;

    @Inject
    public GraphRelatedVertices(final OntologyRepository ontologyRepo, final Graph graph) {
        ontologyRepository = ontologyRepo;
        this.graph = graph;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {
        String graphVertexId = getAttributeString(request, "graphVertexId");
        String limitParentConceptId = getOptionalParameter(request, "limitParentConceptId");

        User user = getUser(request);
        Set<String> limitConceptIds = new HashSet<String>();

        if (limitParentConceptId != null) {
            List<Concept> limitConcepts = ontologyRepository.getConceptByIdAndChildren(limitParentConceptId);
            if (limitConcepts == null) {
                throw new RuntimeException("Bad 'limitParentConceptId', no concept found for id: " + limitParentConceptId);
            }
            for (Concept con : limitConcepts) {
                limitConceptIds.add(con.getId());
            }
        }

        Iterable<Vertex> vertices = graph.getVertex(graphVertexId, user.getAuthorizations())
                .getVertices(Direction.BOTH, user.getAuthorizations());

        JSONObject json = new JSONObject();
        JSONArray verticesJson = new JSONArray();
        for (Vertex vertex : vertices) {
            if (limitConceptIds.size() == 0) {
                verticesJson.put(GraphUtil.toJson(vertex));
            }
            else if (!isLimited(limitConceptIds, vertex)) {
                verticesJson.put(GraphUtil.toJson(vertex));
            }
        }
        json.put("vertices", verticesJson);

        respondWithJson(response, json);

        chain.next(request, response);
    }

    private boolean isLimited(Set<String> limitConceptIds, Vertex vertex) {
        String conceptId = CONCEPT_TYPE.getPropertyValue(vertex);
        return !limitConceptIds.contains(conceptId);
    }
}


@@ -2403,107 +2403,12 @@
 == 0
-) %7B%0A                verticesJson.put(GraphUtil.toJson(vertex));%0A            %7D%0A            else if (
+ %7C%7C 
 !isL

simplify code