List of usage examples for com.mongodb.util JSON parse
public static Object parse(final String jsonString)
Parses a JSON string and returns a corresponding Java object.
From source file:ezbake.data.mongo.helper.MongoFindHelper.java
License:Apache License
/** * * @param skip/*www . j a v a 2 s . c o m*/ * @param limit * @param jsonProjection * @param jsonSort * @param security * @param count * @param operationType * @return */ public DBObject[] getFindAggregationCommandsArray_withcounter(int skip, int limit, String jsonProjection, String jsonSort, EzSecurityToken security, boolean count, String operationType) { final List<DBObject> aggregationCommandsList = new ArrayList<>(); // add the redact operator for the _ezFV field. DBObject redactCommand = RedactHelper.createRedactCommandViz(securityExpressionViz, RedactHelper.FORMAL_VISIBILITY_FIELD, security); DBObject redact = new BasicDBObject("$redact", redactCommand); aggregationCommandsList.add(redact); // add the redact operator for the _ezExtV field. redactCommand = RedactHelper.createRedactCommandViz(securityExpressionViz, RedactHelper.EXTERNAL_COMMUNITY_VISIBILITY_FIELD, security); redact = new BasicDBObject("$redact", redactCommand); aggregationCommandsList.add(redact); // add the redact operator for the Read/Write/Discover/Manage fields String objAuths = "[ ]"; if (security.authorizations != null && security.authorizations.platformObjectAuthorizations != null) { objAuths = Arrays.toString(security.authorizations.platformObjectAuthorizations.toArray()); } // determine the operation field - this is for Platform Visibilities final String field; switch (operationType) { case EzMongoHandler.WRITE_OPERATION: field = RedactHelper.PLATFORM_OBJECT_WRITE_VISIBILITY_FIELD; break; case EzMongoHandler.DISCOVER_OPERATION: field = RedactHelper.PLATFORM_OBJECT_DISCOVER_VISIBILITY_FIELD; break; case EzMongoHandler.MANAGE_OPERATION: field = RedactHelper.PLATFORM_OBJECT_MANAGE_VISIBILITY_FIELD; break; default: field = RedactHelper.PLATFORM_OBJECT_READ_VISIBILITY_FIELD; break; } redactCommand = RedactHelper.createRedactCommand(securityExpressionOperation, field, objAuths, RedactHelper.REDACT_TYPE_OPERATION); redact = new BasicDBObject("$redact", redactCommand); aggregationCommandsList.add(redact); if (!StringUtils.isEmpty(jsonSort)) { final DBObject sortCommand = (DBObject) JSON.parse(jsonSort); final DBObject sort = new BasicDBObject("$sort", sortCommand); aggregationCommandsList.add(sort); } // add the rest of the pipeline operators if (skip > 0) { final DBObject skipCommand = new BasicDBObject("$skip", skip); aggregationCommandsList.add(skipCommand); } if (limit > 0) { final DBObject limitCommand = new BasicDBObject("$limit", limit); aggregationCommandsList.add(limitCommand); } if (!StringUtils.isEmpty(jsonProjection)) { final DBObject projectionCommand = (DBObject) JSON.parse(jsonProjection); final DBObject projection = new BasicDBObject("$project", projectionCommand); aggregationCommandsList.add(projection); } // counter should look like==> "{$group: {_id: null, count: {$sum: 1}}}" if (count) { final DBObject groupFields = new BasicDBObject("_id", "null"); groupFields.put("count", new BasicDBObject("$sum", 1)); final DBObject counter = new BasicDBObject("$group", groupFields); aggregationCommandsList.add(counter); } final DBObject[] aggregationCommandsArray = aggregationCommandsList .toArray(new DBObject[aggregationCommandsList.size()]); appLog.info("in getFindAggregationCommandsArray, aggregationCommandsArray: {}", aggregationCommandsArray); return aggregationCommandsArray; }
From source file:ezbake.data.mongo.helper.MongoUpdateHelper.java
License:Apache License
public int updateDocument(String collectionName, String jsonQuery, String jsonDocument, boolean useUpdateOperators, Visibility vis, EzSecurityToken security) throws TException, EzMongoBaseException, VisibilityParseException, JSONException { TokenUtils.validateSecurityToken(security, ezMongoHandler.getConfigurationProperties()); if (StringUtils.isEmpty(collectionName)) { throw new EzMongoBaseException("collectionName is required."); }/*from w w w .java2s .co m*/ int updatedCount = 0; final String finalCollectionName = ezMongoHandler.getCollectionName(collectionName); DBObject content = (DBObject) JSON.parse(jsonDocument); // see if we are able to update the data in db with user's classification in the user token. // Also need to see if this is a WRITE or MANAGE operation - MANAGE is when users update the // security fields in the Mongo document. String operation = EzMongoHandler.WRITE_OPERATION; if (isUpdatingSecurityFields(content, vis)) { operation = EzMongoHandler.MANAGE_OPERATION; } final List<DBObject> results = ezMongoHandler.getMongoFindHelper().findElements(collectionName, jsonQuery, "{ _id: 1}", null, 0, 0, false, security, false, operation); if (results.size() > 0) { // construct a list of Objects to use as the filter final List<Object> idList = new ArrayList<Object>(); for (final DBObject result : results) { Object id = result.get("_id"); appLog.info("can update DBObject (_id): {}", id); idList.add(id); } final DBObject inClause = new BasicDBObject("$in", idList); final DBObject query = new BasicDBObject("_id", inClause); if (!useUpdateOperators) { if (operation.equals(EzMongoHandler.MANAGE_OPERATION)) { // we need to check if the user is allowed to update with the new Visibility ezMongoHandler.getMongoInsertHelper().checkAbilityToInsert(security, vis, content, null, true, false); } // use $set if we are not using mongo update operators (such as $pull) in the jsonDocument content = new BasicDBObject("$set", content); } else { // when using an update operator such as $pull and if we are doing a MANAGE operation - // we need to set the security fields in the object as well. if (operation.equals(EzMongoHandler.MANAGE_OPERATION)) { DBObject visDBObject = new BasicDBObject(); // we need to check if the user is allowed to update with the new Visibility ezMongoHandler.getMongoInsertHelper().checkAbilityToInsert(security, vis, visDBObject, null, true, false); content.put("$set", visDBObject); } } final boolean upsert = false; final boolean multi = true; updatedCount = updateContent(finalCollectionName, query, content, upsert, multi); } else { appLog.info("Did not find any documents to update."); } return updatedCount; }
From source file:ezbake.data.mongo.redact.RedactHelper.java
License:Apache License
public static DBObject createRedactCommand(String expression, String securityField, String auths, String redactType) {/*from w w w. ja v a 2s . com*/ securityField = "$" + securityField; appLog.info("createRedactCommand, securityField: {}", securityField); String userSecurityExpression = null; if (redactType.equals(REDACT_TYPE_VIZ)) { userSecurityExpression = String.format(expression, securityField, auths); } else if (redactType.equals(REDACT_TYPE_OPERATION)) { userSecurityExpression = String.format(expression, securityField, securityField, securityField, auths); } return (DBObject) JSON.parse(userSecurityExpression); }
From source file:ezbake.training.DatasetServlet.java
License:Apache License
/** * Creates a text index on the "text" field if it doesn't exist * * @throws TException/*from www . j a va 2 s . c o m*/ */ private void createTextIndex(MongoDatasetClient client) throws TException { boolean hasTextIndex = false; String namespace = null; logger.info("getting index info.."); List<String> indexList = client.getIndexInfo(COLLECTION_NAME); for (String index : indexList) { logger.info("we have an index: {}", index); DBObject indexObj = (DBObject) JSON.parse(index); String indexName = (String) indexObj.get("name"); if (namespace == null) { namespace = (String) indexObj.get("ns"); } if (indexName.equals(TEXT_FIELD_NAME + "_text")) { hasTextIndex = true; } } if (!hasTextIndex) { DBObject obj = new BasicDBObject(); // we are putting a text index on the "text" field in the mongo // collection obj.put(TEXT_FIELD_NAME, "text"); String jsonKeys = JSON.serialize(obj); logger.info("creating text index with jsonKeys: {}, COLLECTION_NAME: {}", jsonKeys, COLLECTION_NAME); client.createIndex(COLLECTION_NAME, jsonKeys, null); logger.info("DatasetServlet: created text index: {}", jsonKeys); } else { logger.info("DatasetServlet: we already have the text index."); } }
From source file:ezbake.training.MongoDbServlet.java
License:Apache License
/** * Creates a text index on the "text" field if it doesn't exist * * @throws TException//from ww w . j a v a 2 s .co m */ private void createTextIndex(MongoDatasetClient client) throws TException { boolean hasTextIndex = false; String namespace = null; logger.info("getting index info.."); List<String> indexList = client.getIndexInfo(COLLECTION_NAME); for (String index : indexList) { logger.info("we have an index: {}", index); DBObject indexObj = (DBObject) JSON.parse(index); String indexName = (String) indexObj.get("name"); if (namespace == null) { namespace = (String) indexObj.get("ns"); } if (indexName.equals(TWEET_TEXT_FIELD_NAME + "_text")) { hasTextIndex = true; } } if (!hasTextIndex) { DBObject obj = new BasicDBObject(); // we are putting a text index on the "text" field in the mongo collection obj.put(TWEET_TEXT_FIELD_NAME, "text"); String jsonKeys = JSON.serialize(obj); logger.info("creating text index with jsonKeys: {}, COLLECTION_NAME: {}", jsonKeys, COLLECTION_NAME); client.createIndex(COLLECTION_NAME, jsonKeys, null); logger.info("MongoDbServlet: created text index: {}", jsonKeys); } else { logger.info("MongoDbServlet: we already have the text index."); } }
From source file:ezbake.training.MongoDbServlet.java
License:Apache License
private String searchTweet(HttpServletRequest request, HttpServletResponse response) { String searchText = request.getParameter("searchText"); String result;/*from ww w .j av a 2s . c om*/ try { MongoDatasetClient client = MongoDatasetClient.getInstance(); createTextIndex(client); logger.info("searchText: {}", searchText); List<String> data = client.searchText(COLLECTION_NAME, searchText); if (data.size() == 0) { result = "No results found"; } else { StringBuilder buffer = new StringBuilder(); for (String tweetJSON : data) { buffer.append("<tr>"); DBObject dbObj = (DBObject) JSON.parse(tweetJSON); String _id = dbObj.get("_id").toString(); String id = null; // The "id" field can be a Long or Integer; // it will be Integer if content-publisher was invoked from the tweet webapp // to insert the tweet into mongo. Object idObj = dbObj.get("id"); if (idObj != null && (idObj instanceof Long || idObj instanceof Integer)) { id = idObj.toString(); } DBObject formalVisibilityObj = (DBObject) dbObj.get(RedactHelper.FORMAL_VISIBILITY_FIELD); String formalVisibility = null; if (formalVisibilityObj != null) { formalVisibility = formalVisibilityObj.toString(); } DBObject userObj = (DBObject) dbObj.get(USER_FIELD_NAME); String userName; if (userObj != null) { userName = (String) userObj.get(SCREEN_NAME_FIELD_NAME); if (userName == null) { // it's possible that the tweets were ingested with a // different field name for the screen name. userName = (String) userObj.get(SECONDARY_SCREEN_NAME_FIELD_NAME); } } else { // try a different field name for the username userName = (String) dbObj.get(USERNAME_FIELD_NAME); } String tweet = (String) dbObj.get(TWEET_TEXT_FIELD_NAME); // construct the columns to display on the jsp buffer.append("<td>"); buffer.append(_id); buffer.append("</td>"); buffer.append("<td>"); buffer.append(id); buffer.append("</td>"); buffer.append("<td>"); buffer.append(formalVisibility); buffer.append("</td>"); buffer.append("<td>"); buffer.append("@"); buffer.append(userName); buffer.append(": "); buffer.append(tweet); buffer.append("</td>"); buffer.append("</tr>"); } result = buffer.toString(); logger.info(result); } } catch (Exception e) { result = "Unable to retrieve any results: " + e.getMessage(); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return result; }
From source file:fr.cnes.sitools.dataset.database.mongodb.RequestMongoDB.java
License:Open Source License
@Override public String getFilterClause(List<Predicat> predicats, List<Column> columns) { BasicDBObject whereClause = new BasicDBObject(); // loop over the predicats // boolean first = true; // String result = "{"; ///*from w ww. j ava 2s .c om*/ // Map<String, List<Predicat>> orderedPredicats = orderPredicat(predicats); // for (Predicat predicat : predicats) { // // String filter = getFilter(predicat); // if (filter != null && !"".equals(filter)) { // // DBObject objPredicat = (DBObject) JSON.parse(filter); // // if (objPredicat != null) { // if (first) { // whereClause.append("$and", new ArrayList<DBObject>()); // } // else { // result += ","; // } // first = false; // // result += filter; // // // ((List<DBObject>) whereClause.get("$and")).add(objPredicat); // // // if (whereClause.containsField(key)) { // // // If the key already exists append the value to the existing key // // // DBObject obj = new BasicDBObject(); // // // obj.put("$and", objPredicat.get(key)); // // // // if (!whereClause.containsField("$and")) { // // whereClause.append("$and", new ArrayList<DBObject>()); // // DBObject pred = (DBObject) whereClause.get(key); // // whereClause.remove(key); // // ((List<DBObject>) whereClause.get("$and")).add(pred); // // // // } // // ((List<DBObject>) whereClause.get("$and")).add(objPredicat); // // // // // ((DBObject) whereClause.get(key)).putAll(obj); // // } // // else { // // // if the key doesn't exists just append the predicat to the whereClause // // whereClause.append(key, objPredicat.get(key)); // // } // // } // // } // } for (Predicat predicat : predicats) { String filter = getFilter(predicat); if (filter != null && !"".equals(filter)) { DBObject objPredicat = (DBObject) JSON.parse(filter); if (objPredicat != null) { Set<String> keys = objPredicat.keySet(); for (String key : keys) { if (whereClause.containsField(key)) { ((DBObject) whereClause.get(key)).putAll((DBObject) objPredicat.get(key)); } else { whereClause.append(key, objPredicat.get(key)); } } } } } return whereClause.toString(); }
From source file:fr.cnes.sitools.datasource.mongodb.business.SitoolsMongoDBDataSource.java
License:Open Source License
/** * Make the SQL request/*from ww w .j av a2s .c om*/ * * @param key * the key * @param request * the mongoBD request model * @return List of Object * * */ @SuppressWarnings("unchecked") public List<Object> distinctQuery(String key, MongoDBRequestModel request) { List<Object> results = null; try { DBObject dbObjectQuery = (DBObject) JSON.parse(request.getFilterString()); DB database = getDatabase(); DBCollection collection = database.getCollection(request.getCollectionName()); results = collection.distinct(key, dbObjectQuery); return results; } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } return null; }
From source file:fr.cnes.sitools.datasource.mongodb.business.SitoolsMongoDBDataSource.java
License:Open Source License
/** * Execute a query//from ww w.ja va 2 s.co m * * @param request the mongoBD request model * @return DBCursor */ public DBCursor limitedQuery(MongoDBRequestModel request) { DBCursor cursor = null; try { DBObject dbObjectQuery = (DBObject) JSON.parse(request.getFilterString()); DBObject dbObjectFields = (DBObject) JSON.parse(request.getKeysString()); DBObject dbObjectSort = (DBObject) JSON.parse(request.getSortString()); DB database = getDatabase(); DBCollection collection = database.getCollection(request.getCollectionName()); cursor = collection.find(dbObjectQuery, dbObjectFields).sort(dbObjectSort); cursor.skip(request.getStart()); cursor.limit(request.getLimit()); return cursor; } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); closeCursor(cursor); } return null; }
From source file:fr.cnes.sitools.datasource.mongodb.business.SitoolsMongoDBDataSource.java
License:Open Source License
/** * Gets the number of records for a given query * /* www .ja v a 2s. co m*/ * @param json * the json query * @param collectionName * the collection name to query * @return the number of records for the query, -1 if there is an error */ public int countQuery(String json, String collectionName) { DBObject dbObject = (DBObject) JSON.parse(json); DB database = getDatabase(); DBCollection collection = database.getCollection(collectionName); return (int) collection.count(dbObject); }