use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.
the class MongoTemplate method toDocument.
/**
* @param objectToSave
* @param writer
* @return
*/
private <T> Document toDocument(T objectToSave, MongoWriter<T> writer) {
if (objectToSave instanceof Document) {
return (Document) objectToSave;
}
if (!(objectToSave instanceof String)) {
Document dbDoc = new Document();
writer.write(objectToSave, dbDoc);
if (dbDoc.containsKey(ID_FIELD) && dbDoc.get(ID_FIELD) == null) {
dbDoc.remove(ID_FIELD);
}
return dbDoc;
} else {
try {
return Document.parse((String) objectToSave);
} catch (JSONParseException e) {
throw new MappingException("Could not parse given String to save into a JSON document!", e);
} catch (org.bson.json.JsonParseException e) {
throw new MappingException("Could not parse given String to save into a JSON document!", e);
}
}
}
use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method toDbObject.
/**
* @param objectToSave
* @param writer
* @return
*/
private <T> Document toDbObject(T objectToSave, MongoWriter<T> writer) {
if (objectToSave instanceof Document) {
return (Document) objectToSave;
}
if (!(objectToSave instanceof String)) {
Document dbDoc = new Document();
writer.write(objectToSave, dbDoc);
if (dbDoc.containsKey(ID_FIELD) && dbDoc.get(ID_FIELD) == null) {
dbDoc.remove(ID_FIELD);
}
return dbDoc;
} else {
try {
return Document.parse((String) objectToSave);
} catch (JSONParseException | org.bson.json.JsonParseException e) {
throw new MappingException("Could not parse given String to save into a JSON document!", e);
}
}
}
use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.
the class MappingMongoConverter method read.
@Nullable
@SuppressWarnings("unchecked")
private <S extends Object> S read(TypeInformation<S> type, @Nullable Bson bson, ObjectPath path) {
if (null == bson) {
return null;
}
TypeInformation<? extends S> typeToUse = typeMapper.readType(bson, type);
Class<? extends S> rawType = typeToUse.getType();
if (conversions.hasCustomReadTarget(bson.getClass(), rawType)) {
return conversionService.convert(bson, rawType);
}
if (DBObject.class.isAssignableFrom(rawType)) {
return (S) bson;
}
if (Document.class.isAssignableFrom(rawType)) {
return (S) bson;
}
if (typeToUse.isCollectionLike() && bson instanceof List) {
return (S) readCollectionOrArray(typeToUse, (List<?>) bson, path);
}
if (typeToUse.isMap()) {
return (S) readMap(typeToUse, bson, path);
}
if (bson instanceof Collection) {
throw new MappingException(String.format(INCOMPATIBLE_TYPES, bson, BasicDBList.class, typeToUse.getType(), path));
}
if (typeToUse.equals(ClassTypeInformation.OBJECT)) {
return (S) bson;
}
// Retrieve persistent entity info
Document target = bson instanceof BasicDBObject ? new Document((BasicDBObject) bson) : (Document) bson;
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(typeToUse);
if (entity == null) {
throw new MappingException(String.format(INVALID_TYPE_TO_READ, target, typeToUse.getType()));
}
return read((MongoPersistentEntity<S>) mappingContext.getRequiredPersistentEntity(typeToUse), target, path);
}
use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.
the class MappingMongoConverter method createDBRef.
protected DBRef createDBRef(Object target, MongoPersistentProperty property) {
Assert.notNull(target, "Target object must not be null!");
if (target instanceof DBRef) {
return (DBRef) target;
}
MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass());
targetEntity = targetEntity != null ? targetEntity : mappingContext.getPersistentEntity(property);
if (null == targetEntity) {
throw new MappingException("No mapping metadata found for " + target.getClass());
}
MongoPersistentEntity<?> entity = targetEntity;
MongoPersistentProperty idProperty = entity.getIdProperty();
if (idProperty != null) {
Object id = target.getClass().equals(idProperty.getType()) ? target : entity.getPropertyAccessor(target).getProperty(idProperty);
if (null == id) {
throw new MappingException("Cannot create a reference to an object with a NULL id.");
}
return dbRefResolver.createDbRef(property == null ? null : property.getDBRef(), entity, idMapper.convertId(id));
}
throw new MappingException("No id property found on class " + entity.getType());
}
use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.
the class MongoPersistentEntityIndexResolver method resolveAndAddIndexesForAssociation.
private void resolveAndAddIndexesForAssociation(Association<MongoPersistentProperty> association, List<IndexDefinitionHolder> indexes, String path, String collection) {
MongoPersistentProperty property = association.getInverse();
String propertyDotPath = (StringUtils.hasText(path) ? path + "." : "") + property.getFieldName();
if (property.isAnnotationPresent(GeoSpatialIndexed.class) || property.isAnnotationPresent(TextIndexed.class)) {
throw new MappingException(String.format("Cannot create geospatial-/text- index on DBRef in collection '%s' for path '%s'.", collection, propertyDotPath));
}
IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty(propertyDotPath, collection, property);
if (indexDefinitionHolder != null) {
indexes.add(indexDefinitionHolder);
}
}
Aggregations