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;
}
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;
}
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;
}
Aggregations