Search in sources :

Example 1 with MappingException

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);
        }
    }
}
Also used : JSONParseException(com.mongodb.util.JSONParseException) Document(org.bson.Document) MappingException(org.springframework.data.mapping.MappingException)

Example 2 with MappingException

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);
        }
    }
}
Also used : JSONParseException(com.mongodb.util.JSONParseException) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) MappingException(org.springframework.data.mapping.MappingException)

Example 3 with MappingException

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);
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) Collection(java.util.Collection) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) MappingException(org.springframework.data.mapping.MappingException) Nullable(org.springframework.lang.Nullable)

Example 4 with MappingException

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());
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) DBRef(com.mongodb.DBRef) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.springframework.data.mapping.MappingException)

Example 5 with MappingException

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);
    }
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) MappingException(org.springframework.data.mapping.MappingException)

Aggregations

MappingException (org.springframework.data.mapping.MappingException)12 Document (org.bson.Document)8 BasicDBObject (com.mongodb.BasicDBObject)6 DBObject (com.mongodb.DBObject)5 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)5 BasicDBList (com.mongodb.BasicDBList)3 Collection (java.util.Collection)3 DBRef (com.mongodb.DBRef)2 ReturnDocument (com.mongodb.client.model.ReturnDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 JSONParseException (com.mongodb.util.JSONParseException)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Field (java.lang.reflect.Field)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 ClassTypeInformation (org.springframework.data.util.ClassTypeInformation)1 TypeInformation (org.springframework.data.util.TypeInformation)1 Nullable (org.springframework.lang.Nullable)1