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.device.MongoDeviceManagement.java

License:Open Source License

@Override
public SearchResults<IDeviceLocation> listDeviceLocations(List<String> assignmentTokens,
        IDateRangeSearchCriteria criteria) throws SiteWhereException {
    DBCollection events = getMongoClient().getEventsCollection();
    BasicDBObject query = new BasicDBObject();
    query.put(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_TOKEN, new BasicDBObject("$in", assignmentTokens));
    query.append(MongoDeviceEvent.PROP_EVENT_TYPE, DeviceEventType.Location.name());
    MongoPersistence.addDateSearchCriteria(query, MongoDeviceEvent.PROP_EVENT_DATE, criteria);
    BasicDBObject sort = new BasicDBObject(MongoDeviceEvent.PROP_EVENT_DATE, -1)
            .append(MongoDeviceEvent.PROP_RECEIVED_DATE, -1);
    return MongoPersistence.search(IDeviceLocation.class, events, query, sort, criteria);
}

From source file:com.sitewhere.mongodb.device.MongoDeviceMeasurements.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * /*from   w w w  . j  a va  2s .  c  om*/
 * @param source
 * @param target
 */
public static void toDBObject(IDeviceMeasurements source, BasicDBObject target, boolean isNested) {
    MongoDeviceEvent.toDBObject(source, target, isNested);

    // Save arbitrary measurements.
    List<BasicDBObject> props = new ArrayList<BasicDBObject>();
    for (String key : source.getMeasurements().keySet()) {
        BasicDBObject prop = new BasicDBObject();
        prop.put(PROP_NAME, key);
        prop.put(PROP_VALUE, source.getMeasurement(key));
        props.add(prop);
    }
    target.append(PROP_MEASUREMENTS, props);
}

From source file:com.sitewhere.mongodb.device.MongoDeviceSpecification.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * /*from w  w  w  .  j a  v  a  2 s. c om*/
 * @param source
 * @param target
 */
public static void toDBObject(IDeviceSpecification source, BasicDBObject target) {
    target.append(PROP_TOKEN, source.getToken());
    target.append(PROP_NAME, source.getName());
    target.append(PROP_ASSET_MODULE_ID, source.getAssetModuleId());
    target.append(PROP_ASSET_ID, source.getAssetId());
    target.append(PROP_CONTAINER_POLICY, source.getContainerPolicy().name());
    MongoSiteWhereEntity.toDBObject(source, target);
    MongoMetadataProvider.toDBObject(source, target);

    // Marshal device element schema as JSON.
    if (source.getDeviceElementSchema() != null) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            byte[] schemaJson = mapper.writeValueAsBytes(source.getDeviceElementSchema());
            target.append(PROP_DEVICE_ELEMENT_SCHEMA, schemaJson);
        } catch (JsonProcessingException e) {
            LOGGER.error("Unable to marshal device element schema for MongoDB persistence.", e);
        }
    }
}

From source file:com.sitewhere.mongodb.device.MongoDeviceStateChange.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * /*  w w  w  .  ja v  a  2  s  . c o m*/
 * @param source
 * @param target
 */
public static void toDBObject(IDeviceStateChange source, BasicDBObject target) {
    MongoDeviceEvent.toDBObject(source, target, false);

    target.append(PROP_CATEGORY, source.getCategory().name());
    target.append(PROP_TYPE, source.getType().name());
    target.append(PROP_PREVIOUS_STATE, source.getPreviousState());
    target.append(PROP_NEW_STATE, source.getNewState());

    BasicDBObject params = new BasicDBObject();
    for (String key : source.getData().keySet()) {
        params.append(key, source.getData().get(key));
    }
    target.append(PROP_DATA, params);
}

From source file:com.sitewhere.mongodb.device.MongoDeviceStream.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * /*  w w  w  .ja  v a  2s . co  m*/
 * @param source
 * @param target
 */
public static void toDBObject(IDeviceStream source, BasicDBObject target) {
    target.append(PROP_ASSIGNMENT_TOKEN, source.getAssignmentToken());
    target.append(PROP_STREAM_ID, source.getStreamId());
    target.append(PROP_CONTENT_TYPE, source.getContentType());

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

From source file:com.sitewhere.mongodb.device.MongoDeviceStreamData.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * //from w w w. jav  a2 s . com
 * @param source
 * @param target
 * @param isNested
 */
public static void toDBObject(IDeviceStreamData source, BasicDBObject target, boolean isNested) {
    MongoDeviceEvent.toDBObject(source, target, isNested);

    target.append(PROP_STREAM_ID, source.getStreamId());
    target.append(PROP_SEQUENCE_NUMBER, source.getSequenceNumber());
    target.append(PROP_DATA, source.getData());
}

From source file:com.sitewhere.mongodb.device.MongoSite.java

License:Open Source License

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

    BasicDBObject mapData = new BasicDBObject();
    mapData.append(PROP_MAP_TYPE, source.getMap().getType());
    MongoMetadataProvider.toDBObject(PROP_MAP_METADATA, source.getMap(), mapData);
    target.append(PROP_MAP_DATA, mapData);

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

From source file:com.sitewhere.mongodb.device.MongoZone.java

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * //from  w w w . jav  a  2s.co  m
 * @param source
 * @param target
 */
public static void toDBObject(IZone source, BasicDBObject target) {
    target.append(PROP_TOKEN, source.getToken());
    target.append(PROP_SITE_TOKEN, source.getSiteToken());
    target.append(PROP_NAME, source.getName());
    target.append(PROP_BORDER_COLOR, source.getBorderColor());
    target.append(PROP_FILL_COLOR, source.getFillColor());
    target.append(PROP_OPACITY, source.getOpacity());

    BasicDBList coords = new BasicDBList();
    if (source.getCoordinates() != null) {
        for (ILocation location : source.getCoordinates()) {
            BasicDBObject coord = new BasicDBObject();
            coord.put(MongoDeviceLocation.PROP_LATITUDE, location.getLatitude());
            coord.put(MongoDeviceLocation.PROP_LONGITUDE, location.getLongitude());
            if (location.getElevation() != null) {
                coord.put(MongoDeviceLocation.PROP_ELEVATION, location.getElevation());
            }
            coords.add(coord);
        }
    }
    target.append(PROP_COORDINATES, coords);

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

From source file:com.sitewhere.mongodb.MongoPersistence.java

License:Open Source License

/**
 * Appends filter criteria onto exiting query based on the given date range.
 * //w  w w.j  a  va 2 s  .c  o  m
 * @param query
 * @param criteria
 */
public static void addDateSearchCriteria(BasicDBObject query, String dateField,
        IDateRangeSearchCriteria criteria) {
    if ((criteria.getStartDate() == null) && (criteria.getEndDate() == null)) {
        return;
    }
    BasicDBObject dateClause = new BasicDBObject();
    if (criteria.getStartDate() != null) {
        dateClause.append("$gte", criteria.getStartDate());
    }
    if (criteria.getEndDate() != null) {
        dateClause.append("$lte", criteria.getEndDate());
    }
    query.put(dateField, dateClause);
}

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

License:Open Source License

/**
 * Copy information from SPI into Mongo DBObject.
 * /*from w w  w.j a v a  2s.  co m*/
 * @param source
 * @param target
 */
public static void toDBObject(ISchedule source, BasicDBObject target) {
    target.append(PROP_TOKEN, source.getToken());
    target.append(PROP_NAME, source.getName());
    target.append(PROP_START_DATE, source.getStartDate());
    target.append(PROP_END_DATE, source.getEndDate());
    target.append(PROP_TRIGGER_TYPE, source.getTriggerType().name());

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

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