List of usage examples for com.mongodb DBObject put
Object put(String key, Object v);
From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java
License:Apache License
/** * handle the update and save process for the categories. * * @param category// w w w .ja v a2 s. c om * the NewsCategory object * @param news * the News object to reference to * @throws Exception * the exception */ private void handleCategory(NewsCategory category, News news) throws Exception { // 1. save the category DBObject item = getCategoryById(category.getFs_id(), category.getLanguage()); if (item != null) { item.put("fs_id", category.getFs_id()); item.put("language", category.getLanguage()); item.put("name", category.getName()); item.put("version", category.getVersion()); item.put("lastmodified", category.getLastmodified()); // newsdrilldown // DBObject tmpNews = getNewsById(news.getFs_id(), news.getLanguage()); // if (tmpNews != null) { // DBRef ref = new DBRef(mdb, NEWS_COLLECTION_NAME, // tmpNews.get("_id")); // item.put("newsdrilldown", ref); // } BasicDBObject query = new BasicDBObject(); category.setId((Long) item.get("_id")); query.put("_id", category.getId()); dbCategories.update(query, item); } else { item = new BasicDBObject(); item.put("fs_id", category.getFs_id()); item.put("language", category.getLanguage()); item.put("name", category.getName()); item.put("version", category.getVersion()); item.put("lastmodified", category.getLastmodified()); // newsdrilldown // DBObject tmpNews = getNewsById(news.getFs_id(), news.getLanguage()); // if (tmpNews != null) { // DBRef ref = new DBRef(mdb, NEWS_COLLECTION_NAME, // tmpNews.get("_id")); // item.put("newsdrilldown", ref); // } category.setId(generateIdentifier(CATEGORY_COLLECTION_NAME, mdb)); item.put("_id", category.getId()); dbCategories.insert(item); } // 2. save the metaCategories if (category.getMetaCategories() != null) { BasicDBList metaCatlist = (BasicDBList) item.get("metaCategories_$$manyToManyIds"); if (metaCatlist == null) { metaCatlist = new BasicDBList(); } for (NewsMetaCategory metaCat : category.getMetaCategories()) { handleMetaCategory(metaCat, category); if (!metaCatlist.contains(metaCat.getId())) { metaCatlist.add(metaCat.getId()); } DBObject obj = getMetaCategoryById(metaCat.getFs_id(), metaCat.getLanguage()); //BasicDBList catlist = (BasicDBList) obj.get("categories"); BasicDBList catlist = (BasicDBList) obj.get("categories_$$manyToManyIds"); if (catlist == null) { catlist = new BasicDBList(); } //DBRef catRef = new DBRef(this.mdb, this.CATEGORY_COLLECTION_NAME, category.getId()); if (!catlist.contains(category.getId())) { catlist.add(category.getId()); } obj.put("categories_$$manyToManyIds", catlist); BasicDBObject query = new BasicDBObject(); query.put("_id", (Long) obj.get("_id")); dbMetaCategories.update(query, obj); } item.put("metaCategories_$$manyToManyIds", metaCatlist); BasicDBObject query = new BasicDBObject(); query.put("_id", (Long) item.get("_id")); dbCategories.update(query, item); } }
From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java
License:Apache License
/** * handle the update and save process for the metacategories. * * @param metaCat/*ww w. j a v a 2 s . c om*/ * the NewsMetaCategory object * @param category * the NewsCategory object to reference to * @throws Exception * the exception */ private void handleMetaCategory(NewsMetaCategory metaCat, NewsCategory category) { DBObject item = getMetaCategoryById(metaCat.getFs_id(), metaCat.getLanguage()); if (item != null) { item.put("fs_id", metaCat.getFs_id()); item.put("language", metaCat.getLanguage()); item.put("name", metaCat.getName()); item.put("version", metaCat.getVersion()); item.put("lastmodified", metaCat.getLastmodified()); // category DBObject cat = getCategoryById(category.getFs_id(), category.getLanguage()); if (cat != null) { DBRef ref = new DBRef(mdb, CATEGORY_COLLECTION_NAME, cat.get("_id")); item.put("category", ref); } BasicDBObject query = new BasicDBObject(); query.put("_id", item.get("_id")); dbMetaCategories.update(query, item); } else { item = new BasicDBObject(); item.put("fs_id", metaCat.getFs_id()); item.put("language", metaCat.getLanguage()); item.put("name", metaCat.getName()); item.put("version", metaCat.getVersion()); item.put("lastmodified", metaCat.getLastmodified()); // category DBObject cat = getCategoryById(category.getFs_id(), category.getLanguage()); if (cat != null) { DBRef ref = new DBRef(mdb, CATEGORY_COLLECTION_NAME, cat.get("_id")); item.put("category", ref); } item.put("_id", generateIdentifier(META_CATEGORY_COLLECTION_NAME, mdb)); dbMetaCategories.insert(item); } }
From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java
License:Apache License
/** * Mongo ID generation like it is done in the grails gorm framework * * @param collectionName/*from w ww . ja v a 2 s . c om*/ * The name of the collection the id should be generated for * @param db * The mongodb connection * @return a new id */ private Long generateIdentifier(String collectionName, DB db) { // get or create the Collection for the ID storage DBCollection dbCollection = db.getCollection(collectionName + ".next_id"); // create entry to store the newly generated id DBObject nativeEntry = new BasicDBObject(); while (true) { DBCursor result = dbCollection.find().sort(new BasicDBObject("_id", -1)).limit(1); long nextId; if (result.hasNext()) { final Long current = (Long) result.next().get("_id"); nextId = current + 1; } else { nextId = 1; } nativeEntry.put("_id", 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 (Long) nativeEntry.get("_id"); }
From source file:com.espirit.moddev.examples.uxbridge.newswidget.mongodb.ArticleHandler.java
License:Apache License
/** * Add or update a news article in the db * * @param entity The news article/*from ww w .j a va 2 s. c om*/ */ public void add(UXBEntity entity) throws Exception { DBObject item = getById(Long.parseLong(entity.getUxb_content().getFs_id()), entity.getLanguage()); /* * If the item exists in the db, we update the content and the title of the existing item */ if (item != null) { item.put("content", entity.getUxb_content().getContent()); item.put("title", entity.getUxb_content().getHeadline()); item.put("url", entity.getUxb_content().getUrl()); item.put("aid", Long.parseLong(entity.getUxb_content().getFs_id())); item.put("language", entity.getUxb_content().getLanguage()); item.put("lastmodified", entity.getCreateTime()); BasicDBObject query = new BasicDBObject(); query.put("_id", item.get("_id")); articles.update(query, item); } else { item = buildArticle(entity); item.put("_id", generateIdentifier(COLLECTION_NAME, mdb)); articles.insert(item); } }
From source file:com.espirit.moddev.examples.uxbridge.newswidget.mongodb.ArticleHandler.java
License:Apache License
/** * Deletes an article from the db// ww w. j av a 2 s. c o m * * @param entity The article to delete */ public void delete(UXBEntity entity) throws Exception { // delete item DBObject item = getById(Long.parseLong(entity.getUuid()), entity.getLanguage()); if (item != null) { // delete the article DBObject query = new BasicDBObject(); query.put("_id", item.get("_id")); query.put("aid", item.get("aid")); query.put("language", item.get("language")); articles.remove(query); } }
From source file:com.espirit.moddev.examples.uxbridge.newswidget.mongodb.ArticleHandler.java
License:Apache License
/** * Deletes every article older than expireDate * * @param entity Entity containing the expireDate (= createTime of the entity) *///from w w w .j a v a 2s . com public void cleanup(UXBEntity entity) throws Exception { // delete items DBCursor items = getByLastmodified(entity.getCreateTime()); for (DBObject item : items) { DBObject query = new BasicDBObject(); query.put("_id", item.get("_id")); query.put("aid", item.get("aid")); query.put("language", item.get("language")); articles.remove(query); } }
From source file:com.example.labs.actions.data.ProjectsAction.java
License:Apache License
public String execute() throws Exception { connection = MongoDBConnection.getInstance(); connection.connect("turbines"); DBCollection turbineData = connection.getMongoCollection("turbineData"); DBObject searchQuery = new BasicDBObject(); searchQuery.put("date", BasicDBObjectBuilder.start("$gte", startDate).add("$lte", endDate).get()); DBObject keys = new BasicDBObject(); keys.put("date", "1"); //from query param if (StringUtils.isEmpty(fields)) { success = 0;/* w w w .ja v a 2 s . c om*/ error = "Please provide valid fields"; return ERROR; } String[] fieldKeys = fields.split(","); for (String k : fieldKeys) { keys.put(k, "1"); } DBCursor cursor = turbineData.find(searchQuery, keys); DBObject sortTime = new BasicDBObject(); sortTime.put("date", 1); cursor.sort(sortTime); List<DBObject> dataPoints = cursor.toArray(); LOG.debug("Number of data points: " + dataPoints.size()); data = new ArrayList<Object>(); for (DBObject dataP : dataPoints) { data.add(dataP); } success = 1; return SUCCESS; }
From source file:com.eywa.impl.app.controllers.central.Activity.java
License:Open Source License
public Activity putData(final String key, final Object value) { final DBObject data = getData(); data.put(key, value); setData(data);//w ww.j ava 2 s .co m return this; }
From source file:com.eywa.impl.app.mongo.entities.Hub.java
License:Open Source License
public static void init(final DBObject hub) { if (!StringUtils.hasText(Hub.getId(hub))) { final String id = createUUID(""); Hub.setId(hub, id);// w w w. ja v a 2 s .com hub.put(CREATION_DATE, DateUtils.now().getTime()); } }
From source file:com.eywa.impl.app.mongo.entities.items.jobs.ItemJobMessage.java
License:Open Source License
public static DBObject getRecipientsWithTemplates(final DBObject item) { final DBObject result = new BasicDBObject(); final DBObject recipients = getRecipients(item); final Set<String> users = recipients.keySet(); for (final String userId : users) { final String templateName = MongoUtils.getString(recipients, userId); final DBObject template = getTemplate(item, StringUtils.hasText(templateName) ? templateName : DEFAULT_TEMPLATE); if (null != template) { result.put(userId, template); }//from w w w . j a va2 s.c om } return result; }