Search in sources :

Example 1 with MixedFramedVertexList

use of org.openntf.domino.graph2.annotations.MixedFramedVertexList in project org.openntf.domino by OpenNTF.

the class TermsResource method getTermsObject.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTermsObject(@Context final UriInfo uriInfo, @PathParam(Routes.NAMESPACE) final String namespace, @Context final Request request) throws JsonException, IOException {
    DFramedTransactionalGraph graph = this.getGraph(namespace);
    ParamMap pm = Parameters.toParamMap(uriInfo);
    StringWriter sw = new StringWriter();
    JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, true);
    try {
        List<CharSequence> types = new ArrayList<CharSequence>();
        types.add("org.openntf.domino.graph2.builtin.search.Term");
        List<CharSequence> filterkeys = pm.getFilterKeys();
        List<CharSequence> filtervalues = pm.getFilterValues();
        List<CharSequence> partialkeys = pm.getPartialKeys();
        List<CharSequence> partialvalues = pm.getPartialValues();
        List<CharSequence> startskeys = pm.getStartsKeys();
        List<CharSequence> startsvalues = pm.getStartsValues();
        if (types.size() == 0) {
            writer.outNull();
        } else if (types.size() == 1) {
            CharSequence typename = types.get(0);
            Iterable<?> elements = null;
            if (filterkeys != null) {
                elements = graph.getFilteredElements(typename.toString(), filterkeys, filtervalues);
            } else if (partialkeys != null) {
                elements = graph.getFilteredElementsPartial(typename.toString(), partialkeys, partialvalues);
            } else if (startskeys != null) {
                elements = graph.getFilteredElementsStarts(typename.toString(), startskeys, startsvalues);
            } else {
                elements = graph.getElements(typename.toString());
            }
            if (elements instanceof FramedEdgeList) {
                List<?> result = sortAndLimitList((List<?>) elements, pm);
                writer.outArrayLiteral(result);
            } else if (elements instanceof FramedVertexList) {
                List<?> result = sortAndLimitList((List<?>) elements, pm);
                writer.outArrayLiteral(result);
            } else {
                List<Object> maps = new ArrayList<Object>();
                for (Object element : elements) {
                    maps.add(element);
                }
                writer.outArrayLiteral(maps);
            }
        } else {
            MixedFramedVertexList vresult = null;
            FramedEdgeList eresult = null;
            for (CharSequence typename : types) {
                Iterable<?> elements = null;
                if (filterkeys != null) {
                    elements = graph.getFilteredElements(typename.toString(), filterkeys, filtervalues);
                } else if (partialkeys != null) {
                    elements = graph.getFilteredElementsPartial(typename.toString(), partialkeys, partialvalues);
                } else if (startskeys != null) {
                    elements = graph.getFilteredElementsStarts(typename.toString(), startskeys, startsvalues);
                } else {
                    elements = graph.getElements(typename.toString());
                }
                if (elements instanceof FramedVertexList) {
                    if (vresult == null) {
                        vresult = new MixedFramedVertexList(graph, null, (FramedVertexList) elements);
                    } else {
                        vresult.addAll((List<?>) elements);
                    }
                } else if (elements instanceof FramedEdgeList) {
                    if (eresult == null) {
                        eresult = (FramedEdgeList) elements;
                    } else {
                        eresult.addAll((FramedEdgeList) elements);
                    }
                }
            }
            if (vresult != null) {
                List<?> result = sortAndLimitList(vresult, pm);
                writer.outArrayLiteral(result);
            }
            if (eresult != null) {
                List<?> result = sortAndLimitList(eresult, pm);
                writer.outArrayLiteral(result);
            }
        }
    } catch (UserAccessException uae) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(uae, Response.Status.UNAUTHORIZED));
    } catch (Exception e) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
    String jsonEntity = sw.toString();
    ResponseBuilder berg = getBuilder(jsonEntity, new Date(), true, request);
    Response response = berg.build();
    return response;
}
Also used : ParamMap(org.openntf.domino.rest.service.Parameters.ParamMap) WebApplicationException(javax.ws.rs.WebApplicationException) JsonGraphWriter(org.openntf.domino.rest.json.JsonGraphWriter) ArrayList(java.util.ArrayList) DFramedTransactionalGraph(org.openntf.domino.graph2.impl.DFramedTransactionalGraph) MixedFramedVertexList(org.openntf.domino.graph2.annotations.MixedFramedVertexList) FramedEdgeList(org.openntf.domino.graph2.annotations.FramedEdgeList) FramedVertexList(org.openntf.domino.graph2.annotations.FramedVertexList) MixedFramedVertexList(org.openntf.domino.graph2.annotations.MixedFramedVertexList) UserAccessException(org.openntf.domino.exceptions.UserAccessException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) IOException(java.io.IOException) JsonException(com.ibm.commons.util.io.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException) Date(java.util.Date) Response(javax.ws.rs.core.Response) StringWriter(java.io.StringWriter) FramedEdgeList(org.openntf.domino.graph2.annotations.FramedEdgeList) ArrayList(java.util.ArrayList) FramedVertexList(org.openntf.domino.graph2.annotations.FramedVertexList) MixedFramedVertexList(org.openntf.domino.graph2.annotations.MixedFramedVertexList) List(java.util.List) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with MixedFramedVertexList

