Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

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

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:com.sitewhere.mongodb.scheduling.MongoScheduledJob.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * //from   w w  w  . j av a2s .c  o m
 * @param source
 * @param target
 */
public static void toDBObject(IScheduledJob source, BasicDBObject target) {
    target.append(PROP_TOKEN, source.getToken());
    target.append(PROP_SCHEDULE_TOKEN, source.getScheduleToken());
    target.append(PROP_JOB_TYPE, source.getJobType().name());
    target.append(PROP_JOB_STATE, source.getJobState().name());

    BasicDBObject config = new BasicDBObject();
    for (String key : source.getJobConfiguration().keySet()) {
        config.put(key, source.getJobConfiguration().get(key));
    }
    target.put(PROP_JOB_CONFIGURATION, config);

    MongoSiteWhereEntity.toDBObject(source, target);
    MongoMetadataProvider.toDBObject(source, target);
}

From source file:com.sitewhere.mongodb.tenant.MongoTenant.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * //from   w w w .  j a  va 2s  .  com
 * @param source
 * @param target
 */
public static void toDBObject(ITenant source, BasicDBObject target) {
    target.append(PROP_ID, source.getId());
    target.append(PROP_NAME, source.getName());
    target.append(PROP_AUTH_TOKEN, source.getAuthenticationToken());
    target.append(PROP_LOGO_URL, source.getLogoUrl());
    target.append(PROP_AUTH_USERS, source.getAuthorizedUserIds());
    target.append(PROP_ENGINE_CONF, source.getEngineConfiguration());

    MongoSiteWhereEntity.toDBObject(source, target);
    MongoMetadataProvider.toDBObject(source, target);
}

From source file:com.sitewhere.mongodb.tenant.MongoTenantGroup.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * //from  ww w. ja v a2 s. c o  m
 * @param source
 * @param target
 */
public static void toDBObject(ITenantGroup source, BasicDBObject target) {
    target.append(PROP_TOKEN, source.getToken());
    target.append(PROP_NAME, source.getName());
    target.append(PROP_DESCRIPTION, source.getDescription());
    target.append(PROP_IMAGE_URL, source.getImageUrl());

    MongoSiteWhereEntity.toDBObject(source, target);
    MongoMetadataProvider.toDBObject(source, target);
}

From source file:com.sitewhere.mongodb.tenant.MongoTenantGroupElement.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * //from  w  w  w. j  av a 2s  .com
 * @param source
 * @param target
 */
public static void toDBObject(ITenantGroupElement source, BasicDBObject target) {
    target.append(PROP_GROUP_TOKEN, source.getTenantGroupToken());
    target.append(PROP_TENANT_ID, source.getTenantId());
}

From source file:com.sitewhere.mongodb.tenant.MongoTenantManagement.java

License:Open Source License

@Override
public ISearchResults<ITenant> listTenants(ITenantSearchCriteria criteria) throws SiteWhereException {
    DBCollection tenants = getMongoClient().getTenantsCollection();
    BasicDBObject dbCriteria = new BasicDBObject();
    if (criteria.getTextSearch() != null) {
        try {/*  www.  j  a  v a2 s.  com*/
            Pattern regex = Pattern.compile(Pattern.quote(criteria.getTextSearch()));
            DBObject idSearch = new BasicDBObject(MongoTenant.PROP_ID, regex);
            DBObject nameSearch = new BasicDBObject(MongoTenant.PROP_NAME, regex);
            BasicDBList or = new BasicDBList();
            or.add(idSearch);
            or.add(nameSearch);
            dbCriteria.append("$or", or);
        } catch (PatternSyntaxException e) {
            LOGGER.warn("Invalid regex for searching tenant list. Ignoring.");
        }
    }
    if (criteria.getUserId() != null) {
        dbCriteria.append(MongoTenant.PROP_AUTH_USERS, criteria.getUserId());
    }
    BasicDBObject sort = new BasicDBObject(MongoTenant.PROP_NAME, 1);
    ISearchResults<ITenant> list = MongoPersistence.search(ITenant.class, tenants, dbCriteria, sort, criteria);
    SiteWherePersistence.tenantListLogic(list.getResults(), criteria);
    return list;
}

