public String parseJSONFind(String workspaceID, String jsonString) { StringBuffer ret = new StringBuffer(); ret.append("{\n"); System.out.println(workspaceID); System.out.println(jsonString); int counter = 0; DB db = m.getDB(Tokens.WORKSPACE_DATABASE); DBCollection coll = db.getCollection(workspaceID); BasicDBObject query = (BasicDBObject) JSON.parse(jsonString); // FixStrings.usr2mongo(jsonString) System.out.println("query: " + query.toString()); DBCursor find = coll.find(query); Iterator<DBObject> iter = find.iterator(); while (iter.hasNext()) { counter++; if (counter > 1) ret.append(",\n"); DBObject next = iter.next(); Map toMap = next.toMap(); ret.append("\"" + toMap.get("_id").toString() + "\" : "); // remove the redundant id next.removeField("_id"); // ret+= "\"kbid" + counter + "\" : "; String rec = FixStrings.mongo2usr(next.toString()); ret.append(rec); } ret.append("\n}\n"); // System.out.println(workspaceID); return ret.toString(); }
public String retrieveAll(Boolean onlyNames) { String ret = "["; DBObject resp; DBCursor cur = this.services.find(); while (cur.hasNext()) { resp = cur.next(); resp.removeField("_id"); System.out.println(resp); if (resp.get("state") == null || !((String) resp.get("state")).equals(State.VOID)) { if (onlyNames) { ret += resp.get("name") + ","; } else { ret += resp + ","; } } else { return null; } } ret = ret.substring(0, ret.length() - 1); ret += "]"; return ret; }
@Override public void clearFavorites(String email) throws UserNotFound { if (email == null) { throw new IllegalArgumentException("Email cannot be null"); } DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS); // Search user by email DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email)); if (userDb == null) { throw new UserNotFound("Unable to find user with email '" + email + "'"); } userDb.removeField(MongoDBConstants.USER_PROP_FAVORITES); col.save(userDb); }
public String retrieveJson(String name) { BasicDBObject query = new BasicDBObject("name", name); DBObject resp; // DBCursor cur; resp = this.services.findOne(query); // resp = cur.next(); if (resp != null) { resp.removeField("_id"); System.out.println(resp); } return resp.toString(); }
public <T> Key<T> merge(T entity, WriteConcern wc) { LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>(); DBObject dbObj = mapr.toDBObject(entity, involvedObjects); Key<T> key = getKey(entity); entity = ProxyHelper.unwrap(entity); Object id = getId(entity); if (id == null) throw new MappingException("Could not get id for " + entity.getClass().getName()); Query<T> query = (Query<T>) createQuery(entity.getClass()).filter(Mapper.ID_KEY, id); // remove (immutable) _id field for update. dbObj.removeField(Mapper.ID_KEY); UpdateResults<T> res = update(query, new BasicDBObject("$set", dbObj), false, false, wc); // check for updated count if we have a gle CommandResult gle = res.getWriteResult().getCachedLastError(); if (gle != null && res.getUpdatedCount() == 0) throw new UpdateException("Not updated: " + gle); postSaveOperations(entity, dbObj, involvedObjects); return key; }
@Override public List<Favorite> removeFromFavorites(String email, List<Favorite> favoritesToRemove) throws UserNotFound { if (email == null) { throw new IllegalArgumentException("Email cannot be null"); } DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS); // Search user by email DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email)); if (userDb == null) { throw new UserNotFound("Unable to find user with email '" + email + "'"); } DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES); if (favoritesDb == null) { favoritesDb = new BasicDBObject(); } if (favoritesToRemove != null) { for (Favorite favoriteToRemove : favoritesToRemove) { DBObject favoriteItemsDb = (DBObject) favoritesDb.get(favoriteToRemove.getStationId()); if (favoriteItemsDb != null) { favoritesDb.removeField(favoriteToRemove.getStationId()); } } } userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb); col.save(userDb); List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size()); for (String stationId : favoritesDb.keySet()) { Favorite favorite = new Favorite(); favorite.setStationId(stationId); DBObject favoriteItemDb = (DBObject) favoritesDb.get(stationId); favorite.setLastMessageId( (Long) favoriteItemDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID)); returnValue.add(favorite); } return returnValue; }
/** * Removes the type information from the entire conversion result. * * @param object * @param recursively whether to apply the removal recursively * @return */ private Object removeTypeInfo(Object object, boolean recursively) { if (!(object instanceof DBObject)) { return object; } DBObject dbObject = (DBObject) object; String keyToRemove = null; for (String key : dbObject.keySet()) { if (recursively) { Object value = dbObject.get(key); if (value instanceof BasicDBList) { for (Object element : (BasicDBList) value) { removeTypeInfo(element, recursively); } } else { removeTypeInfo(value, recursively); } } if (typeMapper.isTypeKey(key)) { keyToRemove = key; if (!recursively) { break; } } } if (keyToRemove != null) { dbObject.removeField(keyToRemove); } return dbObject; }
/** * @param dbName * @param collName * @param documentId * @param content * @param requestEtag * @return */ @Override public OperationResult upsertDocumentPost( String dbName, String collName, Object documentId, DBObject content, ObjectId requestEtag) { DB db = client.getDB(dbName); DBCollection coll = db.getCollection(collName); ObjectId newEtag = new ObjectId(); if (content == null) { content = new BasicDBObject(); } content.put("_etag", newEtag); Object _idInContent = content.get("_id"); content.removeField("_id"); if (_idInContent == null) { // new document since the id was just auto-generated content.put("_id", documentId); coll.insert(content); return new OperationResult(HttpStatus.SC_CREATED, newEtag); } BasicDBObject idQuery = new BasicDBObject("_id", documentId); DBObject oldDocument = coll.findAndModify(idQuery, null, null, false, content, false, true); if (oldDocument != null) { // upsertDocument // check the old etag (in case restore the old document version) return optimisticCheckEtag(coll, oldDocument, newEtag, requestEtag, HttpStatus.SC_OK); } else { // insert return new OperationResult(HttpStatus.SC_CREATED, newEtag); } }
public Service retrieve(String name) { BasicDBObject query = new BasicDBObject("name", name); DBObject resp; // DBCursor cur; Service service = new Service(); ArrayList<BasicDBObject> params, plugins; resp = this.services.findOne(query); if (resp != null) { resp.removeField("_id"); System.out.println(resp); service.setName((String) resp.get("name")); service.setUri((String) resp.get("uri")); try { service.setState((String) resp.get("state")); } catch (TypeNotPresentException ex) { service.setState(State.GENERATED); } // service.setPluginName((String)resp.get("pluginName")); params = (ArrayList<BasicDBObject>) resp.get("params"); for (BasicDBObject param : params) { service.addParam((String) param.get("name"), (Boolean) param.get("required")); } plugins = (ArrayList<BasicDBObject>) resp.get("plugins"); for (BasicDBObject plugin : plugins) { service.addPlugin((String) plugin.get("class_name"), (String) plugin.get("type")); } return service; } else { return null; } }
private DBObject removeNullEntries(DBObject nativeEntry) { for (String key : new HashSet<String>(nativeEntry.keySet())) { Object o = nativeEntry.get(key); if (o == null) { nativeEntry.removeField(key); } else if (o instanceof Object[]) { for (Object o2 : (Object[]) o) { if (o2 instanceof DBObject) { removeNullEntries((DBObject) o2); } } } else if (o instanceof List) { for (Object o2 : (List) o) { if (o2 instanceof DBObject) { removeNullEntries((DBObject) o2); } } } else if (o instanceof DBObject) { removeNullEntries((DBObject) o); } } return nativeEntry; }
/** * The MongoDB dialect replaces the name of the column identifier, so when the tuple is extracted * from the db we replace the column name of the identifier with the original one. We are assuming * the identifier is not embedded and is a single property. */ private void replaceIdentifierColumnName(DBObject result, EntityKey key) { Object idValue = result.get(MongoDBDialect.ID_FIELDNAME); result.removeField(MongoDBDialect.ID_FIELDNAME); result.put(key.getColumnNames()[0], idValue); }