use of com.tinkerpop.frames.VertexFrame in project org.openntf.domino by OpenNTF.
the class FramedResource method processJsonObject.
@SuppressWarnings({ "resource", "unlikely-arg-type" })
private void processJsonObject(final JsonJavaObject jsonItems, final DFramedTransactionalGraph graph, final JsonGraphWriter writer, final ParamMap pm) /* , Map<Object, Object> resultMap */
{
Map<CaseInsensitiveString, Object> cisMap = new HashMap<CaseInsensitiveString, Object>();
for (String jsonKey : jsonItems.keySet()) {
CaseInsensitiveString cis = new CaseInsensitiveString(jsonKey);
cisMap.put(cis, jsonItems.get(jsonKey));
}
List<String> ids = pm.get(Parameters.ID);
boolean commit = true;
if (ids == null) {
Map<String, String> map = new HashMap<String, String>();
map.put("error", "Cannot POST to frame without an id parameter");
try {
writer.outObject(map);
} catch (JsonException e) {
throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
} catch (IOException e) {
throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
} else {
if (ids.size() == 0) {
System.out.println("Cannot POST to frame without an id parameter");
Map<String, String> map = new HashMap<String, String>();
map.put("error", "Cannot POST to frame without an id parameter");
try {
writer.outObject(map);
} catch (JsonException e) {
throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
} catch (IOException e) {
throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
} else {
for (String id : ids) {
// System.out.println("TEMP DEBUG POSTing to " + id);
Class<?> type = null;
if (pm.getTypes() != null) {
List<CharSequence> types = pm.getTypes();
String typename = types.get(0).toString();
type = graph.getTypeRegistry().findClassByName(typename);
}
NoteCoordinate nc = NoteCoordinate.Utils.getNoteCoordinate(id.trim());
Object element = graph.getElement(nc, type);
if (element instanceof VertexFrame) {
VertexFrame parVertex = (VertexFrame) element;
Map<CaseInsensitiveString, Method> adders = graph.getTypeRegistry().getAdders(parVertex.getClass());
CaseInsensitiveString rawLabel = new CaseInsensitiveString(jsonItems.getAsString("@label"));
Method method = adders.get(rawLabel);
if (method == null) {
method = adders.get(rawLabel + "In");
}
if (method != null) {
String rawId = jsonItems.getAsString("@id");
NoteCoordinate othernc = NoteCoordinate.Utils.getNoteCoordinate(rawId);
Object otherElement = graph.getElement(othernc, null);
if (otherElement instanceof VertexFrame) {
VertexFrame otherVertex = (VertexFrame) otherElement;
try {
Object result = method.invoke(parVertex, otherVertex);
if (result == null) {
System.out.println("Invokation of method " + method.getName() + " on a vertex of type " + DGraphUtils.findInterface(parVertex) + " with an argument of type " + DGraphUtils.findInterface(otherVertex) + " resulted in null when we expected an Edge");
}
JsonFrameAdapter adapter = new JsonFrameAdapter(graph, (EdgeFrame) result, null, false);
Iterator<String> frameProperties = adapter.getJsonProperties();
CaseInsensitiveString actionName = null;
CaseInsensitiveString preactionName = null;
for (CaseInsensitiveString cis : cisMap.keySet()) {
if (cis.equals("%preaction")) {
preactionName = new CaseInsensitiveString(String.valueOf(cisMap.get(cis)));
}
}
if (preactionName != null) {
commit = adapter.runAction(preactionName);
}
if (commit) {
while (frameProperties.hasNext()) {
CaseInsensitiveString key = new CaseInsensitiveString(frameProperties.next());
if (!key.startsWith("@")) {
Object value = cisMap.get(key);
if (value != null) {
adapter.putJsonProperty(key.toString(), value);
cisMap.remove(key);
}
}
}
for (CaseInsensitiveString cis : cisMap.keySet()) {
if (cis.equals("%action")) {
actionName = new CaseInsensitiveString(String.valueOf(cisMap.get(cis)));
} else if (!cis.startsWith("@")) {
Object value = cisMap.get(cis);
if (value != null) {
adapter.putJsonProperty(cis.toString(), value);
}
}
}
adapter.updateReadOnlyProperties();
if (actionName != null) {
commit = adapter.runAction(actionName);
}
}
writer.outObject(result);
} catch (Exception e) {
throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
} else {
throw new WebApplicationException(ErrorHelper.createErrorResponse("otherElement is not a VertexFrame. It's a " + (otherElement == null ? "null" : DGraphUtils.findInterface(otherElement).getName()), Response.Status.INTERNAL_SERVER_ERROR));
}
} else {
Class[] interfaces = element.getClass().getInterfaces();
String intList = "";
for (Class inter : interfaces) {
intList = intList + inter.getName() + ", ";
}
String methList = "";
for (CaseInsensitiveString key : adders.keySet()) {
methList = methList + key.toString() + ", ";
}
throw new WebApplicationException(ErrorHelper.createErrorResponse("No method found for " + rawLabel + " on element " + intList + ": " + ((VertexFrame) element).asVertex().getId() + " methods " + methList, Response.Status.INTERNAL_SERVER_ERROR));
}
} else {
throw new WebApplicationException(ErrorHelper.createErrorResponse("Element is null", Response.Status.INTERNAL_SERVER_ERROR));
}
}
}
if (commit) {
graph.commit();
} else {
graph.rollback();
}
}
}
use of com.tinkerpop.frames.VertexFrame in project org.openntf.domino by OpenNTF.
the class JsonGraphWriter method outLiteral.
@Override
protected void outLiteral(Object paramObject, final boolean paramBoolean) throws IOException, JsonException {
// paramObject.getClass().getName()));
if (paramObject != null) {
Class<?> objClass = paramObject.getClass();
IJsonWriterAdapter adapter = factory_.getJsonWriterAdapter(objClass);
if (adapter != null) {
paramObject = adapter.toJson(paramObject);
}
}
if (this.getFactory().isNull(paramObject)) {
outNull();
// } else if (paramObject instanceof Term) {
// JsonSearchAdapter adapter = new JsonSearchAdapter(graph_, (Term) paramObject, parameters_,
// isCollectionRoute_);
// super.outObject(adapter);
// } else if (paramObject instanceof Value) {
// JsonSearchAdapter adapter = new JsonSearchAdapter(graph_, (Value) paramObject, parameters_,
// isCollectionRoute_);
// super.outObject(adapter);
// } else if (paramObject instanceof RichTextReference) {
// JsonSearchAdapter adapter = new JsonSearchAdapter(graph_, (RichTextReference) paramObject, parameters_,
// isCollectionRoute_);
// super.outObject(adapter);
} else if (paramObject instanceof VertexFrame) {
JsonFrameAdapter adapter = new JsonFrameAdapter(graph_, (VertexFrame) paramObject, parameters_, isCollectionRoute_);
outObject(adapter);
} else if (paramObject instanceof EdgeFrame) {
JsonFrameAdapter adapter = new JsonFrameAdapter(graph_, (EdgeFrame) paramObject, parameters_, isCollectionRoute_);
outObject(adapter);
} else if (paramObject instanceof Class<?>) {
String className = ((Class) paramObject).getName();
outStringLiteral(className);
} else if (paramObject instanceof RichTextItem) {
outRichTextItem((RichTextItem) paramObject);
} else if (paramObject instanceof Enum) {
String className = ((Enum) paramObject).getClass().getName();
String enumName = ((Enum) paramObject).name();
outStringLiteral(className + " " + enumName);
} else if (paramObject instanceof CharSequence) {
outStringLiteral(paramObject.toString());
} else if (paramObject instanceof Set) {
// System.out.println("TEMP DEBUG Got a set!");
outArrayLiteral(((Set) paramObject).toArray());
} else if (this.getFactory().isString(paramObject)) {
outStringLiteral(this.getFactory().getString(paramObject));
} else if (this.getFactory().isNumber(paramObject)) {
outNumberLiteral(this.getFactory().getNumber(paramObject));
} else if (this.getFactory().isBoolean(paramObject)) {
outBooleanLiteral(this.getFactory().getBoolean(paramObject));
} else if (this.getFactory().isObject(paramObject)) {
outObject(paramObject, paramBoolean);
} else if (this.getFactory().isArray(paramObject)) {
outArrayLiteral(paramObject, paramBoolean);
} else if (paramObject instanceof JsonReference) {
outReference((JsonReference) paramObject);
} else if (paramObject instanceof DateTime) {
DateTime dt = (DateTime) paramObject;
outDateLiteral_(dt.toJavaDate());
} else if (paramObject instanceof DateRange) {
DateRange dt = (DateRange) paramObject;
outDateRangeLiteral(dt);
} else if (paramObject instanceof NoteCoordinate) {
outNoteCoordinate((NoteCoordinate) paramObject);
} else if (paramObject instanceof Date) {
outDateLiteral_((Date) paramObject);
} else {
outStringLiteral("JsonGenerator cannot process unknown type of " + ((paramObject != null) ? paramObject.getClass().getName() : "<null>"));
}
}
use of com.tinkerpop.frames.VertexFrame in project org.openntf.domino by OpenNTF.
the class DataInitializer method run.
@SuppressWarnings("unused")
@Override
public void run() {
long testStartTime = System.nanoTime();
marktime = System.nanoTime();
ConferenceGraph graph = new ConferenceGraph();
DFramedTransactionalGraph<DGraph> framedGraph = graph.getFramedGraph();
DGraph baseGraph = framedGraph.getBaseGraph();
Iterable<Vertex> vertices = baseGraph.getVertices("@", "Form=\"Presentation\"");
// NTF There's no specific need to do it this way. I just wanted to test the TypeField-based framing
Map<String, Object> jsonMap = null;
for (Vertex vertex : vertices) {
VertexFrame frame = framedGraph.frame(vertex, DVertexFrame.class);
}
long testEndTime = System.nanoTime();
System.out.println("Completed " + getClass().getSimpleName() + " run in " + ((testEndTime - testStartTime) / 1000000) + " ms");
//
// VertexFrame frame = framedGraph.toVertexFrame(jsonMap);
// System.out.println("Got a frame of " + frame.getClass().getName());
// if (frame instanceof Presentation) {
// ((Presentation) frame).setStatus(Status.CANCELLED);
// System.out.println("Result: " + ((Presentation) frame).getStatus());
// } else {
// System.out.println("Didn't get a Presentation. GOt a " + frame.getClass().getName());
// }
framedGraph.commit();
}
use of com.tinkerpop.frames.VertexFrame 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 com.tinkerpop.frames.VertexFrame in project org.openntf.domino by OpenNTF.
the class FramedResource method processJsonUpdate.
@SuppressWarnings("unlikely-arg-type")
private void processJsonUpdate(final JsonJavaObject jsonItems, final DFramedTransactionalGraph graph, final JsonGraphWriter writer, final ParamMap pm, final boolean isPut) throws JsonException, IOException {
Map<CaseInsensitiveString, Object> cisMap = new HashMap<CaseInsensitiveString, Object>();
for (String jsonKey : jsonItems.keySet()) {
CaseInsensitiveString cis = new CaseInsensitiveString(jsonKey);
cisMap.put(cis, jsonItems.get(jsonKey));
}
List<String> ids = pm.get(Parameters.ID);
boolean commit = true;
if (ids.size() == 0) {
// TODO no id
} else {
JsonFrameAdapter adapter = null;
for (String id : ids) {
NoteCoordinate nc = NoteCoordinate.Utils.getNoteCoordinate(id.trim());
Object element = graph.getElement(nc, null);
if (element instanceof EdgeFrame) {
adapter = new JsonFrameAdapter(graph, (EdgeFrame) element, null, false);
} else if (element instanceof VertexFrame) {
adapter = new JsonFrameAdapter(graph, (VertexFrame) element, null, false);
} else if (element == null) {
throw new RuntimeException("Cannot force a metaversalid through REST API. Requested URL: " + ODAGraphService.getCurrentRequest().getRequestURI());
} else {
throw new RuntimeException(// TODO
"TODO. Requested URL: " + ODAGraphService.getCurrentRequest().getRequestURI());
}
Iterator<String> frameProperties = adapter.getJsonProperties();
CaseInsensitiveString actionName = null;
CaseInsensitiveString preactionName = null;
List<Object> actionArguments = null;
for (CaseInsensitiveString cis : cisMap.keySet()) {
if (cis.equals("%preaction")) {
preactionName = new CaseInsensitiveString(String.valueOf(cisMap.get(cis)));
}
if (cis.equals("%args")) {
Object result = cisMap.get(cis);
if (result instanceof List) {
actionArguments = (List) result;
}
}
}
if (preactionName != null) {
if (actionArguments != null) {
commit = adapter.runAction(preactionName, actionArguments);
} else {
commit = adapter.runAction(preactionName);
}
}
if (commit) {
while (frameProperties.hasNext()) {
CaseInsensitiveString key = new CaseInsensitiveString(frameProperties.next());
if (!key.startsWith("@") && !key.startsWith("%")) {
Object value = cisMap.get(key);
if (value != null) {
adapter.putJsonProperty(key.toString(), value);
cisMap.remove(key);
} else if (isPut) {
adapter.putJsonProperty(key.toString(), value);
}
}
}
for (CaseInsensitiveString cis : cisMap.keySet()) {
if (cis.equals("%action")) {
actionName = new CaseInsensitiveString(String.valueOf(cisMap.get(cis)));
} else if (!cis.startsWith("@") && !cis.startsWith("%")) {
Object value = cisMap.get(cis);
if (value != null) {
adapter.putJsonProperty(cis.toString(), value);
}
}
}
adapter.updateReadOnlyProperties();
if (actionName != null) {
if (actionArguments != null) {
commit = adapter.runAction(actionName, actionArguments);
} else {
commit = adapter.runAction(actionName);
}
}
}
writer.outObject(element);
}
if (commit) {
graph.commit();
} else {
graph.rollback();
}
}
}
Aggregations