/** * * @param clazz * @param dbObj * @return */ public Object createInstance(final Class clazz, final DBObject dbObj) { if (injectOnConstructor(clazz)) { return this.injector.getInstance(clazz); } return injectMembers(this.delegate.createInstance(clazz, dbObj)); }
private Collection<?> createNewCollection(final MappedField mf) { ObjectFactory of = mapr.getOptions().objectFactory; return mf.isSet() ? of.createSet(mf) : of.createList(mf); }
/** * @param mf * @return */ public Map createMap(final MappedField mf) { final Class clazz = mf.getType(); if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { // there is no good way to find the clazz to use, yet, so delegate return injectMembers(this.delegate.createMap(mf)); } if (injectOnConstructor(clazz)) { return (Map) this.injector.getInstance(clazz); } return injectMembers(this.delegate.createMap(mf)); }
/** * @param mf * @return */ public List createList(final MappedField mf) { final Class clazz = mf.getType(); if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { // there is no good way to find the clazz to use, yet, so delegate return injectMembers(this.delegate.createList(mf)); } if (injectOnConstructor(clazz)) { return (List) this.injector.getInstance(clazz); } return injectMembers(this.delegate.createList(mf)); }
/** * @param mf * @return */ public Set createSet(final MappedField mf) { final Class clazz = mf.getType(); if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { // there is no good way to find the clazz to use, yet, so delegate return injectMembers(this.delegate.createSet(mf)); } if (injectOnConstructor(clazz)) { return (Set) this.injector.getInstance(clazz); } return injectMembers(this.delegate.createSet(mf)); }
private void readCollection(final DBObject dbObject, final MappedField mf, final Object entity, EntityCache cache, Mapper mapr) { Collection values = mf.isSet() ? mapr.getOptions().objectFactory.createSet(mf) : mapr.getOptions().objectFactory.createList(mf);
/** * Converts a DBObject back to a type-safe java object (POJO) * * @param entityClass The type to return, or use; can be overridden by the @see Mapper.CLASS_NAME_FIELDNAME in the DBObject */ public Object fromDBObject(final Class entityClass, final DBObject dbObject, EntityCache cache) { if (dbObject == null) { Throwable t = new Throwable(); log.error("Somebody passed in a null dbObject; bad client!", t); return null; } Object entity = null; entity = opts.objectFactory.createInstance(entityClass, dbObject); entity = fromDBObject(dbObject, entity, cache); return entity; }
@Override public Object decode(Class targetClass, Object fromDBObject, final MappedField mf) throws MappingException { if (fromDBObject == null) return null; final Map values = mapr.getOptions().objectFactory.createMap(mf); new IterHelper<Object, Object>().loopMap((Object) fromDBObject, new MapIterCallback<Object, Object>() { @Override public void eval(Object key, Object val) { Object objKey = converters.decode(mf.getMapKeyClass(), key); values.put(objKey, converters.decode(mf.getSubClass(), val)); } }); return values; }
Collection references = mf.isSet() ? mapr.getOptions().objectFactory.createSet(mf) : mapr.getOptions().objectFactory.createList(mf);
public TypeConverter addConverter(Class<? extends TypeConverter> clazz) { return addConverter((TypeConverter) this.mapr.getOptions().objectFactory.createInstance(clazz)); }
private void writeMap(final MappedField mf, final DBObject dbObject, String name, Object fieldValue, Mapper mapr) { Map<Object, Object> map = (Map<Object, Object>) fieldValue; if ((map != null)) { Map values = mapr.getOptions().objectFactory.createMap(mf); if (ProxyHelper.isProxy(map) && ProxyHelper.isUnFetched(map)) { ProxiedEntityReferenceMap proxy = (ProxiedEntityReferenceMap) map; Map<Object, Key<?>> refMap = proxy.__getReferenceMap(); for (Map.Entry<Object, Key<?>> entry : refMap.entrySet()) { Object strKey = entry.getKey(); values.put(strKey, mapr.keyToRef(entry.getValue())); } } else { for (Map.Entry<Object, Object> entry : map.entrySet()) { String strKey = mapr.getConverters().encode(entry.getKey()).toString(); values.put(strKey, mapr.keyToRef(getKey(entry.getValue(), mapr))); } } if (values.size() > 0 || mapr.getOptions().storeEmpties) { dbObject.put(name, values); } } }
/** * * @param mapr * @param mf * @param dbObj * @return */ public Object createInstance(final Mapper mapr, final MappedField mf, final DBObject dbObj) { final Class clazz = mf.getType(); if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { // there is no good way to find the clazz to use, yet, so delegate return injectMembers(this.delegate.createInstance(mapr, mf, dbObj)); } if (injectOnConstructor(clazz)) { return this.injector.getInstance(clazz); } return injectMembers(this.delegate.createInstance(mapr, mf, dbObj)); }
private void readMap(final DBObject dbObject, final MappedField mf, final Object entity, final EntityCache cache, final Mapper mapr) { final Map map = mapr.getOptions().objectFactory.createMap(mf); DBObject dbObj = (DBObject) mf.getDbObjectValue(dbObject); new IterHelper<Object, Object>().loopMap((Object) dbObj, new MapIterCallback<Object, Object>() { @Override public void eval(Object key, Object val) { Object newEntity = null; //run converters if (val != null) { if (mapr.getConverters().hasSimpleValueConverter(mf) || mapr.getConverters().hasSimpleValueConverter(mf.getSubClass())) newEntity = mapr.getConverters().decode(mf.getSubClass(), val, mf); else { if (val instanceof DBObject) newEntity = readMapOrCollectionOrEntity((DBObject) val, mf, cache, mapr); else throw new MappingException("Embedded element isn't a DBObject! How can it be that is a " + val.getClass()); } } Object objKey = mapr.getConverters().decode(mf.getMapKeyClass(), key); map.put(objKey, newEntity); } }); if (map.size() > 0) { mf.setFieldValue(entity, map); } }
/** * * @param clazz * @return */ public Object createInstance(final Class clazz) { Assert.parameterNotNull(clazz, "clazz"); if (injectOnConstructor(clazz)) { return this.injector.getInstance(clazz); } return injectMembers(this.delegate.createInstance(clazz)); }
private void readMap(final DBObject dbObject, final MappedField mf, final Object entity, final Reference refAnn, final EntityCache cache, final Mapper mapr) { Class referenceObjClass = mf.getSubClass(); Map m = mapr.getOptions().objectFactory.createMap(mf); DBObject dbVal = (DBObject) mf.getDbObjectValue(dbObject); if (dbVal != null) { if (refAnn.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) { // replace map by proxy to it. m = mapr.getProxyFactory().createMapProxy(m, referenceObjClass, refAnn.ignoreMissing(), mapr.getDatastoreProvider()); } final Map map = m; new IterHelper<Object, Object>().loopMap(dbVal, new MapIterCallback<Object, Object>() { @Override public void eval(Object key, Object val) { DBRef dbRef = (DBRef) val; Object objKey = mapr.getConverters().decode(mf.getMapKeyClass(), key); if (refAnn.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) { ProxiedEntityReferenceMap proxiedMap = (ProxiedEntityReferenceMap) map; proxiedMap.__put(objKey, mapr.refToKey(dbRef)); } else { Object resolvedObject = resolveObject(dbRef, mf, cache, mapr); map.put(objKey, resolvedObject); } } }); } mf.setFieldValue(entity, m); }
private Object readMapOrCollectionOrEntity(DBObject dbObj, MappedField mf, EntityCache cache, Mapper mapr) { if (Map.class.isAssignableFrom(mf.getSubClass()) || Iterable.class.isAssignableFrom(mf.getSubClass())) { MapOrCollectionMF mocMF = new MapOrCollectionMF((ParameterizedType) mf.getSubType()); mapr.fromDBObject(dbObj, mocMF, cache); return mocMF.getValue(); } else { Object newEntity = mapr.getOptions().objectFactory.createInstance(mapr, mf, dbObj); return mapr.fromDBObject(dbObj, newEntity, cache); } }
private Object getOrCreateInstance(Class<?> clazz) { if (mapr.isCached(clazz)) return mapr.getCachedClass(clazz); Object o = mapr.getOptions().objectFactory.createInstance(clazz); try { mapr.cacheClass(clazz, o); } catch (ConcurrentModificationException duplicate) { if (log.isErrorEnabled()) log.error("Race-condition",duplicate); } return o; }
Object resolveObject(final DBRef dbRef, final MappedField mf, EntityCache cache, Mapper mapr) { if (dbRef == null) return null; Key key = mapr.createKey(mf.isSingleValue() ? mf.getType() : mf.getSubClass(), dbRef.getId()); Object cached = cache.getEntity(key); if (cached != null) return cached; //TODO: if _db is null, set it? DBObject refDbObject = (DBObject) dbRef.fetch(); if (refDbObject != null) { Object refObj = mapr.getOptions().objectFactory.createInstance(mapr, mf, refDbObject); refObj = mapr.fromDBObject(refDbObject, refObj, cache); cache.putEntity(key, refObj); return refObj; } boolean ignoreMissing = mf.getAnnotation(Reference.class) != null && mf.getAnnotation(Reference.class).ignoreMissing(); if (!ignoreMissing) { throw new MappingException("The reference(" + dbRef.toString() + ") could not be fetched for " + mf.getFullName()); } else { return null; } }
refObj = mapr.getConverters().decode(mf.getType(), dbVal, mf); else { refObj = mapr.getOptions().objectFactory.createInstance(mapr, mf, ((DBObject) dbVal)); refObj = mapr.fromDBObject(((DBObject) dbVal), refObj, cache);