Search in sources :

Example 1 with DKeyResolver

use of org.openntf.domino.graph2.DKeyResolver in project org.openntf.domino by OpenNTF.

the class SearchResource method getSearchObject.

@SuppressWarnings("unchecked")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSearchObject(@Context final UriInfo uriInfo, @PathParam(Routes.NAMESPACE) final String namespace, @Context final Request request) throws JsonException, IOException {
    @SuppressWarnings("rawtypes") DFramedTransactionalGraph graph = this.getGraph(namespace);
    ParamMap pm = Parameters.toParamMap(uriInfo);
    StringWriter sw = new StringWriter();
    JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, false);
    Date lastModified = new Date();
    boolean getLastMod = false;
    try {
        if (pm.get(Parameters.ID) != null) {
            List<String> ids = pm.get(Parameters.ID);
            if (ids.size() == 0) {
                writer.outNull();
            } else if (ids.size() == 1) {
                String id = ids.get(0).trim();
                NoteCoordinate nc = null;
                if (id.startsWith("E")) {
                    nc = ViewEntryCoordinate.Utils.getViewEntryCoordinate(id);
                } else if (id.startsWith("V")) {
                    nc = ViewEntryCoordinate.Utils.getViewEntryCoordinate(id);
                } else {
                    nc = NoteCoordinate.Utils.getNoteCoordinate(id);
                    getLastMod = true;
                }
                Object elem = graph.getElement(nc, null);
                if (elem == null) {
                    throw new WebApplicationException(ErrorHelper.createErrorResponse("Graph element not found for id " + id, Response.Status.NOT_FOUND));
                }
                if (elem instanceof DVertexFrame && getLastMod) {
                    lastModified = ((DVertexFrame) elem).getModified();
                }
                if (elem instanceof DEdgeFrame && getLastMod) {
                    lastModified = ((DEdgeFrame) elem).getModified();
                }
                writer.outObject(elem);
            } else {
                List<Value> valueResults = null;
                List<RichTextReference> rtRefResults = null;
                for (String id : ids) {
                    NoteCoordinate nc = NoteCoordinate.Utils.getNoteCoordinate(id.trim());
                    Object rawterm = graph.getElement(nc, null);
                    // System.out.println("TEMP DEBUG got a " + DGraphUtils.getInterfaceList(rawterm) + " from id " + nc);
                    Term term = null;
                    if (rawterm instanceof Term) {
                        term = (Term) rawterm;
                    } else {
                    // System.out.println("TEMP DEBUG didn't get a Term!");
                    }
                    List<Value> values = term.getValues();
                    // System.out.println("TEMP DEBUG found " + values.size()  + " values for term " + term.getValue() + " in a " + values.getClass().getName());
                    List<RichTextReference> rtRefs = term.getRichTextReferences();
                    // System.out.println("TEMP DEBUG found " + rtRefs.size()  + " rtrefs for term " + term.getValue() + " in a " + rtRefs.getClass().getName());
                    if (valueResults == null) {
                        valueResults = values;
                    } else {
                        valueResults.retainAll(values);
                    // System.out.println("TEMP DEBUG retained " + valueResults.size()  + " values for term " + term.getValue());
                    }
                    if (rtRefResults == null) {
                        rtRefResults = rtRefs;
                    } else {
                        rtRefResults.retainAll(rtRefs);
                    // System.out.println("TEMP DEBUG retained " + rtRefResults.size()  + " rtrefs for term " + term.getValue());
                    }
                }
                List<Object> combinedResults = new ArrayList<Object>();
                combinedResults.addAll(valueResults);
                combinedResults.addAll(rtRefResults);
                writer.outArrayLiteral(combinedResults);
            }
        } else if (pm.getKeys() != null) {
            Class<?> type = Term.class;
            DKeyResolver resolver = graph.getKeyResolver(type);
            List<CharSequence> keys = pm.getKeys();
            if (keys.size() == 0) {
                writer.outNull();
            } else {
                List<Value> valueResults = null;
                List<RichTextReference> rtRefResults = null;
                String key = URLDecoder.decode(String.valueOf(keys.get(0)), "UTF-8");
                String[] array = key.split(" ");
                for (String cur : array) {
                    NoteCoordinate nc = resolver.resolveKey(type, cur);
                    Object rawterm = graph.getElement(nc, null);
                    Term term = null;
                    if (rawterm instanceof Term) {
                        term = (Term) rawterm;
                    } else {
                    }
                    List<Value> values = term.getValues();
                    List<RichTextReference> rtRefs = term.getRichTextReferences();
                    if (valueResults == null) {
                        valueResults = values;
                    } else {
                        valueResults.retainAll(values);
                    }
                    if (rtRefResults == null) {
                        rtRefResults = rtRefs;
                    } else {
                        rtRefResults.retainAll(rtRefs);
                    }
                }
                List<Object> combinedResults = new ArrayList<Object>();
                combinedResults.addAll(valueResults);
                combinedResults.addAll(rtRefResults);
                writer.outArrayLiteral(combinedResults);
            }
            graph.rollback();
        } else {
            // MultivaluedMap<String, String> mvm = uriInfo.getQueryParameters();
            Map<String, Object> jsonMap = new LinkedHashMap<String, Object>();
            jsonMap.put("namespace", namespace);
            jsonMap.put("status", "active");
            writer.outObject(jsonMap);
        }
    } catch (UserAccessException uae) {
        return ErrorHelper.createErrorResponse("User " + Factory.getSession(SessionType.CURRENT).getEffectiveUserName() + " is not authorized to access this resource", Response.Status.UNAUTHORIZED);
    } catch (KeyNotFoundException knfe) {
        ResponseBuilder rb = Response.noContent();
        return rb.build();
    } catch (Exception e) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
    String jsonEntity = sw.toString();
    ResponseBuilder berg = getBuilder(jsonEntity, lastModified, 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) DFramedTransactionalGraph(org.openntf.domino.graph2.impl.DFramedTransactionalGraph) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) List(java.util.List) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) NoteCoordinate(org.openntf.domino.big.NoteCoordinate) DKeyResolver(org.openntf.domino.graph2.DKeyResolver) Term(org.openntf.domino.graph2.builtin.search.Term) UserAccessException(org.openntf.domino.exceptions.UserAccessException) Date(java.util.Date) UserAccessException(org.openntf.domino.exceptions.UserAccessException) KeyNotFoundException(org.openntf.domino.graph2.impl.DEdgeEntryList.KeyNotFoundException) IOException(java.io.IOException) JsonException(com.ibm.commons.util.io.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException) DVertexFrame(org.openntf.domino.graph2.builtin.DVertexFrame) Response(javax.ws.rs.core.Response) DEdgeFrame(org.openntf.domino.graph2.builtin.DEdgeFrame) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParamMap(org.openntf.domino.rest.service.Parameters.ParamMap) KeyNotFoundException(org.openntf.domino.graph2.impl.DEdgeEntryList.KeyNotFoundException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with DKeyResolver

use of org.openntf.domino.graph2.DKeyResolver in project org.openntf.domino by OpenNTF.

the class FramedResource 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);
    if (pm.getVersion() != null) {
        System.out.println("TEMP DEBUG Version number parameter detected " + pm.getVersion().get(0));
    }
    StringWriter sw = new StringWriter();
    JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, false);
    Date lastModified = new Date();
    boolean getLastMod = false;
    try {
        if (pm.get(Parameters.ID) != null) {
            List<String> ids = pm.get(Parameters.ID);
            if (ids.size() == 0) {
                writer.outNull();
            } else if (ids.size() == 1) {
                String id = ids.get(0).trim();
                NoteCoordinate nc = null;
                if (id.startsWith("E")) {
                    nc = ViewEntryCoordinate.Utils.getViewEntryCoordinate(id);
                } else if (id.startsWith("V")) {
                    nc = ViewEntryCoordinate.Utils.getViewEntryCoordinate(id);
                } else {
                    nc = NoteCoordinate.Utils.getNoteCoordinate(id);
                    getLastMod = true;
                // System.out.println("TEMP DEBUG isIcon: " +
                // String.valueOf(nc.isIcon()));
                }
                if (nc == null) {
                // writer.outStringProperty("message", "NoteCoordinate
                // is null for id " + id);
                }
                if (graph == null) {
                // writer.outStringProperty("message", "Graph is null
                // for namespace " + namespace);
                }
                NoteCoordinate versionNC = null;
                if (pm.getVersion() != null) {
                    String versionString = pm.getVersion().get(0).toString();
                    System.out.println("Version parameter detected: " + versionString);
                    SimpleDateFormat sdf = TypeUtils.getDefaultDateFormat();
                    Date versionDate = sdf.parse(versionString);
                    try {
                        Session sess = Factory.getSession(SessionType.CURRENT);
                        Document doc = sess.getDocumentByMetaversalID(nc.toString());
                        Database db = doc.getAncestorDatabase();
                        List<DocumentBackupContributor> contributors = Factory.findApplicationServices(DocumentBackupContributor.class);
                        if (contributors != null) {
                            for (DocumentBackupContributor contributor : contributors) {
                                Optional<Document> versionDoc = contributor.createSidecarDocument(db, doc.getUniversalID(), versionDate);
                                if (versionDoc.isPresent()) {
                                    versionNC = versionDoc.get().getNoteCoordinate();
                                    break;
                                }
                            }
                        }
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
                Object elem = null;
                if (versionNC != null) {
                    elem = graph.getElement(versionNC, null);
                // System.out.println("Got an element from graph with id " + ((Element)elem).getId());
                } else {
                    elem = graph.getElement(nc, null);
                }
                if (elem == null) {
                    // builder = Response.status(Status.NOT_FOUND);
                    // writer.outStringProperty("currentUsername",
                    // Factory.getSession(SessionType.CURRENT).getEffectiveUserName());
                    // throw new IllegalStateException();
                    Response response = ErrorHelper.createErrorResponse("Graph element not found for id " + String.valueOf(id), Response.Status.NOT_FOUND);
                    throw new WebApplicationException(response);
                }
                try {
                    if (elem instanceof DVertexFrame && getLastMod) {
                        lastModified = ((DVertexFrame) elem).getModified();
                    }
                    if (elem instanceof DEdgeFrame && getLastMod) {
                        lastModified = ((DEdgeFrame) elem).getModified();
                    }
                } catch (UserAccessException uae) {
                    return ErrorHelper.createErrorResponse("User " + Factory.getSession(SessionType.CURRENT).getEffectiveUserName() + " is not authorized to access this resource", Response.Status.UNAUTHORIZED);
                } catch (Exception e) {
                    throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
                }
                writer.outObject(elem);
            } else {
                List<Object> maps = new ArrayList<Object>();
                for (String id : ids) {
                    NoteCoordinate nc = NoteCoordinate.Utils.getNoteCoordinate(id.trim());
                    maps.add(graph.getElement(nc, null));
                }
                writer.outArrayLiteral(maps);
            }
        } else if (pm.getKeys() != null) {
            Class<?> type = null;
            if (pm.getTypes() != null) {
                List<CharSequence> types = pm.getTypes();
                String typename = types.get(0).toString();
                type = graph.getTypeRegistry().findClassByName(typename);
            }
            DKeyResolver resolver = graph.getKeyResolver(type);
            List<CharSequence> keys = pm.getKeys();
            if (keys.size() == 0) {
                writer.outNull();
            } else if (keys.size() == 1) {
                CharSequence id = keys.get(0);
                NoteCoordinate nc = resolver.resolveKey(type, URLDecoder.decode(String.valueOf(id), "UTF-8"));
                Object elem = null;
                if (nc != null) {
                    // is null for id " + id);
                    try {
                        elem = graph.getElement(nc);
                    } catch (Exception e) {
                    // NOOP NTF - this is possible and not an error condition. That's why we have .handleMissingKey.
                    }
                }
                if (elem == null) {
                    elem = resolver.handleMissingKey(type, id);
                    if (elem == null) {
                        Response response = ErrorHelper.createErrorResponse("Graph element not found for key " + id, Response.Status.NOT_FOUND);
                        throw new WebApplicationException(response);
                    }
                }
                if (elem instanceof Vertex) {
                    // System.out.println("TEMP DEBUG Framing a vertex of
                    // type "
                    // + elem.getClass().getName());
                    VertexFrame vf = (VertexFrame) graph.frame((Vertex) elem, type);
                    if (vf instanceof DVertexFrame) {
                        lastModified = ((DVertexFrame) vf).getModified();
                    }
                    writer.outObject(vf);
                } else if (elem instanceof Edge) {
                    EdgeFrame ef = (EdgeFrame) graph.frame((Edge) elem, type);
                    if (ef instanceof DEdgeFrame) {
                        lastModified = ((DEdgeFrame) ef).getModified();
                    }
                    writer.outObject(ef);
                }
            } else {
                List<Object> maps = new ArrayList<Object>();
                for (CharSequence id : keys) {
                    NoteCoordinate nc = resolver.resolveKey(type, id);
                    maps.add(graph.getElement(nc, null));
                }
                writer.outArrayLiteral(maps);
            }
            graph.rollback();
        } else {
            MultivaluedMap<String, String> mvm = uriInfo.getQueryParameters();
            for (@SuppressWarnings("unused") String key : mvm.keySet()) {
            // System.out.println("TEMP DEBUG: " + key + ": " +
            // mvm.getFirst(key));
            }
            Map<String, Object> jsonMap = new LinkedHashMap<String, Object>();
            jsonMap.put("namespace", namespace);
            jsonMap.put("status", "active");
            writer.outObject(jsonMap);
        }
    } catch (UserAccessException uae) {
        return ErrorHelper.createErrorResponse("User " + Factory.getSession(SessionType.CURRENT).getEffectiveUserName() + " is not authorized to access this resource", Response.Status.UNAUTHORIZED);
    } catch (KeyNotFoundException knfe) {
        ResponseBuilder rb = Response.noContent();
        return rb.build();
    } catch (Exception e) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
    String jsonEntity = sw.toString();
    ResponseBuilder berg = getBuilder(jsonEntity, lastModified, true, request);
    Response response = berg.build();
    return response;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) 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) CaseInsensitiveString(org.openntf.domino.types.CaseInsensitiveString) Document(org.openntf.domino.Document) StringWriter(java.io.StringWriter) Database(org.openntf.domino.Database) List(java.util.List) ArrayList(java.util.ArrayList) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) NoteCoordinate(org.openntf.domino.big.NoteCoordinate) DEdgeFrame(org.openntf.domino.graph2.builtin.DEdgeFrame) EdgeFrame(com.tinkerpop.frames.EdgeFrame) Optional(java.util.Optional) DKeyResolver(org.openntf.domino.graph2.DKeyResolver) UserAccessException(org.openntf.domino.exceptions.UserAccessException) Date(java.util.Date) KeyNotFoundException(org.openntf.domino.graph2.impl.DEdgeEntryList.KeyNotFoundException) JsonException(com.ibm.commons.util.io.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) IOException(java.io.IOException) DVertexFrame(org.openntf.domino.graph2.builtin.DVertexFrame) Response(javax.ws.rs.core.Response) DVertexFrame(org.openntf.domino.graph2.builtin.DVertexFrame) VertexFrame(com.tinkerpop.frames.VertexFrame) DEdgeFrame(org.openntf.domino.graph2.builtin.DEdgeFrame) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) DocumentBackupContributor(org.openntf.domino.contributor.DocumentBackupContributor) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) SimpleDateFormat(java.text.SimpleDateFormat) Edge(com.tinkerpop.blueprints.Edge) Map(java.util.Map) ParamMap(org.openntf.domino.rest.service.Parameters.ParamMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) KeyNotFoundException(org.openntf.domino.graph2.impl.DEdgeEntryList.KeyNotFoundException) Session(org.openntf.domino.Session) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with DKeyResolver

