Example usage for com.mongodb DBObject keySet

List of usage examples for com.mongodb DBObject keySet

Introduction

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

Prototype

Set<String> keySet();

Source Link

Document

Returns this object's fields' names

Usage

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

License:Open Source License

/**
 * Copy information from Mongo DBObject to model object.
 * /*from  www .ja  v  a  2 s. c o  m*/
 * @param source
 * @param target
 */
public static void fromDBObject(DBObject source, BatchOperation target) {
    String token = (String) source.get(PROP_TOKEN);
    String operationType = (String) source.get(PROP_OPERATION_TYPE);
    String procStatus = (String) source.get(PROP_PROC_STATUS);
    Date procStart = (Date) source.get(PROP_PROC_START_DATE);
    Date procEnd = (Date) source.get(PROP_PROC_END_DATE);

    target.setToken(token);
    if (operationType != null) {
        target.setOperationType(OperationType.valueOf(operationType));
    }
    if (procStatus != null) {
        target.setProcessingStatus(BatchOperationStatus.valueOf(procStatus));
    }
    target.setProcessingStartedDate(procStart);
    target.setProcessingEndedDate(procEnd);

    // Load parameters from nested object.
    DBObject params = (DBObject) source.get(PROP_PARAMETERS);
    if (params != null) {
        for (String key : params.keySet()) {
            target.getParameters().put(key, (String) params.get(key));
        }
    }

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

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

License:Open Source License

/**
 * Copy information from Mongo DBObject to model object.
 * //from  ww  w  . j  a  v a  2  s. c o  m
 * @param source
 * @param target
 */
public static void fromDBObject(DBObject source, DeviceCommandInvocation target) {
    MongoDeviceEvent.fromDBObject(source, target, false);

    String initiatorName = (String) source.get(PROP_INITIATOR);
    String initiatorId = (String) source.get(PROP_INITIATOR_ID);
    String targetName = (String) source.get(PROP_TARGET);
    String targetId = (String) source.get(PROP_TARGET_ID);
    String commandToken = (String) source.get(PROP_COMMAND_TOKEN);
    String statusName = (String) source.get(PROP_STATUS);

    if (initiatorName != null) {
        target.setInitiator(CommandInitiator.valueOf(initiatorName));
    }
    if (targetName != null) {
        target.setTarget(CommandTarget.valueOf(targetName));
    }
    if (statusName != null) {
        target.setStatus(CommandStatus.valueOf(statusName));
    }
    target.setInitiatorId(initiatorId);
    target.setTargetId(targetId);
    target.setCommandToken(commandToken);

    Map<String, String> params = new HashMap<String, String>();
    DBObject dbparams = (DBObject) source.get(PROP_PARAMETER_VALUES);
    if (dbparams != null) {
        for (String key : dbparams.keySet()) {
            params.put(key, (String) dbparams.get(key));
        }
    }
    target.setParameterValues(params);
}

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

License:Open Source License

/**
 * Copy information from Mongo DBObject to model object.
 * /*from   w  w  w.ja v  a2 s  .c  om*/
 * @param source
 * @param target
 */
public static void fromDBObject(DBObject source, DeviceStateChange target) {
    MongoDeviceEvent.fromDBObject(source, target, false);

    String category = (String) source.get(PROP_CATEGORY);
    String type = (String) source.get(PROP_TYPE);
    String previousState = (String) source.get(PROP_PREVIOUS_STATE);
    String newState = (String) source.get(PROP_NEW_STATE);

    if (category != null) {
        target.setCategory(StateChangeCategory.valueOf(category));
    }
    if (type != null) {
        target.setType(StateChangeType.valueOf(type));
    }
    target.setPreviousState(previousState);
    target.setNewState(newState);

    Map<String, String> data = new HashMap<String, String>();
    DBObject dbdata = (DBObject) source.get(PROP_DATA);
    if (dbdata != null) {
        for (String key : dbdata.keySet()) {
            data.put(key, (String) dbdata.get(key));
        }
    }
    target.setData(data);
}

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

License:Open Source License

/**
 * Copy information from Mongo DBObject to model object.
 * //from  w ww  .j  a  v  a 2s  .c  o  m
 * @param source
 * @param target
 */
public static void fromDBObject(DBObject source, Schedule target) {
    String token = (String) source.get(PROP_TOKEN);
    String name = (String) source.get(PROP_NAME);
    String type = (String) source.get(PROP_TRIGGER_TYPE);
    Date startDate = (Date) source.get(PROP_START_DATE);
    Date endDate = (Date) source.get(PROP_END_DATE);

    target.setToken(token);
    target.setName(name);
    target.setStartDate(startDate);
    target.setEndDate(endDate);
    target.setTriggerType(TriggerType.valueOf(type));

    DBObject config = (DBObject) source.get(PROP_TRIGGER_CONFIGURATION);
    if (config != null) {
        for (String key : config.keySet()) {
            target.getTriggerConfiguration().put(key, (String) config.get(key));
        }
    }

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

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

License:Open Source License

/**
 * Copy information from Mongo DBObject to model object.
 * //from   w w  w .ja va  2s .c o  m
 * @param source
 * @param target
 */
public static void fromDBObject(DBObject source, ScheduledJob target) {
    String token = (String) source.get(PROP_TOKEN);
    String scheduleToken = (String) source.get(PROP_SCHEDULE_TOKEN);
    String type = (String) source.get(PROP_JOB_TYPE);
    String state = (String) source.get(PROP_JOB_STATE);

    target.setToken(token);
    target.setScheduleToken(scheduleToken);
    target.setJobType(ScheduledJobType.valueOf(type));
    target.setJobState(ScheduledJobState.valueOf(state));

    DBObject config = (DBObject) source.get(PROP_JOB_CONFIGURATION);
    if (config != null) {
        for (String key : config.keySet()) {
            target.getJobConfiguration().put(key, (String) config.get(key));
        }
    }

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

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

License:Open Source License

/**
 * @param row a DBObject row/*from  ww  w . ja v  a 2  s.co  m*/
 * @param fieldsToFilter list of field names to filter
 * @return
 */
public static TreeMap<String, Object> getDataFromRow(DBObject row, String... fieldsToFilter) {
    if (row == null) {
        return null;
    }

    if (row instanceof BasicDBList) {
        throw new IllegalArgumentException("cannot convert an array to a map");
    }

    List<String> _fieldsToFilter = Arrays.asList(fieldsToFilter);

    TreeMap<String, Object> properties = new TreeMap<>();

    row.keySet().stream().forEach((key) -> {
        if (!_fieldsToFilter.contains(key)) {
            properties.put(key, getElement(row.get(key)));
        }
    });

    return properties;
}

From source file:com.softinstigate.restheart.hal.HALUtils.java

License:Open Source License

/**
 *
 * @param rep//from ww  w.j  a v a  2s  .c  o m
 * @param data
 */
public static void addData(Representation rep, DBObject data) {
    // collection properties
    data.keySet().stream().forEach((key) -> {
        Object value = data.get(key);

        if (value instanceof ObjectId) {
            rep.addProperty(key, value.toString());
        } else {
            rep.addProperty(key, value);
        }
    });
}

From source file:com.softinstigate.restheart.handlers.document.DocumentRepresentationFactory.java

License:Open Source License

/**
 *
 * @param href//from   w  ww. j av  a2s. c  o m
 * @param exchange
 * @param context
 * @param data
 * @return
 * @throws IllegalQueryParamenterException
 */
public static Representation getDocument(String href, HttpServerExchange exchange, RequestContext context,
        DBObject data) throws IllegalQueryParamenterException {
    Representation rep = new Representation(href);

    rep.addProperty("_type", context.getType().name());

    // document properties
    data.keySet().stream().forEach((key) -> {
        Object value = data.get(key);

        if (value instanceof ObjectId) {
            value = value.toString();
        }

        rep.addProperty(key, value);
    });

    // document links
    TreeMap<String, String> links;

    links = getRelationshipsLinks(context, data);

    if (links != null) {
        links.keySet().stream().forEach((k) -> {
            rep.addLink(new Link(k, links.get(k)));
        });
    }

    // link templates and curies
    String requestPath = URLUtilis.removeTrailingSlashes(exchange.getRequestPath());
    if (context.isParentAccessible()) // this can happen due to mongo-mounts mapped URL
    {
        rep.addLink(new Link("rh:coll", URLUtilis.getPerentPath(requestPath)));
    }
    rep.addLink(new Link("rh", "curies", Configuration.RESTHEART_ONLINE_DOC_URL + "/#api-doc-{rel}", false),
            true);

    ResponseHelper.injectWarnings(rep, exchange, context);

    return rep;
}

From source file:com.softinstigate.restheart.handlers.injectors.BodyInjectorHandler.java

License:Open Source License

/**
 *
 * @param exchange//from  w  ww  .ja  va 2  s.  c  o  m
 * @param context
 * @throws Exception
 */
@Override
public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception {
    if (context.getMethod() == RequestContext.METHOD.GET || context.getMethod() == RequestContext.METHOD.OPTIONS
            || context.getMethod() == RequestContext.METHOD.DELETE) {
        next.handleRequest(exchange, context);
        return;
    }

    // check content type
    HeaderValues contentTypes = exchange.getRequestHeaders().get(Headers.CONTENT_TYPE);

    if (contentTypes == null || contentTypes.isEmpty() || contentTypes.stream().noneMatch(
            ct -> ct.startsWith(Representation.HAL_JSON_MEDIA_TYPE) || ct.startsWith(JSON_MEDIA_TYPE))) // content type header can be also: Content-Type: application/json; charset=utf-8
    {
        ResponseHelper.endExchangeWithMessage(exchange, HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE,
                "Contet-Type must be either " + Representation.HAL_JSON_MEDIA_TYPE + " or " + JSON_MEDIA_TYPE);
        return;
    }

    String _content = ChannelReader.read(exchange.getRequestChannel());

    DBObject content;

    try {
        content = (DBObject) JSON.parse(_content);
    } catch (JSONParseException ex) {
        ResponseHelper.endExchangeWithMessage(exchange, HttpStatus.SC_NOT_ACCEPTABLE, "invalid data", ex);
        return;
    }

    HashSet<String> keysToRemove = new HashSet<>();

    if (content == null) {
        context.setContent(null);
    } else {
        // filter out reserved keys
        content.keySet().stream().filter(key -> key.startsWith("_") && !key.equals("_id")).forEach(key -> {
            keysToRemove.add(key);
        });

        keysToRemove.stream().map(keyToRemove -> {
            content.removeField(keyToRemove);
            return keyToRemove;
        }).forEach(keyToRemove -> {
            context.addWarning("the reserved field " + keyToRemove + " was filtered out from the request");
        });

        // inject the request content in the context
        context.setContent(content);
    }

    next.handleRequest(exchange, context);
}

From source file:com.stratio.connector.mongodb.core.engine.storage.MongoInsertHandler.java

License:Apache License

/**
 * Insert if not exist./*from   ww w . j  a v  a  2 s. co m*/
 *
 * @param targetTable
 *            the target table
 * @param row
 *            the row
 * @param pk
 *            the pk
 * @throws MongoValidationException
 *             if the operation is not supported by the connector
 * @throws MongoInsertException
 *             if the insertion fails
 */
public void insertIfNotExist(TableMetadata targetTable, Row row, Object pk)
        throws MongoValidationException, MongoInsertException {

    DBObject doc = getBSONFromRow(targetTable, row);
    BasicDBObject find = new BasicDBObject("_id", pk);

    try {
        if (bulkWriteOperation != null) {
            bulkWriteOperation.find(find).upsert().update(new BasicDBObject("$setOnInsert", doc));
        } else {
            collection.update(find, new BasicDBObject("$setOnInsert", doc), true, false);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Row updated with fields: " + doc.keySet());
        }
    } catch (MongoException e) {
        logger.error("Error inserting data: " + e.getMessage());
        throw new MongoInsertException(e.getMessage(), e);
    }
}