use of org.openntf.domino.contributor.DocumentBackupContributor 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.contributor.DocumentBackupContributor in project org.openntf.domino by OpenNTF.
the class JsonFrameAdapter method getJsonProperty.
@SuppressWarnings("unlikely-arg-type")
@Override
public Object getJsonProperty(final String paramKey) {
Object result = null;
Object frame = getFrame();
if (frame != null) {
CaseInsensitiveString key = new CaseInsensitiveString(paramKey);
if (key.equals("@id")) {
if (frame instanceof VertexFrame) {
Vertex v = ((VertexFrame) frame).asVertex();
// if (v instanceof DProxyVertex) {
// result = ((DProxyVertex)v).getProxyDelegate().getId().toString();
// } else {
result = String.valueOf(v.getId());
// }
} else if (frame instanceof EdgeFrame) {
result = String.valueOf(((EdgeFrame) frame).asEdge().getId());
}
} else if (key.equals("@editable")) {
if (frame instanceof VertexFrame) {
result = ((DVertex) ((VertexFrame) frame).asVertex()).isEditable();
} else if (frame instanceof EdgeFrame) {
result = ((DEdge) ((EdgeFrame) frame).asEdge()).isEditable();
}
} else if (key.equals("@proxyid")) {
// System.out.println("TEMP DEBUG @proxyid requested");
if (frame instanceof VertexFrame) {
Vertex v = ((VertexFrame) frame).asVertex();
if (v instanceof DProxyVertex) {
result = ((DProxyVertex) v).getProperty(DProxyVertex.PROXY_ITEM, String.class);
}
}
} else if (key.equals("@versions")) {
if (frame instanceof VertexFrame) {
try {
Vertex v = ((VertexFrame) frame).asVertex();
Object id = v.getId();
Session sess = Factory.getSession(SessionType.CURRENT);
Document doc = sess.getDocumentByMetaversalID(id.toString());
if (doc != null) {
Database db = doc.getAncestorDatabase();
List<DocumentBackupContributor> contributors = Factory.findApplicationServices(DocumentBackupContributor.class);
if (contributors != null) {
for (DocumentBackupContributor contributor : contributors) {
Optional<List<Date>> dates = contributor.getRevisionDates(db, doc.getUniversalID());
if (dates.isPresent()) {
result = dates.get();
break;
}
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
if (result == null) {
List<String> resultList = new ArrayList<String>();
resultList.add("N/A");
result = resultList;
}
} else if (key.equals("@debug")) {
Map<String, String> debugMap = new LinkedHashMap<String, String>();
debugMap.put("frameIdentity", String.valueOf(System.identityHashCode(this)));
if (frame instanceof VertexFrame) {
Vertex v = ((VertexFrame) frame).asVertex();
debugMap.put("vertexIdentity", String.valueOf(System.identityHashCode(v)));
} else if (frame instanceof EdgeFrame) {
Edge e = ((EdgeFrame) frame).asEdge();
debugMap.put("vertexIdentity", String.valueOf(System.identityHashCode(e)));
}
result = debugMap;
} else if (key.equals("@type")) {
if (frame instanceof VertexFrame) {
result = type_;
} else if (frame instanceof EdgeFrame) {
result = type_;
}
} else if (key.equals("@label")) {
if (frame instanceof EdgeFrame) {
Edge e = ((EdgeFrame) frame).asEdge();
result = e.getLabel();
}
} else if (key.equals("@in") && frame instanceof EdgeFrame) {
if (getInProperties() == null) {
// why not just make a frame adapter with the vertex?
// because that's another I/O operation. We already have the
// information needed to
DEdge dedge = (DEdge) ((EdgeFrame) frame).asEdge();
Map<String, String> minProps = new LinkedHashMap<String, String>();
minProps.put("@id", dedge.getVertexId(Direction.IN).toString());
Class<?> inType = graph_.getTypeRegistry().getInType(type_);
if (inType == null) {
minProps.put("@type", "Vertex");
} else {
minProps.put("@type", inType.getName());
}
result = minProps;
} else {
ParamMap inMap = new ParamMap();
inMap.put(Parameters.PROPS, CaseInsensitiveString.toStrings(getInProperties()));
if (getIncludeEdges()) {
inMap.put(Parameters.EDGES, EMPTY_STRINGS);
}
if (getIncludeCounts()) {
inMap.put(Parameters.COUNTS, EMPTY_STRINGS);
}
Method inMethod = graph_.getTypeRegistry().getIn(type_);
if (inMethod != null) {
try {
Object raw = inMethod.invoke(frame, (Object[]) null);
if (raw instanceof VertexFrame) {
VertexFrame inFrame = (VertexFrame) raw;
result = new JsonFrameAdapter(graph_, inFrame, inMap, isCollectionRoute_);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("In method not found for type: " + type_.getName());
}
}
} else if (key.equals("@out") && frame instanceof EdgeFrame) {
if (getOutProperties() == null) {
// why not just make a frame adapter with the vertex?
// because that's another I/O operation. We already have the
// information needed to
DEdge dedge = (DEdge) ((EdgeFrame) frame).asEdge();
Map<String, String> minProps = new LinkedHashMap<String, String>();
minProps.put("@id", dedge.getVertexId(Direction.OUT).toString());
Class<?> outType = graph_.getTypeRegistry().getOutType(type_);
if (outType == null) {
minProps.put("@type", "Vertex");
} else {
minProps.put("@type", outType.getName());
}
result = minProps;
} else {
ParamMap outMap = new ParamMap();
outMap.put(Parameters.PROPS, CaseInsensitiveString.toStrings(getOutProperties()));
if (getIncludeEdges()) {
outMap.put(Parameters.EDGES, EMPTY_STRINGS);
}
if (getIncludeCounts()) {
outMap.put(Parameters.COUNTS, EMPTY_STRINGS);
}
Method outMethod = graph_.getTypeRegistry().getOut(type_);
if (outMethod != null) {
try {
Object raw = outMethod.invoke(frame, (Object[]) null);
if (raw instanceof VertexFrame) {
VertexFrame outFrame = (VertexFrame) raw;
result = new JsonFrameAdapter(graph_, outFrame, outMap, isCollectionRoute_);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} else if (key.equals("@edges")) {
Map<String, Integer> edgeCounts = new LinkedHashMap<String, Integer>();
Set<CaseInsensitiveString> counterKeys = getCounters().keySet();
// " edge types");
for (CaseInsensitiveString label : counterKeys) {
Method crystal = getCounters().get(label);
if (crystal != null) {
// key);
try {
Object raw = crystal.invoke(getFrame(), (Object[]) null);
if (raw instanceof Integer) {
edgeCounts.put(label.toString(), (Integer) raw);
} else {
}
} catch (IllegalArgumentException iae) {
System.out.println("TEMP DEBUG Attempting to invoke " + crystal.getDeclaringClass().getName() + "." + crystal.getName() + " on an " + getFrame().getClass().getName() + " (" + DGraphUtils.getInterfaceList(getFrame()) + ")");
throw new RuntimeException(iae);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
// System.out.println("TEMP DEBUG No method found for
// key "
// + key);
}
}
result = edgeCounts;
} else if (key.equals("@actions")) {
List<CaseInsensitiveString> actionList = new ArrayList<CaseInsensitiveString>();
Set<CaseInsensitiveString> actionNames = getActions().keySet();
for (CaseInsensitiveString name : actionNames) {
actionList.add(name);
}
result = actionList;
} else if (key.startsWith("@counts")) {
String label = key.toString().substring("@counts".length());
Method crystal = getCounters().get(new CaseInsensitiveString(label));
if (crystal != null) {
try {
Object raw = crystal.invoke(getFrame(), (Object[]) null);
if (raw instanceof Integer) {
result = raw;
} else {
// System.out.println("TEMP DEBUG Invokation of a
// counter resulted in a "
// + (raw == null ? "null" :
// raw.getClass().getName()));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
// System.out.println("TEMP DEBUG No method found for key "
// + label);
}
} else if (key.equals("@columninfo")) {
if (frame instanceof ViewVertex) {
Map<String, String> columnInfo = new LinkedHashMap<String, String>();
if (frame instanceof ViewVertex) {
View view = ((ViewVertex) frame).asView();
if (view != null) {
try {
for (ViewColumn column : view.getColumns()) {
String progName = column.getItemName();
String title = column.getTitle();
columnInfo.put(progName, title);
}
} catch (Throwable t) {
columnInfo.put("@error", "Unable to access column information");
}
}
} else {
System.err.println("Frame is not a ViewVertex. It is " + DGraphUtils.findInterface(frame));
}
return columnInfo;
}
} else if (key.equals("@viewinfo")) {
if (frame instanceof DbInfoVertex) {
List viewInfo = ((DbInfoVertex) frame).getViewInfo();
return viewInfo;
}
} else if (key.startsWith("#") && frame instanceof VertexFrame) {
CharSequence label = key.subSequence(1, key.length());
Method crystal = getIncidences().get(label);
if (crystal != null) {
try {
result = crystal.invoke(frame, (Object[]) null);
} catch (IllegalArgumentException iae) {
throw new RuntimeException(iae.getMessage() + " where the expect call is " + crystal.getDeclaringClass().getName() + "." + crystal.getName() + " but the object is " + DGraphUtils.getInterfaceList(frame));
} catch (Exception e) {
throw new RuntimeException(e);
}
if (result != null) {
if (!(result instanceof Iterable)) {
if (result instanceof EdgeFrame) {
Vertex v = ((VertexFrame) frame).asVertex();
List<Edge> edges = new org.openntf.domino.graph2.impl.DEdgeList((DVertex) v);
edges.add(((EdgeFrame) result).asEdge());
result = new FramedEdgeList(getGraph(), ((VertexFrame) frame).asVertex(), edges, crystal.getReturnType());
}
}
if (getIncludeVertices()) {
// EdgeList into VertexList");
if (result instanceof DEdgeList) {
result = ((DEdgeList) result).toVertexList();
} else if (result instanceof FramedEdgeList) {
result = ((FramedEdgeList<?>) result).toVertexList();
} else {
System.err.println("TEMP DEBUG: Expected a DEdgeList but got a " + result.getClass().getName());
}
}
if (getFilterKeys() != null && !isCollectionRoute_) {
if (result instanceof DEdgeList) {
// System.out.println("TEMP DEBUG: Applying
// a filter to a DEdgeList");
List<CharSequence> filterKeys = getFilterKeys();
List<CharSequence> filterValues = getFilterValues();
for (int i = 0; i < filterKeys.size(); i++) {
result = ((DEdgeList) result).applyFilter(filterKeys.get(i).toString(), filterValues.get(i).toString());
}
} else if (result instanceof DVertexList) {
// System.out.println("TEMP DEBUG: Applying
// a filter to a DVertexList");
List<CharSequence> filterKeys = getFilterKeys();
List<CharSequence> filterValues = getFilterValues();
for (int i = 0; i < filterKeys.size(); i++) {
result = ((DVertexList) result).applyFilter(filterKeys.get(i).toString(), filterValues.get(i).toString());
}
} else if (result instanceof FramedEdgeList) {
// System.out.println("TEMP DEBUG: Applying
// a filter to a FramedEdgeList");
List<CharSequence> filterKeys = getFilterKeys();
List<CharSequence> filterValues = getFilterValues();
for (int i = 0; i < filterKeys.size(); i++) {
result = ((FramedEdgeList<?>) result).applyFilter(filterKeys.get(i).toString(), filterValues.get(i).toString());
}
} else if (result instanceof FramedVertexList) {
List<CharSequence> filterKeys = getFilterKeys();
List<CharSequence> filterValues = getFilterValues();
for (int i = 0; i < filterKeys.size(); i++) {
String curkey = filterKeys.get(i).toString();
String curvalue = filterValues.get(i).toString();
// System.out.println("TEMP DEBUG:
// Applying a filter to a
// FramedVertexList - "
// + curkey + ":" + curvalue);
result = ((FramedVertexList<?>) result).applyFilter(curkey, curvalue);
}
}
}
if (getStartsValues() != null) {
// + result.getClass().getName());
if (result instanceof DEdgeEntryList) {
((DEdgeEntryList) result).initEntryList(getStartsValues());
} else if (result instanceof FramedEdgeList) {
((FramedEdgeList) result).applyFilter("lookup", getStartsValues());
}
}
if (getFilterValues() != null && getFilterKeys() == null) {
if (result instanceof DEdgeEntryList) {
// System.out.println("TEMP DEBUG filtering a
// DEdgeEntryList");
((DEdgeEntryList) result).filterEntryList(getFilterValues());
} else if (result instanceof FramedEdgeList) {
((FramedEdgeList) result).applyFilter("filter", getFilterValues());
}
}
if (getOrderBys() != null) {
if (result instanceof FramedEdgeList) {
// System.out.println("Ordering an edge
// list");
result = ((FramedEdgeList<?>) result).sortBy(getOrderBys(), getDescending());
} else if (result instanceof FramedVertexList) {
// System.out.println("Ordering a vertex
// list");
result = ((FramedVertexList<?>) result).sortBy(getOrderBys(), getDescending());
}
}
if (getStart() >= 0) {
// + " for a " + result.getClass().getName());
if (getCount() > 0) {
int end = getStart() + getCount();
if (result instanceof FramedEdgeList) {
// System.out.println("TEMP DEBUG Sublisting
// a FramedEdgeList...");
int size = ((FramedEdgeList<?>) result).size();
result = ((FramedEdgeList<?>) result).subList(getStart(), (end > size ? size : end));
} else if (result instanceof FramedVertexList) {
int size = ((FramedVertexList<?>) result).size();
result = ((FramedVertexList<?>) result).subList(getStart(), (end > size ? size : end));
} else if (result instanceof DEdgeEntryList) {
// System.out.println("TEMP DEBUG Sublisting
// a DEdgeEntryList...");
int size = ((DEdgeEntryList) result).size();
result = ((DEdgeEntryList) result).subList(getStart(), (end > size ? size : end));
}
} else {
if (result instanceof FramedEdgeList) {
result = ((FramedEdgeList<?>) result).subList(getStart(), ((FramedEdgeList<?>) result).size());
} else if (result instanceof FramedVertexList) {
result = ((FramedVertexList<?>) result).subList(getStart(), ((FramedVertexList<?>) result).size());
} else if (result instanceof DEdgeEntryList) {
// System.out.println("TEMP DEBUG Sublisting
// a DEdgeEntryList...");
result = ((DEdgeEntryList) result).subList(getStart() + 1, ((DEdgeEntryList) result).size());
}
}
}
// }
if (result instanceof FramedVertexList) {
ParamMap listMap = new ParamMap();
if (getIncludeEdges()) {
listMap.put(Parameters.EDGES, EMPTY_STRINGS);
}
if (getIncludeCounts()) {
listMap.put(Parameters.COUNTS, EMPTY_STRINGS);
}
listMap.put(Parameters.PROPS, CaseInsensitiveString.toStrings(this.getProperties()));
listMap.put(Parameters.HIDEPROPS, CaseInsensitiveString.toStrings(this.getHideProperties()));
result = new JsonFrameListAdapter(getGraph(), (FramedVertexList<?>) result, listMap, isCollectionRoute_);
}
}
} else {
// NTF actually, this is a perfectly normal outcome.
// System.err.println("No edge method found for label " +
// label + " in a VertexFrame of type "
// + DGraphUtils.findInterface(frame) + " with id "
// + ((VertexFrame) frame).asVertex().getId());
}
} else {
// System.out.println("TEMP DEBUG finding property " + key);
Method crystal = getGetters().get(key);
if (crystal != null) {
try {
result = crystal.invoke(frame, (Object[]) null);
} catch (UserAccessException uae) {
throw uae;
} catch (Throwable t) {
if (frame instanceof EdgeFrame) {
result = ((EdgeFrame) frame).asEdge().getProperty(paramKey);
} else if (frame instanceof VertexFrame) {
result = ((VertexFrame) frame).asVertex().getProperty(paramKey);
} else {
System.err.println("Trying to get property " + paramKey + " from an object " + frame.getClass().getName());
}
}
} else {
if (frame instanceof ViewVertex.Contains) {
result = ((EdgeFrame) frame).asEdge().getProperty(paramKey);
} else if (frame instanceof VertexFrame) {
result = ((VertexFrame) frame).asVertex().getProperty(paramKey);
} else if (frame instanceof EdgeFrame) {
result = ((EdgeFrame) frame).asEdge().getProperty(paramKey);
} else {
System.err.println("No method found for key " + paramKey);
}
}
}
} else {
System.err.println("Unable to get property " + paramKey + " on a null object");
}
return result;
}
Aggregations