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:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public List<Favorite> getFavorites(String email) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }//  www .  j  a v a2  s .  c o  m
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
        throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES);
    if (favoritesDb == null) {
        favoritesDb = new BasicDBObject();

        userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb);
        col.save(userDb);
    }

    List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size());
    for (String stationId : favoritesDb.keySet()) {
        Favorite favorite = new Favorite();
        favorite.setStationId(stationId);
        DBObject favoriteItemsDb = (DBObject) favoritesDb.get(stationId);
        favorite.setLastMessageId(
                (Long) favoriteItemsDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID));
        returnValue.add(favorite);
    }
    return returnValue;
}

From source file:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public List<Favorite> addToFavorites(String email, List<Favorite> localFavorites) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }/*  w ww . jav a  2  s.c  om*/
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
        throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES);
    if (favoritesDb == null) {
        favoritesDb = new BasicDBObject();
    }

    if (localFavorites != null) {
        for (Favorite localFavorite : localFavorites) {
            DBObject favoriteItemsDb = (DBObject) favoritesDb.get(localFavorite.getStationId());
            long lastMessageIdDb = -1;
            if (favoriteItemsDb == null) {
                favoriteItemsDb = new BasicDBObject();
            } else {
                if (favoriteItemsDb.containsField(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID)) {
                    lastMessageIdDb = (Long) favoriteItemsDb
                            .get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID);
                }
            }

            favoriteItemsDb.put(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID,
                    Math.max(lastMessageIdDb, localFavorite.getLastMessageId()));
            favoritesDb.put(localFavorite.getStationId(), favoriteItemsDb);
        }
    }
    userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb);
    col.save(userDb);

    List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size());
    for (String stationId : favoritesDb.keySet()) {
        Favorite favorite = new Favorite();
        favorite.setStationId(stationId);
        DBObject favoriteItemDb = (DBObject) favoritesDb.get(stationId);
        favorite.setLastMessageId((Long) favoriteItemDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID));
        returnValue.add(favorite);
    }
    return returnValue;
}

From source file:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public List<Favorite> removeFromFavorites(String email, List<Favorite> favoritesToRemove) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }/*from ww  w .  j a  va 2  s. c  o  m*/
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
        throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES);
    if (favoritesDb == null) {
        favoritesDb = new BasicDBObject();
    }

    if (favoritesToRemove != null) {
        for (Favorite favoriteToRemove : favoritesToRemove) {
            DBObject favoriteItemsDb = (DBObject) favoritesDb.get(favoriteToRemove.getStationId());
            if (favoriteItemsDb != null) {
                favoritesDb.removeField(favoriteToRemove.getStationId());
            }
        }
    }
    userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb);
    col.save(userDb);

    List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size());
    for (String stationId : favoritesDb.keySet()) {
        Favorite favorite = new Favorite();
        favorite.setStationId(stationId);
        DBObject favoriteItemDb = (DBObject) favoritesDb.get(stationId);
        favorite.setLastMessageId((Long) favoriteItemDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID));
        returnValue.add(favorite);
    }
    return returnValue;
}

From source file:com.avbravo.ejbjmoordb.couchbase.internal.DocumentToJavaCouchbase.java

