List of usage examples for org.springframework.data.mongodb.core.mapping MongoPersistentProperty getTypeInformation
TypeInformation<?> getTypeInformation();
From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java
@SuppressWarnings({ "unchecked" })
protected void writePropertyInternal(Object obj, DBObject dbo, MongoPersistentProperty prop) {
if (obj == null) {
return;//from ww w . j a v a 2s .co m
}
String name = prop.getFieldName();
TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass());
TypeInformation<?> type = prop.getTypeInformation();
if (valueType.isCollectionLike()) {
DBObject collectionInternal = createCollection(asCollection(obj), prop);
dbo.put(name, collectionInternal);
return;
}
if (valueType.isMap()) {
BasicDBObject mapDbObj = new BasicDBObject();
writeMapInternal((Map<Object, Object>) obj, mapDbObj, type);
dbo.put(name, mapDbObj);
return;
}
if (prop.isDbReference()) {
DBRef dbRefObj = createDBRef(obj, prop.getDBRef());
if (null != dbRefObj) {
dbo.put(name, dbRefObj);
return;
}
}
// Lookup potential custom target type
Class<?> basicTargetType = conversions.getCustomWriteTarget(obj.getClass(), null);
if (basicTargetType != null) {
dbo.put(name, conversionService.convert(obj, basicTargetType));
return;
}
BasicDBObject propDbObj = new BasicDBObject();
addCustomTypeKeyIfNecessary(type, obj, propDbObj);
MongoPersistentEntity<?> entity = isSubtype(prop.getType(), obj.getClass())
? mappingContext.getPersistentEntity(obj.getClass())
: mappingContext.getPersistentEntity(type);
writeInternal(obj, propDbObj, entity);
dbo.put(name, propDbObj);
}
From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java
/** * Writes the given {@link Collection} using the given {@link MongoPersistentProperty} information. * // w w w.j a v a 2 s .c om * @param collection must not be {@literal null}. * @param property must not be {@literal null}. * @return */ protected DBObject createCollection(Collection<?> collection, MongoPersistentProperty property) { if (!property.isDbReference()) { return writeCollectionInternal(collection, property.getTypeInformation(), new BasicDBList()); } BasicDBList dbList = new BasicDBList(); for (Object element : collection) { if (element == null) { continue; } DBRef dbRef = createDBRef(element, property.getDBRef()); dbList.add(dbRef); } return dbList; }
From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java
@SuppressWarnings("unchecked") protected Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, StandardEvaluationContext ctx, String spelExpr) {//www. ja va2 s.c o m Object o; if (null != spelExpr) { Expression x = spelExpressionParser.parseExpression(spelExpr); o = x.getValue(ctx); } else { Object sourceValue = dbo.get(prop.getFieldName()); if (sourceValue == null) { return null; } Class<?> propertyType = prop.getType(); if (conversions.hasCustomReadTarget(sourceValue.getClass(), propertyType)) { return conversionService.convert(sourceValue, propertyType); } if (sourceValue instanceof DBRef) { sourceValue = ((DBRef) sourceValue).fetch(); } if (sourceValue instanceof DBObject) { if (prop.isMap()) { return readMap(prop.getTypeInformation(), (DBObject) sourceValue); } else if (prop.isArray() && sourceValue instanceof BasicDBObject && ((DBObject) sourceValue).keySet().size() == 0) { // It's empty return Array.newInstance(prop.getComponentType(), 0); } else if (prop.isCollectionLike() && sourceValue instanceof BasicDBList) { return readCollectionOrArray( (TypeInformation<? extends Collection<?>>) prop.getTypeInformation(), (BasicDBList) sourceValue); } TypeInformation<?> toType = typeMapper.readType((DBObject) sourceValue, prop.getTypeInformation()); // It's a complex object, have to read it in if (toType != null) { // TODO: why do we remove the type? // dbo.removeField(CUSTOM_TYPE_KEY); o = read(toType, (DBObject) sourceValue); } else { o = read(mappingContext.getPersistentEntity(prop.getTypeInformation()), (DBObject) sourceValue); } } else { o = sourceValue; } } return o; }