use of org.openntf.domino.graph2.annotations.MixedFramedVertexList in project org.openntf.domino by OpenNTF.

the class FramedCollectionResource method getFramedObject.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFramedObject(@Context final UriInfo uriInfo, @PathParam(Routes.NAMESPACE) final String namespace, @Context final Request request) throws JsonException, IOException {
    DFramedTransactionalGraph graph = this.getGraph(namespace);
    ParamMap pm = Parameters.toParamMap(uriInfo);
    StringWriter sw = new StringWriter();
    JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, true);
    try {
        if (pm.getTypes() != null) {
            List<CharSequence> types = pm.getTypes();
            List<CharSequence> filterkeys = pm.getFilterKeys();
            List<CharSequence> filtervalues = pm.getFilterValues();
            List<CharSequence> partialkeys = pm.getPartialKeys();
            List<CharSequence> partialvalues = pm.getPartialValues();
            List<CharSequence> startskeys = pm.getStartsKeys();
            List<CharSequence> startsvalues = pm.getStartsValues();
            if (types.size() == 0) {
                writer.outNull();
            } else if (types.size() == 1) {
                long startTime = new Date().getTime();
                CharSequence typename = types.get(0);
                Iterable<?> elements = null;
                if (filterkeys != null) {
                    elements = graph.getFilteredElements(typename.toString(), filterkeys, filtervalues);
                } else if (partialkeys != null) {
                    elements = graph.getFilteredElementsPartial(typename.toString(), partialkeys, partialvalues);
                } else if (startskeys != null) {
                    elements = graph.getFilteredElementsStarts(typename.toString(), startskeys, startsvalues);
                } else {
                    elements = graph.getElements(typename.toString());
                }
                if (elements instanceof FramedEdgeList) {
                    List<?> result = sortAndLimitList((List<?>) elements, pm);
                    writer.outArrayLiteral(result);
                } else if (elements instanceof FramedVertexList) {
                    List<?> result = sortAndLimitList((List<?>) elements, pm);
                    writer.outArrayLiteral(result);
                } else {
                    List<Object> maps = new ArrayList<Object>();
                    for (Object element : elements) {
                        maps.add(element);
                    }
                    writer.outArrayLiteral(maps);
                }
                long endTime = new Date().getTime();
                System.out.println("TEMP DEBUG Output for " + typename + " took " + (endTime - startTime) + "ms");
            } else {
                MixedFramedVertexList vresult = null;
                FramedEdgeList eresult = null;
                for (CharSequence typename : types) {
                    Iterable<?> elements = null;
                    if (filterkeys != null) {
                        elements = graph.getFilteredElements(typename.toString(), filterkeys, filtervalues);
                    } else if (partialkeys != null) {
                        elements = graph.getFilteredElementsPartial(typename.toString(), partialkeys, partialvalues);
                    } else if (startskeys != null) {
                        elements = graph.getFilteredElementsStarts(typename.toString(), startskeys, startsvalues);
                    } else {
                        elements = graph.getElements(typename.toString());
                    }
                    /*
						 * if (elements != null) { System.out.
						 * println("TEMP DEBUG found elements for type " +
						 * typename.toString()); } else {
						 * System.out.println("TEMP DEBUG NO elements for type "
						 * + typename.toString()); }
						 */
                    if (elements instanceof FramedVertexList) {
                        if (vresult == null) {
                            vresult = new MixedFramedVertexList(graph, null, (FramedVertexList) elements);
                        } else {
                            vresult.addAll((List<?>) elements);
                        }
                    } else if (elements instanceof FramedEdgeList) {
                        if (eresult == null) {
                            eresult = (FramedEdgeList) elements;
                        } else {
                            eresult.addAll((FramedEdgeList) elements);
                        }
                    }
                }
                if (vresult != null) {
                    List<?> result = sortAndLimitList(vresult, pm);
                    writer.outArrayLiteral(result);
                }
                if (eresult != null) {
                    List<?> result = sortAndLimitList(eresult, pm);
                    writer.outArrayLiteral(result);
                }
            }
        } else {
            // System.out.println("TEMP DEBUG: ID was null therefore we
            // can't report...");
            Map<String, Object> jsonMap = new LinkedHashMap<String, Object>();
            jsonMap.put("namespace", namespace);
            jsonMap.put("status", "active");
            writer.outObject(jsonMap);
        }
    } catch (UserAccessException uae) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(uae, Response.Status.UNAUTHORIZED));
    } catch (Exception e) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
    String jsonEntity = sw.toString();
    ResponseBuilder berg = getBuilder(jsonEntity, new Date(), true, request);
    Response response = berg.build();
    return response;
}
Also used : ParamMap(org.openntf.domino.rest.service.Parameters.ParamMap) WebApplicationException(javax.ws.rs.WebApplicationException) JsonGraphWriter(org.openntf.domino.rest.json.JsonGraphWriter) ArrayList(java.util.ArrayList) DFramedTransactionalGraph(org.openntf.domino.graph2.impl.DFramedTransactionalGraph) MixedFramedVertexList(org.openntf.domino.graph2.annotations.MixedFramedVertexList) FramedEdgeList(org.openntf.domino.graph2.annotations.FramedEdgeList) CaseInsensitiveString(org.openntf.domino.types.CaseInsensitiveString) FramedVertexList(org.openntf.domino.graph2.annotations.FramedVertexList) MixedFramedVertexList(org.openntf.domino.graph2.annotations.MixedFramedVertexList) UserAccessException(org.openntf.domino.exceptions.UserAccessException) Date(java.util.Date) JsonException(com.ibm.commons.util.io.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Response(javax.ws.rs.core.Response) StringWriter(java.io.StringWriter) FramedEdgeList(org.openntf.domino.graph2.annotations.FramedEdgeList) List(java.util.List) ArrayList(java.util.ArrayList) FramedVertexList(org.openntf.domino.graph2.annotations.FramedVertexList) MixedFramedVertexList(org.openntf.domino.graph2.annotations.MixedFramedVertexList) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with MixedFramedVertexList

use of org.openntf.domino.graph2.annotations.MixedFramedVertexList in project org.openntf.domino by OpenNTF.

the class MixedFramedVertexList method applyFilter.

public MixedFramedVertexList applyFilter(final String key, final Object value) {
    DVertexList vertList = new DVertexList((DVertex) sourceVertex_);
    if (this.size() > 0) {
        for (Object raw : this) {
            VertexFrame vertex = (VertexFrame) raw;
            try {
                if ("@type".equals(key)) {
                    if (DGraphUtils.isType(vertex, TypeUtils.toString(value))) {
                        vertList.add((DVertex) vertex.asVertex());
                    }
                } else {
                    Object vertexVal = DGraphUtils.getFramedProperty(getGraph(), vertex, key);
                    if (vertexVal instanceof Collection) {
                        for (Object rawVal : (Collection) vertexVal) {
                            if (value.equals(TypeUtils.toString(rawVal))) {
                                vertList.add((DVertex) vertex.asVertex());
                            }
                        }
                    } else {
                        if (value.equals(TypeUtils.toString(vertexVal))) {
                            vertList.add((DVertex) vertex.asVertex());
                        }
                    }
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
    MixedFramedVertexList result = new MixedFramedVertexList(getGraph(), sourceVertex_, vertList);
    return result;
}
Also used : VertexFrame(com.tinkerpop.frames.VertexFrame) Collection(java.util.Collection) DVertexList(org.openntf.domino.graph2.impl.DVertexList)

Aggregations

JsonException (com.ibm.commons.util.io.json.JsonException)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Response (javax.ws.rs.core.Response)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 UserAccessException (org.openntf.domino.exceptions.UserAccessException)2 FramedEdgeList (org.openntf.domino.graph2.annotations.FramedEdgeList)2 FramedVertexList (org.openntf.domino.graph2.annotations.FramedVertexList)2 MixedFramedVertexList (org.openntf.domino.graph2.annotations.MixedFramedVertexList)2 DFramedTransactionalGraph (org.openntf.domino.graph2.impl.DFramedTransactionalGraph)2 JsonGraphWriter (org.openntf.domino.rest.json.JsonGraphWriter)2 ParamMap (org.openntf.domino.rest.service.Parameters.ParamMap)2 JsonJavaObject (com.ibm.commons.util.io.json.JsonJavaObject)1 VertexFrame (com.tinkerpop.frames.VertexFrame)1