Java Code Examples for org.springframework.data.mapping.PreferredConstructor

The following examples show how to use org.springframework.data.mapping.PreferredConstructor. 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
private ConstructorExpression<?> getConstructorExpression(Class<?> type, RelationalPath<?> pathBase) {
    PreferredConstructor<?, ?> constructor = PreferredConstructorDiscoverer.discover(type);

    if (constructor == null) {
        throw new IllegalArgumentException(
                "Could not discover preferred constructor for " + type);
    }

    Map<String, Path<?>> columnNameToColumn = pathBase.getColumns()
                                                      .stream()
                                                      .collect(Collectors.toMap(
                                                              column -> column.getMetadata().getName(),
                                                              Function.identity()));

    Path<?>[] paths = constructor.getParameters()
                                 .stream()
                                 .map(PreferredConstructor.Parameter::getName)
                                 .map(columnNameToColumn::get)
                                 .toArray(Path[]::new);

    return Projections.constructor(type, paths);
}
 
Example 3
private boolean shouldSkipProperty(StructAccessor struct,
		SpannerPersistentProperty spannerPersistentProperty,
		Set<String> includeColumns,
		boolean readAllColumns,
		boolean allowMissingColumns,
		PreferredConstructor<?, SpannerPersistentProperty> persistenceConstructor) {
	String columnName = spannerPersistentProperty.getColumnName();
	boolean notRequiredByPartialRead = !readAllColumns && !includeColumns.contains(columnName);


	return spannerPersistentProperty.isLazyInterleaved() ||
			notRequiredByPartialRead
			|| isMissingColumn(struct, allowMissingColumns, columnName)
			|| struct.isNull(columnName)
			|| persistenceConstructor.isConstructorParameter(spannerPersistentProperty);
}
 
Example 4
@Override
public <T> T getParameterValue(PreferredConstructor.Parameter<T, RelationalPersistentProperty> parameter) {
    RelationalPersistentProperty property = entity.getRequiredPersistentProperty(parameter.getName());
    Object value = row.getValue(property.getColumnName());
    Class<T> type = parameter.getRawType();

    return convertIfNecessary(value, type);
}
 
Example 5
/**
 * 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 6
/**
 * 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();
}