From source file:com.sitewhere.mongodb.user.MongoGrantedAuthority.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * /*from  www .  j a  v a 2  s .c o m*/
 * @param source
 * @param target
 */
public static void toDBObject(IGrantedAuthority source, BasicDBObject target) {
    target.append(PROP_AUTHORITY, source.getAuthority());
    target.append(PROP_DESCRIPTION, source.getDescription());
}

From source file:com.sitewhere.mongodb.user.MongoUser.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * //from  w  w w.  jav a2 s . com
 * @param source
 * @param target
 */
public static void toDBObject(IUser source, BasicDBObject target) {
    target.append(PROP_USERNAME, source.getUsername());
    target.append(PROP_HASHED_PASSWORD, source.getHashedPassword());
    target.append(PROP_FIRST_NAME, source.getFirstName());
    target.append(PROP_LAST_NAME, source.getLastName());
    target.append(PROP_LAST_LOGIN, source.getLastLogin());
    target.append(PROP_AUTHORITIES, source.getAuthorities());
    if (source.getStatus() != null) {
        target.append(PROP_STATUS, source.getStatus().name());
    }
    MongoSiteWhereEntity.toDBObject(source, target);
    MongoMetadataProvider.toDBObject(source, target);
}

From source file:com.softinstigate.restheart.db.CollectionDAO.java

License:Open Source License

/**
 * Upsert the collection properties.//  ww  w  . j  a va 2  s  .co  m
 *
 * @param dbName the database name of the collection
 * @param collName the collection name
 * @param content the new collection properties
 * @param etag the entity tag. must match to allow actual write (otherwise
 * http error code is returned)
 * @param updating true if updating existing document
 * @param patching true if use patch semantic (update only specified fields)
 * @return the HttpStatus code to set in the http response
 */
public static int upsertCollection(String dbName, String collName, DBObject content, ObjectId etag,
        boolean updating, boolean patching) {
    DB db = DBDAO.getDB(dbName);

    DBCollection coll = db.getCollection(collName);

    if (patching && !updating) {
        return HttpStatus.SC_NOT_FOUND;
    }

    if (updating) {
        if (etag == null) {
            return HttpStatus.SC_CONFLICT;
        }

        BasicDBObject idAndEtagQuery = new BasicDBObject("_id", "_properties");
        idAndEtagQuery.append("_etag", etag);

        if (coll.count(idAndEtagQuery) < 1) {
            return HttpStatus.SC_PRECONDITION_FAILED;
        }
    }

    ObjectId timestamp = new ObjectId();
    Instant now = Instant.ofEpochSecond(timestamp.getTimestamp());

    if (content == null) {
        content = new BasicDBObject();
    }

    content.removeField("_id"); // make sure we don't change this field

    if (updating) {
        content.removeField("_crated_on"); // don't allow to update this field
        content.put("_etag", timestamp);
    } else {
        content.put("_id", "_properties");
        content.put("_created_on", now.toString());
        content.put("_etag", timestamp);
    }

    if (patching) {
        coll.update(PROPS_QUERY, new BasicDBObject("$set", content), true, false);
        return HttpStatus.SC_OK;
    } else {
        // we use findAndModify to get the @created_on field value from the existing properties document
        // we need to put this field back using a second update 
        // it is not possible in a single update even using $setOnInsert update operator
        // in this case we need to provide the other data using $set operator and this makes it a partial update (patch semantic) 

        DBObject old = coll.findAndModify(PROPS_QUERY, fieldsToReturn, null, false, content, false, true);

        if (old != null) {
            Object oldTimestamp = old.get("_created_on");

            if (oldTimestamp == null) {
                oldTimestamp = now.toString();
                logger.warn("properties of collection {} had no @created_on field. set to now",
                        coll.getFullName());
            }

            // need to readd the @created_on field 
            BasicDBObject createdContet = new BasicDBObject("_created_on", "" + oldTimestamp);
            createdContet.markAsPartialObject();
            coll.update(PROPS_QUERY, new BasicDBObject("$set", createdContet), true, false);

            return HttpStatus.SC_OK;
        } else {
            // need to readd the @created_on field 
            BasicDBObject createdContet = new BasicDBObject("_created_on", now.toString());
            createdContet.markAsPartialObject();
            coll.update(PROPS_QUERY, new BasicDBObject("$set", createdContet), true, false);

            initDefaultIndexes(coll);

            return HttpStatus.SC_CREATED;
        }
    }
}

