use of org.openntf.domino.graph2.builtin.DEdgeFrame 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;
}
use of org.openntf.domino.graph2.builtin.DEdgeFrame 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;
}
use of org.openntf.domino.graph2.builtin.DEdgeFrame in project org.openntf.domino by OpenNTF.
the class JsonFrameAdapter method getJsonProperties.
@Override
public Iterator<String> getJsonProperties() {
// System.out
// .println("TEMP DEBUG getting Json properties list for a frame of type
// "
// + frame_.getClass().getName());
List<String> result = new ArrayList<String>();
// if (getActionsParam() != null) {
// for (CharSequence cis : getActionsParam()) {
// result.add("%" + cis.toString());
// }
// }
result.add("@id");
result.add("@type");
result.add("@editable");
if (getIncludeDebug()) {
result.add("@debug");
}
if (getFrame() instanceof VertexFrame) {
result.add("@versions");
}
Collection<CharSequence> props = getProperties();
if (props == null) {
// NoteCoordinate nc = (NoteCoordinate)
// ((VertexFrame)frame_).asVertex().getId();
props = new ArrayList<CharSequence>();
props.addAll(getGetters().keySet());
Class<?> klazz = frame_.getClass();
boolean incl = isInclusive(klazz);
// }
if (props == null || props.size() < 5 || incl) {
if (frame_ instanceof DVertexFrame) {
try {
Set<String> raw = ((DVertexFrame) frame_).asVertex().getPropertyKeys();
// Set<CharSequence> raw = ((DVertexFrame) frame_).asMap().keySet();
// System.out.println("TEMP DEBUG raw vertex has " + raw.size() + " properties");
props.addAll(CaseInsensitiveString.toCaseInsensitive(raw));
} catch (Throwable t) {
Throwable cause = t.getCause();
if (cause != null) {
System.err.println("Exception trying to process a frame of type " + DGraphUtils.findInterface(frame_) + " resulting in a " + cause.getClass().getSimpleName());
cause.printStackTrace();
try {
throw cause;
} catch (Throwable e) {
e.printStackTrace();
}
}
}
} else if (frame_ instanceof DEdgeFrame) {
// Set<CharSequence> raw = ((DEdgeFrame)
// frame_).asMap().keySet();
// props.addAll(CaseInsensitiveString.toCaseInsensitive(raw));
}
}
}
for (CharSequence cis : props) {
result.add(cis.toString());
// System.out.println("Adding " + cis.toString());
}
Object frame = getFrame();
if (frame instanceof VertexFrame && getIncludeEdges()) {
result.add("@edges");
}
if (getIncludeActions()) {
result.add("@actions");
}
if (frame instanceof VertexFrame && getIncludeCounts()) {
for (CaseInsensitiveString key : getCounters().keySet()) {
result.add("@counts" + key.toString());
}
}
if (frame instanceof VertexFrame) {
Vertex v = ((VertexFrame) frame).asVertex();
if (v instanceof DProxyVertex) {
result.add("@proxyid");
}
}
if (frame instanceof VertexFrame && getLabels() != null) {
for (CharSequence cis : getLabels()) {
result.add("#" + cis.toString());
}
}
if (frame instanceof EdgeFrame) {
result.add("@in");
result.add("@out");
result.add("@label");
}
if (frame instanceof ViewVertex) {
result.add("@columninfo");
}
// }
if (frame instanceof ViewVertex.Contains) {
Edge edge = ((ViewVertex.Contains) frame).asEdge();
if (edge instanceof DEdge) {
result.addAll(((DEdge) edge).getDelegate().keySet());
}
}
Collection<CharSequence> hideProps = getHideProperties();
if (hideProps != null && !hideProps.isEmpty()) {
for (CharSequence cis : hideProps) {
result.remove(cis.toString());
}
}
// System.out.println("TEMP DEBUG getting properties for a " + DGraphUtils.getInterfaceList(frame) + " -- " + Strings.getString(result, ","));
return result.iterator();
}
use of org.openntf.domino.graph2.builtin.DEdgeFrame in project org.openntf.domino by OpenNTF.
the class JsonSearchAdapter method getJsonProperties.
@Override
public Iterator<String> getJsonProperties() {
List<String> result = new ArrayList<String>();
result.add("@id");
result.add("@type");
result.add("@editable");
if (getIncludeDebug()) {
result.add("@debug");
}
Collection<CharSequence> props = getProperties();
if (props == null) {
// NoteCoordinate nc = (NoteCoordinate)
// ((VertexFrame)frame_).asVertex().getId();
props = new ArrayList<CharSequence>();
props.addAll(getGetters().keySet());
if (props == null || props.size() < 5) {
if (frame_ instanceof DVertexFrame) {
try {
Set<String> raw = ((DVertexFrame) frame_).asVertex().getPropertyKeys();
props.addAll(CaseInsensitiveString.toCaseInsensitive(raw));
} catch (Throwable t) {
Throwable cause = t.getCause();
if (cause != null) {
System.err.println("Exception trying to process a frame of type " + DGraphUtils.findInterface(frame_) + " resulting in a " + cause.getClass().getSimpleName());
cause.printStackTrace();
try {
throw cause;
} catch (Throwable e) {
e.printStackTrace();
}
}
}
} else if (frame_ instanceof DEdgeFrame) {
// Set<CharSequence> raw = ((DEdgeFrame)
// frame_).asMap().keySet();
// props.addAll(CaseInsensitiveString.toCaseInsensitive(raw));
}
}
}
for (CharSequence cis : props) {
result.add(cis.toString());
}
Object frame = getFrame();
if (frame instanceof VertexFrame && getIncludeEdges()) {
result.add("@edges");
}
if (getIncludeActions()) {
result.add("@actions");
}
if (frame instanceof VertexFrame && getIncludeCounts()) {
for (CaseInsensitiveString key : getCounters().keySet()) {
result.add("@counts" + key.toString());
}
}
if (frame instanceof VertexFrame) {
Vertex v = ((VertexFrame) frame).asVertex();
if (v instanceof DProxyVertex) {
result.add("@proxyid");
}
}
if (frame instanceof VertexFrame && getLabels() != null) {
for (CharSequence cis : getLabels()) {
result.add("#" + cis.toString());
}
}
if (frame instanceof EdgeFrame) {
result.add("@in");
result.add("@out");
}
if (frame instanceof ViewVertex) {
result.add("@columninfo");
}
if (frame instanceof ViewVertex.Contains) {
Edge edge = ((ViewVertex.Contains) frame).asEdge();
if (edge instanceof DEdge) {
result.addAll(((DEdge) edge).getDelegate().keySet());
}
}
Collection<CharSequence> hideProps = getHideProperties();
if (hideProps != null && !hideProps.isEmpty()) {
for (CharSequence cis : hideProps) {
result.remove(cis.toString());
}
}
return result.iterator();
}
Aggregations