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.avanza.ymer.FakeDocumentCollection.java

License:Apache License

@Override
public void insert(DBObject dbObject) {
    for (DBObject object : collection) {
        if (object.get("_id").equals(dbObject.get("_id"))) {
            throw new DuplicateDocumentKeyException("_id: " + dbObject.get("_id"));
        }/*from   w  w w . j a v  a 2s . c  o m*/
    }
    if (dbObject.get("_id") == null) {
        dbObject.put("_id", "testid_" + idGenerator.incrementAndGet());
    }
    collection.add(dbObject);
}

From source file:com.avanza.ymer.FakeDocumentCollection.java

License:Apache License

private void removeById(BasicDBObject dbObject) {
    Iterator<DBObject> it = collection.iterator();
    while (it.hasNext()) {
        DBObject next = it.next();
        if (next.get("_id").equals(dbObject.get("_id"))) {
            it.remove();//from ww w  .  java 2 s  . com
            return;
        }
    }
}

From source file:com.avanza.ymer.FakeDocumentCollection.java

License:Apache License

@Override
public DBObject findById(Object id) {
    for (DBObject next : collection) {
        if (next.get("_id").equals(id)) {
            return next;
        }/*from w  w w  .  j  av a2  s  .co m*/
    }
    return null;
}

From source file:com.avanza.ymer.MongoDocumentCollection.java

License:Apache License

@Override
public void replace(DBObject oldVersion, DBObject newVersion) {
    if (!oldVersion.get("_id").equals(newVersion.get("_id"))) {
        dbCollection.insert(newVersion);
        dbCollection.remove(new BasicDBObject("_id", oldVersion.get("_id")));
    } else {//  www.ja va2 s .com
        dbCollection.update(new BasicDBObject("_id", oldVersion.get("_id")), newVersion);
    }
}

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

/**
 *
 * @param <T>//from  w w w. j a  v a 2s.c om
 * @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>//w w  w  .jav a  2  s  .  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 w w.  j a  va2 s.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>//from  w  ww . j  a v a  2  s. co  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.avekshaa.cis.database.IncrementalMapReduce.java

License:Open Source License

private ObjectId getSavepoint() {
    BasicDBObject query = new BasicDBObject("key", this.key);
    DBObject savepoint = savePointCollection.findOne(query);
    if (savepoint != null) {
        return (ObjectId) savepoint.get("savepoint");
    } else {//from   w ww  . j  av  a 2 s  .  c  om
        return new ObjectId(new Date(0));
    }
}

From source file:com.avekshaa.cis.database.IncrementalMapReduce.java

License:Open Source License

private ObjectId getLastInserted() {
    BasicDBObject sort = new BasicDBObject("_id", -1);
    DBObject latest = coll.findOne(null, null, sort);
    if (latest != null) {
        return (ObjectId) latest.get("_id");
    } else {/*from w ww .j  a v  a 2s.  c om*/
        return new ObjectId(new Date(0));
    }
}