From source file:com.softinstigate.restheart.db.CollectionDAO.java

License:Open Source License

/**
 * Deletes a collection.//  w  ww. ja  va2  s  .c o  m
 *
 * @param dbName the database name of the collection
 * @param collName the collection name
 * @param etag the entity tag. must match to allow actual write (otherwise
 * http error code is returned)
 * @return the HttpStatus code to set in the http response
 */
public static int deleteCollection(String dbName, String collName, ObjectId etag) {
    DBCollection coll = getCollection(dbName, collName);

    BasicDBObject checkEtag = new BasicDBObject("_id", "_properties");
    checkEtag.append("_etag", etag);

    DBObject exists = coll.findOne(checkEtag, fieldsToReturn);

    if (exists == null) {
        return HttpStatus.SC_PRECONDITION_FAILED;
    } else {
        coll.drop();
        return HttpStatus.SC_NO_CONTENT;
    }
}

From source file:com.softinstigate.restheart.db.DBDAO.java

License:Open Source License

/**
 *
 * @param dbName//ww  w  .ja  va  2s.c om
 * @param content
 * @param etag
 * @param patching
 * @return
 */
public static int upsertDB(String dbName, DBObject content, ObjectId etag, boolean patching) {
    DB db = client.getDB(dbName);

    boolean existing = db.getCollectionNames().size() > 0;

    if (patching && !existing) {
        return HttpStatus.SC_NOT_FOUND;
    }

    DBCollection coll = db.getCollection("_properties");

    // check the etag
    if (db.collectionExists("_properties")) {
        if (etag == null) {
            return HttpStatus.SC_CONFLICT;
        }

        BasicDBObject idAndEtagQuery = new BasicDBObject("_id", "_properties");
        idAndEtagQuery.append("_etag", etag);

        if (coll.count(idAndEtagQuery) < 1) {
            return HttpStatus.SC_PRECONDITION_FAILED;
        }
    }

    // apply new values
    ObjectId timestamp = new ObjectId();
    Instant now = Instant.ofEpochSecond(timestamp.getTimestamp());

    if (content == null) {
        content = new BasicDBObject();
    }

    content.put("_etag", timestamp);
    content.removeField("_created_on"); // make sure we don't change this field
    content.removeField("_id"); // make sure we don't change this field

    if (patching) {
        coll.update(METADATA_QUERY, new BasicDBObject("$set", content), true, false);

        return HttpStatus.SC_OK;
    } else {
        // we use findAndModify to get the @created_on field value from the existing document
        // we need to put this field back using a second update 
        // it is not possible in a single update even using $setOnInsert update operator
        // in this case we need to provide the other data using $set operator and this makes it a partial update (patch semantic) 
        DBObject old = coll.findAndModify(METADATA_QUERY, fieldsToReturn, null, false, content, false, true);

        if (old != null) {
            Object oldTimestamp = old.get("_created_on");

            if (oldTimestamp == null) {
                oldTimestamp = now.toString();
                logger.warn("properties of collection {} had no @created_on field. set to now",
                        coll.getFullName());
            }

            // need to readd the @created_on field 
            BasicDBObject createdContet = new BasicDBObject("_created_on", "" + oldTimestamp);
            createdContet.markAsPartialObject();
            coll.update(METADATA_QUERY, new BasicDBObject("$set", createdContet), true, false);

            return HttpStatus.SC_OK;
        } else {
            // need to readd the @created_on field 
            BasicDBObject createdContet = new BasicDBObject("_created_on", now.toString());
            createdContet.markAsPartialObject();
            coll.update(METADATA_QUERY, new BasicDBObject("$set", createdContet), true, false);

            return HttpStatus.SC_CREATED;
        }
    }
}