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.eywa.impl.app.mongo.entities.items.jobs.ItemJobMessage.java

License:Open Source License

public static DBObject getRecipientsWithTemplates(final DBObject item) {
    final DBObject result = new BasicDBObject();
    final DBObject recipients = getRecipients(item);
    final Set<String> users = recipients.keySet();
    for (final String userId : users) {
        final String templateName = MongoUtils.getString(recipients, userId);
        final DBObject template = getTemplate(item,
                StringUtils.hasText(templateName) ? templateName : DEFAULT_TEMPLATE);
        if (null != template) {
            result.put(userId, template);
        }/*from  w w  w. ja  va2 s.  com*/
    }
    return result;
}

From source file:com.gigaspaces.persistency.datasource.MongoQueryFactory.java

License:Open Source License

private static BasicDBObjectBuilder replaceParameters(Object[] parameters, SpaceDocumentMapper<DBObject> mapper,
        BasicDBObjectBuilder builder, Integer index) {

    BasicDBObjectBuilder newBuilder = BasicDBObjectBuilder.start();

    DBObject document = builder.get();

    Iterator<String> iterator = document.keySet().iterator();

    while (iterator.hasNext()) {
        String field = iterator.next();
        Object ph = document.get(field);

        if (index >= parameters.length)
            return builder;

        if (ph instanceof String) {
            if (PARAM_PLACEHOLDER.equals(ph)) {
                Object p = mapper.toObject(parameters[index++]);

                if (p instanceof DBObject
                        && Constants.CUSTOM_BINARY.equals(((DBObject) p).get(Constants.TYPE))) {
                    newBuilder.add(field + "." + Constants.HASH, ((DBObject) p).get(Constants.HASH));
                } else {
                    newBuilder.add(field, p);
                }/*from  w w w .  ja v a2 s . c o  m*/
            }
        } else if (ph instanceof Pattern) {
            Pattern p = (Pattern) ph;

            if (LIKE.equalsIgnoreCase(p.pattern())) {
                newBuilder.add(field, convertLikeExpression((String) parameters[index++]));
            } else if (RLIKE.equalsIgnoreCase(p.pattern())) {
                newBuilder.add(field, Pattern.compile((String) parameters[index++], Pattern.CASE_INSENSITIVE));
            }
        } else {
            DBObject element = (DBObject) ph;

            Object p = mapper.toObject(parameters[index]);

            if (p instanceof DBObject) {
                String t = (String) ((DBObject) p).get(Constants.TYPE);
                String op = element.keySet().iterator().next();

                if (Constants.CUSTOM_BINARY.equals(t) && !op.equals("$ne"))
                    return newBuilder;
            }

            BasicDBObjectBuilder doc = replaceParameters(parameters, mapper,
                    BasicDBObjectBuilder.start(element.toMap()), index);

            newBuilder.add(field, doc.get());
        }
    }
    return newBuilder;
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

private Object toPojo(DBObject bson) {

    String className = (String) bson.get(Constants.TYPE);

    try {//from   ww  w  .  j ava2  s .  c o  m
        Class<?> type = getClassFor(className);

        Object pojo = repository.getConstructor(getClassFor(className)).newInstance();

        Iterator<String> iterator = bson.keySet().iterator();

        while (iterator.hasNext()) {

            String property = iterator.next();

            if (Constants.TYPE.equals(property))
                continue;

            Object value = bson.get(property);

            boolean isArray = false;

            if (value instanceof BasicDBList) {
                isArray = true;

            }

            if (value == null)
                continue;

            if (Constants.ID_PROPERTY.equals(property))
                property = spaceTypeDescriptor.getIdPropertyName();

            ISetterMethod<Object> setter = repository.getSetter(type, property);

            Object val;
            if (isArray) {
                val = toExtractArray((BasicDBList) value, setter.getParameterTypes()[0]);
            } else {
                val = fromDBObject(value);
            }

            if (type(setter.getParameterTypes()[0]) == TYPE_SHORT)
                val = ((Integer) val).shortValue();

            setter.set(pojo, val);
        }
        return pojo;
    } catch (InvocationTargetException e) {
        throw new SpaceMongoException("can not invoke constructor or method: " + bson, e);
    } catch (InstantiationException e) {
        throw new SpaceMongoException("Could not find default constructor for: " + bson, e);
    } catch (IllegalAccessException e) {
        throw new SpaceMongoException("can not access constructor or method: " + bson, e);
    }
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

private Object toSpaceDocument(DBObject bson) {

    SpaceDocument document = new SpaceDocument((String) bson.get(Constants.TYPE));
    Iterator<String> iterator = bson.keySet().iterator();

    while (iterator.hasNext()) {

        String property = iterator.next();

        if (Constants.TYPE.equals(property))
            continue;

        Object value = bson.get(property);

        if (value == null)
            continue;

        if (Constants.ID_PROPERTY.equals(property))
            property = spaceTypeDescriptor.getIdPropertyName();

        document.setProperty(property, fromDBObject(value));
    }/*from  w  w  w  .  j  ava 2 s.c  o m*/

    return document;
}

From source file:com.gigaspaces.persistency.metadata.SpaceDocumentMapperImpl.java

License:Open Source License

private Object toPojo(DBObject bson) {

    String className = (String) bson.get(TYPE);
    try {/*from  w w  w  .  j a v  a2 s  . c o  m*/
        Class<?> type = getClassFor(className);
        Object pojo = repository.getConstructor(getClassFor(className)).newInstance();

        for (String property : bson.keySet()) {

            if (TYPE.equals(property))
                continue;

            Object value = bson.get(property);

            if (value == null)
                continue;

            if (_ID.equals(property))
                property = spaceTypeDescriptor.getIdPropertyName();

            ISetterMethod<Object> setter = repository.getSetter(type, property);

            Object val = fromDBObject(value);

            setter.set(pojo, val);
        }
        return pojo;
    } catch (InvocationTargetException e) {
        throw new SpaceMongoException("can not invoke constructor or method: " + bson, e);
    } catch (InstantiationException e) {
        throw new SpaceMongoException("Could not find default constructor for: " + bson, e);
    } catch (IllegalAccessException e) {
        throw new SpaceMongoException("can not access constructor or method: " + bson, e);
    }

}

From source file:com.gigaspaces.persistency.metadata.SpaceDocumentMapperImpl.java

License:Open Source License

private Object toSpaceDocument(DBObject bson) {

    SpaceDocument document = new SpaceDocument((String) bson.get(TYPE));

    for (String property : bson.keySet()) {

        if (TYPE.equals(property))
            continue;

        Object value = bson.get(property);

        if (value == null)
            continue;

        if (_ID.equals(property))
            property = spaceTypeDescriptor.getIdPropertyName();

        document.setProperty(property, fromDBObject(value));
    }/* ww  w . jav  a2 s.  c  om*/

    return document;
}

From source file:com.gigaspaces.persistency.MongoClientConnector.java

License:Open Source License

private static DBObject normalize(DBObject obj) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();

    Iterator<String> iterator = obj.keySet().iterator();
    builder.push("$set");
    while (iterator.hasNext()) {

        String key = iterator.next();

        if (Constants.ID_PROPERTY.equals(key))
            continue;

        Object value = obj.get(key);

        if (value == null)
            continue;

        builder.add(key, value);// w  w  w .j a  v a  2s . c  o m
    }

    return builder.get();
}

From source file:com.gigaspaces.persistency.parser.SQL2MongoBaseVisitor.java

License:Open Source License

public T visitParse(SQL2MongoParser.ParseContext ctx) {

    T r = visitChildren(ctx);//w  w  w.  j  av  a 2s .  c o m

    for (DBObject o : ors) {
        query.putAll(o);
    }

    for (DBObject o : ands)
        query.putAll(o);

    DBObject o = atom.get();

    if (o.keySet().size() > 0)
        query.putAll(o);

    return r;
}

From source file:com.github.bluetiger9.nosql.benchmarking.clients.document.mongodb.MongoDBClient.java

License:Open Source License

@Override
public Map<String, String> get(String key) throws ClientException {
    final DBObject document = dbCollection.findOne(new BasicDBObject("_id", key));

    if (document == null)
        return null;

    final Map<String, String> result = new HashMap<>();
    for (String attribute : document.keySet()) {
        result.put(attribute, document.get(attribute).toString());
    }//from  w  w w . jav a2 s . c  om
    return result;
}

From source file:com.github.camellabs.iot.cloudlet.document.driver.mongodb.MongoQueryBuilder.java

License:Apache License

public DBObject jsonToMongoQuery(DBObject jsonQuery) {
    BasicDBObject mongoQuery = new BasicDBObject();
    for (String originalKey : jsonQuery.keySet()) {
        String compoundKey = originalKey.replaceAll("(.)_", "$1.");

        String suffixOperator = findFirstMatchOperator(originalKey);
        if (suffixOperator != null) {
            addRestriction(mongoQuery, compoundKey, suffixOperator, SIMPLE_SUFFIX_OPERATORS.get(suffixOperator),
                    jsonQuery.get(originalKey));
            continue;
        }/*from w ww .j  av  a 2s . c  o m*/

        if (originalKey.endsWith("Contains")) {
            addRestriction(mongoQuery, compoundKey, "Contains", "$regex",
                    ".*" + jsonQuery.get(originalKey) + ".*");
        } else {
            mongoQuery.put(compoundKey, new BasicDBObject("$eq", jsonQuery.get(originalKey)));
        }
    }
    return mongoQuery;
}