@PUT
@Path("/{indexName}")
@Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON})
@Timed(name = "http.rest.indices.object.put", absolute = true)
public Response putElementInIndex(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName) {
final Index index = this.getIndexFromGraph(graphName, indexName);
final RexsterApplicationGraph rag = this.getRexsterApplicationGraph(graphName);
final IndexableGraph graph = (IndexableGraph) rag.getGraph();
if (index == null) {
JSONObject error = generateErrorObject("Index not found.");
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).entity(error).build());
}
String key = null;
Object value = null;
String id = null;
final JSONObject theRequestObject = this.getRequestObject();
Object temp = theRequestObject.opt(Tokens.KEY);
if (null != temp)
key = temp.toString();
temp = theRequestObject.opt(Tokens.VALUE);
if (null != temp)
value = ElementHelper.getTypedPropertyValue(temp.toString());
temp = theRequestObject.opt(Tokens.ID);
if (null != temp)
id = temp.toString();
if (key != null && value != null && id != null) {
try {
if (Vertex.class.isAssignableFrom(index.getIndexClass())) {
index.put(key, value, graph.getVertex(id));
rag.tryCommit();
} else if (Edge.class.isAssignableFrom(index.getIndexClass())) {
index.put(key, value, graph.getEdge(id));
rag.tryCommit();
} else {
rag.tryRollback();
final JSONObject error = generateErrorObject("Index class must be either vertex or edge");
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(error).build());