Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject() 

Source Link

Document

Creates an empty object.

Usage

From source file:AdminServer.ServiceS.java

private void check_logout() {
    if (currentUser != null) {
        admins.update(currentUser,//from ww  w  .j  av a 2s . c o m
                new BasicDBObject().append("$set", new BasicDBObject().append("checked", false)));
        admins.update(currentUser,
                new BasicDBObject().append("$set", new BasicDBObject().append("checked", false)));
        System.out.println(this.currentUser.get("name").toString() + " logged out @" + this.serialNum);
        this.currentUser = null;
    }
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

/**
 * <p>//from   w  w  w. java  2s .com
 * Triggers "refresh" -- whatever that means -- of the implementation.
 * The general contract is that any should always leave itself in a
 * consistent, operational state, and that the refresh atomically updates
 * internal state from old to new.
 * </p>
 *
 * @param alreadyRefreshed s that are known to have already been refreshed as
 *                         a result of an initial call to a method on some object. This ensures
 *                         that objects in a refresh dependency graph aren't refreshed twice
 *                         needlessly.
 * @see #refreshData(String, Iterable, boolean)
 */
@Override
public void refresh(Collection<Refreshable> alreadyRefreshed) {
    BasicDBObject query = new BasicDBObject();
    query.put("deleted_at", new BasicDBObject("$gt", mongoTimestamp));
    DBCursor cursor = collection.find(query);
    Date ts = new Date(0);
    while (cursor.hasNext()) {
        Map<String, Object> user = (Map<String, Object>) cursor.next().toMap();
        String userID = getID(user.get(mongoUserID), true);
        Collection<List<String>> items = Lists.newArrayList();
        List<String> item = Lists.newArrayList();
        item.add(getID(user.get(mongoItemID), false));
        item.add(Float.toString(getPreference(user.get(mongoPreference))));
        items.add(item);
        try {
            refreshData(userID, items, false);
        } catch (NoSuchUserException e) {
            log.warn("No such user ID: {}", userID);
        } catch (NoSuchItemException e) {
            log.warn("No such items: {}", items);
        }
        if (ts.compareTo(getDate(user.get("created_at"))) < 0) {
            ts = getDate(user.get("created_at"));
        }
    }
    query = new BasicDBObject();
    query.put("created_at", new BasicDBObject("$gt", mongoTimestamp));
    cursor = collection.find(query);
    while (cursor.hasNext()) {
        Map<String, Object> user = (Map<String, Object>) cursor.next().toMap();
        if (!user.containsKey("deleted_at")) {
            String userID = getID(user.get(mongoUserID), true);
            Collection<List<String>> items = Lists.newArrayList();
            List<String> item = Lists.newArrayList();
            item.add(getID(user.get(mongoItemID), false));
            item.add(Float.toString(getPreference(user.get(mongoPreference))));
            items.add(item);
            try {
                refreshData(userID, items, true);
            } catch (NoSuchUserException e) {
                log.warn("No such user ID: {}", userID);
            } catch (NoSuchItemException e) {
                log.warn("No such items: {}", items);
            }
            if (ts.compareTo(getDate(user.get("created_at"))) < 0) {
                ts = getDate(user.get("created_at"));
            }
        }
    }
    if (mongoTimestamp.compareTo(ts) < 0) {
        mongoTimestamp = ts;
    }
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

private void buildModel() throws UnknownHostException {
    userIsObject = false;//from w ww  .ja va2 s . c o  m
    itemIsObject = false;
    idCounter = 0;
    preferenceIsString = true;
    Mongo mongoDDBB = new Mongo(mongoHost, mongoPort);
    DB db = mongoDDBB.getDB(mongoDB);
    mongoTimestamp = new Date(0);
    FastByIDMap<Collection<Preference>> userIDPrefMap = new FastByIDMap<Collection<Preference>>();
    if (!mongoAuth || db.authenticate(mongoUsername, mongoPassword.toCharArray())) {
        collection = db.getCollection(mongoCollection);
        collectionMap = db.getCollection(mongoMapCollection);
        DBObject indexObj = new BasicDBObject();
        indexObj.put("element_id", 1);
        collectionMap.ensureIndex(indexObj);
        indexObj = new BasicDBObject();
        indexObj.put("long_value", 1);
        collectionMap.ensureIndex(indexObj);
        collectionMap.remove(new BasicDBObject());
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            Map<String, Object> user = (Map<String, Object>) cursor.next().toMap();
            if (!user.containsKey("deleted_at")) {
                long userID = Long.parseLong(fromIdToLong(getID(user.get(mongoUserID), true), true));
                long itemID = Long.parseLong(fromIdToLong(getID(user.get(mongoItemID), false), false));
                float ratingValue = getPreference(user.get(mongoPreference));
                Collection<Preference> userPrefs = userIDPrefMap.get(userID);
                if (userPrefs == null) {
                    userPrefs = Lists.newArrayListWithCapacity(2);
                    userIDPrefMap.put(userID, userPrefs);
                }
                userPrefs.add(new GenericPreference(userID, itemID, ratingValue));
                if (user.containsKey("created_at")
                        && mongoTimestamp.compareTo(getDate(user.get("created_at"))) < 0) {
                    mongoTimestamp = getDate(user.get("created_at"));
                }
            }
        }
    }
    delegate = new GenericDataModel(GenericDataModel.toDataMap(userIDPrefMap, true));
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

private void removeMongoUserItem(String userID, String itemID) {
    String userId = fromLongToId(Long.parseLong(userID));
    String itemId = fromLongToId(Long.parseLong(itemID));
    if (isUserItemInDB(userId, itemId)) {
        mongoTimestamp = new Date();
        BasicDBObject query = new BasicDBObject();
        query.put(mongoUserID, userIsObject ? new ObjectId(userId) : userId);
        query.put(mongoItemID, itemIsObject ? new ObjectId(itemId) : itemId);
        if (mongoFinalRemove) {
            log.info(collection.remove(query).toString());
        } else {/*from w ww. j  a  va2  s .c om*/
            BasicDBObject update = new BasicDBObject();
            update.put("$set", new BasicDBObject("deleted_at", mongoTimestamp));
            log.info(collection.update(query, update).toString());
        }
        log.info("Removing userID: {} itemID: {}", userID, itemId);
    }
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

private void addMongoUserItem(String userID, String itemID, String preferenceValue) {
    String userId = fromLongToId(Long.parseLong(userID));
    String itemId = fromLongToId(Long.parseLong(itemID));
    if (!isUserItemInDB(userId, itemId)) {
        mongoTimestamp = new Date();
        BasicDBObject user = new BasicDBObject();
        Object userIdObject = userIsObject ? new ObjectId(userId) : userId;
        Object itemIdObject = itemIsObject ? new ObjectId(itemId) : itemId;
        user.put(mongoUserID, userIdObject);
        user.put(mongoItemID, itemIdObject);
        user.put(mongoPreference, preferenceIsString ? preferenceValue : Double.parseDouble(preferenceValue));
        user.put("created_at", mongoTimestamp);
        collection.insert(user);/*from   w w  w .  ja v a2 s  .  c  om*/
        log.info("Adding userID: {} itemID: {} preferenceValue: {}", userID, itemID, preferenceValue);
    }
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

private boolean isUserItemInDB(String userID, String itemID) {
    BasicDBObject query = new BasicDBObject();
    Object userId = userIsObject ? new ObjectId(userID) : userID;
    Object itemId = itemIsObject ? new ObjectId(itemID) : itemID;
    query.put(mongoUserID, userId);/*w  ww. ja v a  2s  . c o m*/
    query.put(mongoItemID, itemId);
    return collection.findOne(query) != null;
}

From source file:amazonwebcrawler.Mongo.java

public void inputData(Map<String, Object> product) {
    try {//  w w w .j  a v  a2s .c  o m
        /**** Insert ****/
        // create a document to store key and value
        BasicDBObject document = new BasicDBObject();
        document.put("name", (String) product.get("name"));
        document.put("rating", (double) product.get("rating"));
        document.put("url", (String) product.get("url"));
        document.put("price", (String) product.get("price"));
        document.put("ID", (String) product.get("ID"));
        document.put("tag", (List<String>) product.get("tag"));
        document.put("review", (List<String>) product.get("review"));
        document.put("createdDate", new Date());
        removeData((String) product.get("name"));
        myTable.insert(document);
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:amazonwebcrawler.Mongo.java

public void removeData(String name) {
    try {/* w w  w .j a  v a 2 s .c om*/
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", name);

        DBCursor cursor = myTable.find(searchQuery);

        while (cursor.hasNext()) {
            myTable.remove(cursor.next());
        }
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:amazonwebcrawler.Mongo.java

public void searchData(String name) {
    try {/*from ww  w.j a  va 2s.  c o m*/
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", name);

        DBCursor cursor = myTable.find(searchQuery);

        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:amazonwebcrawler.Mongo.java

public void searchAndReplaceData(DBCollection table, String[] args) {
    try {//from  w w  w.  jav  a 2s . c  om
        BasicDBObject query = new BasicDBObject();
        query.put("name", "mkyong");

        BasicDBObject newDocument = new BasicDBObject();
        newDocument.put("name", "mkyong-updated");

        BasicDBObject updateObj = new BasicDBObject();
        updateObj.put("$set", newDocument);

        table.update(query, updateObj);
    } catch (MongoException e) {
        e.printStackTrace();
    }
}