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:com.ga.forms.DailyLogAddUI.java
private void checkInOutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkInOutButtonActionPerformed DailyLogRecord log = new DailyLogRecord(); if (DailyLogAddUI.checkIn && !DailyLogAddUI.breakDone) { args = new HashMap(); DailyLogAddUI.breakDone = true;/*from w ww .ja v a2 s . c o m*/ args.put("checked-in", Boolean.toString(DailyLogAddUI.checkIn)); args.put("had-break", Boolean.toString(DailyLogAddUI.breakDone)); args.put("checked-out", Boolean.toString(DailyLogAddUI.checkOut)); if (yesRdButton.isSelected() == true) { this.timeOnBreak = "00:30"; } else if (customRdButton.isSelected() == true) { this.timeOnBreak = customBreakTimeTextField.getText(); } else { this.timeOnBreak = "00:00"; } log.setDailyLogRecord(dateDisplayLbl.getText(), dayDisplayLbl.getText(), checkInTimeCombo.getSelectedItem().toString(), "", this.timeOnBreak, "", "", "", args); doc = (DBObject) JSON.parse(log.getDailyLogRecord().toString()); args.clear(); args.put("date", dateDisplayLbl.getText()); log.updateRecord(doc, args); } else if (DailyLogAddUI.checkIn && DailyLogAddUI.breakDone && !DailyLogAddUI.checkOut) { args = new HashMap(); DailyLogAddUI.checkOut = true; args.put("checked-in", Boolean.toString(DailyLogAddUI.checkIn)); args.put("had-break", Boolean.toString(DailyLogAddUI.breakDone)); args.put("checked-out", Boolean.toString(DailyLogAddUI.checkOut)); DailyLogDuration durationAgent = new DailyLogDuration(); durationAgent.calculateCurrentDuration(checkInTimeCombo.getSelectedItem().toString(), this.timeOnBreak, checkOutTimeCombo.getSelectedItem().toString()); this.duration = durationAgent.getCurrentDuration(); JFrame parent = new JFrame(); JOptionPane optionPane = new JOptionPane("Duration: " + this.duration + "\n\nDo you want to Check Out?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); JDialog msgDialog = optionPane.createDialog(parent, "DLM"); msgDialog.setVisible(true); if (optionPane.getValue().equals(0)) { int hours = Integer.parseInt(this.duration.split(":")[0]); int minutes = Integer.parseInt(this.duration.split(":")[1]); if (hours < 9) { durationAgent.calculateUnderTime(this.duration); this.underTime = durationAgent.getUnderTime(); this.overTime = "00:00"; } else if (hours >= 9 && minutes > 0 && minutes < 60) { this.underTime = "00:00"; durationAgent.calculateOverTime(this.duration); this.overTime = durationAgent.getOverTime(); } else { this.underTime = "00:00"; this.overTime = "00:00"; } log.setDailyLogRecord(dateDisplayLbl.getText(), dayDisplayLbl.getText(), checkInTimeCombo.getSelectedItem().toString(), checkOutTimeCombo.getSelectedItem().toString(), this.timeOnBreak, this.duration, this.underTime, this.overTime, args); doc = (DBObject) JSON.parse(log.getDailyLogRecord().toString()); args.clear(); args.put("date", dateDisplayLbl.getText()); log.updateRecord(doc, args); } else { msgDialog.dispose(); } } else { args = new HashMap(); DailyLogAddUI.checkIn = true; args.put("checked-in", Boolean.toString(DailyLogAddUI.checkIn)); args.put("had-break", Boolean.toString(DailyLogAddUI.breakDone)); args.put("checked-out", Boolean.toString(DailyLogAddUI.checkOut)); log.setDailyLogRecord(dateDisplayLbl.getText(), dayDisplayLbl.getText(), checkInTimeCombo.getSelectedItem().toString(), "", "", "", "", "", args); doc = (DBObject) JSON.parse(log.getDailyLogRecord().toString()); log.insertRecord(doc); } }
From source file:com.gatf.executor.dataprovider.MongoDBTestDataSource.java
License:Apache License
public List<Map<String, String>> provide(GatfTestDataProvider provider, AcceptanceTestContext context) { List<Map<String, String>> result = new ArrayList<Map<String, String>>(); Assert.assertNotNull("provider cannot be null", provider); Assert.assertTrue("provider cannot be null", provider.getArgs() != null && provider.getArgs().length > 0); Assert.assertNotNull("mongodb-collection cannot be empty", provider.getArgs()[0]); Assert.assertNotNull("queryString cannot be empty", provider.getQueryStr()); Assert.assertNotNull("variableNames cannot be empty", provider.getSourceProperties()); Assert.assertNotNull("propertyNames cannot be empty", provider.getProviderProperties()); String dbName = args[2].trim(); String collName = provider.getArgs()[0].trim(); String queryString = provider.getQueryStr().trim(); String variableNames = provider.getProviderProperties(); String propertyNames = provider.getSourceProperties(); Assert.assertNotNull("mongodb-collection cannot be empty", collName.isEmpty()); Assert.assertFalse("queryString cannot be empty", queryString.isEmpty()); List<String> variableNamesArr = new ArrayList<String>(); for (String varName : variableNames.split(",")) { if (!varName.trim().isEmpty()) { variableNamesArr.add(varName); }/*from w w w.j a v a 2 s.c o m*/ } Assert.assertTrue("need to define at-least a single variable name", !variableNames.isEmpty() && variableNames.split(",").length > 0 && variableNamesArr.size() > 0); List<String> propertyNamesArr = new ArrayList<String>(); for (String varName : propertyNames.split(",")) { if (!varName.trim().isEmpty()) { propertyNamesArr.add(varName); } } Assert.assertTrue("need to define at-least a single property name", !propertyNames.isEmpty() && propertyNames.split(",").length > 0 && propertyNamesArr.size() > 0); Assert.assertTrue("property name and variable name sizes don't match", propertyNamesArr.size() == variableNamesArr.size()); StringBuilder build = new StringBuilder(); build.append("Provider configuration [\n"); build.append(String.format("dataSource name is %s\n", getDataSourceName())); build.append(String.format("mongodb-collection is %s\n", collName)); build.append(String.format("queryString is %s\n", queryString)); build.append(String.format("propertyNames is %s\n", propertyNames)); build.append(String.format("variableNames is %s]", variableNames)); logger.info(build.toString()); Resource res = null; try { res = getResource(); MongoClient mongoClient = (MongoClient) res.object; DB db = null; try { db = mongoClient.getDB(dbName); DBCollection coll = db.getCollection(collName); Assert.assertNotNull(String.format("Mongodb collection %s not found", collName), coll); DBObject queryObject = null; try { queryObject = (DBObject) JSON.parse(queryString); } catch (Exception e) { Assert.assertNotNull("queryString passed is invalid"); } DBCursor cursor = null; try { cursor = coll.find(queryObject); while (cursor.hasNext()) { DBObject object = cursor.next(); Map<String, String> row = new HashMap<String, String>(); for (int i = 0; i < variableNamesArr.size(); i++) { Assert.assertTrue( String.format("Could not find %s field in the result document returned", propertyNamesArr.get(i)), object.containsField(propertyNamesArr.get(i))); row.put(variableNamesArr.get(i), object.get(propertyNamesArr.get(i)).toString()); } result.add(row); } } catch (Exception e) { throw new AssertionError(e); } finally { if (cursor != null) cursor.close(); } } catch (Exception e) { throw new AssertionError( String.format("Fetching Test Data failed while executing query %s with the error %s", queryString, ExceptionUtils.getStackTrace(e))); } finally { if (mongoClient != null) mongoClient.close(); } } catch (Exception e) { throw new AssertionError( String.format("Fetching Test Data failed while executing query %s with the error %s", queryString, ExceptionUtils.getStackTrace(e))); } finally { if (res != null) releaseToPool(res); } return result; }
From source file:com.gigaspaces.persistency.datasource.MongoQueryFactory.java
License:Open Source License
public static BasicDBObjectBuilder bind(String parsedQuery, Object[] parameters, SpaceTypeDescriptor spaceTypeDescriptor) { SpaceDocumentMapper<DBObject> mapper = new DefaultSpaceDocumentMapper(spaceTypeDescriptor); DBObject obj = (DBObject) JSON.parse(parsedQuery); BasicDBObjectBuilder query = BasicDBObjectBuilder.start(obj.toMap()); if (parameters != null) { query = replaceParameters(parameters, mapper, query, 0); }//from www.j av a 2 s . co m return query; }
From source file:com.github.bluetiger9.nosql.benchmarking.clients.document.couchbase.CouchbaseClient.java
License:Open Source License
@Override public Map<String, String> get(String key) throws ClientException { final String json = (String) super.get(key); if (json == null) return null; @SuppressWarnings("unchecked") final Map<String, String> document = (Map<String, String>) JSON.parse(json); final Map<String, String> result = new HashMap<>(); for (Map.Entry<String, String> entry : document.entrySet()) { result.put(entry.getKey(), entry.getValue()); }/*from w ww . j a va 2 s . co m*/ return result; }
From source file:com.github.maasdi.di.trans.steps.mongodbdelete.MongoDbDelete.java
License:Apache License
public DBObject getQueryFromJSON(String json, Object[] row) throws KettleException { DBObject query;/* w w w. j a v a 2s . c om*/ String jsonQuery = environmentSubstitute(json); if (Const.isEmpty(jsonQuery)) { query = new BasicDBObject(); } else { if (meta.isExecuteForEachIncomingRow() && row != null) { jsonQuery = fieldSubstitute(jsonQuery, getInputRowMeta(), row); } query = (DBObject) JSON.parse(jsonQuery); } return query; }
From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java
License:Apache License
/** * Utility method to configure Mongo connection options * * @param optsBuilder/*from ww w . j a v a 2 s . c om*/ * an options builder * @param connTimeout * the connection timeout to use (can be null) * @param socketTimeout * the socket timeout to use (can be null) * @param readPreference * the read preference to use (can be null) * @param writeConcern * the writeConcern to use (can be null) * @param wTimeout * the w timeout to use (can be null) * @param journaled * whether to use journaled writes * @param tagSet * the tag set to use in conjunction with the read preference (can be null) * @param vars * variables to use * @param log * for logging * @throws KettleException * if a problem occurs */ private void configureConnectionOptions(MongoClientOptions.Builder optsBuilder, String connTimeout, String socketTimeout, String readPreference, String writeConcern, String wTimeout, boolean journaled, List<String> tagSet, VariableSpace vars, LogChannelInterface log) throws KettleException { // connection timeout if (!Const.isEmpty(connTimeout)) { String connS = vars.environmentSubstitute(connTimeout); try { int cTimeout = Integer.parseInt(connS); if (cTimeout > 0) { optsBuilder.connectTimeout(cTimeout); } } catch (NumberFormatException n) { throw new KettleException(n); } } // socket timeout if (!Const.isEmpty(socketTimeout)) { String sockS = vars.environmentSubstitute(socketTimeout); try { int sockTimeout = Integer.parseInt(sockS); if (sockTimeout > 0) { optsBuilder.socketTimeout(sockTimeout); } } catch (NumberFormatException n) { throw new KettleException(n); } } if (log != null) { String rpLogSetting = NamedReadPreference.PRIMARY.getName(); if (!Const.isEmpty(readPreference)) { rpLogSetting = readPreference; } log.logBasic( BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.UsingReadPreference", rpLogSetting)); //$NON-NLS-1$ } DBObject firstTagSet = null; DBObject[] remainingTagSets = new DBObject[0]; if (tagSet != null && tagSet.size() > 0) { if (tagSet.size() > 1) { remainingTagSets = new DBObject[tagSet.size() - 1]; } firstTagSet = (DBObject) JSON.parse(tagSet.get(0).trim()); for (int i = 1; i < tagSet.size(); i++) { remainingTagSets[i - 1] = (DBObject) JSON.parse(tagSet.get(i).trim()); } if (log != null && (!Const.isEmpty(readPreference) && !readPreference.equalsIgnoreCase(NamedReadPreference.PRIMARY.getName()))) { StringBuilder builder = new StringBuilder(); for (String s : tagSet) { builder.append(s).append(" "); //$NON-NLS-1$ } log.logBasic(BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.UsingReadPreferenceTagSets", //$NON-NLS-1$ builder.toString())); } } else { if (log != null) { log.logBasic( BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.NoReadPreferenceTagSetsDefined")); //$NON-NLS-1$ } } // read preference if (!Const.isEmpty(readPreference)) { String rp = vars.environmentSubstitute(readPreference); NamedReadPreference preference = NamedReadPreference.byName(rp); if ((firstTagSet != null) && (preference.getPreference() instanceof TaggableReadPreference)) { optsBuilder.readPreference(preference.getTaggableReadPreference(firstTagSet, remainingTagSets)); } else { optsBuilder.readPreference(preference.getPreference()); } } // write concern writeConcern = vars.environmentSubstitute(writeConcern); wTimeout = vars.environmentSubstitute(wTimeout); WriteConcern concern = null; if (Const.isEmpty(writeConcern) && Const.isEmpty(wTimeout) && !journaled) { // all defaults - timeout 0, journal = false, w = 1 concern = new WriteConcern(); concern.setWObject(new Integer(1)); if (log != null) { log.logBasic(BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.ConfiguringWithDefaultWriteConcern")); //$NON-NLS-1$ } } else { int wt = 0; if (!Const.isEmpty(wTimeout)) { try { wt = Integer.parseInt(wTimeout); } catch (NumberFormatException n) { throw new KettleException(n); } } if (!Const.isEmpty(writeConcern)) { // try parsing as a number first try { int wc = Integer.parseInt(writeConcern); concern = new WriteConcern(wc, wt, false, journaled); } catch (NumberFormatException n) { // assume its a valid string - e.g. "majority" or a custom // getLastError label associated with a tag set concern = new WriteConcern(writeConcern, wt, false, journaled); } } else { concern = new WriteConcern(1, wt, false, journaled); } if (log != null) { String lwc = "w = " + concern.getW() + ", wTimeout = " + concern.getWtimeout() + ", journaled = " + concern.getJ(); log.logBasic( BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.ConfiguringWithWriteConcern", lwc)); } } optsBuilder.writeConcern(concern); }
From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java
License:Apache License
public List<MongoField> discoverFields(String db, String collection, String query, String fields, boolean isPipeline, int docsToSample) throws KettleException { DBCursor cursor = null;//from w w w . ja v a 2 s . co m try { int numDocsToSample = docsToSample; if (numDocsToSample < 1) { numDocsToSample = 100; // default } List<MongoField> discoveredFields = new ArrayList<MongoField>(); Map<String, MongoField> fieldLookup = new HashMap<String, MongoField>(); try { DB database = getDb(db); if (Const.isEmpty(collection)) { throw new KettleException( BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.NoCollectionSpecified")); //$NON-NLS-1$ } DBCollection dbcollection = database.getCollection(collection); Iterator<DBObject> pipeSample = null; if (isPipeline) { pipeSample = setUpPipelineSample(query, numDocsToSample, dbcollection); } else { if (Const.isEmpty(query) && Const.isEmpty(fields)) { cursor = dbcollection.find().limit(numDocsToSample); } else { DBObject dbObject = (DBObject) JSON.parse(Const.isEmpty(query) ? "{}" //$NON-NLS-1$ : query); DBObject dbObject2 = (DBObject) JSON.parse(fields); cursor = dbcollection.find(dbObject, dbObject2).limit(numDocsToSample); } } int actualCount = 0; while (cursor != null ? cursor.hasNext() : pipeSample.hasNext()) { actualCount++; DBObject nextDoc = (cursor != null ? cursor.next() : pipeSample.next()); docToFields(nextDoc, fieldLookup); } postProcessPaths(fieldLookup, discoveredFields, actualCount); return discoveredFields; } catch (Exception e) { throw new KettleException(e); } finally { if (cursor != null) { cursor.close(); } } } catch (Exception ex) { if (ex instanceof KettleException) { throw (KettleException) ex; } else { throw new KettleException( BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.UnableToDiscoverFields"), ex); //$NON-NLS-1$ } } }
From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java
License:Apache License
public static List<DBObject> jsonPipelineToDBObjectList(String jsonPipeline) throws KettleException { List<DBObject> pipeline = new ArrayList<DBObject>(); StringBuilder b = new StringBuilder(jsonPipeline.trim()); // extract the parts of the pipeline int bracketCount = -1; List<String> parts = new ArrayList<String>(); int i = 0;//from ww w . j a va 2 s.c o m while (i < b.length()) { if (b.charAt(i) == '{') { if (bracketCount == -1) { // trim anything off before this point b.delete(0, i); bracketCount = 0; i = 0; } bracketCount++; } if (b.charAt(i) == '}') { bracketCount--; } if (bracketCount == 0) { String part = b.substring(0, i + 1); parts.add(part); bracketCount = -1; if (i == b.length() - 1) { break; } b.delete(0, i + 1); i = 0; } i++; } for (String p : parts) { if (!Const.isEmpty(p)) { DBObject o = (DBObject) JSON.parse(p); pipeline.add(o); } } if (pipeline.size() == 0) { throw new KettleException( BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.UnableToParsePipelineOperators")); //$NON-NLS-1$ } return pipeline; }
From source file:com.google.code.log4mongo.MongoDbPatternLayoutAppender.java
License:Apache License
/** * Inserts a BSON representation of a LoggingEvent into a MongoDB collection. * A PatternLayout is used to format a JSON document containing data available * in the LoggingEvent and, optionally, additional data returned by custom PatternConverters. * <p>/* w w w. j a va 2 s. c o m*/ * The format of the JSON document is specified in the .layout.ConversionPattern property. * * @param loggingEvent The LoggingEvent that will be formatted and stored in MongoDB */ @Override protected void append(final LoggingEvent loggingEvent) { if (isInitialized()) { DBObject bson = null; String json = layout.format(loggingEvent); if (json.length() > 0) { Object obj = JSON.parse(json); if (obj instanceof DBObject) { bson = (DBObject) obj; } } if (bson != null) { try { getCollection().insert(bson); } catch (MongoException e) { errorHandler.error("Failed to insert document to MongoDB", e, ErrorCode.WRITE_FAILURE); } } } }
From source file:com.grallandco.impl.MongoCAPIBehavior.java
License:Apache License
/** * Load the documents into MongoDB/*from w w w. ja v a2 s. c o m*/ * @param database * @param docs * @return */ @Override public List<Object> bulkDocs(String database, List<Map<String, Object>> docs) { DB db = MongoConnectionManager.getMongoClient().getDB(getDatabaseName(database)); List<Object> result = new ArrayList<Object>(); logger.log(Level.INFO, String.format("Replicating %d document(s)", docs.size())); for (Map<String, Object> doc : docs) { Map<String, Object> meta = (Map<String, Object>) doc.get("meta"); Map<String, Object> json = (Map<String, Object>) doc.get("json"); String base64 = (String) doc.get("base64"); if (meta == null) { // if there is no meta-data section, there is nothing we can do logger.log(Level.WARNING, "Document without meta in bulk_docs, ignoring...."); continue; } else if ("non-JSON mode".equals(meta.get("att_reason")) || "invalid_json".equals(meta.get("att_reason"))) { // optimization, this tells us the body isn't json json = new HashMap<String, Object>(); } else if (json == null && base64 != null) { // use Java 6/7 XML Base64 library // TODO : see if it makes sense to use Java8 Library java.util.Base64 String jsonValue = new String(DatatypeConverter.parseBase64Binary(base64)); DBObject o = (DBObject) JSON.parse(jsonValue); DBObject mongoJson = BasicDBObjectBuilder.start("_id", meta.get("id")).get(); // need to check if json keys do not contains . or $ and replace them with other char // TODO : Copy the doc, put _id at the top and clean key names Set<String> keys = o.keySet(); for (String key : keys) { String newKey = key; newKey = newKey.replace(".", MongoDBCouchbaseReplicator.dotReplacement); newKey = newKey.replace("$", MongoDBCouchbaseReplicator.dollarReplacement); mongoJson.put(newKey, o.get(key)); } // add meta data if configured if (MongoDBCouchbaseReplicator.keepMeta) { mongoJson.put("meta", new BasicDBObject(meta)); } String collectionName = MongoDBCouchbaseReplicator.defaultCollection; if (o.get(MongoDBCouchbaseReplicator.collectionField) != null) { collectionName = (String) o.get(MongoDBCouchbaseReplicator.collectionField); } try { if (MongoDBCouchbaseReplicator.replicationType.equalsIgnoreCase("insert_only")) { // this will raise an exception db.getCollection(collectionName).insert(mongoJson); } else { // insert & update db.getCollection(collectionName).save(mongoJson); } } catch (MongoException e) { if (e.getCode() == 11000) { logger.log(Level.INFO, "Not replicating updated document " + meta.get("id")); } else { logger.log(Level.SEVERE, e.getMessage()); } } } String id = (String) meta.get("id"); String rev = (String) meta.get("rev"); Map<String, Object> itemResponse = new HashMap<String, Object>(); itemResponse.put("id", id); itemResponse.put("rev", rev); result.add(itemResponse); } return result; }