List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents)
From source file:org.exoplatform.chat.services.mongodb.ChatServiceImpl.java
License:Open Source License
public String getSpaceRoom(String space) { String room = ChatUtils.getRoomId(space); DBCollection coll = db().getCollection(M_ROOM_PREFIX + M_ROOMS_COLLECTION); BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("_id", room); DBCursor cursor = coll.find(basicDBObject); if (!cursor.hasNext()) { try {//from w ww.j a v a2 s .com basicDBObject.put("space", space); basicDBObject.put("type", TYPE_ROOM_SPACE); coll.insert(basicDBObject); ensureIndexInRoom(room); } catch (MongoException me) { LOG.warning(me.getCode() + " : " + room + " : " + me.getMessage()); } } return room; }
From source file:org.exoplatform.chat.services.mongodb.ChatServiceImpl.java
License:Open Source License
public String getTeamRoom(String team, String user) { String room = ChatUtils.getRoomId(team, user); DBCollection coll = db().getCollection(M_ROOM_PREFIX + M_ROOMS_COLLECTION); BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("_id", room); DBCursor cursor = coll.find(basicDBObject); if (!cursor.hasNext()) { try {/*from www . j a va 2s . c o m*/ basicDBObject.put("team", team); basicDBObject.put("user", user); basicDBObject.put("type", TYPE_ROOM_TEAM); coll.insert(basicDBObject); ensureIndexInRoom(room); } catch (MongoException me) { LOG.warning(me.getCode() + " : " + room + " : " + me.getMessage()); } } return room; }
From source file:org.exoplatform.chat.services.mongodb.ChatServiceImpl.java
License:Open Source License
public String getExternalRoom(String identifier) { String room = ChatUtils.getExternalRoomId(identifier); DBCollection coll = db().getCollection(M_ROOM_PREFIX + M_ROOMS_COLLECTION); BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("_id", room); DBCursor cursor = coll.find(basicDBObject); if (!cursor.hasNext()) { try {/*from w w w . j a v a2s. co m*/ basicDBObject.put("identifier", identifier); basicDBObject.put("type", TYPE_ROOM_EXTERNAL); coll.insert(basicDBObject); ensureIndexInRoom(room); } catch (MongoException me) { LOG.warning(me.getCode() + " : " + room + " : " + me.getMessage()); } } return room; }
From source file:org.exoplatform.chat.services.mongodb.ChatServiceImpl.java
License:Open Source License
public String getRoom(List<String> users) { Collections.sort(users);/* w ww .j av a 2 s . com*/ String room = ChatUtils.getRoomId(users); DBCollection coll = db().getCollection(M_ROOM_PREFIX + M_ROOMS_COLLECTION); BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("_id", room); DBCursor cursor = coll.find(basicDBObject); if (!cursor.hasNext()) { try { basicDBObject.put("users", users); basicDBObject.put("type", TYPE_ROOM_USER); coll.insert(basicDBObject); ensureIndexInRoom(room); } catch (MongoException me) { LOG.warning(me.getCode() + " : " + room + " : " + me.getMessage()); } } return room; }
From source file:org.exoplatform.chat.services.mongodb.NotificationServiceImpl.java
License:Open Source License
public void addNotification(String user, String from, String type, String category, String categoryId, String content, String link, String options) { DBCollection coll = db().getCollection(M_NOTIFICATIONS); BasicDBObject doc = new BasicDBObject(); content = StringUtils.chomp(content); content = content.replaceAll("&", "&"); content = content.replaceAll("<", "<"); content = content.replaceAll(">", ">"); content = content.replaceAll("\"", """); content = content.replaceAll("\n", "<br/>"); content = content.replaceAll("\\\\", "\"); content = content.replaceAll("\t", " "); doc.put("timestamp", System.currentTimeMillis()); doc.put("user", user); doc.put("from", from); doc.put("type", type); doc.put("category", category); doc.put("categoryId", categoryId); doc.put("content", content); if (options != null) { options = options.replaceAll("<", "<"); options = options.replaceAll(">", ">"); options = options.replaceAll("'", "\\\\\""); // options = options.replaceAll("\"", """); // options = options.replaceAll("\\\\", "\"); doc.put("options", options); }// w w w . j ava 2 s .c o m doc.put("link", link); doc.put("isRead", false); coll.insert(doc); }
From source file:org.fao.fenix.wds.web.rest.faostat.FAOSTATDownloadTable.java
License:Open Source License
private void save(Date date, String rest, String payload) throws UnknownHostException { MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance(); Mongo mongo = mgr.getMongo(null);//from w w w . j a va 2s . com DB db = mongo.getDB(SCHEMA); DBCollection collection = db.getCollection("logs"); BasicDBObject document = new BasicDBObject(); document.put("date", date); document.put("rest", rest); DBObject dbObject = (DBObject) JSON.parse(payload); document.put("payload", dbObject); collection.insert(document); }
From source file:org.forgerock.openidm.repo.mongodb.impl.DBHelper.java
License:Open Source License
private static void populateDefaultUser(DBCollection collection, JsonValue completeConfig, String user, String pwd, String roles) { DBObject query = (DBObject) JSON.parse("{\"_openidm_id\": \"" + user + "\"}"); if (collection.count(query) > 0) { return;//ww w. ja v a2s . c o m } Map<String, Object> defAdmin = new LinkedHashMap<String, Object>(); defAdmin.put("_id", user); defAdmin.put("_openidm_id", user); defAdmin.put("userName", user); try { defAdmin.put("password", JSON.parse(pwd)); } catch (com.mongodb.util.JSONParseException e) { defAdmin.put("password", pwd); } String[] role = roles.split(","); defAdmin.put("roles", role); BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(defAdmin); DBObject o = builder.get(); collection.insert(o); }
From source file:org.forgerock.openidm.repo.mongodb.impl.MongoDBRepoService.java
License:Open Source License
/** * Creates a new object in the object set. * <p>// w ww . j a va 2s .co m * This method sets the {@code _id} property to the assigned identifier for the object, * and the {@code _rev} property to the revised object version (For optimistic concurrency) * * @param fullId the client-generated identifier to use, or {@code null} if server-generated identifier is requested. * @param obj the contents of the object to create in the object set. * @throws NotFoundException if the specified id could not be resolved. * @throws ForbiddenException if access to the object or object set is forbidden. * @throws PreconditionFailedException if an object with the same ID already exists. */ @Override public void create(String fullId, Map<String, Object> obj) throws ObjectSetException { String localId = getLocalId(fullId); String type = getObjectType(fullId, false); if (fullId == null || localId == null) { throw new NotFoundException( "The repository requires clients to supply an identifier for the object to create. Full identifier: " + fullId + " local identifier: " + localId); } else if (type == null) { throw new NotFoundException( "The object identifier did not include sufficient information to determine the object type: " + fullId); } obj.put(DocumentUtil.TAG_ID, localId); obj.put(DocumentUtil.MONGODB_PRIMARY_KEY, localId); obj.put(DocumentUtil.TAG_REV, "0"); BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(obj); DBObject jo = builder.get(); jo = DocumentUtil.normalizeForWrite(jo); DBCollection collection = getCollection(type); collection.insert(jo); logger.debug("Completed create for id: {} revision: {}", fullId, jo.get(DocumentUtil.TAG_REV)); logger.trace("Create payload for id: {} doc: {}", fullId, jo); }
From source file:org.giggsoff.jspritproj.CurrentTask.java
public void getCurrentPosition() { Date cur = new Date(); cur.setTime(cur.getTime());/*from ww w .j ava 2 s . co m*/ cur.setTime(cur.getTime() + diff.longValue() * 1000); for (int i = 0; i < Main.ar.size(); i++) { Point p = null; if (cur.after(lastTime.get(i)) && curNum.get(i) != 0) { if (Main.ar.get(i).get(curNum.get(i)).type > 1 && Main.ar.get(i).get(lastNum.get(i)).type > 1) { DB db = mongo.getDB("orion"); boolean saved = false; if (!Main.ar.get(i).get(curNum.get(i)).id.equals(Main.ar.get(i).get(lastNum.get(i)).id)) { DBCollection col = db.getCollection("routes"); String usuarioJSON = "{\"from\":\"" + Main.ar.get(i).get(lastNum.get(i)).id + "\"," + "\"to\":\"" + Main.ar.get(i).get(curNum.get(i)).id + "\"," + "\"time\":\"" + cur.getTime() + "\"," + "\"diff\":" + diff + "}"; DBObject jsonObject = (DBObject) JSON.parse(usuarioJSON); col.insert(jsonObject); saved = true; } else if (Main.ar.get(i).get(curNum.get(i)).type == 2) { DBCollection col = db.getCollection("volumes"); Double proc = (Main.ar.get(i).get(curNum.get(i)).dt.getTime() - Main.ar.get(i).get(lastNum.get(i)).dt.getTime()) / 1000. - 5. + 5 * new Random().nextDouble(); SGB sg = SGB.findSGB(Main.sgbList, Main.ar.get(i).get(curNum.get(i)).id); if (sg != null) { String usuarioJSON = "{\"id\":\"" + Main.ar.get(i).get(curNum.get(i)).id + "\"," + "\"process\":" + proc + "," + "\"time\":\"" + cur.getTime() + "\"," + "\"percent\":" + sg.volume / sg.max * 100 + "}"; DBObject jsonObject = (DBObject) JSON.parse(usuarioJSON); col.insert(jsonObject); saved = true; } } if (saved) { System.out.println(cur); System.out.println(lastTime.get(i)); System.out.println(Main.ar.get(i).get(curNum.get(i)).dt); System.out.println(Main.ar.get(i).get(curNum.get(i)).id); } } lastTime.get(i).setTime(Main.ar.get(i).get(curNum.get(i)).dt.getTime()); curNum.set(i, 0); lastNum.set(i, 0); } for (int j = 0; j < Main.ar.get(i).size() - 1; j++) { if (Main.ar.get(i).get(j + 1).dt.after(cur) && !Main.ar.get(i).get(j).dt.after(cur)) { lastNum.set(i, j); curNum.set(i, j + 1); Polygon curpart = new Polygon(); Date start = Main.ar.get(i).get(j).dt; Date end = Main.ar.get(i).get(j + 1).dt; int n = 0; for (int k = j; k > 0; k--) { if (Main.ar.get(i).get(j).dt.equals(Main.ar.get(i).get(k).dt)) { n = k; } else { break; } } for (int l = n; l <= j; l++) { curpart.addPoint(Main.ar.get(i).get(l)); } Double len = curpart.getLength(); Double tdiff = new Long((end.getTime() - start.getTime()) / 1000).doubleValue(); Random r = new Random(); double randomValue = -0.5 + r.nextDouble(); diff += randomValue; Double tcurr = new Long((cur.getTime() - start.getTime()) / 1000).doubleValue() + randomValue; Double curlen = len * tcurr / tdiff; Double leni = 0.; for (int l = 0; l < curpart.size() - 1; l++) { leni += curpart.getLength(l); if (leni > curlen) { Line line = new Line(curpart.get(l), curpart.get(l + 1)); Double perc = Math.abs((curlen - leni + curpart.getLength(l)) / curpart.getLength(l)); p = line.getPercent(perc, cur); //System.out.println(i+": "+p); Main.trposition.set(i, p); break; } } break; } } } if (Main.trposition.size() > 0) { String odf = ODF.generateODFLocations(Main.trposition); System.out.println("\nODFLoc"); //System.out.println(odf); ODF.sendODF(odf); System.out.println("\nODFLoc"); } }
From source file:org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.java
License:Apache License
@Override protected Object generateIdentifier(final PersistentEntity persistentEntity, final DBObject nativeEntry) { return mongoTemplate.execute(new DbCallback<Object>() { public Object doInDB(DB con) throws MongoException, DataAccessException { @SuppressWarnings("hiding") String collectionName = getCollectionName(persistentEntity, nativeEntry); DBCollection dbCollection = con.getCollection(collectionName + NEXT_ID_SUFFIX); // If there is a numeric identifier then we need to rely on optimistic concurrency controls to obtain a unique identifer // sequence. If the identifier is not numeric then we assume BSON ObjectIds. if (hasNumericalIdentifier) { while (true) { DBCursor result = dbCollection.find().sort(new BasicDBObject(MONGO_ID_FIELD, -1)).limit(1); long nextId; if (result.hasNext()) { final Long current = getMappingContext().getConversionService() .convert(result.next().get(MONGO_ID_FIELD), Long.class); nextId = current + 1; } else { nextId = 1;//from w w w.java 2 s . c o m } nativeEntry.put(MONGO_ID_FIELD, nextId); final WriteResult writeResult = dbCollection.insert(nativeEntry); final CommandResult lastError = writeResult.getLastError(); if (lastError.ok()) { break; } final Object code = lastError.get("code"); // duplicate key error try again if (code != null && code.equals(11000)) { continue; } break; } return nativeEntry.get(MONGO_ID_FIELD); } ObjectId objectId = ObjectId.get(); if (ObjectId.class.isAssignableFrom(persistentEntity.getIdentity().getType())) { nativeEntry.put(MONGO_ID_FIELD, objectId); return objectId; } String stringId = objectId.toString(); nativeEntry.put(MONGO_ID_FIELD, stringId); return stringId; } }); }