Example usage for com.mongodb DBCollection save

List of usage examples for com.mongodb DBCollection save

Introduction

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

Prototype

public WriteResult save(final DBObject document) 

Source Link

Document

Update an existing document or insert a document depending on the parameter.

Usage

From source file:net.jurre.edutil.persistence.MongoDB.java

License:Open Source License

public void savePluginConfig(PluginConfiguration config) {
    BasicDBObject dataObj = new BasicDBObject();

    dataObj.put("docType", "PluginConfiguration");
    dataObj.put("pluginName", config.getPluginName());
    dataObj.put("parameters", config.getAllParameters());

    DBCollection collection = this.db.getCollection(MongoDB.CONFIG_COLLECTION);
    collection.save(dataObj);
}

From source file:net.tbnr.util.bungee.cooldowns.TCooldownManager.java

License:Open Source License

public static boolean canContinue(String key, TCooldown cooldown) {
    if (database == null)
        return true;
    DBCollection collection = TCooldownManager.getCollection();
    BasicDBObject key2 = new BasicDBObject("key", key);
    DBCursor key1 = collection.find(key2);
    DBObject object = null;/* w  ww. java  2 s .  c  om*/
    while (key1.hasNext()) {
        object = key1.next();
    }
    if (object == null) {
        collection.save(key2.append("cooldown-time_stored", cooldown.getTime_stored()).append("cooldown-length",
                cooldown.getLength()));
        return true;
    } else {
        TCooldown cooldown1 = new TCooldown((Long) object.get("cooldown-time_stored"),
                (Long) object.get("cooldown-length"));
        if (cooldown1.canContinue()) {
            collection.remove(object);
            return canContinue(key, new TCooldown(cooldown.getLength()));
        } else {
            return false;
        }
    }
}

From source file:net.tbnr.util.ErrorHandler.java

License:Open Source License

/**
 * Stores the {@link com.mongodb.DBObject}
 *
 * @param object The {@link com.mongodb.DBObject} to store
 *//*  ww  w . j ava  2  s .c om*/
private static void storeErrorReport(DBObject object) {
    object.put("time", Calendar.getInstance().getTimeInMillis());
    DBCollection errors = GearzBungee.getInstance().getMongoDB().getCollection("errors");
    errors.save(object);
    GearzBungee.getInstance().getLogger().severe("Logging an error, check reports for more info.");
}

From source file:net.tbnr.util.player.cooldowns.TCooldownManager.java

License:Open Source License

public static boolean canContinue(String key, TCooldown cooldown) {
    if (database == null) {
        return true;
    }/*  ww w .j  av  a  2  s .  c om*/
    DBCollection collection = TCooldownManager.getCollection();
    BasicDBObject key2 = new BasicDBObject("key", key);
    DBCursor key1 = collection.find(key2);
    DBObject object = null;
    while (key1.hasNext()) {
        object = key1.next();
    }
    if (object == null) {
        collection.save(key2.append("cooldown-time_stored", cooldown.getTime_stored()).append("cooldown-length",
                cooldown.getLength()));
        return true;
    } else {
        TCooldown cooldown1 = new TCooldown((Long) object.get("cooldown-time_stored"),
                (Long) object.get("cooldown-length"));
        if (cooldown1.canContinue()) {
            collection.remove(object);
            return canContinue(key, new TCooldown(cooldown.getLength()));
        } else {
            return false;
        }

    }
}

From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java

/**
 * /*  w  ww .  ja va2 s  .  co  m*/
 * @param parachutists
 * @return
 */