/**
 *
 * @param <T>//ww w .  ja  va  2  s .co  m
 * @param clazz
 * @param dbObject
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object fromDocumentRecursive(Object dbObject, FieldDescriptor fieldDescriptor) {
    try {
        //Test.msg("     ---------------------------------------------------");
        //Test.msg("            fromDocumentRecursive{} " + fieldDescriptor.getName());

        //  System.out.println("  fromDocumentRecursive: " + fieldDescriptor.getName());
        if (dbObject == null) {

            return fieldDescriptor.getDefaultValue();
        }

        Class<?> fieldType = fieldDescriptor.getField().getType();
        if (fieldDescriptor.isSimple()) {
            //Test.msg("                   [isSimple] " + fieldDescriptor.getSimpleValue(dbObject));
            return fieldDescriptor.getSimpleValue(dbObject);
        } else if (fieldDescriptor.isArray()) {
            //Test.msg("                   [ isArray]");
            BasicDBList dbList = (BasicDBList) dbObject;
            if (fieldType.getComponentType().isPrimitive()) {

                return ReflectionUtils.dbListToArrayOfPrimitives(dbList, fieldType);
            }
            List list = new ArrayList();
            for (Object listEl : dbList) {

                if (listEl == null || ReflectionUtils.isSimpleClass(listEl.getClass())) {

                    list.add(listEl);
                } else {

                    list.add(fromDocument((Class<Object>) fieldType.getComponentType(), (Document) listEl,
                            embeddedBeansList, referencedBeansList));
                }
            }

            Object[] arrayPrototype = (Object[]) Array.newInstance(fieldType.getComponentType(), 0);
            return list.toArray(arrayPrototype);
        } else if (fieldDescriptor.isList()) {
            //Test.msg("                 [isList()  ]" + fieldDescriptor.getName());
            if (isEmbedded(fieldDescriptor.getName())) {
                //Test.msg("                          -->[isEmbedded().a  ]" + fieldDescriptor.getName());

                List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;
                //Test.msg("                          -->[isEmbedded().b  ]" + fieldDescriptor.getName());
                List list = (List) fieldDescriptor.newInstance();
                //Test.msg("                          -->[isEmbedded().c  ]" + fieldDescriptor.getName());
                for (Object listEl : dbList) {
                    //Test.msg("                          -->[isEmbedded().d  ]" + fieldDescriptor.getName());
                    if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                        //Test.msg("                          -->[isEmbedded().e  ]" + fieldDescriptor.getName());
                        list.add(listEl);
                        //Test.msg("                          -->[isEmbedded().f  ]" + fieldDescriptor.getName());
                    } else {
                        //Test.msg("                          -->[isEmbedded().g  ]" + fieldDescriptor.getName());
                        list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                (Document) listEl, embeddedBeansList, referencedBeansList));
                        //Test.msg("                          -->[isEmbedded().h  ]" + fieldDescriptor.getName());
                    }
                }

                return list;
            } else {
                if (isReferenced(fieldDescriptor.getName())) {
                    //Referenciado
                    //Test.msg("                    [es Referenciado]");
                    if (referencedBeans.getLazy()) {
                        //Test.msg("                 [    Lazy == true no carga los relacionados ]");

                        List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;
                        List list = (List) fieldDescriptor.newInstance();
                        for (Object listEl : dbList) {
                            if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                                list.add(listEl);
                            } else {
                                list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                        (Document) listEl, embeddedBeansList, referencedBeansList));
                            }
                        }

                        return list;
                    } else {
                        //Test.msg("                 [    Lazy == false carga los relacionados ]");

                        List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;
                        List list = (List) fieldDescriptor.newInstance();

                        for (Object listEl : dbList) {

                            if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                                list.add(listEl);
                            } else {
                                Document doc = (Document) listEl;
                                Class[] paramString = new Class[2];
                                paramString[0] = String.class;
                                Class cls = Class.forName(referencedBeans.getRepository());
                                Object obj = lookUpClassInBeanManager(cls);
                                //                                    Object obj = cls.newInstance();
                                Method method;
                                String value = "";
                                if (referencedBeans.getJavatype().toLowerCase().equals("integer")) {
                                    //@Id de tipo Integer
                                    //Test.msg(" @Id de tipo Integer");
                                    Integer n = (Integer) doc.get(referencedBeans.getField());
                                    method = cls.getDeclaredMethod("findById", String.class, Integer.class);

                                    t1 = (T) method.invoke(obj, referencedBeans.getField(), n);

                                } else {
                                    //Test.msg(" @Id de tipo String");
                                    value = (String) doc.get(referencedBeans.getField());
                                    paramString[1] = String.class;
                                    method = cls.getDeclaredMethod("findById", paramString);

                                    String[] param = { referencedBeans.getField(), value };
                                    t1 = (T) method.invoke(obj, param);
                                }

                                list.add(t1);

                            }
                        }

                        return list;
                    }

                } else {
                    //Test.msg("                    No es[Embebido] ni  [Referenciado]");
                    List<BasicDBObject> foundDocument = (ArrayList<BasicDBObject>) dbObject;
                    List list = (List) fieldDescriptor.newInstance();

                    for (Object listEl : foundDocument) {
                        if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                            list.add(listEl);
                        } else {

                            list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                    (Document) listEl, embeddedBeansList, referencedBeansList));
                        }
                    }

                    return list;
                }
            }

        } else if (fieldDescriptor.isSet()) {
            //Test.msg("                  [isSet()  ]");
            BasicDBList dbList = (BasicDBList) dbObject;
            Set set = (Set) fieldDescriptor.newInstance();
            for (Object listEl : dbList) {

                if (ReflectionUtils.isSimpleClass(listEl.getClass())) {

                    set.add(listEl);
                } else {

                    set.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                            (Document) listEl, embeddedBeansList, referencedBeansList));
                }
            }
            return set;
        } else if (fieldDescriptor.isMap()) {
            //Test.msg("                 isMap()  ]");
            DBObject dbMap = (DBObject) dbObject;
            Map map = (Map) fieldDescriptor.newInstance();
            for (Object key : dbMap.keySet()) {

                Object mapEl = dbMap.get(key.toString());
                if (mapEl == null || ReflectionUtils.isSimpleClass(mapEl.getClass())) {

                    map.put(key, mapEl);
                } else {

                    map.put(key, fromDocument(ReflectionUtils.genericTypeOfMapValue(fieldDescriptor.getField()),
                            (Document) mapEl, embeddedBeansList, referencedBeansList));
                }
            }
            return map;
        } else if (fieldDescriptor.isObject()) {
            //Test.msg("                   [isObject] " + fieldDescriptor.getName() + " ]");
            if (isEmbedded(fieldDescriptor.getName())) {
                //Test.msg("                  [es Embebido]");
                Object object = fieldDescriptor.newInstance();
                for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                    try {
                        childDescriptor.getField().set(object, fromDocumentRecursive(
                                ((Document) dbObject).get(childDescriptor.getName()), childDescriptor));
                    } catch (Exception e) {
                        throw new JmoordbException("Failed to set field value " + childDescriptor.getName(), e);
                    }
                }
                return object;
            } else {
                if (isReferenced(fieldDescriptor.getName())) {
                    //Referenciado
                    //Test.msg("                     [es Referenciado] ");
                    if (referencedBeans.getLazy()) {
                        //Test.msg("               [    {Lazy == true} No carga los relacionados ]");
                        Object object = fieldDescriptor.newInstance();
                        for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                            try {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {

                                    childDescriptor.getField().set(object,
                                            fromDocumentRecursive(
                                                    ((Document) dbObject).get(childDescriptor.getName()),
                                                    childDescriptor));
                                }
                            } catch (Exception e) {
                                throw new JmoordbException(
                                        "Failed to set field value " + childDescriptor.getName(), e);
                            }
                        }
                        return object;
                        //                       
                    } else {
                        //Test.msg("                 [   Lazy == false carga los relacionados ]");
                        //cargar todos los relacionads
                        Object object = fieldDescriptor.newInstance();
                        Class cls = Class.forName(referencedBeans.getRepository());
                        Object obj = lookUpClassInBeanManager(cls);
                        //                            Object obj = cls.newInstance();
                        Method method;

                        //             
                        if (referencedBeans.getJavatype().toLowerCase().equals("integer")) {
                            //Test.msg("         Integer");
                            //@Id de tipo Integer
                            Class[] paramString = new Class[2];
                            method = cls.getDeclaredMethod("findById", String.class, Integer.class);

                            Integer value = 0;
                            for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {
                                    Object x = ((Document) dbObject).get(childDescriptor.getName());
                                    value = (Integer) childDescriptor.getSimpleValue(x);
                                }
                            }

                            t1 = (T) method.invoke(obj, referencedBeans.getField(), value);

                        } else {
                            //Test.msg ( "       String");
                            Class[] paramString = new Class[2];
                            paramString[0] = String.class;
                            paramString[1] = String.class;
                            //Test.msg ( "       invocare method findById");
                            method = cls.getDeclaredMethod("findById", paramString);

                            String value = "";
                            for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {
                                    Object x = ((Document) dbObject).get(childDescriptor.getName());
                                    value = (String) childDescriptor.getSimpleValue(x);
                                    //Test.msg("    value= "+value);
                                }
                            }
                            String[] param = { referencedBeans.getField(), value };
                            //Test.msg("    param= "+referencedBeans.getField());
                            t1 = (T) method.invoke(obj, param);
                            //Test.msg("   paso  el  invoke t1");

                        }
                        //Test.msg( " voy a imprimir t1");
                        //Test.msg(" t1= "+t1.toString());
                        return t1;

                    }
                } else {
                    //Test.msg("                   [No es Referenced]");
                    new JmoordbException(
                            "@Embedded or @Reference is required for this field " + fieldDescriptor.getName());
                    return new Document();
                }
            }

        }
    } catch (Exception e) {
        throw new JmoordbException("Failed to set field value " + fieldDescriptor.getName(), e);
    }
    return null;
}

From source file:com.avbravo.ejbjmoordb.mongodb.internal.DocumentToJavaJmoordbResult.java

/**
 *
 * @param <T>//from   w  ww.ja v a  2s . c o m
 * @param clazz
 * @param dbObject
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object fromDocumentRecursive(Object dbObject, FieldDescriptor fieldDescriptor) {
    try {
        //Test.msg("==================================================");
        //Test.msg("  fromDocumentRecursive: " + fieldDescriptor.getName());

        if (dbObject == null) {

            return fieldDescriptor.getDefaultValue();
        }

        Class<?> fieldType = fieldDescriptor.getField().getType();
        if (fieldDescriptor.isSimple()) {
            //Test.msg("   [isSimple] " + fieldDescriptor.getSimpleValue(dbObject));
            return fieldDescriptor.getSimpleValue(dbObject);
        } else if (fieldDescriptor.isArray()) {
            //Test.msg("   [ isArray]");
            BasicDBList dbList = (BasicDBList) dbObject;
            if (fieldType.getComponentType().isPrimitive()) {

                return ReflectionUtils.dbListToArrayOfPrimitives(dbList, fieldType);
            }
            List list = new ArrayList();
            for (Object listEl : dbList) {

                if (listEl == null || ReflectionUtils.isSimpleClass(listEl.getClass())) {

                    list.add(listEl);
                } else {

                    list.add(fromDocument((Class<Object>) fieldType.getComponentType(), (Document) listEl,
                            embeddedBeansList, referencedBeansList));
                }
            }

            Object[] arrayPrototype = (Object[]) Array.newInstance(fieldType.getComponentType(), 0);
            return list.toArray(arrayPrototype);
        } else if (fieldDescriptor.isList()) {
            //Test.msg(" [isList()  ]" + fieldDescriptor.getName());
            if (isEmbedded(fieldDescriptor.getName())) {
                //     //Test.msg("     [es Embebido]");

                List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;

                List list = (List) fieldDescriptor.newInstance();

                for (Object listEl : dbList) {

                    if (ReflectionUtils.isSimpleClass(listEl.getClass())) {

                        list.add(listEl);
                    } else {

                        list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                (Document) listEl, embeddedBeansList, referencedBeansList));
                    }
                }

                return list;
            } else {
                if (isReferenced(fieldDescriptor.getName())) {
                    //Referenciado
                    //Test.msg("     [es Referenciado]");
                    if (referencedBeans.getLazy()) {
                        //Test.msg("[    Lazy == true no carga los relacionados ]");

                        List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;
                        List list = (List) fieldDescriptor.newInstance();
                        for (Object listEl : dbList) {
                            if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                                list.add(listEl);
                            } else {
                                list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                        (Document) listEl, embeddedBeansList, referencedBeansList));
                            }
                        }

                        return list;
                    } else {
                        //Test.msg("[    Lazy == false carga los relacionados ]");

                        List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;
                        List list = (List) fieldDescriptor.newInstance();

                        for (Object listEl : dbList) {

                            if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                                list.add(listEl);
                            } else {
                                Document doc = (Document) listEl;
                                Class[] paramString = new Class[2];
                                paramString[0] = String.class;
                                Class cls = Class.forName(referencedBeans.getRepository());
                                //                                    Object obj = cls.newInstance();
                                Object obj = lookUpClassInBeanManager(cls);
                                Method method;
                                String value = "";
                                if (referencedBeans.getJavatype().toLowerCase().equals("integer")) {
                                    //@Id de tipo Integer
                                    Integer n = (Integer) doc.get(referencedBeans.getField());
                                    method = cls.getDeclaredMethod("findById", String.class, Integer.class);
                                    ////Test.msg(" voy a optional Integer");

                                    t1 = (T) method.invoke(obj, referencedBeans.getField(), n);

                                } else {
                                    //Test.msg(" voy a optional String");
                                    value = (String) doc.get(referencedBeans.getField());
                                    paramString[1] = String.class;
                                    method = cls.getDeclaredMethod("findById", paramString);

                                    String[] param = { referencedBeans.getField(), value };

                                    t1 = (T) method.invoke(obj, param);
                                }

                                list.add(t1);

                            }
                        }

                        return list;
                    }

                } else {
                    //Test.msg("    No es[Embebido] ni  [Referenciado]");
                    List<BasicDBObject> foundDocument = (ArrayList<BasicDBObject>) dbObject;
                    List list = (List) fieldDescriptor.newInstance();

                    for (Object listEl : foundDocument) {
                        if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                            list.add(listEl);
                        } else {

                            list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                    (Document) listEl, embeddedBeansList, referencedBeansList));
                        }
                    }

                    return list;
                }
            }

        } else if (fieldDescriptor.isSet()) {
            //Test.msg(" [isSet()  ]");
            BasicDBList dbList = (BasicDBList) dbObject;
            Set set = (Set) fieldDescriptor.newInstance();
            for (Object listEl : dbList) {

                if (ReflectionUtils.isSimpleClass(listEl.getClass())) {

                    set.add(listEl);
                } else {

                    set.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                            (Document) listEl, embeddedBeansList, referencedBeansList));
                }
            }
            return set;
        } else if (fieldDescriptor.isMap()) {
            //Test.msg(" isMap()  ]");
            DBObject dbMap = (DBObject) dbObject;
            Map map = (Map) fieldDescriptor.newInstance();
            for (Object key : dbMap.keySet()) {

                Object mapEl = dbMap.get(key.toString());
                if (mapEl == null || ReflectionUtils.isSimpleClass(mapEl.getClass())) {

                    map.put(key, mapEl);
                } else {

                    map.put(key, fromDocument(ReflectionUtils.genericTypeOfMapValue(fieldDescriptor.getField()),
                            (Document) mapEl, embeddedBeansList, referencedBeansList));
                }
            }
            return map;
        } else if (fieldDescriptor.isObject()) {
            //Test.msg("   [isObject] " + fieldDescriptor.getName() + " ]");
            if (isEmbedded(fieldDescriptor.getName())) {
                //Test.msg("  [es Embebido]");
                Object object = fieldDescriptor.newInstance();
                for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                    try {
                        childDescriptor.getField().set(object, fromDocumentRecursive(
                                ((Document) dbObject).get(childDescriptor.getName()), childDescriptor));
                    } catch (Exception e) {
                        throw new JmoordbException(
                                "[isObject]Failed to set field value " + childDescriptor.getName(), e);
                    }
                }
                return object;
            } else {
                if (isReferenced(fieldDescriptor.getName())) {
                    //Referenciado
                    //Test.msg("         [es Referenciado] ");
                    if (referencedBeans.getLazy()) {
                        //Test.msg("[    {Lazy == true} No carga los relacionados ]");
                        Object object = fieldDescriptor.newInstance();
                        for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                            try {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {

                                    childDescriptor.getField().set(object,
                                            fromDocumentRecursive(
                                                    ((Document) dbObject).get(childDescriptor.getName()),
                                                    childDescriptor));
                                }
                            } catch (Exception e) {
                                throw new JmoordbException(
                                        "Failed to set field value " + childDescriptor.getName(), e);
                            }
                        }
                        return object;
                        //                       
                    } else {
                        //Test.msg("                 [   Lazy == false carga los relacionados ]");
                        Class cls = Class.forName(referencedBeans.getRepository());

                        Object obj = lookUpClassInBeanManager(cls);
                        Method method;

                        //             
                        if (referencedBeans.getJavatype().toLowerCase().equals("integer")) {
                            //@Id de tipo Integer
                            Class[] paramString = new Class[2];
                            method = cls.getDeclaredMethod("findById", String.class, Integer.class);

                            Integer value = 0;
                            for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {
                                    Object x = ((Document) dbObject).get(childDescriptor.getName());
                                    value = (Integer) childDescriptor.getSimpleValue(x);
                                }
                            }

                            t1 = (T) method.invoke(obj, referencedBeans.getField(), value);

                        } else {
                            Class[] paramString = new Class[2];
                            paramString[0] = String.class;
                            paramString[1] = String.class;
                            method = cls.getDeclaredMethod("findById", paramString);

                            String value = "";
                            for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {
                                    Object x = ((Document) dbObject).get(childDescriptor.getName());
                                    value = (String) childDescriptor.getSimpleValue(x);
                                }
                            }
                            String[] param = { referencedBeans.getField(), value };
                            t1 = (T) method.invoke(obj, param);
                        }

                        return t1;

                    }
                } else {
                    //Test.msg("                   [No es Referenced]");
                    new JmoordbException(
                            "@Embedded or @Reference is required for this field " + fieldDescriptor.getName());
                    return new Document();
                }
            }

        }
    } catch (Exception e) {
        throw new JmoordbException("fromDocumentRecursive() " + fieldDescriptor.getName(), e);
    }
    return null;
}

From source file:com.avbravo.ejbjmoordb.mongodb.internal.DocumentToJavaMongoDB.java

/**
 *
 * @param <T>/*w ww.j  a v a 2s  .c o  m*/
 * @param clazz
 * @param dbObject
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object fromDocumentRecursive(Object dbObject, FieldDescriptor fieldDescriptor) {
    try {
        //Test.msg("==================================================");
        //Test.msg("  fromDocumentRecursive: " + fieldDescriptor.getName());

        if (dbObject == null) {

            return fieldDescriptor.getDefaultValue();
        }

        Class<?> fieldType = fieldDescriptor.getField().getType();
        if (fieldDescriptor.isSimple()) {
            //Test.msg("   [isSimple] " + fieldDescriptor.getSimpleValue(dbObject));
            return fieldDescriptor.getSimpleValue(dbObject);
        } else if (fieldDescriptor.isArray()) {
            //Test.msg("   [ isArray]");
            BasicDBList dbList = (BasicDBList) dbObject;
            if (fieldType.getComponentType().isPrimitive()) {

                return ReflectionUtils.dbListToArrayOfPrimitives(dbList, fieldType);
            }
            List list = new ArrayList();
            for (Object listEl : dbList) {

                if (listEl == null || ReflectionUtils.isSimpleClass(listEl.getClass())) {

                    list.add(listEl);
                } else {

                    list.add(fromDocument((Class<Object>) fieldType.getComponentType(), (Document) listEl,
                            embeddedBeansList, referencedBeansList));
                }
            }

            Object[] arrayPrototype = (Object[]) Array.newInstance(fieldType.getComponentType(), 0);
            return list.toArray(arrayPrototype);
        } else if (fieldDescriptor.isList()) {
            //Test.msg(" [isList()  ]" + fieldDescriptor.getName());
            if (isEmbedded(fieldDescriptor.getName())) {
                //     //Test.msg("     [es Embebido]");

                List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;

                List list = (List) fieldDescriptor.newInstance();

                for (Object listEl : dbList) {

                    if (ReflectionUtils.isSimpleClass(listEl.getClass())) {

                        list.add(listEl);
                    } else {

                        list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                (Document) listEl, embeddedBeansList, referencedBeansList));
                    }
                }

                return list;
            } else {
                if (isReferenced(fieldDescriptor.getName())) {
                    //Referenciado
                    //Test.msg("     [es Referenciado]");
                    if (referencedBeans.getLazy()) {
                        //Test.msg("[    Lazy == true no carga los relacionados ]");

                        List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;
                        List list = (List) fieldDescriptor.newInstance();
                        for (Object listEl : dbList) {
                            if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                                list.add(listEl);
                            } else {
                                list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                        (Document) listEl, embeddedBeansList, referencedBeansList));
                            }
                        }

                        return list;
                    } else {
                        //Test.msg("[    Lazy == false carga los relacionados ]");
                        //
                        List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;
                        List list = (List) fieldDescriptor.newInstance();

                        for (Object listEl : dbList) {

                            if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                                list.add(listEl);
                            } else {
                                Document doc = (Document) listEl;
                                Class[] paramString = new Class[2];
                                paramString[0] = String.class;
                                Class cls = Class.forName(referencedBeans.getRepository());
                                //                                    Object obj = cls.newInstance();
                                Object obj = lookUpClassInBeanManager(cls);
                                Method method;
                                String value = "";
                                if (referencedBeans.getJavatype().toLowerCase().equals("integer")) {
                                    //@Id de tipo Integer
                                    Integer n = (Integer) doc.get(referencedBeans.getField());
                                    //  method = cls.getDeclaredMethod("findById", String.class, Integer.class);
                                    //Invocar el metodod de la superclase
                                    Class parent = cls.getSuperclass();
                                    method = parent.getDeclaredMethod("search", String.class, Integer.class);

                                    ////Test.msg(" voy a optional Integer");

                                    t1 = (T) method.invoke(obj, referencedBeans.getField(), n);

                                } else {
                                    //Test.msg(" voy a optional String");
                                    value = (String) doc.get(referencedBeans.getField());
                                    paramString[1] = String.class;
                                    Class parent = cls.getSuperclass();
                                    method = parent.getDeclaredMethod("search", paramString);
                                    //                                        method = cls.getMethod("search", paramString);
                                    //method = cls.getMethod("search", paramString);

                                    String[] param = { referencedBeans.getField(), value };

                                    t1 = (T) method.invoke(obj, param);
                                }

                                list.add(t1);

                            }
                        }

                        return list;
                    }

                } else {
                    //Test.msg("    No es[Embebido] ni  [Referenciado]");
                    List<BasicDBObject> foundDocument = (ArrayList<BasicDBObject>) dbObject;
                    List list = (List) fieldDescriptor.newInstance();

                    for (Object listEl : foundDocument) {
                        if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                            list.add(listEl);
                        } else {

                            list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                    (Document) listEl, embeddedBeansList, referencedBeansList));
                        }
                    }

                    return list;
                }
            }

        } else if (fieldDescriptor.isSet()) {
            //Test.msg(" [isSet()  ]");
            BasicDBList dbList = (BasicDBList) dbObject;
            Set set = (Set) fieldDescriptor.newInstance();
            for (Object listEl : dbList) {

                if (ReflectionUtils.isSimpleClass(listEl.getClass())) {

                    set.add(listEl);
                } else {

                    set.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                            (Document) listEl, embeddedBeansList, referencedBeansList));
                }
            }
            return set;
        } else if (fieldDescriptor.isMap()) {
            //Test.msg(" isMap()  ]");
            DBObject dbMap = (DBObject) dbObject;
            Map map = (Map) fieldDescriptor.newInstance();
            for (Object key : dbMap.keySet()) {

                Object mapEl = dbMap.get(key.toString());
                if (mapEl == null || ReflectionUtils.isSimpleClass(mapEl.getClass())) {

                    map.put(key, mapEl);
                } else {

                    map.put(key, fromDocument(ReflectionUtils.genericTypeOfMapValue(fieldDescriptor.getField()),
                            (Document) mapEl, embeddedBeansList, referencedBeansList));
                }
            }
            return map;
        } else if (fieldDescriptor.isObject()) {
            //Test.msg("   [isObject] " + fieldDescriptor.getName() + " ]");
            if (isEmbedded(fieldDescriptor.getName())) {
                //Test.msg("  [es Embebido]");
                Object object = fieldDescriptor.newInstance();
                for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                    try {
                        childDescriptor.getField().set(object, fromDocumentRecursive(
                                ((Document) dbObject).get(childDescriptor.getName()), childDescriptor));
                    } catch (Exception e) {
                        throw new JmoordbException(
                                "[isObject]Failed to set field value " + childDescriptor.getName(), e);
                    }
                }
                return object;
            } else {
                if (isReferenced(fieldDescriptor.getName())) {
                    //Referenciado
                    //Test.msg("         [es Referenciado] ");
                    if (referencedBeans.getLazy()) {
                        //Test.msg("[    {Lazy == true} No carga los relacionados ]");
                        Object object = fieldDescriptor.newInstance();
                        for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                            try {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {

                                    childDescriptor.getField().set(object,
                                            fromDocumentRecursive(
                                                    ((Document) dbObject).get(childDescriptor.getName()),
                                                    childDescriptor));
                                }
                            } catch (Exception e) {
                                throw new JmoordbException(
                                        "Failed to set field value " + childDescriptor.getName(), e);
                            }
                        }
                        return object;
                        //                       
                    } else {
                        //Test.msg("                 [   Lazy == false carga los relacionados ]");
                        Class cls = Class.forName(referencedBeans.getRepository());

                        Object obj = lookUpClassInBeanManager(cls);
                        Method method;

                        //             
                        if (referencedBeans.getJavatype().toLowerCase().equals("integer")) {
                            //@Id de tipo Integer
                            Class[] paramString = new Class[2];
                            Class parent = cls.getSuperclass();
                            //  method = cls.getDeclaredMethod("findById", String.class, Integer.class);
                            method = parent.getDeclaredMethod("search", String.class, Integer.class);

                            Integer value = 0;
                            for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {
                                    Object x = ((Document) dbObject).get(childDescriptor.getName());
                                    value = (Integer) childDescriptor.getSimpleValue(x);
                                }
                            }

                            t1 = (T) method.invoke(obj, referencedBeans.getField(), value);

                        } else {
                            Class[] paramString = new Class[2];
                            paramString[0] = String.class;
                            paramString[1] = String.class;
                            Class parent = cls.getSuperclass();
                            //method = cls.getDeclaredMethod("findById", paramString);
                            method = parent.getDeclaredMethod("search", paramString);

                            String value = "";
                            for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {
                                    Object x = ((Document) dbObject).get(childDescriptor.getName());
                                    value = (String) childDescriptor.getSimpleValue(x);
                                }
                            }
                            String[] param = { referencedBeans.getField(), value };
                            t1 = (T) method.invoke(obj, param);
                        }

                        return t1;

                    }
                } else {
                    //Test.msg("                   [No es Referenced]");
                    new JmoordbException(
                            "@Embedded or @Reference is required for this field " + fieldDescriptor.getName());
                    return new Document();
                }
            }

        }
    } catch (Exception e) {
        throw new JmoordbException("fromDocumentRecursive() " + fieldDescriptor.getName(), e);
    }
    return null;
}

