Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

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

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:com.ejbmongoembeddedtomcat.converter.CustomerConverter.java

public static Customer toCustomer(DBObject doc) {
    Customer cus = new Customer();
    cus.setName((String) doc.get("name"));
    cus.setAddress((String) doc.get("address"));
    cus.setKota((String) doc.get("kota"));
    ObjectId id = (ObjectId) doc.get("_id");
    cus.setId(id.toString());/*w  ww  .j a  v a  2  s .co  m*/

    return cus;
}

From source file:com.ejbmongoembeddedtomcat.service.MongoService.java

public Customer createCustomer(Customer cus) {

    DBObject doc = CustomerConverter.toDBObject(cus);
    this.col.insert(doc);
    ObjectId id = (ObjectId) doc.get("_id");
    cus.setId(id.toString());//from   w ww. j  a  v a 2 s  .  c  o m
    return cus;
}

From source file:com.englishtown.vertx.GridFSModule.java

License:Open Source License

public void getChunk(Message<JsonObject> message, final JsonObject jsonObject) {

    ObjectId id = getObjectId(message, jsonObject, "files_id");

    Integer n = getRequiredInt("n", message, jsonObject, 0);
    if (n == null) {
        return;/* w w w . ja  v  a  2 s. co  m*/
    }

    String bucket = jsonObject.getString("bucket", GridFS.DEFAULT_BUCKET);

    DBCollection collection = db.getCollection(bucket + ".chunks");
    DBObject dbObject = BasicDBObjectBuilder.start("files_id", id).add("n", n).get();

    DBObject result = collection.findOne(dbObject);

    if (result == null) {
        message.reply(new byte[0]);
        return;
    }

    byte[] data = (byte[]) result.get("data");
    boolean reply = jsonObject.getBoolean("reply", false);
    Handler<Message<JsonObject>> replyHandler = null;

    if (reply) {
        replyHandler = new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> reply) {
                int n = jsonObject.getInteger("n") + 1;
                jsonObject.putNumber("n", n);
                getChunk(reply, jsonObject);
            }
        };
    }

    // TODO: Change to reply with a Buffer instead of a byte[]?
    message.reply(data, replyHandler);

}

From source file:com.epam.dlab.migration.mongo.changelog.DlabChangeLog.java

License:Apache License

@SuppressWarnings("unchecked")
private void updateSchedulerFieldsForExploratory(DBCollection userInstances, DBObject dbObject) {
    updateSchedulerFields(dbObject);/*from  w  w  w .ja v a  2 s.co m*/
    Optional.ofNullable(dbObject.get("computational_resources")).map(cr -> (List<DBObject>) cr)
            .ifPresent(computationalResources -> computationalResources.forEach(this::updateSchedulerFields));
    userInstances.update(new BasicDBObject(ID, dbObject.get(ID)), dbObject);
}

From source file:com.epam.dlab.migration.mongo.changelog.DlabChangeLog.java

License:Apache License

private void updateSchedulerFields(DBObject dbObject) {
    final Object schedulerData = dbObject.get("scheduler_data");
    if (schedulerData != null) {
        final Object daysRepeat = ((DBObject) schedulerData).removeField("days_repeat");
        ((DBObject) schedulerData).put("start_days_repeat", daysRepeat);
        ((DBObject) schedulerData).put("stop_days_repeat", daysRepeat);
    }/*  w  w w .ja  v  a  2 s  .c  om*/
}

From source file:com.epam.ta.reportportal.database.ActivityDocumentHandler.java

License:Open Source License

@Override
public void processDocument(DBObject dbObject) throws MongoException, DataAccessException {
    Set<String> keySet = dbObject.keySet();
    ChartObject activity = new ChartObject();
    Map<String, String> objectValues = new HashMap<>();
    for (String key : keySet) {
        switch (key) {
        case HISTORY:
            Map<String, String> historyProps = transformHistoryMap(dbObject, "history");
            objectValues.putAll(historyProps);
            break;
        case Modifiable.LAST_MODIFIED:
            objectValues.put(key, String.valueOf(((Date) dbObject.get(key)).getTime()));
            break;
        case ID:/*from   w  w w  . ja v  a  2s  . co  m*/
            activity.setId(dbObject.get(ID).toString());
            break;
        default:
            objectValues.put(key, dbObject.get(key).toString());
            break;
        }
    }
    activity.setValues(objectValues);
    result.add(activity);
}

From source file:com.epam.ta.reportportal.database.ActivityDocumentHandler.java

License:Open Source License

