Search in sources :

Example 81 with DBObject

use of com.mongodb.DBObject in project morphia by mongodb.

the class TestDAO method testDAO.

@Test
public void testDAO() throws Exception {
    getMorphia().map(Hotel.class);
    final Hotel borg = new Hotel();
    borg.setName("Hotel Borg");
    borg.setStars(4);
    borg.setTakesCreditCards(true);
    borg.setStartDate(new Date());
    borg.setType(Hotel.Type.LEISURE);
    final Address address = new Address();
    address.setStreet("Posthusstraeti 11");
    address.setPostCode("101");
    address.setSecretWord("philodendron");
    borg.setAddress(address);
    final HotelDAO hotelDAO = new HotelDAO(getMorphia(), getMongoClient());
    hotelDAO.save(borg);
    assertEquals(1, hotelDAO.count());
    assertNotNull(borg.getId());
    final Hotel hotelLoaded = hotelDAO.get(borg.getId());
    assertEquals(borg.getName(), hotelLoaded.getName());
    assertEquals(borg.getAddress().getPostCode(), hotelLoaded.getAddress().getPostCode());
    final DBObject dbObject = getMorphia().toDBObject(borg);
    assertNull(((DBObject) dbObject.get("address")).get("secretWord"));
    Assert.assertNull(hotelLoaded.getAddress().getSecretWord());
    final Hotel hotelByValue = hotelDAO.findOne("name", "Hotel Borg");
    assertNotNull(hotelByValue);
    assertEquals(borg.getStartDate(), hotelByValue.getStartDate());
    assertTrue(hotelDAO.exists("stars", 4));
    final Hotel hilton = new Hotel();
    hilton.setName("Hilton Hotel");
    hilton.setStars(4);
    hilton.setTakesCreditCards(true);
    hilton.setStartDate(new Date());
    hilton.setType(Hotel.Type.BUSINESS);
    final Address hiltonAddress = new Address();
    hiltonAddress.setStreet("Some street 44");
    hiltonAddress.setPostCode("101");
    hilton.setAddress(hiltonAddress);
    hotelDAO.save(hilton);
    final List<Hotel> allHotels = hotelDAO.find().asList();
    assertEquals(2, allHotels.size());
    assertEquals(1, hotelDAO.createQuery().asList(new FindOptions().skip(1).limit(10)).size());
    assertEquals(1, hotelDAO.createQuery().asList(new FindOptions().limit(1)).size());
    assertTrue(hotelDAO.exists("type", Hotel.Type.BUSINESS));
    assertNotNull(hotelDAO.findOne("type", Hotel.Type.LEISURE));
    // try updating
    final UpdateOperations<Hotel> mods = hotelDAO.createUpdateOperations().inc("stars", 1);
    hotelDAO.update(hotelDAO.createQuery().filter("stars", 4), mods);
    assertEquals(2, hotelDAO.count(hotelDAO.createQuery().filter("stars", 5)));
    hotelDAO.deleteById(borg.getId());
    assertEquals(1, hotelDAO.count());
    hotelDAO.getCollection().drop();
    assertEquals(0, hotelDAO.count());
}
Also used : FindOptions(org.mongodb.morphia.query.FindOptions) Address(org.mongodb.morphia.testmodel.Address) HotelDAO(org.mongodb.morphia.testdaos.HotelDAO) DBObject(com.mongodb.DBObject) Hotel(org.mongodb.morphia.testmodel.Hotel) Date(java.util.Date) Test(org.junit.Test)

Example 82 with DBObject

use of com.mongodb.DBObject in project morphia by mongodb.

the class ReferenceMapper method writeCollection.

