@Override public TypeInformation<?> getTypeHint() { MongoPersistentProperty property = getProperty(); if (property == null) { return super.getTypeHint(); } if (property.getActualType().isInterface() || java.lang.reflect.Modifier.isAbstract(property.getActualType().getModifiers())) { return ClassTypeInformation.OBJECT; } return NESTED_DOCUMENT; }
private static void assertPropertyType(MongoPersistentProperty persistentProperty, Class<?>... validMatches) { for (Class<?> potentialMatch : validMatches) { if (ClassUtils.isAssignable(potentialMatch, persistentProperty.getActualType())) { return; } } throw new MappingException( String.format("Missmatching types for %s. Found %s expected one of %s.", persistentProperty.getField(), persistentProperty.getActualType(), StringUtils.arrayToCommaDelimitedString(validMatches))); } }
private static void potentiallyAssertDBRefTargetType(MongoPersistentProperty persistentProperty) { if (persistentProperty.isDbReference() && persistentProperty.getDBRef().lazy()) { if (persistentProperty.isArray() || Modifier.isFinal(persistentProperty.getActualType().getModifiers())) { throw new MappingException(String.format( "Invalid lazy DBRef property for %s. Found %s which must not be an array nor a final class.", persistentProperty.getField(), persistentProperty.getActualType())); } } }
/** * Returns whether the given {@link Field} represents an association reference that together with the given value * requires conversion to a {@link org.springframework.data.mongodb.core.mapping.DBRef} object. We check whether the * type of the given value is compatible with the type of the given document field in order to deal with potential * query field exclusions, since MongoDB uses the {@code int} {@literal 0} as an indicator for an excluded field. * * @param documentField must not be {@literal null}. * @param value * @return */ protected boolean isAssociationConversionNecessary(Field documentField, @Nullable Object value) { Assert.notNull(documentField, "Document field must not be null!"); if (value == null) { return false; } if (!documentField.isAssociation()) { return false; } Class<? extends Object> type = value.getClass(); MongoPersistentProperty property = documentField.getProperty(); if (property.getActualType().isAssignableFrom(type)) { return true; } MongoPersistentEntity<?> entity = documentField.getPropertyEntity(); return entity.hasIdProperty() && (type.equals(DBRef.class) || entity.getRequiredIdProperty().getActualType().isAssignableFrom(type)); }
private String getMappedPropertyPath(String path, Class<?> probeType) { MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(probeType); Iterator<String> parts = Arrays.asList(path.split("\\.")).iterator(); final Stack<MongoPersistentProperty> stack = new Stack<>(); List<String> resultParts = new ArrayList<>(); while (parts.hasNext()) { String part = parts.next(); MongoPersistentProperty prop = entity.getPersistentProperty(part); if (prop == null) { entity.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> { if (property.getFieldName().equals(part)) { stack.push(property); } }); if (stack.isEmpty()) { return ""; } prop = stack.pop(); } resultParts.add(prop.getName()); if (prop.isEntity() && mappingContext.hasPersistentEntityFor(prop.getActualType())) { entity = mappingContext.getRequiredPersistentEntity(prop.getActualType()); } else { break; } } return StringUtils.collectionToDelimitedString(resultParts, "."); }
private Document mapProperty(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, String sourceFieldName, Document source) { Document sink = new Document(source); if (entity != null && sink.containsKey(Type.objectType().representation())) { MongoPersistentProperty property = entity.getPersistentProperty(sourceFieldName); if (property != null && property.isEntity()) { sink = mapSchemaObject(mappingContext.getPersistentEntity(property.getActualType()), source); } } return mapEnumValuesIfNecessary(sink); }
mappingContext.getPersistentEntity(persistentProperty.getActualType()), optionsForNestedType, guard); } catch (CyclicPropertyReferenceException e) { LOGGER.info(e.getMessage());
@Override public TypeInformation<?> getTypeHint() { MongoPersistentProperty property = getProperty(); if (property == null) { return super.getTypeHint(); } if (property.getActualType().isInterface() || java.lang.reflect.Modifier.isAbstract(property.getActualType().getModifiers())) { return ClassTypeInformation.OBJECT; } return NESTED_DOCUMENT; }
private static void assertPropertyType(MongoPersistentProperty persistentProperty, Class<?>... validMatches) { for (Class<?> potentialMatch : validMatches) { if (ClassUtils.isAssignable(potentialMatch, persistentProperty.getActualType())) { return; } } throw new MappingException( String.format("Missmatching types for %s. Found %s expected one of %s.", persistentProperty.getField(), persistentProperty.getActualType(), StringUtils.arrayToCommaDelimitedString(validMatches))); } }
private static void potentiallyAssertDBRefTargetType(MongoPersistentProperty persistentProperty) { if (persistentProperty.isDbReference() && persistentProperty.getDBRef().lazy()) { if (persistentProperty.isArray() || Modifier.isFinal(persistentProperty.getActualType().getModifiers())) { throw new MappingException(String.format( "Invalid lazy DBRef property for %s. Found %s which must not be an array nor a final class.", persistentProperty.getField(), persistentProperty.getActualType())); } } }
/** * Returns whether the given {@link Field} represents an association reference that together with the given value * requires conversion to a {@link org.springframework.data.mongodb.core.mapping.DBRef} object. We check whether the * type of the given value is compatible with the type of the given document field in order to deal with potential * query field exclusions, since MongoDB uses the {@code int} {@literal 0} as an indicator for an excluded field. * * @param documentField must not be {@literal null}. * @param value * @return */ protected boolean isAssociationConversionNecessary(Field documentField, @Nullable Object value) { Assert.notNull(documentField, "Document field must not be null!"); if (value == null) { return false; } if (!documentField.isAssociation()) { return false; } Class<? extends Object> type = value.getClass(); MongoPersistentProperty property = documentField.getProperty(); if (property.getActualType().isAssignableFrom(type)) { return true; } MongoPersistentEntity<?> entity = documentField.getPropertyEntity(); return entity.hasIdProperty() && (type.equals(DBRef.class) || entity.getRequiredIdProperty().getActualType().isAssignableFrom(type)); }
private String getMappedPropertyPath(String path, Class<?> probeType) { MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(probeType); Iterator<String> parts = Arrays.asList(path.split("\\.")).iterator(); final Stack<MongoPersistentProperty> stack = new Stack<>(); List<String> resultParts = new ArrayList<>(); while (parts.hasNext()) { String part = parts.next(); MongoPersistentProperty prop = entity.getPersistentProperty(part); if (prop == null) { entity.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> { if (property.getFieldName().equals(part)) { stack.push(property); } }); if (stack.isEmpty()) { return ""; } prop = stack.pop(); } resultParts.add(prop.getName()); if (prop.isEntity() && mappingContext.hasPersistentEntityFor(prop.getActualType())) { entity = mappingContext.getRequiredPersistentEntity(prop.getActualType()); } else { break; } } return StringUtils.collectionToDelimitedString(resultParts, "."); }
private Document mapProperty(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, String sourceFieldName, Document source) { Document sink = new Document(source); if (entity != null && sink.containsKey(Type.objectType().representation())) { MongoPersistentProperty property = entity.getPersistentProperty(sourceFieldName); if (property != null && property.isEntity()) { sink = mapSchemaObject(mappingContext.getPersistentEntity(property.getActualType()), source); } } return mapEnumValuesIfNecessary(sink); }
mappingContext.getPersistentEntity(persistentProperty.getActualType()), optionsForNestedType, guard); } catch (CyclicPropertyReferenceException e) { LOGGER.info(e.getMessage());