Java Code Examples for org.springframework.data.mapping.model.ParameterValueProvider

The following examples show how to use org.springframework.data.mapping.model.ParameterValueProvider. These examples are extracted from open source projects. 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 Project: sdn-rx   Source File: DefaultNeo4jConverter.java    License: Apache License 2.0 6 votes vote down vote up
private <ET> ET instantiate(Neo4jPersistentEntity<ET> nodeDescription,
	MapAccessor values,
	KnownObjects knownObjects,
	Collection<RelationshipDescription> relationships,
	Collection<String> surplusLabels) {

	ParameterValueProvider<Neo4jPersistentProperty> parameterValueProvider = new ParameterValueProvider<Neo4jPersistentProperty>() {
		@Override
		public Object getParameterValue(PreferredConstructor.Parameter parameter) {

			Neo4jPersistentProperty matchingProperty = nodeDescription
				.getRequiredPersistentProperty(parameter.getName());

			if (matchingProperty.isRelationship()) {
				return createInstanceOfRelationships(matchingProperty, values, knownObjects, relationships)
					.orElse(null);
			} else if (matchingProperty.isDynamicLabels()) {
				return createDynamicLabelsProperty(matchingProperty.getTypeInformation(), surplusLabels);
			}
			return readValueForProperty(extractValueOf(matchingProperty, values), parameter.getType());
		}
	};

	return INSTANTIATORS.getInstantiatorFor(nodeDescription)
		.createInstance(nodeDescription, parameterValueProvider);
}
 
Example 2
Source Project: spring-vault   Source File: MappingVaultConverter.java    License: Apache License 2.0 6 votes vote down vote up
private ParameterValueProvider<VaultPersistentProperty> getParameterProvider(VaultPersistentEntity<?> entity,
		SecretDocument source) {

	VaultPropertyValueProvider provider = new VaultPropertyValueProvider(source);

	PersistentEntityParameterValueProvider<VaultPersistentProperty> parameterProvider = new PersistentEntityParameterValueProvider<>(
			entity, provider, source);

	return new ParameterValueProvider<VaultPersistentProperty>() {

		@Nullable
		@Override
		public <T> T getParameterValue(Parameter<T, VaultPersistentProperty> parameter) {

			Object value = parameterProvider.getParameterValue(parameter);
			return value != null ? readValue(value, parameter.getType()) : null;
		}
	};
}
 
Example 3
Source Project: spring-data   Source File: DefaultArangoConverter.java    License: Apache License 2.0 5 votes vote down vote up
private ParameterValueProvider<ArangoPersistentProperty> getParameterProvider(
	final ArangoPersistentEntity<?> entity,
	final VPackSlice source) {

	final PropertyValueProvider<ArangoPersistentProperty> provider = new ArangoPropertyValueProvider(entity,
			source);
	return new PersistentEntityParameterValueProvider<>(entity, provider, null);
}
 
Example 4
Source Project: dubbox   Source File: MappingSolrConverter.java    License: 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 5
Source Project: dubbox   Source File: MappingSolrConverter.java    License: Apache License 2.0 5 votes vote down vote up
private ParameterValueProvider<SolrPersistentProperty> getParameterValueProvider(SolrPersistentEntity<?> entity,
		Map<String, ?> source, Object parent) {

	SolrPropertyValueProvider provider = new SolrPropertyValueProvider(source, parent);
	PersistentEntityParameterValueProvider<SolrPersistentProperty> parameterProvider = new PersistentEntityParameterValueProvider<SolrPersistentProperty>(
			entity, provider, parent);

	return parameterProvider;
}
 
Example 6
Source Project: spring-vault   Source File: MappingVaultConverter.java    License: 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 7
@Override
@SuppressWarnings("unchecked")
public <R> R read(Class<R> aClass, BaseEntity entity) {
	if (entity == null) {
		return null;
	}
	DatastorePersistentEntity<R> ostensiblePersistentEntity = (DatastorePersistentEntity<R>) this.mappingContext
			.getPersistentEntity(aClass);

	if (ostensiblePersistentEntity == null) {
		throw new DatastoreDataException("Unable to convert Datastore Entity to " + aClass);
	}

	EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider(entity, this.conversions);

	DatastorePersistentEntity<?> persistentEntity = getDiscriminationPersistentEntity(ostensiblePersistentEntity,
			propertyValueProvider);

	ParameterValueProvider<DatastorePersistentProperty> parameterValueProvider =
			new PersistentEntityParameterValueProvider<>(persistentEntity, propertyValueProvider, null);

	EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(persistentEntity);
	Object instance;
	try {
		instance = instantiator.createInstance(persistentEntity, parameterValueProvider);
		PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance);

		persistentEntity.doWithColumnBackedProperties((datastorePersistentProperty) -> {
			// if a property is a constructor argument, it was already computed on instantiation
			if (!persistentEntity.isConstructorArgument(datastorePersistentProperty)) {
				Object value = propertyValueProvider
						.getPropertyValue(datastorePersistentProperty);
				accessor.setProperty(datastorePersistentProperty, value);
			}
		});
	}
	catch (DatastoreDataException ex) {
		throw new DatastoreDataException("Unable to read " + persistentEntity.getName() + " entity", ex);
	}

	return (R) instance;
}
 
Example 8
/**
 * Reads a single POJO from a Cloud Spanner row.
 * @param type the type of POJO
 * @param source the Cloud Spanner row
 * @param includeColumns the columns to read. If null then all columns will be read.
 * @param allowMissingColumns if true, then properties with no corresponding column are
 * not mapped. If false, then an exception is thrown.
 * @param <R> the type of the POJO.
 * @return the POJO
 */