public void addJumpers(ArrayList<Parachutist> parachutists) {

    DBCollection dbCollectionParachutists = db.getCollection("jumpers");
    dbCollectionParachutists.drop();

    setParachutistCounter(1);

    int counter;

    for (Parachutist parachutist : parachutists) {
        ArrayList<Integer> memberLicenses = new ArrayList<>();
        for (License license : parachutist.getLicenses()) {
            memberLicenses.add(new Integer(license.getId()));
        }

        ArrayList<Integer> memberClubs = new ArrayList<>();
        for (Club club : parachutist.getMemberclubs()) {
            memberClubs.add(new Integer(club.getId()));
        }

        counter = getParachutistCounter();
        BasicDBObject parachutistMongoObject = new BasicDBObject("melwinId", parachutist.getMelwinId())
                .append("_class", "no.nlf.models.mongoclasses.MongoParachutist")
                .append("memberclubs", memberClubs).append("licenses", memberLicenses)
                .append("firstname", parachutist.getFirstname()).append("lastname", parachutist.getLastname())
                .append("id", counter).append("birthdate", parachutist.getBirthdate())
                .append("gender", parachutist.getGender()).append("street", parachutist.getStreet())
                .append("postnumber", parachutist.getPostnumber())
                .append("postplace", parachutist.getPostplace()).append("mail", parachutist.getMail())
                .append("phone", parachutist.getPhone());

        dbCollectionParachutists.save(parachutistMongoObject);
    }

}

From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java

/**
 * Saves a single Parachuteobject to mongoDB
 * //from   w w  w.j  a va 2s  .  c o m
 * @param parachutist
 */
public void addJumperToDb(Parachutist parachutist) {
    DBCollection dbCollectionParachutistsCount = db.getCollection("jumpercounter");

    DBCollection dbCollectionParachutists = db.getCollection("jumpers");

    BasicDBObject incrementJumpcount = new BasicDBObject().append("$inc", new BasicDBObject().append("seq", 1));

    BasicDBObject query = new BasicDBObject("_id", "jumperCount");

    int countInt = (int) dbCollectionParachutistsCount.findOne().get("seq");

    ArrayList<Integer> memberLicenses = new ArrayList<>();
    for (License license : parachutist.getLicenses()) {
        memberLicenses.add(license.getId());
    }

    ArrayList<Integer> memberClubs = new ArrayList<>();
    for (Club club : parachutist.getMemberclubs()) {
        memberClubs.add(club.getId());
    }

    dbCollectionParachutistsCount.update(query, incrementJumpcount);

    BasicDBObject parachutistMongoObject = new BasicDBObject("melwinId", parachutist.getMelwinId())
            .append("_class", "no.nlf.models.mongoclasses.MongoParachutist").append("memberclubs", memberClubs)
            .append("licenses", memberLicenses).append("firstname", parachutist.getFirstname())
            .append("lastname", parachutist.getLastname()).append("id", countInt + 1)
            .append("birthdate", parachutist.getBirthdate()).append("gender", parachutist.getGender())
            .append("street", parachutist.getStreet()).append("postnumber", parachutist.getPostnumber())
            .append("postplace", parachutist.getPostplace()).append("mail", parachutist.getMail())
            .append("phone", parachutist.getPhone());

    dbCollectionParachutists.save(parachutistMongoObject);
}

From source file:org.alfresco.extension.wcmdeployment.mongodb.MongoDbDeploymentTarget.java

License:Open Source License

private DBObject createVersionDoc(final DB database) {
    DBObject result = null;/*from   www  .  j  a  v  a 2 s  .co  m*/

    if (database != null) {
        DBCollection collection = database.getCollection("deploymentSystem");
        result = new BasicDBObject();
        result.put("_id", "version");
        result.put("version", 0);
        collection.save(result);
    }

    return (result);
}

From source file:org.apache.camel.component.gridfs.GridFsConsumer.java

License:Apache License

