List of usage examples for com.mongodb BasicDBObject append
@Override public BasicDBObject append(final String key, final Object val)
From source file:dsll.pinterest.crawler.Reduce.java
private static Text updateBoardContent(String url, DBCollection baordsCollection) throws JSONException, IOException { String id = url.split("/")[4]; DBCursor c = baordsCollection.find(new BasicDBObject("ID", id)); DBObject oldPin = c.next();//from ww w . j av a 2 s. c o m JSONArray oldPins = new JSONArray(oldPin.get("pins").toString()); Elements pinsCont = Jsoup.connect(url).get().select("div[class=pinWrapper]"); // new pins JSONArray pins = new JSONArray(); for (Element pinCont : pinsCont) { JSONObject pin = new JSONObject(); pin.append("src", pinCont.select("div[class=pinHolder]>a").first().attr("href")); pins.put(pin); } List<String> oldPinURL = new ArrayList<String>(); for (int i = 0; i < oldPins.length(); i++) { oldPinURL.add(oldPins.getJSONObject(i).getString("src")); } for (int i = 0; i < pins.length(); i++) { if (oldPinURL.contains(pins.getJSONObject(i).getString("src"))) { continue; } oldPins.put(pins.getJSONObject(i)); } BasicDBObject newAttr = new BasicDBObject(); newAttr.append("pins", oldPins); BasicDBObject update = new BasicDBObject().append("$set", newAttr); baordsCollection.update(new BasicDBObject("ID", id), update); return new Text("baord " + id + " updated..."); }
From source file:edu.emory.bmi.datacafe.mongo.MongoUtil.java
License:Open Source License
/** * Gets a BasicDBObject with a few attributes added or removed. * * @param preferredAttributes the attributes to be added. * @param removedAttributes the attributes to be removed. * @return the BasicDBObject.//from w ww . j a v a 2s. c o m */ public static BasicDBObject getDBObjFromAttributes(String[] preferredAttributes, String[] removedAttributes) { BasicDBObject basicDBObject = new BasicDBObject(); if (preferredAttributes != null) { for (String preferredAttribute : preferredAttributes) { basicDBObject.append(preferredAttribute, 1); } } if (removedAttributes != null) { for (String removedAttribute : removedAttributes) { basicDBObject.append(removedAttribute, 0); } } return basicDBObject; }
From source file:edu.slu.action.ObjectAction.java
/** * Internal helper method to update the history.next property of an object. This will occur because updateObject will create a new object from a given object, and that * given object will have a new next value of the new object. Watch out for missing __rerum or malformed __rerum.history * /* w ww . j a va 2s .com*/ * @param idForUpdate the @id of the object whose history.next needs to be updated * @param newNextID the @id of the newly created object to be placed in the history.next array. * @return Boolean altered true on success, false on fail */ private boolean alterHistoryNext(String idForUpdate, String newNextID) { //TODO @theHabes As long as we trust the objects we send to this, we can take out the lookup and pass in objects as parameters System.out.println("Trying to alter history..."); Boolean altered = false; BasicDBObject query = new BasicDBObject(); query.append("@id", idForUpdate); DBObject myAnno = mongoDBService.findOneByExample(Constant.COLLECTION_ANNOTATION, query); DBObject myAnnoWithHistoryUpdate; JSONObject annoToUpdate = JSONObject.fromObject(myAnno); if (null != myAnno) { try { annoToUpdate.getJSONObject("__rerum").getJSONObject("history").getJSONArray("next").add(newNextID); //write back to the anno from mongo myAnnoWithHistoryUpdate = (DBObject) JSON.parse(annoToUpdate.toString()); //make the JSONObject a DB object System.out.println("Update in Mongo"); mongoDBService.update(Constant.COLLECTION_ANNOTATION, myAnno, myAnnoWithHistoryUpdate); //update in mongo System.out.println("Done"); altered = true; } catch (Exception e) { //@cubap @theHabes #44. What if obj does not have __rerum or __rerum.history System.out.println("This object did not have a propery history..."); writeErrorResponse( "This object does not contain the proper history property. It may not be from RERUM, the update failed.", HttpServletResponse.SC_CONFLICT); } } else { //THIS IS A 404 System.out.println("Couldnt find the object to alter history..."); writeErrorResponse("Object for update not found...", HttpServletResponse.SC_NOT_FOUND); } return altered; }
From source file:edu.slu.action.ObjectAction.java
/** * Public facing servlet action to find all upstream versions of an object. This is the action the user hits with the API. * If this object is `prime`, it will be the only object in the array. * @param oid variable assigned by urlrewrite rule for /id in urlrewrite.xml * @respond JSONArray to the response out for parsing by the client application. * @throws Exception /* w w w. jav a 2 s . c om*/ */ public void getAllAncestors() throws Exception { if (null != oid && methodApproval(request, "get")) { BasicDBObject query = new BasicDBObject(); query.append("_id", oid); BasicDBObject mongo_obj = (BasicDBObject) mongoDBService .findOneByExample(Constant.COLLECTION_ANNOTATION, query); if (null != mongo_obj) { JSONObject safe_received = JSONObject.fromObject(mongo_obj); //We can trust this is the object as it exists in mongo List<DBObject> ls_versions = getAllVersions(safe_received); JSONArray ancestors = getAllAncestors(ls_versions, safe_received, new JSONArray()); try { //@cubap @theHabes TODO how can we make this Web Annotation compliant? //addWebAnnotationHeaders(oid, isContainerType(ancestors), isLD(ancestors)); response.addHeader("Access-Control-Allow-Origin", "*"); response.setStatus(HttpServletResponse.SC_OK); out = response.getWriter(); out.write(mapper.writer().withDefaultPrettyPrinter().writeValueAsString(ancestors)); } catch (IOException ex) { Logger.getLogger(ObjectAction.class.getName()).log(Level.SEVERE, null, ex); } } else { writeErrorResponse("No object found with provided id '" + oid + "'.", HttpServletResponse.SC_NOT_FOUND); } } }
From source file:edu.slu.action.ObjectAction.java
/** * Public facing servlet to gather for all versions downstream from a provided `key object`. * @param oid variable assigned by urlrewrite rule for /id in urlrewrite.xml * @throws java.lang.Exception//from w w w.ja v a2 s.c o m * @respond JSONArray to the response out for parsing by the client application. */ public void getAllDescendents() throws Exception { if (null != oid && methodApproval(request, "get")) { BasicDBObject query = new BasicDBObject(); query.append("_id", oid); BasicDBObject mongo_obj = (BasicDBObject) mongoDBService .findOneByExample(Constant.COLLECTION_ANNOTATION, query); if (null != mongo_obj) { JSONObject safe_received = JSONObject.fromObject(mongo_obj); //We can trust this is the object as it exists in mongo List<DBObject> ls_versions = getAllVersions(safe_received); JSONArray descendents = getAllDescendents(ls_versions, safe_received, new JSONArray()); try { //@cubap @theHabes TODO how can we make this Web Annotation compliant? //addWebAnnotationHeaders(oid, isContainerType(descendents), isLD(descendents)); response.addHeader("Access-Control-Allow-Origin", "*"); response.setStatus(HttpServletResponse.SC_OK); out = response.getWriter(); out.write(mapper.writer().withDefaultPrettyPrinter().writeValueAsString(descendents)); } catch (IOException ex) { Logger.getLogger(ObjectAction.class.getName()).log(Level.SEVERE, null, ex); } } else { writeErrorResponse("No object found with provided id '" + oid + "'.", HttpServletResponse.SC_NOT_FOUND); } } }
From source file:edu.slu.action.ObjectAction.java
/** * Internal private method to loads all derivative versions from the `root` object. It should always receive a reliable object, not one from the user. * Used to resolve the history tree for storing into memory. * @param obj A JSONObject to find all versions of. If it is root, make sure to prepend it to the result. If it isn't root, query for root from the ID * found in prime using that result as a reliable root object. * @return All versions from the store of the object in the request * @throws Exception /*from w w w . j a v a2 s . c o m*/ */ private List<DBObject> getAllVersions(JSONObject obj) throws Exception { List<DBObject> ls_versions = null; BasicDBObject rootObj; BasicDBObject query = new BasicDBObject(); BasicDBObject queryForRoot = new BasicDBObject(); String primeID; //@cubap @theHabes #44. What if obj does not have __rerum or __rerum.history if (obj.getJSONObject("__rerum").getJSONObject("history").getString("prime").equals("root")) { primeID = obj.getString("@id"); //Get all objects whose prime is this things @id query.append("__rerum.history.prime", primeID); ls_versions = mongoDBService.findByExample(Constant.COLLECTION_ANNOTATION, query); for (int i = 0; i < ls_versions.size(); i++) { BasicDBObject version = (BasicDBObject) ls_versions.get(i); version.remove("_id"); ls_versions.set(i, version); } rootObj = (BasicDBObject) JSON.parse(obj.toString()); rootObj.remove("_id"); //Prepend the rootObj we know about ls_versions.add(0, rootObj); } else { primeID = obj.getJSONObject("__rerum").getJSONObject("history").getString("prime"); //Get all objects whose prime is equal to this ID query.append("__rerum.history.prime", primeID); ls_versions = mongoDBService.findByExample(Constant.COLLECTION_ANNOTATION, query); for (int i = 0; i < ls_versions.size(); i++) { BasicDBObject version = (BasicDBObject) ls_versions.get(i); version.remove("_id"); ls_versions.set(i, version); } queryForRoot.append("@id", primeID); rootObj = (BasicDBObject) mongoDBService.findOneByExample(Constant.COLLECTION_ANNOTATION, queryForRoot); //Prepend the rootObj whose ID we knew and we queried for rootObj.remove("_id"); ls_versions.add(0, rootObj); } return ls_versions; }
From source file:edu.slu.action.ObjectAction.java
/** * Get annotation by objectiD. Strip all unnecessary key:value pairs before returning. * @param oid variable assigned by urlrewrite rule for /id in urlrewrite.xml * @rspond with the new annotation ID in the Location header and the new object created in the body. * @throws java.io.IOException//from w w w .j av a2 s .co m * @throws javax.servlet.ServletException */ public void getByID() throws IOException, ServletException, Exception { request.setCharacterEncoding("UTF-8"); if (null != oid && methodApproval(request, "get")) { //find one version by objectID BasicDBObject query = new BasicDBObject(); query.append("_id", oid); DBObject myAnno = mongoDBService.findOneByExample(Constant.COLLECTION_ANNOTATION, query); if (null != myAnno) { BasicDBObject bdbo = (BasicDBObject) myAnno; JSONObject jo = JSONObject.fromObject(myAnno.toMap()); //String idForHeader = jo.getString("_id"); //The following are rerum properties that should be stripped. They should be in __rerum. jo.remove("_id"); jo.remove("addedTime"); jo.remove("originalAnnoID"); jo.remove("version"); jo.remove("permission"); jo.remove("forkFromID"); // retained for legacy v0 objects jo.remove("serverName"); jo.remove("serverIP"); // @context may not be here and shall not be added, but the response // will not be ld+json without it. try { addWebAnnotationHeaders(oid, isContainerType(jo), isLD(jo)); response.addHeader("Content-Type", "application/json; charset=utf-8"); response.addHeader("Access-Control-Allow-Origin", "*"); response.setStatus(HttpServletResponse.SC_OK); out = response.getWriter(); out.write(mapper.writer().withDefaultPrettyPrinter().writeValueAsString(jo)); } catch (IOException ex) { Logger.getLogger(ObjectAction.class.getName()).log(Level.SEVERE, null, ex); } } else { writeErrorResponse("No object found with provided id '" + oid + "'.", HttpServletResponse.SC_NOT_FOUND); } } }
From source file:edu.slu.action.ObjectAction.java
/** * Get annotations by given properties. * @param Object with key:value pairs with conditions to match against. * @reutrn list of annotations that match the given conditions. DO NOT RETURN DELETED OBJECTS *///from w w w. j a va 2 s .co m // This is not Web Annotation standard as the specifications states you respond with a single object, not a list. Not sure what to do with these. // @cubap answer: I asked on oac-discuss and was told Web Annotation hasn't handled lists yet, so just be nice. public void getByProperties() throws IOException, ServletException, Exception { //System.out.println("v1 getByProperties"); //want to use methodApproval(request, "get"), but these have body so...post if (null != processRequestBody(request, false) && methodApproval(request, "getProps")) { JSONObject received = JSONObject.fromObject(content); BasicDBObject query = new BasicDBObject(); Set<String> set_received = received.keySet(); for (String key : set_received) { query.append(key, received.get(key)); } //Ignore deleted annotations! https://docs.mongodb.com/v2.6/reference/operator/query/exists/ JSONObject existsFlag = new JSONObject(); existsFlag.element("$exists", false); query.append("__deleted", existsFlag); List<DBObject> ls_result = mongoDBService.findByExample(Constant.COLLECTION_ANNOTATION, query); JSONArray ja = new JSONArray(); for (DBObject dbo : ls_result) { BasicDBObject itemToAdd = (BasicDBObject) dbo; itemToAdd.remove("_id"); itemToAdd.remove("addedTime"); // retained for legacy v0 objects itemToAdd.remove("originalAnnoID");// retained for legacy v0 objects itemToAdd.remove("version");// retained for legacy v0 objects itemToAdd.remove("permission");// retained for legacy v0 objects itemToAdd.remove("forkFromID"); // retained for legacy v0 objects itemToAdd.remove("serverName");// retained for legacy v0 objects itemToAdd.remove("serverIP");// retained for legacy v0 objects ja.add((BasicDBObject) itemToAdd); } try { response.addHeader("Content-Type", "application/json; charset=utf-8"); // not ld+json because it is an array response.setCharacterEncoding("UTF-8"); response.addHeader("Access-Control-Allow-Origin", "*"); response.setStatus(HttpServletResponse.SC_OK); out = response.getWriter(); out.write(mapper.writer().withDefaultPrettyPrinter().writeValueAsString(ja)); } catch (IOException ex) { Logger.getLogger(ObjectAction.class.getName()).log(Level.SEVERE, null, ex); } /* if(ls_result.size() > 0){ try { response.addHeader("Content-Type","application/json"); // not ld+json because it is an array response.addHeader("Access-Control-Allow-Origin", "*"); response.setStatus(HttpServletResponse.SC_OK); out = response.getWriter(); out.write(mapper.writer().withDefaultPrettyPrinter().writeValueAsString(ja)); } catch (IOException ex) { Logger.getLogger(ObjectAction.class.getName()).log(Level.SEVERE, null, ex); } } else{ writeErrorResponse("Object(s) not found using provided properties '"+received+"'.", HttpServletResponse.SC_NOT_FOUND); } */ } }
From source file:edu.slu.action.ObjectAction.java
/** * Public facing servlet to PATCH set values of an existing RERUM object. * @respond with state of new object in the body * @throws java.io.IOException/* w ww .j ava 2 s . c o m*/ * @throws javax.servlet.ServletException */ public void patchSetUpdate() throws IOException, ServletException, Exception { Boolean historyNextUpdatePassed = false; System.out.println("patch set update"); if (null != processRequestBody(request, true) && methodApproval(request, "set")) { BasicDBObject query = new BasicDBObject(); JSONObject received = JSONObject.fromObject(content); if (received.containsKey("@id")) { String updateHistoryNextID = received.getString("@id"); query.append("@id", updateHistoryNextID); BasicDBObject originalObject = (BasicDBObject) mongoDBService .findOneByExample(Constant.COLLECTION_ANNOTATION, query); //The originalObject DB object BasicDBObject updatedObject = (BasicDBObject) originalObject.copy(); //A copy of the original, this will be saved as a new object. Make all edits to this variable. boolean alreadyDeleted = checkIfDeleted(JSONObject.fromObject(originalObject)); boolean isReleased = checkIfReleased(JSONObject.fromObject(originalObject)); if (alreadyDeleted) { writeErrorResponse("The object you are trying to update is deleted.", HttpServletResponse.SC_FORBIDDEN); } else if (isReleased) { writeErrorResponse("The object you are trying to update is released. Fork to make changes.", HttpServletResponse.SC_FORBIDDEN); } else { if (null != originalObject) { Set<String> update_anno_keys = received.keySet(); int updateCount = 0; //If the object already in the database contains the key found from the object recieved from the user... for (String key : update_anno_keys) { if (originalObject.containsKey(key)) { //Keys matched. Ignore it, set only works for new keys. //@cubap @theHabes do we want to build that this happened into the response at all? } else { //this is a new key, this is a set. Allow null values. updatedObject.append(key, received.get(key)); updateCount += 1; } } if (updateCount > 0) { JSONObject newObject = JSONObject.fromObject(updatedObject);//The edited original object meant to be saved as a new object (versioning) newObject = configureRerumOptions(newObject, true); //__rerum for the new object being created because of the update action newObject.remove("@id"); //This is being saved as a new object, so remove this @id for the new one to be set. //Since we ignore changes to __rerum for existing objects, we do no configureRerumOptions(updatedObject); DBObject dbo = (DBObject) JSON.parse(newObject.toString()); String newNextID = mongoDBService.save(Constant.COLLECTION_ANNOTATION, dbo); String newNextAtID = Constant.RERUM_ID_PREFIX + newNextID; BasicDBObject dboWithObjectID = new BasicDBObject((BasicDBObject) dbo); dboWithObjectID.append("@id", newNextAtID); newObject.element("@id", newNextAtID); newObject.remove("_id"); mongoDBService.update(Constant.COLLECTION_ANNOTATION, dbo, dboWithObjectID); historyNextUpdatePassed = alterHistoryNext(updateHistoryNextID, newNextAtID); //update history.next or original object to include the newObject @id if (historyNextUpdatePassed) { System.out.println("object patch set updated: " + newNextAtID); JSONObject jo = new JSONObject(); JSONObject iiif_validation_response = checkIIIFCompliance(newNextAtID, "2.1"); jo.element("code", HttpServletResponse.SC_OK); jo.element("original_object_id", updateHistoryNextID); jo.element("new_obj_state", newObject); //FIXME: @webanno standards say this should be the response. jo.element("iiif_validation", iiif_validation_response); try { addWebAnnotationHeaders(newNextID, isContainerType(newObject), isLD(newObject)); addLocationHeader(newObject); response.addHeader("Content-Type", "application/json; charset=utf-8"); response.setContentType("UTF-8"); response.addHeader("Access-Control-Allow-Origin", "*"); response.setStatus(HttpServletResponse.SC_OK); out = response.getWriter(); out.write(mapper.writer().withDefaultPrettyPrinter().writeValueAsString(jo)); } catch (IOException ex) { Logger.getLogger(ObjectAction.class.getName()).log(Level.SEVERE, null, ex); } } else { //The error is already written to response.out, do nothing. } } else { // Nothing could be patched addLocationHeader(received); writeErrorResponse("Nothing could be PATCHed", HttpServletResponse.SC_NO_CONTENT); } } else { //This could means it was an external object, but those fail for PATCH updates. writeErrorResponse("Object " + received.getString("@id") + " not found in RERUM, could not update. PUT update to make this object a part of RERUM.", HttpServletResponse.SC_BAD_REQUEST); } } } else { writeErrorResponse("Object did not contain an @id, could not update.", HttpServletResponse.SC_BAD_REQUEST); } } }
From source file:edu.slu.action.ObjectAction.java
/** * Public facing servlet to PATCH unset values of an existing RERUM object. * @respond with state of new object in the body * @throws java.io.IOException/*from w w w.jav a 2s. c o m*/ * @throws javax.servlet.ServletException */ public void patchUnsetUpdate() throws IOException, ServletException, Exception { Boolean historyNextUpdatePassed = false; System.out.println("Patch unset update"); if (null != processRequestBody(request, true) && methodApproval(request, "unset")) { BasicDBObject query = new BasicDBObject(); JSONObject received = JSONObject.fromObject(content); if (received.containsKey("@id")) { String updateHistoryNextID = received.getString("@id"); query.append("@id", updateHistoryNextID); BasicDBObject originalObject = (BasicDBObject) mongoDBService .findOneByExample(Constant.COLLECTION_ANNOTATION, query); //The originalObject DB object BasicDBObject updatedObject = (BasicDBObject) originalObject.copy(); //A copy of the original, this will be saved as a new object. Make all edits to this variable. boolean alreadyDeleted = checkIfDeleted(JSONObject.fromObject(originalObject)); boolean isReleased = checkIfReleased(JSONObject.fromObject(originalObject)); if (alreadyDeleted) { writeErrorResponse("The object you are trying to update is deleted.", HttpServletResponse.SC_FORBIDDEN); } else if (isReleased) { writeErrorResponse("The object you are trying to update is released. Fork to make changes.", HttpServletResponse.SC_FORBIDDEN); } else { if (null != originalObject) { Set<String> update_anno_keys = received.keySet(); int updateCount = 0; //If the object already in the database contains the key found from the object recieved from the user... for (String key : update_anno_keys) { if (originalObject.containsKey(key)) { if (key.equals("@id") || key.equals("__rerum") || key.equals("objectID") || key.equals("_id")) { // Ignore these in a PATCH. DO NOT update, DO NOT count as an attempt to update } else { if (null != received.get(key)) { //Found matching keys and value is not null. Ignore these. //@cubap @theHabes do we want to build that this happened into the response at all? } else { //Found matching keys and value is null, this is an unset updatedObject.remove(key); updateCount += 1; } } } else { //Original object does not contain this key, perhaps the user meant set. //@cubap @theHabes do we want to build that this happened into the response at all? } } if (updateCount > 0) { JSONObject newObject = JSONObject.fromObject(updatedObject);//The edited original object meant to be saved as a new object (versioning) newObject = configureRerumOptions(newObject, true); //__rerum for the new object being created because of the update action newObject.remove("@id"); //This is being saved as a new object, so remove this @id for the new one to be set. //Since we ignore changes to __rerum for existing objects, we do no configureRerumOptions(updatedObject); DBObject dbo = (DBObject) JSON.parse(newObject.toString()); String newNextID = mongoDBService.save(Constant.COLLECTION_ANNOTATION, dbo); String newNextAtID = Constant.RERUM_ID_PREFIX + newNextID; BasicDBObject dboWithObjectID = new BasicDBObject((BasicDBObject) dbo); dboWithObjectID.append("@id", newNextAtID); newObject.element("@id", newNextAtID); newObject.remove("_id"); mongoDBService.update(Constant.COLLECTION_ANNOTATION, dbo, dboWithObjectID); historyNextUpdatePassed = alterHistoryNext(updateHistoryNextID, newNextAtID); //update history.next or original object to include the newObject @id if (historyNextUpdatePassed) { System.out.println("Patch unset updated: " + newNextAtID); JSONObject jo = new JSONObject(); JSONObject iiif_validation_response = checkIIIFCompliance(newNextAtID, "2.1"); jo.element("code", HttpServletResponse.SC_OK); jo.element("original_object_id", updateHistoryNextID); jo.element("new_obj_state", newObject); //FIXME: @webanno standards say this should be the response. jo.element("iiif_validation", iiif_validation_response); try { addWebAnnotationHeaders(newNextID, isContainerType(newObject), isLD(newObject)); addLocationHeader(newObject); response.addHeader("Access-Control-Allow-Origin", "*"); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("Content-Type", "application/json; charset=utf-8"); response.setContentType("UTF-8"); out = response.getWriter(); out.write(mapper.writer().withDefaultPrettyPrinter().writeValueAsString(jo)); } catch (IOException ex) { Logger.getLogger(ObjectAction.class.getName()).log(Level.SEVERE, null, ex); } } else { //The error is already written to response.out, do nothing. } } else { // Nothing could be patched addLocationHeader(received); writeErrorResponse("Nothing could be PATCHed", HttpServletResponse.SC_NO_CONTENT); } } else { //This could means it was an external object, but those fail for PATCH updates. writeErrorResponse("Object " + received.getString("@id") + " not found in RERUM, could not update. PUT update to make this object a part of RERUM.", HttpServletResponse.SC_BAD_REQUEST); } } } else { writeErrorResponse("Object did not contain an @id, could not update.", HttpServletResponse.SC_BAD_REQUEST); } } }