private void writeCollection(final MappedField mf, final DBObject dbObject, final String name, final Object fieldValue, final Reference refAnn, final Mapper mapper) {
    if (fieldValue != null) {
        final List values = new ArrayList();
        if (ProxyHelper.isProxy(fieldValue) && ProxyHelper.isUnFetched(fieldValue)) {
            final ProxiedEntityReferenceList p = (ProxiedEntityReferenceList) fieldValue;
            final List<Key<?>> getKeysAsList = p.__getKeysAsList();
            for (final Key<?> key : getKeysAsList) {
                addValue(values, key, mapper, refAnn.idOnly());
            }
        } else {
            if (mf.getType().isArray()) {
                for (final Object o : (Object[]) fieldValue) {
                    addValue(values, o, mapper, refAnn.idOnly());
                }
            } else {
                for (final Object o : (Iterable) fieldValue) {
                    addValue(values, o, mapper, refAnn.idOnly());
                }
            }
        }
        if (!values.isEmpty() || mapper.getOptions().isStoreEmpties()) {
            dbObject.put(name, values);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) List(java.util.List) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) DBObject(com.mongodb.DBObject) Key(org.mongodb.morphia.Key)

Example 83 with DBObject

use of com.mongodb.DBObject in project morphia by mongodb.

the class ReferenceMapper method readCollection.

private void readCollection(final Datastore datastore, final Mapper mapper, final DBObject dbObject, final MappedField mf, final Object entity, final Reference refAnn, final EntityCache cache) {
    // multiple references in a List
    final Class referenceObjClass = mf.getSubClass();
    // load reference class.  this "fixes" #816
    mapper.getMappedClass(referenceObjClass);
    Collection references = mf.isSet() ? mapper.getOptions().getObjectFactory().createSet(mf) : mapper.getOptions().getObjectFactory().createList(mf);
    if (refAnn.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) {
        final Object dbVal = mf.getDbObjectValue(dbObject);
        if (dbVal != null) {
            references = mapper.getProxyFactory().createListProxy(datastore, references, referenceObjClass, refAnn.ignoreMissing());
            final ProxiedEntityReferenceList referencesAsProxy = (ProxiedEntityReferenceList) references;
            if (dbVal instanceof List) {
                referencesAsProxy.__addAll(refAnn.idOnly() ? mapper.getKeysByManualRefs(referenceObjClass, (List) dbVal) : mapper.getKeysByRefs((List) dbVal));
            } else {
                referencesAsProxy.__add(refAnn.idOnly() ? mapper.manualRefToKey(referenceObjClass, dbVal) : mapper.refToKey((DBRef) dbVal));
            }
        }
    } else {
        final Object dbVal = mf.getDbObjectValue(dbObject);
        final Collection refs = references;
        new IterHelper<String, Object>().loopOrSingle(dbVal, new IterCallback<Object>() {

            @Override
            public void eval(final Object val) {
                final Object ent = resolveObject(datastore, mapper, cache, mf, refAnn.idOnly(), val);
                if (ent == null) {
                    LOG.warning("Null reference found when retrieving value for " + mf.getFullName());
                } else {
                    refs.add(ent);
                }
            }
        });
    }
    if (mf.getType().isArray()) {
        mf.setFieldValue(entity, ReflectionUtils.convertToArray(mf.getSubClass(), ReflectionUtils.iterToList(references)));
    } else {
        mf.setFieldValue(entity, references);
    }
}
Also used : Collection(java.util.Collection) DBCollection(com.mongodb.DBCollection) DBObject(com.mongodb.DBObject) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) ArrayList(java.util.ArrayList) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) List(java.util.List)

Example 84 with DBObject

use of com.mongodb.DBObject in project morphia by mongodb.

the class ReferenceMapper method writeMap.

private void writeMap(final MappedField mf, final DBObject dbObject, final String name, final Object fieldValue, final Reference refAnn, final Mapper mapper) {
    final Map<Object, Object> map = (Map<Object, Object>) fieldValue;
    if ((map != null)) {
        final Map values = mapper.getOptions().getObjectFactory().createMap(mf);
        if (ProxyHelper.isProxy(map) && ProxyHelper.isUnFetched(map)) {
            final ProxiedEntityReferenceMap proxy = (ProxiedEntityReferenceMap) map;
            final Map<Object, Key<?>> refMap = proxy.__getReferenceMap();
            for (final Map.Entry<Object, Key<?>> entry : refMap.entrySet()) {
                final Object key = entry.getKey();
                values.put(key, refAnn.idOnly() ? mapper.keyToId(entry.getValue()) : mapper.keyToDBRef(entry.getValue()));
            }
        } else {
            for (final Map.Entry<Object, Object> entry : map.entrySet()) {
                final String strKey = mapper.getConverters().encode(entry.getKey()).toString();
                values.put(strKey, refAnn.idOnly() ? mapper.keyToId(getKey(entry.getValue(), mapper)) : mapper.keyToDBRef(getKey(entry.getValue(), mapper)));
            }
        }
        if (!values.isEmpty() || mapper.getOptions().isStoreEmpties()) {
            dbObject.put(name, values);
        }
    }
}
Also used : ProxiedEntityReferenceMap(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceMap) DBObject(com.mongodb.DBObject) ProxiedEntityReferenceMap(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceMap) Map(java.util.Map) Key(org.mongodb.morphia.Key)

Example 85 with DBObject

use of com.mongodb.DBObject in project morphia by mongodb.

the class ReferenceMapper method writeSingle.

private void writeSingle(final DBObject dbObject, final String name, final Object fieldValue, final Reference refAnn, final Mapper mapper) {
    if (fieldValue == null) {
        if (mapper.getOptions().isStoreNulls()) {
            dbObject.put(name, null);
        }
    } else {
        Key<?> key = getKey(fieldValue, mapper);
        if (refAnn.idOnly()) {
            Object id = mapper.keyToId(key);
            if (id != null && mapper.isMapped(id.getClass())) {
                id = mapper.toMongoObject(id, true);
            }
            dbObject.put(name, id);
        } else {
            dbObject.put(name, mapper.keyToDBRef(key));
        }
    }
}
Also used : DBObject(com.mongodb.DBObject)

Aggregations

DBObject (com.mongodb.DBObject)646 BasicDBObject (com.mongodb.BasicDBObject)445 Test (org.junit.Test)267 YearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest)95 DBCollection (com.mongodb.DBCollection)84 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)79 ApiOperation (io.swagger.annotations.ApiOperation)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)70 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)63 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)53 ArrayList (java.util.ArrayList)44 DBCursor (com.mongodb.DBCursor)42 HashMap (java.util.HashMap)40 List (java.util.List)35 ObjectId (org.bson.types.ObjectId)30 BasicDBList (com.mongodb.BasicDBList)29 Map (java.util.Map)28 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)20 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)20 BSONObject (org.bson.BSONObject)19