From source file:com.avbravo.jmoordbdianna.mongodb.internal.DocumentToJavaMongoDB.java

/**
 *
 * @param <T>// w w  w. j av a  2  s  . c o  m
 * @param clazz
 * @param dbObject
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object fromDocumentRecursive(Object dbObject, FieldDescriptor fieldDescriptor) {
    try {
        //Test.msg("  fromDocumentRecursive: " + fieldDescriptor.getName());
        if (dbObject == null) {

            return fieldDescriptor.getDefaultValue();
        }

        Class<?> fieldType = fieldDescriptor.getField().getType();
        if (fieldDescriptor.isSimple()) {
            //Test.msg("   [isSimple] " + fieldDescriptor.getSimpleValue(dbObject));
            return fieldDescriptor.getSimpleValue(dbObject);
        } else if (fieldDescriptor.isArray()) {
            //   //Test.msg("   [ isArray]");
            BasicDBList dbList = (BasicDBList) dbObject;
            if (fieldType.getComponentType().isPrimitive()) {

                return ReflectionUtils.dbListToArrayOfPrimitives(dbList, fieldType);
            }
            List list = new ArrayList();
            for (Object listEl : dbList) {

                if (listEl == null || ReflectionUtils.isSimpleClass(listEl.getClass())) {

                    list.add(listEl);
                } else {

                    list.add(fromDocument((Class<Object>) fieldType.getComponentType(), (Document) listEl,
                            embeddedBeansList, referencedBeansList));
                }
            }

            Object[] arrayPrototype = (Object[]) Array.newInstance(fieldType.getComponentType(), 0);
            return list.toArray(arrayPrototype);
        } else if (fieldDescriptor.isList()) {
            //Test.msg(" [isList()  ]" + fieldDescriptor.getName());
            if (isEmbedded(fieldDescriptor.getName())) {
                //      //Test.msg("     [es Embebido]");

                List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;

                List list = (List) fieldDescriptor.newInstance();

                for (Object listEl : dbList) {

                    if (ReflectionUtils.isSimpleClass(listEl.getClass())) {

                        list.add(listEl);
                    } else {

                        list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                (Document) listEl, embeddedBeansList, referencedBeansList));
                    }
                }

                return list;
            } else {
                if (isReferenced(fieldDescriptor.getName())) {
                    //Referenciado
                    //   //Test.msg("     [es Referenciado]");
                    if (referencedBeans.getLazy()) {
                        //    //Test.msg("[    Lazy == true no carga los relacionados ]");

                        List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;
                        List list = (List) fieldDescriptor.newInstance();
                        for (Object listEl : dbList) {
                            if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                                list.add(listEl);
                            } else {
                                list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                        (Document) listEl, embeddedBeansList, referencedBeansList));
                            }
                        }

                        return list;
                    } else {
                        //Test.msg("[    Lazy == false carga los relacionados ]");

                        List<BasicDBObject> dbList = (ArrayList<BasicDBObject>) dbObject;
                        List list = (List) fieldDescriptor.newInstance();

                        for (Object listEl : dbList) {

                            if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                                list.add(listEl);
                            } else {
                                Document doc = (Document) listEl;
                                Class[] paramString = new Class[2];
                                paramString[0] = String.class;
                                Class cls = Class.forName(referencedBeans.getFacade());
                                Object obj = cls.newInstance();
                                Method method;
                                String value = "";
                                if (referencedBeans.getJavatype().toLowerCase().equals("integer")) {
                                    //@Id de tipo Integer
                                    Integer n = (Integer) doc.get(referencedBeans.getField());
                                    method = cls.getDeclaredMethod("findById", String.class, Integer.class);
                                    //Test.msg(" voy a optional Integer");

                                    t1 = (T) method.invoke(obj, referencedBeans.getField(), n);

                                } else {
                                    //Test.msg(" voy a optional String");
                                    value = (String) doc.get(referencedBeans.getField());
                                    paramString[1] = String.class;
                                    method = cls.getDeclaredMethod("findById", paramString);

                                    String[] param = { referencedBeans.getField(), value };

                                    t1 = (T) method.invoke(obj, param);
                                }

                                list.add(t1);

                            }
                        }

                        return list;
                    }

                } else {
                    //Test.msg("    No es[Embebido] ni  [Referenciado]");
                    List<BasicDBObject> foundDocument = (ArrayList<BasicDBObject>) dbObject;
                    List list = (List) fieldDescriptor.newInstance();

                    for (Object listEl : foundDocument) {
                        if (ReflectionUtils.isSimpleClass(listEl.getClass())) {
                            list.add(listEl);
                        } else {

                            list.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                                    (Document) listEl, embeddedBeansList, referencedBeansList));
                        }
                    }

                    return list;
                }
            }

        } else if (fieldDescriptor.isSet()) {
            //Test.msg(" [isSet()  ]");
            BasicDBList dbList = (BasicDBList) dbObject;
            Set set = (Set) fieldDescriptor.newInstance();
            for (Object listEl : dbList) {

                if (ReflectionUtils.isSimpleClass(listEl.getClass())) {

                    set.add(listEl);
                } else {

                    set.add(fromDocument(ReflectionUtils.genericType(fieldDescriptor.getField()),
                            (Document) listEl, embeddedBeansList, referencedBeansList));
                }
            }
            return set;
        } else if (fieldDescriptor.isMap()) {
            //Test.msg(" isMap()  ]");
            DBObject dbMap = (DBObject) dbObject;
            Map map = (Map) fieldDescriptor.newInstance();
            for (Object key : dbMap.keySet()) {

                Object mapEl = dbMap.get(key.toString());
                if (mapEl == null || ReflectionUtils.isSimpleClass(mapEl.getClass())) {

                    map.put(key, mapEl);
                } else {

                    map.put(key, fromDocument(ReflectionUtils.genericTypeOfMapValue(fieldDescriptor.getField()),
                            (Document) mapEl, embeddedBeansList, referencedBeansList));
                }
            }
            return map;
        } else if (fieldDescriptor.isObject()) {
            //   //Test.msg("   [isObject] " + fieldDescriptor.getName() + " ]");
            if (isEmbedded(fieldDescriptor.getName())) {
                //     //Test.msg("  [es Embebido]");
                Object object = fieldDescriptor.newInstance();
                for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                    try {
                        childDescriptor.getField().set(object, fromDocumentRecursive(
                                ((Document) dbObject).get(childDescriptor.getName()), childDescriptor));
                    } catch (Exception e) {
                        throw new JmoordbException("Failed to set field value " + childDescriptor.getName(), e);
                    }
                }
                return object;
            } else {
                if (isReferenced(fieldDescriptor.getName())) {
                    //Referenciado
                    //Test.msg("         [es Referenciado] ");
                    if (referencedBeans.getLazy()) {
                        //    //Test.msg("[    {Lazy == true} No carga los relacionados ]");
                        Object object = fieldDescriptor.newInstance();
                        for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                            try {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {

                                    childDescriptor.getField().set(object,
                                            fromDocumentRecursive(
                                                    ((Document) dbObject).get(childDescriptor.getName()),
                                                    childDescriptor));
                                }
                            } catch (Exception e) {
                                throw new JmoordbException(
                                        "Failed to set field value " + childDescriptor.getName(), e);
                            }
                        }
                        return object;
                        //                       
                    } else {
                        //   //Test.msg("[   Lazy == false carga los relacionados ]");
                        //cargar todos los relacionads
                        Object object = fieldDescriptor.newInstance();
                        Class cls = Class.forName(referencedBeans.getFacade());
                        Object obj = cls.newInstance();
                        Method method;

                        //             
                        if (referencedBeans.getJavatype().toLowerCase().equals("integer")) {
                            //@Id de tipo Integer
                            Class[] paramString = new Class[2];
                            method = cls.getDeclaredMethod("findById", String.class, Integer.class);

                            Integer value = 0;
                            for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {
                                    Object x = ((Document) dbObject).get(childDescriptor.getName());
                                    value = (Integer) childDescriptor.getSimpleValue(x);
                                }
                            }

                            t1 = (T) method.invoke(obj, referencedBeans.getField(), value);

                        } else {
                            Class[] paramString = new Class[2];
                            paramString[0] = String.class;
                            paramString[1] = String.class;
                            method = cls.getDeclaredMethod("findById", paramString);

                            String value = "";
                            for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) {
                                if (childDescriptor.getField().getName().equals(referencedBeans.getField())) {
                                    Object x = ((Document) dbObject).get(childDescriptor.getName());
                                    value = (String) childDescriptor.getSimpleValue(x);
                                }
                            }
                            String[] param = { referencedBeans.getField(), value };
                            t1 = (T) method.invoke(obj, param);
                        }

                        return t1;

                    }
                } else {
                    //Test.msg("                   [No es Referenced]");
                    new JmoordbException(
                            "@Embedded or @Reference is required for this field " + fieldDescriptor.getName());
                    return new Document();
                }
            }

        }
    } catch (Exception e) {
        throw new JmoordbException("Failed to set field value " + fieldDescriptor.getName(), e);
    }
    return null;
}

From source file:com.caci.dummyserver.InstanceEndpointDefinition.java

private Boolean doIdsMatch(List<Pair<String, String>> keys, String json, String table) {
    DBObject obj = json != null && !json.equals("") ? (DBObject) JSON.parse(json) : null;

    if (obj == null) {
        return false;
    }// w ww . ja  v a2 s .co  m

    Collection<String> jsonFields = obj.keySet();
    for (Pair<String, String> key : keys) {
        String keyName = key.getKey();
        String jsonField = keyName.equals(":" + table) ? "id" : keyName.substring(1);

        if (!jsonFields.contains(jsonField) || !key.getValue().equals(obj.get(jsonField).toString())) {
            return false;
        }
        break;
    }

    return true;
}

From source file:com.caci.dummyserver.MongoRepository.java

private DBObject pruneDBObject(DBObject obj, Collection<String> fields) {
    if (fields != null && fields.size() > 0) {
        ArrayList<String> fieldList = new ArrayList<String>(obj.keySet());
        for (String field : fieldList) {
            if (!fields.contains(field)) {
                obj.removeField(field);//from  w  w  w. j  a  v a2 s. c om
            }
        }
    }
    return obj;
}

From source file:com.ccoe.build.alerts.Compare.java

License:Apache License

public void judgeSingleRule(Rule rule, Condition current, Condition previous) {
    String collection = rule.getCollection();
    DBCollection dbc = Connector.connectCollection(db, collection);

    if (!StringUtils.isEmpty(rule.getCollectionPresentName())) {
        collection = rule.getCollectionPresentName();
    }//from   ww  w.j a v a 2s  .  co  m

    DBObject totaldbo = Connector.getLastRecord(dbc, current.getStartDate(), current.getEndDate());

    String field = rule.getField();
    String operatorStr = rule.getOperator();

    Date oneWeekBack = DateUtils.getUTCOneWeekBack(current.getEndDate());
    double movingAverage = Connector.getMovingAverage(dbc, field, oneWeekBack, current.getEndDate());
    String flag = "";
    if (Connector.DAYS < 7) {
        //flag = "~";
    }

    if ("lt".equals(operatorStr)) {
        operatorStr = "<";
    } else if ("le".equals(operatorStr)) {
        operatorStr = "<=";
    } else if ("eq".equals(operatorStr)) {
        operatorStr = "=";
    } else if ("gt".equals(operatorStr)) {
        operatorStr = ">";
    } else if ("ge".equals(operatorStr)) {
        operatorStr = ">=";
    }

    String goalStr = rule.getGoal();
    String lowerLimit = rule.getLower();
    String upperLimit = rule.getUpper();
    // only support type double
    double lower = Double.parseDouble(lowerLimit);
    double upper = Double.parseDouble(upperLimit);
    DecimalFormat df = new DecimalFormat("##0.00");
    SingleResult singleResult = null;

    if (totaldbo == null) {
        singleResult = new SingleResult(collection, field, "N/A", lightRed, operatorStr, goalStr, lowerLimit,
                upperLimit, "N/A", flag);

        if (!StringUtils.isEmpty(rule.getFieldPresentName())) {
            singleResult.setField(rule.getFieldPresentName());
        }
        if (!StringUtils.isEmpty(rule.getLink())) {
            singleResult.setLink(rule.getLink());
        }
        alertResult.getResultlist().add(singleResult);
        return;
    }

    DBObject dbo = (DBObject) totaldbo.get("Data");
    Set<String> keyset = dbo.keySet();

    for (String keyname : keyset) {
        if (field.equals(keyname)) {
            Object keyvalue = dbo.get(keyname);
            double keyvaluenum = 0;
            if (keyvalue instanceof Integer) {
                int k1 = (Integer) keyvalue;
                keyvaluenum = Double.parseDouble("" + k1);
            } else if (keyvalue instanceof Double) {
                keyvaluenum = (Double) keyvalue;
            }

            String color = normalColor;
            if (keyvaluenum >= lower && keyvaluenum <= upper) {
                color = normalColor;
            } else if ((keyvaluenum < lower && "<=".equals(operatorStr))
                    || (keyvaluenum > upper && ">=".equals(operatorStr))) {
                color = lightGreen;
            } else if ((keyvaluenum < lower && ">=".equals(operatorStr))
                    || (keyvaluenum > upper && "<=".equals(operatorStr))) {
                color = lightRed;
            }
            singleResult = new SingleResult(collection, field, df.format(keyvaluenum), color, operatorStr,
                    goalStr, lowerLimit, upperLimit, df.format(movingAverage), flag);
        }
    }
    if (singleResult == null) {
        singleResult = new SingleResult(collection, field, "N/A", lightRed, operatorStr, goalStr, lowerLimit,
                upperLimit, "N/A", flag);
    }

    if (!StringUtils.isEmpty(rule.getFieldPresentName())) {
        singleResult.setField(rule.getFieldPresentName());
    }

    if (!StringUtils.isEmpty(rule.getLink())) {
        singleResult.setLink(rule.getLink());
    }
    alertResult.getResultlist().add(singleResult);
    return;
}