Example usage for com.mongodb BasicDBList BasicDBList

List of usage examples for com.mongodb BasicDBList BasicDBList

Introduction

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

Prototype

BasicDBList

Source Link

Usage

From source file:com.threecrickets.sincerity.logging.MongoDbManager.java

License:LGPL

@Override
protected void writeInternal(LogEvent event) {
    if (collection == null)
        throw new AppenderLoggingException("Not connected to MongoDB");

    Document o = new Document();

    o.put("timestamp", new Date(event.getTimeMillis()));
    o.put("logger", event.getLoggerName());

    Level level = event.getLevel();
    if (level != null)
        o.put("level", level.name());

    Marker marker = event.getMarker();// www. j av a 2 s . com
    if (marker != null)
        o.put("marker", marker.getName());

    Message message = event.getMessage();
    if (message != null)
        o.put("message", message.getFormattedMessage());

    StackTraceElement eventSource = event.getSource();
    if (eventSource != null) {
        Document source = new Document();
        source.put("class", eventSource.getClassName());
        source.put("method", eventSource.getMethodName());
        source.put("file", eventSource.getFileName());
        source.put("line", eventSource.getLineNumber());
        o.put("source", source);
    }

    Document thread = new Document();

    thread.put("name", event.getThreadName());

    Map<String, String> eventContextMap = event.getContextMap();
    if (eventContextMap != null) {
        Document contextMap = new Document();
        for (Map.Entry<String, String> entry : eventContextMap.entrySet())
            contextMap.put(entry.getKey(), entry.getValue());
        thread.put("contextMap", contextMap);
    }

    ContextStack eventContextStack = event.getContextStack();
    if (eventContextStack != null) {
        BasicDBList contextStack = new BasicDBList();
        for (String entry : eventContextStack)
            contextStack.add(entry);
        thread.put("contextStack", contextStack);
    }

    o.put("thread", thread);

    try {
        collection.insertOne(o);
    } catch (MongoException e) {
        throw new AppenderLoggingException("Can't write to MongoDB", e);
    }
}

From source file:com.wenwo.platform.dao.util.WenwoQueryMapper.java

License:Apache License

/**
 * Returns the given {@link DBObject} representing a keyword by mapping the keyword's value.
 * //from w w  w.java 2 s  . c om
 * @param query the {@link DBObject} representing a keyword (e.g. {@code $ne :  } )
 * @param entity
 * @return
 */
private DBObject getMappedKeyword(Keyword query, MongoPersistentEntity<?> entity) {

    // $or/$nor
    if (query.key.matches(N_OR_PATTERN)) {

        Iterable<?> conditions = (Iterable<?>) query.value;
        BasicDBList newConditions = new BasicDBList();

        for (Object condition : conditions) {
            newConditions.add(getMappedObject((DBObject) condition, entity));
        }

        return new BasicDBObject(query.key, newConditions);
    }

    return new BasicDBObject(query.key, convertSimpleOrDBObject(query.value, entity));
}

From source file:com.wenwo.platform.dao.util.WenwoQueryMapper.java

License:Apache License

/**
 * Converts the given source assuming it's actually an association to anoter object.
 * /*from w  w w  .  jav  a  2  s  .c o  m*/
 * @param source
 * @param property
 * @return
 */
private Object convertAssociation(Object source, MongoPersistentProperty property) {

    if (property == null || !property.isAssociation()) {
        return source;
    }

    if (source instanceof Iterable) {
        BasicDBList result = new BasicDBList();
        for (Object element : (Iterable<?>) source) {
            result.add(element instanceof DBRef ? element : converter.toDBRef(element, property));
        }
        return result;
    }

    return source == null || source instanceof DBRef ? source : converter.toDBRef(source, property);
}

From source file:com.zjy.mongo.splitter.MultiCollectionSplitBuilder.java

License:Apache License

public String toJSON() {
    BasicDBList returnVal = new BasicDBList();
    for (CollectionSplitterConf conf : collectionSplitters) {
        returnVal.add(new BasicDBObject(conf.toConfigMap()));
    }//from   w w  w. j  a va 2 s  .co m
    return JSON.serialize(returnVal);
}

From source file:course.BlogPostDAO.java

License:Apache License

public String addPost(String title, String body, List tags, String username) {

    System.out.println("inserting blog entry " + title + " " + body);

    String permalink = title.replaceAll("\\s", "_"); // whitespace becomes _
    permalink = permalink.replaceAll("\\W", ""); // get rid of non alphanumeric
    permalink = permalink.toLowerCase();

    BasicDBObject post = new BasicDBObject();
    // XXX HW 3.2, Work Here
    // Remember that a valid post has the following keys:
    // author, body, permalink, tags, comments, date
    ///*from   w ww . j  a va  2 s  .c  o m*/
    // A few hints:
    // - Don't forget to create an empty list of comments
    // - for the value of the date key, today's datetime is fine.
    // - tags are already in list form that implements suitable interface.
    // - we created the permalink for you above.
    // - Build the post object and insert it

    post.append("title", title).append("body", body).append("author", username).append("tags", tags)
            .append("comments", new BasicDBList()).append("date", new Date()).append("permalink", permalink);
    postsCollection.insert(post);
    return permalink;
}

From source file:cse.alumni.archive.MainFrame.java

