Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

In this page you can find the example usage for com.mongodb DBObject get.

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

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/*from w w w .  j  a v  a 2  s.  co m*/
 *            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/*w ww  .j av  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//w  w  w . ja  v  a  2  s . c o  m
 *            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   w w  w  . j av  a 2  s .  c o  m*/
 */
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/*from w w  w  .  j  a va 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  ww  .ja  va2s.  c  o  m
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.ewcms.common.query.mongo.CriteriaWapper.java

License:Open Source License

/**
 * ???/*ww  w  . j  a  va 2  s . co  m*/
 * 
 * @param dbo
 * @return
 */
private boolean hasCriteria(DBObject dbo) {
    boolean has = false;
    for (String key : dbo.keySet()) {
        Object v = dbo.get(key);
        if (v instanceof DBObject) {
            has = has || !((DBObject) v).keySet().isEmpty();
        } else {
            has = has || true;
        }
    }
    return has;
}

From source file:com.example.rest.DbConnection.java

public User getData(int number) {
    BasicDBObject searchQuery = new BasicDBObject();
    User retUser = null;//ww w .jav  a  2 s . co m

    searchQuery.append("DriverLicense", number);

    DBCursor cursor = table.find(searchQuery);

    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        retUser = new User((int) obj.get("DriverLicense"));
        retUser.setFirstName((String) obj.get("FirstName"));
        retUser.setLastName((String) obj.get("LastName"));
        retUser.setDob((String) obj.get("DOB"));
        retUser.setAddress((String) obj.get("Address"));
        //retUser = (User) cursor.next();
    }
    return retUser;
}

From source file:com.example.rest.DbConnection.java

public List<User> allCustomer() {
    BasicDBObject query = new BasicDBObject();
    DBCursor cursor = table.find();//w w  w  .j av a2  s  .c  om
    List<User> all = new ArrayList<User>();
    User retUser = null;
    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        retUser = new User((int) obj.get("DriverLicense"));
        retUser.setFirstName((String) obj.get("FirstName"));
        retUser.setLastName((String) obj.get("LastName"));
        retUser.setDob((String) obj.get("DOB"));
        retUser.setAddress((String) obj.get("Address"));
        all.add(retUser);
    }
    return all;
}

From source file:com.example.rest.DbConnection.java

public String getOrder(int number) {
    BasicDBObject document = new BasicDBObject();
    String order = null;//from w w  w  . ja v a2  s .  c  o m

    document.append("DriverLicense", number);
    DBCursor cursor = orderTable.find(document);
    if (cursor.hasNext()) {
        DBObject obj = cursor.next();
        order = (String) obj.get("OrderDetails");
    }
    return order;
}