@Override
public void run() {
    DBCursor c = null;/*  ww  w  .jav  a 2  s . c  o  m*/
    java.util.Date fromDate = null;

    QueryStrategy s = endpoint.getQueryStrategy();
    boolean usesTimestamp = (s != QueryStrategy.FileAttribute);
    boolean persistsTimestamp = (s == QueryStrategy.PersistentTimestamp
            || s == QueryStrategy.PersistentTimestampAndFileAttribute);
    boolean usesAttribute = (s == QueryStrategy.FileAttribute || s == QueryStrategy.TimeStampAndFileAttribute
            || s == QueryStrategy.PersistentTimestampAndFileAttribute);

    DBCollection ptsCollection = null;
    DBObject persistentTimestamp = null;
    if (persistsTimestamp) {
        ptsCollection = endpoint.getDB().getCollection(endpoint.getPersistentTSCollection());
        // ensure standard indexes as long as collections are small
        try {
            if (ptsCollection.count() < 1000) {
                ptsCollection.createIndex(new BasicDBObject("id", 1));
            }
        } catch (MongoException e) {
            //TODO: Logging
        }
        persistentTimestamp = ptsCollection.findOne(new BasicDBObject("id", endpoint.getPersistentTSObject()));
        if (persistentTimestamp == null) {
            persistentTimestamp = new BasicDBObject("id", endpoint.getPersistentTSObject());
            fromDate = new java.util.Date();
            persistentTimestamp.put("timestamp", fromDate);
            ptsCollection.save(persistentTimestamp);
        }
        fromDate = (java.util.Date) persistentTimestamp.get("timestamp");
    } else if (usesTimestamp) {
        fromDate = new java.util.Date();
    }
    try {
        Thread.sleep(endpoint.getInitialDelay());
        while (isStarted()) {
            if (c == null || c.getCursorId() == 0) {
                if (c != null) {
                    c.close();
                }
                String queryString = endpoint.getQuery();
                DBObject query;
                if (queryString == null) {
                    query = new BasicDBObject();
                } else {
                    query = (DBObject) JSON.parse(queryString);
                }
                if (usesTimestamp) {
                    query.put("uploadDate", new BasicDBObject("$gt", fromDate));
                }
                if (usesAttribute) {
                    query.put(endpoint.getFileAttributeName(), null);
                }
                c = endpoint.getFilesCollection().find(query);
            }
            boolean dateModified = false;
            while (c.hasNext() && isStarted()) {
                GridFSDBFile file = (GridFSDBFile) c.next();
                GridFSDBFile forig = file;
                if (usesAttribute) {
                    file.put(endpoint.getFileAttributeName(), "processing");
                    DBObject q = BasicDBObjectBuilder.start("_id", file.getId()).append("camel-processed", null)
                            .get();
                    forig = (GridFSDBFile) endpoint.getFilesCollection().findAndModify(q, null, null, false,
                            file, true, false);
                }
                if (forig != null) {
                    file = endpoint.getGridFs().findOne(new BasicDBObject("_id", file.getId()));

                    Exchange exchange = endpoint.createExchange();
                    exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA,
                            JSON.serialize(file.getMetaData()));
                    exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
                    exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
                    exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
                    exchange.getIn().setBody(file.getInputStream(), InputStream.class);
                    try {
                        getProcessor().process(exchange);
                        //System.out.println("Processing " + file.getFilename());
                        if (usesAttribute) {
                            forig.put(endpoint.getFileAttributeName(), "done");
                            endpoint.getFilesCollection().save(forig);
                        }
                        if (usesTimestamp) {
                            if (file.getUploadDate().compareTo(fromDate) > 0) {
                                fromDate = file.getUploadDate();
                                dateModified = true;
                            }
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            if (persistsTimestamp && dateModified) {
                persistentTimestamp.put("timestamp", fromDate);
                ptsCollection.save(persistentTimestamp);
            }
            Thread.sleep(endpoint.getDelay());
        }
    } catch (Throwable e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (c != null) {
        c.close();
    }
}

From source file:org.apache.camel.component.mongodb.MongoDbProducer.java

License:Apache License

protected void doSave(Exchange exchange) throws Exception {
    DBCollection dbCol = calculateCollection(exchange);
    DBObject saveObj = exchange.getIn().getMandatoryBody(DBObject.class);

    WriteConcern wc = extractWriteConcern(exchange);
    WriteResult result = wc == null ? dbCol.save(saveObj) : dbCol.save(saveObj, wc);

    prepareResponseMessage(exchange, MongoDbOperation.save);
    // we always return the WriteResult, because whether the getLastError was called or not, the user will have the means to call it or 
    // obtain the cached CommandResult
    processAndTransferWriteResult(result, exchange);
}

From source file:org.apache.isis.objectstore.nosql.db.mongo.MongoDb.java

License:Apache License

private void writeSerialNumber(final long serialNumber) {
    final DBCollection system = db.getCollection(SERIALNUMBERS_COLLECTION_NAME);
    DBObject object = system.findOne();/*from w ww .j a  v a 2s .  c o  m*/
    if (object == null) {
        object = new BasicDBObject();
    }
    object.put("next-id", Long.toString(serialNumber));
    system.save(object);
    LOG.info("serial number written: " + serialNumber);
}