private void textFieldSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldSearchActionPerformed
    // TODO add your handling code here:
    try {//from  ww  w. jav  a  2 s  . c  om
        String regNo = textFieldSearch.getText().trim();

        DBObject clause1 = new BasicDBObject("regNo", regNo);
        DBObject clause2 = new BasicDBObject("contact", regNo);
        BasicDBList or = new BasicDBList();
        or.add(clause1);
        or.add(clause2);
        DBObject query = new BasicDBObject("$or", or);

        dBObject = users.findOne(query);

        String[] ses = new String[6];
        ses[0] = dBObject.get("name").toString();
        ses[1] = dBObject.get("regNo").toString();
        ses[2] = dBObject.get("contact").toString();
        ses[3] = dBObject.get("passingYear").toString();
        ses[4] = dBObject.get("currentWorkPlace").toString();
        ses[5] = dBObject.get("contribution").toString();
        setTextFieldData(ses);
        textFieldsUnEditable();
        buttonDelete.setVisible(true);
        buttonUpdate.setVisible(true);
        buttonUpdate.setText("UPDATE");
    } catch (NullPointerException exception) {
        JOptionPane.showMessageDialog(this, "Student Not Found", "ERROR!", JOptionPane.ERROR_MESSAGE);
    } catch (NumberFormatException exception) {
        JOptionPane.showMessageDialog(this, "Give Valid Student Registration No", "ERROR!",
                JOptionPane.ERROR_MESSAGE);
    }

}

From source file:cz.cuni.mff.d3s.been.mapstore.mongodb.MongoMapStore.java

License:Open Source License

@Override
public void deleteAll(Collection keys) {
    BasicDBList dbo = new BasicDBList();
    for (Object key : keys) {
        dbo.add(new BasicDBObject("_id", key));
    }//from  w w w  .  j av  a2  s  . com
    BasicDBObject dbb = new BasicDBObject("$or", dbo);
    coll.remove(dbb);
}

From source file:cz.cuni.mff.d3s.been.mapstore.mongodb.MongoMapStore.java

License:Open Source License

@Override
public Map loadAll(Collection keys) {
    try {/*from  w w w  .  j  a v a  2 s  .  com*/
        Map map = new HashMap();
        BasicDBList dbo = new BasicDBList();
        for (Object key : keys) {
            dbo.add(new BasicDBObject("_id", key));
        }
        BasicDBObject dbb = new BasicDBObject("$or", dbo);
        DBCursor cursor = coll.find(dbb);
        while (cursor.hasNext()) {
            try {
                DBObject obj = cursor.next();
                Class clazz = Class.forName(obj.get("_class").toString());

                Object object = converter.toObject(clazz, obj);

                if (object instanceof TaskEntry) {
                    setLoadedFromMapStore((TaskEntry) object);
                }

                map.put(obj.get("_id"), object);
            } catch (ClassNotFoundException e) {
                log.error(e.getMessage(), e);
            }
        }
        return map;
    } catch (Throwable t) {
        log.warn("Cannot load collection from MongoDB", t);
    } finally {
        mapIsLoaded = true;
    }
    return null;
}

From source file:cz.cuni.mff.d3s.been.mapstore.mongodb.MongoMapStore.java

License:Open Source License

@Override
public Set loadAllKeys() {

    boolean loaded = false;
    ILock l = hazelcastInstance.getLock("LOADING_" + mapName);
    l.lock();/*from w ww.j  a v a2  s .  com*/

    IList loadedList = hazelcastInstance.getList(LOADED_LIST);
    if (loadedList.contains(mapName)) {
        loaded = true;
    }

    Set keyset = Collections.emptySet();

    if (!loaded) {
        try {
            keyset = new HashSet();
            BasicDBList dbo = new BasicDBList();
            dbo.add("_id");
            DBCursor cursor = coll.find(null, dbo);
            while (cursor.hasNext()) {
                keyset.add(cursor.next().get("_id"));
            }
            loadedList.add(mapName);
        } catch (Throwable t) {
            keyset = Collections.emptySet();
            log.warn("Cannot load keys from MongoDB", t);
        }
    }
    l.unlock();

    return keyset;
}

From source file:dao.SearchTopicDao.java

public ArrayList<PublicDiscussion> searchTopic(String searchvalue) {
    BasicDBList or = new BasicDBList();
    ArrayList<PublicDiscussion> arpb = new ArrayList<PublicDiscussion>();
    BasicDBObject topic = new BasicDBObject("topic",
            new BasicDBObject("$regex", java.util.regex.Pattern.compile(searchvalue)).append("$options", "i"));
    BasicDBObject content = new BasicDBObject("content",
            new BasicDBObject("$regex", java.util.regex.Pattern.compile(searchvalue)).append("$options", "i"));
    BasicDBObject tags = new BasicDBObject("tags",
            new BasicDBObject("$regex", java.util.regex.Pattern.compile(searchvalue)).append("$options", "i"));
    or.add(topic);/*from   w ww.j  ava  2s.c om*/
    or.add(content);
    or.add(tags);
    DBObject query = new BasicDBObject("$or", or);

    DBCursor cursor = col.find(query);

    if (cursor.size() > 0) {
        System.out.println("data is exists");
    } else {
        System.out.println("data  is not exists");
    }

    try {
        DBObject oneDetails;
        while (cursor.hasNext()) {
            oneDetails = cursor.next();

            PublicDiscussion tmpDiscussion = PublicPostConverter.toPublicDiscussion(oneDetails);
            arpb.add(tmpDiscussion);

        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return arpb;

}