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());
}
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);
}
}
}
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);
}
}
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);
}
}
}
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));
}
}
}
Aggregations