org.springframework.data.mapping.model.ConvertingPropertyAccessor Java Examples

The following examples show how to use org.springframework.data.mapping.model.ConvertingPropertyAccessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MappingSolrConverter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private <S extends Object> S read(final SolrPersistentEntity<S> entity, final Map<String, ?> source,
		Object parent) {
	ParameterValueProvider<SolrPersistentProperty> parameterValueProvider = getParameterValueProvider(entity,
			source, parent);

	EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
	final S instance = instantiator.createInstance(entity, parameterValueProvider);
	final PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance),
			getConversionService());

	entity.doWithProperties(new PropertyHandler<SolrPersistentProperty>() {

		@Override
		public void doWithPersistentProperty(SolrPersistentProperty persistentProperty) {
			if (entity.isConstructorArgument(persistentProperty)) {
				return;
			}

			Object o = getValue(persistentProperty, source, instance);
			if (o != null) {
				accessor.setProperty(persistentProperty, o);
			}
		}
	});

	return instance;
}
 
Example #2
Source File: MappingCosmosConverter.java    From spring-data-cosmosdb with MIT License 5 votes vote down vote up
public CosmosItemProperties writeCosmosItemProperties(Object sourceEntity) {
    if (sourceEntity == null) {
        return null;
    }

    final CosmosPersistentEntity<?> persistentEntity =
        mappingContext.getPersistentEntity(sourceEntity.getClass());

    if (persistentEntity == null) {
        throw new MappingException("no mapping metadata for entity type: " + sourceEntity.getClass().getName());
    }

    final ConvertingPropertyAccessor accessor = getPropertyAccessor(sourceEntity);
    final CosmosPersistentProperty idProperty = persistentEntity.getIdProperty();
    final CosmosItemProperties cosmosItemProperties;

    try {
        cosmosItemProperties =
            new CosmosItemProperties(objectMapper.writeValueAsString(sourceEntity));
    } catch (JsonProcessingException e) {
        throw new CosmosDBAccessException("Failed to map document value.", e);
    }

    if (idProperty != null) {
        final Object value = accessor.getProperty(idProperty);
        final String id = value == null ? null : value.toString();
        cosmosItemProperties.id(id);
    }

    return cosmosItemProperties;
}
 
Example #3
Source File: MappingCosmosConverter.java    From spring-data-cosmosdb with MIT License 5 votes vote down vote up
private ConvertingPropertyAccessor getPropertyAccessor(Object entity) {
    final CosmosPersistentEntity<?> entityInformation =
        mappingContext.getPersistentEntity(entity.getClass());

    Assert.notNull(entityInformation, "EntityInformation should not be null.");
    final PersistentPropertyAccessor accessor = entityInformation.getPropertyAccessor(entity);
    return new ConvertingPropertyAccessor(accessor, conversionService);
}
 
Example #4
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
private <S> S read(VaultPersistentEntity<S> entity, SecretDocument source) {

		ParameterValueProvider<VaultPersistentProperty> provider = getParameterProvider(entity, source);
		EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(entity);
		S instance = instantiator.createInstance(entity, provider);

		PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance),
				this.conversionService);

		VaultPersistentProperty idProperty = entity.getIdProperty();
		SecretDocumentAccessor documentAccessor = new SecretDocumentAccessor(source);

		// make sure id property is set before all other properties
		Object idValue;

		if (entity.requiresPropertyPopulation()) {
			if (idProperty != null && !entity.isConstructorArgument(idProperty)
					&& documentAccessor.hasValue(idProperty)) {

				idValue = readIdValue(idProperty, documentAccessor);
				accessor.setProperty(idProperty, idValue);
			}

			VaultPropertyValueProvider valueProvider = new VaultPropertyValueProvider(documentAccessor);

			readProperties(entity, accessor, idProperty, documentAccessor, valueProvider);
		}

		return instance;
	}
 
Example #5
Source File: MappingSolrConverter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
protected void write(Object source, final Map target, SolrPersistentEntity<?> entity) {

	final PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(source),
			getConversionService());

	entity.doWithProperties(new PropertyHandler<SolrPersistentProperty>() {

		@SuppressWarnings("unchecked")
		@Override
		public void doWithPersistentProperty(SolrPersistentProperty persistentProperty) {

			Object value = accessor.getProperty(persistentProperty);
			if (value == null || persistentProperty.isReadonly()) {
				return;
			}

			if (persistentProperty.containsWildcard() && !persistentProperty.isMap()) {
				throw new IllegalArgumentException("Field '" + persistentProperty.getFieldName()
						+ "' must not contain wildcards. Consider excluding Field from beeing indexed.");
			}

			Collection<SolrInputField> fields;
			if (persistentProperty.isMap() && persistentProperty.containsWildcard()) {
				fields = writeWildcardMapPropertyToTarget(target, persistentProperty, (Map<?, ?>) value);
			} else {
				fields = writeRegularPropertyToTarget(target, persistentProperty, value);
			}

			if (persistentProperty.isBoosted()) {
				for (SolrInputField field : fields) {
					field.setBoost(persistentProperty.getBoost());
				}
			}
		}
	});

	if (entity.isBoosted() && target instanceof SolrInputDocument) {
		((SolrInputDocument) target).setDocumentBoost(entity.getBoost());
	}

}
 
Example #6
Source File: TypicalEntityReaderBenchmark.java    From spring-data-dev-tools with Apache License 2.0 4 votes vote down vote up
/**
 * Typical code used to read entities in {@link org.springframework.data.convert.EntityReader}.
 *
 * @param data
 * @param classToRead
 * @param queryCustomConversions {@literal true} to call {@link CustomConversions#hasCustomReadTarget(Class, Class)}.
 * @return
 */
@SuppressWarnings("unchecked")
private Object read(Map<String, Object> data, Class<?> classToRead, boolean queryCustomConversions) {

	if (queryCustomConversions) {
		customConversions.hasCustomReadTarget(Map.class, classToRead);
	}

	MyPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(classToRead);
	PreferredConstructor<?, MyPersistentProperty> constructor = persistentEntity.getPersistenceConstructor();

	ParameterValueProvider<MyPersistentProperty> provider = constructor.isNoArgConstructor() //
			? NONE //
			: new ParameterValueProvider<MyPersistentProperty>() {

				@Override
				public <T> T getParameterValue(Parameter<T, MyPersistentProperty> parameter) {
					return (T) getValue(data, parameter.getName(), parameter.getType().getType(), queryCustomConversions);
				}
			};

	EntityInstantiator instantiator = instantiators.getInstantiatorFor(persistentEntity);
	Object instance = instantiator.createInstance(persistentEntity, provider);

	if (!persistentEntity.requiresPropertyPopulation()) {
		return instance;
	}

	PropertyValueProvider<MyPersistentProperty> valueProvider = new PropertyValueProvider<MyPersistentProperty>() {

		@Override
		public <T> T getPropertyValue(MyPersistentProperty property) {
			return (T) getValue(data, property.getName(), property.getType(), queryCustomConversions);
		}
	};

	PersistentPropertyAccessor<?> accessor = new ConvertingPropertyAccessor<>(
			persistentEntity.getPropertyAccessor(instance), conversionService);

	readProperties(data, persistentEntity, valueProvider, accessor);

	return accessor.getBean();
}