private Map<String, String> transformHistoryMap(DBObject dbObject, String dbField) {
    @SuppressWarnings("unchecked")
    Map<String, Map<String, String>> tempHistory = (Map<String, Map<String, String>>) dbObject.get(dbField);
    if (null == tempHistory)
        return null;

    Map<String, String> chartObjectValues = new HashMap<>();

    for (Map.Entry<String, Map<String, String>> entry : tempHistory.entrySet()) {
        Activity.FieldValues fieldValues = new Activity.FieldValues();
        for (Field field : fieldValues.getClass().getDeclaredFields()) {
            String innerDbField = getDbRepresentation(field);
            AccessibleField innerField = Accessible.on(fieldValues).field(field);
            Map<String, String> values = entry.getValue();
            if ((values != null) && (null != values.get(innerDbField)))
                innerField.setValue(values.get(innerDbField));
        }//  w ww  .  ja  v a 2 s  . co m
        chartObjectValues.put(entry.getKey() + "$" + Activity.FieldValues.OLD_VALUE, fieldValues.getOldValue());
        chartObjectValues.put(entry.getKey() + "$" + Activity.FieldValues.NEW_VALUE, fieldValues.getNewValue());
    }
    return chartObjectValues;
}

From source file:com.epam.ta.reportportal.database.LaunchesDurationDocumentHandler.java

License:Open Source License

@Override
public void processDocument(DBObject dbObject) throws MongoException, DataAccessException {
    ChartObject chartObject = new ChartObject();
    Map<String, String> values = new HashMap<>();
    chartObject.setId(dbObject.get(ID).toString());
    if (dbObject.containsField(START_TIME) && dbObject.containsField(END_TIME)) {
        Long startTime = ((Date) dbObject.get(START_TIME)).getTime();
        Long endTime = ((Date) dbObject.get(END_TIME)).getTime();
        long duration = endTime - startTime;
        values.put(START_TIME, String.valueOf(startTime));
        values.put(END_TIME, String.valueOf(endTime));
        values.put(DURATION, String.valueOf(duration));
    }/*from w ww  . j ava  2s.c om*/

    if (dbObject.containsField(NAME)) {
        chartObject.setName(dbObject.get(NAME).toString());
    }

    if (dbObject.containsField(NUMBER)) {
        chartObject.setNumber(dbObject.get(NUMBER).toString());
    }
    if (dbObject.containsField(STATUS)) {
        values.put(STATUS, dbObject.get(STATUS).toString());
    }
    chartObject.setValues(values);
    result.add(chartObject);
}

From source file:com.epam.ta.reportportal.database.LaunchesTableDocumentHandler.java

License:Open Source License

@Override
public void processDocument(DBObject dbObject) throws MongoException, DataAccessException {
    if (contentFields == null) {
        return;/*from   w  w  w.j  a v a2  s.  c  o m*/
    }

    ChartObject chartObject = new ChartObject();
    Map<String, String> values = new HashMap<>();
    for (String contentField : contentFields) {
        String[] split = contentField.split("\\.");
        Object object;
        if (split.length == 1) {
            object = dbObject.get(contentField);
        } else {
            object = getValue(dbObject, split);
        }

        chartObject.setId(dbObject.get(ID).toString());

        if (contentField.equalsIgnoreCase(NAME))
            chartObject.setName(object.toString());
        else if (contentField.equalsIgnoreCase(NUMBER))
            chartObject.setNumber(object.toString());
        else if (contentField.equalsIgnoreCase(START_TIME))
            chartObject.setStartTime(valueOf(((Date) object).getTime()));
        else {
            if (object instanceof Date) {
                values.put(contentField, valueOf(((Date) object).getTime()));
            } else if (object instanceof List) {
                values.put(contentField,
                        ((List<?>) object).stream().map(Object::toString).collect(Collectors.joining(",")));
            } else {
                values.put(contentField, null != object ? object.toString() : null);
            }
        }
    }
    chartObject.setValues(values);
    list.add(chartObject);
}

From source file:com.epam.ta.reportportal.database.LaunchesTableDocumentHandler.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
private Object getValue(DBObject dbObject, String[] path) {
    boolean isExist = true;
    int counter = 1;
    Map innerDbObject = (Map) dbObject.get(path[0]);
    while (isExist) {
        if (counter >= path.length) {
            Map<String, Integer> convert = innerDbObject;
            // find total of custom defect types
            // statistics.issueCounter.productBug.total = 3
            // statistics.issueCounter.productBug.pb1 = 1
            // statistics.issueCounter.productBug.pb2 = 2
            // statistics.issueCounter.productBug.pb3 = 0
            return convert.keySet().size() > 0
                    ? valueOf(convert.values().stream().mapToInt(Integer::intValue).max().orElse(0))
                    : "0";
        }//ww  w .j a va2s  .co  m
        if (innerDbObject.containsKey(path[counter])) {
            Object innerValue = innerDbObject.get(path[counter]);
            if (innerValue instanceof Map) {
                innerDbObject = (Map) innerValue;
                counter++;
            } else {
                return innerValue;
            }
        } else {
            // return 0 if subtype is absent
            return 0;
        }
    }
    return innerDbObject;
}