use of org.openntf.domino.graph2.DKeyResolver in project org.openntf.domino by OpenNTF.

the class DFramedTransactionalGraph method getKeyResolver.

public DKeyResolver getKeyResolver(final Class<?> type) {
    DGraph base = (DGraph) this.getBaseGraph();
    DKeyResolver result = base.getKeyResolver(type);
    if (result == null) {
        DConfiguration config = (DConfiguration) getConfig();
        result = base.getKeyResolver(config.getDefaultVertexFrameType());
        if (result == null) {
            result = new DefaultKeyResolver(this);
            base.addKeyResolver(result);
        }
    }
    return result;
}
Also used : DKeyResolver(org.openntf.domino.graph2.DKeyResolver)

Aggregations

DKeyResolver (org.openntf.domino.graph2.DKeyResolver)3 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 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)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 NoteCoordinate (org.openntf.domino.big.NoteCoordinate)2 UserAccessException (org.openntf.domino.exceptions.UserAccessException)2 DEdgeFrame (org.openntf.domino.graph2.builtin.DEdgeFrame)2 DVertexFrame (org.openntf.domino.graph2.builtin.DVertexFrame)2 KeyNotFoundException (org.openntf.domino.graph2.impl.DEdgeEntryList.KeyNotFoundException)2 DFramedTransactionalGraph (org.openntf.domino.graph2.impl.DFramedTransactionalGraph)2