@SuppressWarnings("unchecked")
public <R> R read(Class<R> type, Struct source, Set<String> includeColumns,
		boolean allowMissingColumns) {
	boolean readAllColumns = includeColumns == null;
	SpannerPersistentEntity<R> persistentEntity =
			(SpannerPersistentEntity<R>) this.spannerMappingContext.getPersistentEntity(type);

	StructAccessor structAccessor = new StructAccessor(source);

	StructPropertyValueProvider propertyValueProvider = new StructPropertyValueProvider(
			structAccessor,
			this.converter,
			this, allowMissingColumns);

	PreferredConstructor<?, SpannerPersistentProperty> persistenceConstructor = persistentEntity
			.getPersistenceConstructor();

	// @formatter:off
	ParameterValueProvider<SpannerPersistentProperty> parameterValueProvider =
					new PersistentEntityParameterValueProvider<>(persistentEntity, propertyValueProvider, null);
	// @formatter:on

	EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(persistentEntity);
	R instance = instantiator.createInstance(persistentEntity, parameterValueProvider);
	PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance);

	persistentEntity.doWithProperties(
			(PropertyHandler<SpannerPersistentProperty>) (spannerPersistentProperty) -> {
				if (spannerPersistentProperty.isEmbedded()) {
					accessor.setProperty(spannerPersistentProperty,
							read(spannerPersistentProperty.getType(), source,
									includeColumns, allowMissingColumns));
				}
				else {
					if (!shouldSkipProperty(structAccessor, spannerPersistentProperty,
							includeColumns, readAllColumns, allowMissingColumns,
							persistenceConstructor)) {

						Object value = propertyValueProvider
								.getPropertyValue(spannerPersistentProperty);
						accessor.setProperty(spannerPersistentProperty, value);
					}
				}
			});

	return instance;
}
 
Example 9
/**
 * 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();
}
 
Example 10
Source Project: spring-data-crate   Source File: MappingCrateConverter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Read an incoming {@link CrateDocument} into the target entity.
 *
 * @param entity the target entity.
 * @param source the document to convert.
 * @param parent an optional parent object.
 * @param <R> the entity type.
 * @return the converted entity.
 */
@SuppressWarnings("unchecked")
protected <R> R read(final CratePersistentEntity<R> entity, final CrateDocument source, final Object parent) {
	
	final DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(source, spELContext);
	
    ParameterValueProvider<CratePersistentProperty> provider = getParameterProvider(entity, source, evaluator, parent);
    
    EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);

    R instance = instantiator.createInstance(entity, provider);
    final PersistentPropertyAccessor propertyAccessor = entity.getPropertyAccessor(instance);
    final R result = (R)propertyAccessor.getBean();
    final CratePersistentProperty idProperty = entity.getIdProperty();
    final CratePersistentProperty versionProperty = entity.getVersionProperty();
    
    if(entity.hasIdProperty()) {
    	Object idValue = getValueInternal(idProperty, source, result);
    	propertyAccessor.setProperty(idProperty, idValue);
    }
    
    if(entity.hasVersionProperty()) {
    	Object versionValue = getValueInternal(versionProperty, source, result);
    	propertyAccessor.setProperty(versionProperty, versionValue);
    }
    
    for(CratePersistentProperty property : entity.getPersistentProperties()) {
    	// skip id and version properties as they may have potentially been set above.  
		if((idProperty != null && idProperty.equals(property)) || (versionProperty != null && versionProperty.equals(property))) {
			continue;
		}
		
		if(!source.containsKey(property.getFieldName()) || entity.isConstructorArgument(property)) {
			continue;
		}
		
		propertyAccessor.setProperty(property, getValueInternal(property, source, result));
    }
    
    entity.doWithAssociations(new AssociationHandler<CratePersistentProperty>() {
    	
      @Override
      public void doWithAssociation(final Association<CratePersistentProperty> association) {	    	  
    	  CratePersistentProperty inverseProp = association.getInverse();
    	  Object obj = getValueInternal(inverseProp, source, result);
    	  propertyAccessor.setProperty(inverseProp, obj);
      }	      
    });

    return result;
  }
 
Example 11
Source Project: spring-data-crate   Source File: MappingCrateConverter.java    License: Apache License 2.0 4 votes vote down vote up
public ConverterAwareSpELExpressionParameterValueProvider(final SpELExpressionEvaluator evaluator, final ConversionService conversionService, 
														  final ParameterValueProvider<CratePersistentProperty> delegate, final Object parent) {
  super(evaluator, conversionService, delegate);
  this.parent = parent;
}
 
Example 12
Source Project: spring-data-crate   Source File: MappingCrateConverter.java    License: Apache License 2.0 3 votes vote down vote up
/**
   * Creates a new parameter provider.
   *
   * @param entity the persistent entity.
   * @param source the source document.
   * @param evaluator the SPEL expression evaluator.
   * @param parent the optional parent.
   * @return a new parameter value provider.
   */
private ParameterValueProvider<CratePersistentProperty> getParameterProvider(final CratePersistentEntity<?> entity, final CrateDocument source, 
																			 final DefaultSpELExpressionEvaluator evaluator, final Object parent) {
	
    CratePropertyValueProvider provider = new CratePropertyValueProvider(source, evaluator, parent);
    
    PersistentEntityParameterValueProvider<CratePersistentProperty> parameterProvider = new PersistentEntityParameterValueProvider<>(entity, provider, parent);

    return new ConverterAwareSpELExpressionParameterValueProvider(evaluator, conversionService, parameterProvider, parent);
}