use of org.springframework.data.mapping.PropertyHandler in project spring-data-mongodb by spring-projects.
the class MongoExampleMapper method getMappedPropertyPath.
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, ".");
}
use of org.springframework.data.mapping.PropertyHandler in project commons-dao by reportportal.
the class ReportPortalRepositoryImpl method partialUpdate.
@Override
public void partialUpdate(T t) {
ID id = getEntityInformation().getId(t);
if (null == id) {
throw new IllegalArgumentException("ID property should not be null");
}
Update update = new Update();
final MongoPersistentEntity<?> persistentEntity = mongoOperations.getConverter().getMappingContext().getPersistentEntity(getEntityInformation().getJavaType());
persistentEntity.doWithProperties((PropertyHandler<MongoPersistentProperty>) persistentProperty -> {
if (!persistentEntity.isIdProperty(persistentProperty)) {
Object value = Accessible.on(t).field(persistentProperty.getField()).getValue();
if (null != value) {
update.set(persistentProperty.getFieldName(), value);
}
}
});
WriteResult writeResult = mongoOperations.updateFirst(query(where(persistentEntity.getIdProperty().getFieldName()).is(id)), update, getEntityInformation().getCollectionName());
if (1 != writeResult.getN()) {
throw new IncorrectResultSizeDataAccessException(1, writeResult.getN());
}
}
use of org.springframework.data.mapping.PropertyHandler in project spring-data-mongodb by spring-projects.
the class MongoPersistentEntityIndexResolver method resolveIndexForEntity.
/**
* Resolve the {@link IndexDefinition}s for given {@literal root} entity by traversing {@link MongoPersistentProperty}
* scanning for index annotations {@link Indexed}, {@link CompoundIndex} and {@link GeospatialIndex}. The given
* {@literal root} has therefore to be annotated with {@link Document}.
*
* @param root must not be null.
* @return List of {@link IndexDefinitionHolder}. Will never be {@code null}.
* @throws IllegalArgumentException in case of missing {@link Document} annotation marking root entities.
*/
public List<IndexDefinitionHolder> resolveIndexForEntity(final MongoPersistentEntity<?> root) {
Assert.notNull(root, "Index cannot be resolved for given 'null' entity.");
Document document = root.findAnnotation(Document.class);
Assert.notNull(document, "Given entity is not collection root.");
final List<IndexDefinitionHolder> indexInformation = new ArrayList<>();
indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root));
indexInformation.addAll(potentiallyCreateTextIndexDefinition(root));
root.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> this.potentiallyAddIndexForProperty(root, property, indexInformation, new CycleGuard()));
indexInformation.addAll(resolveIndexesForDbrefs("", root.getCollection(), root));
return indexInformation;
}
use of org.springframework.data.mapping.PropertyHandler in project spring-data-jdbc by spring-projects.
the class DefaultDataAccessStrategy method getPropertyMap.
private <S> MapSqlParameterSource getPropertyMap(final S instance, JdbcPersistentEntity<S> persistentEntity) {
MapSqlParameterSource parameters = new MapSqlParameterSource();
persistentEntity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) property -> {
if (!property.isEntity()) {
Object value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
Object convertedValue = convert(value, property.getColumnType());
parameters.addValue(property.getColumnName(), convertedValue, JdbcUtil.sqlTypeFor(property.getColumnType()));
}
});
return parameters;
}
use of org.springframework.data.mapping.PropertyHandler in project spring-cloud-gcp by spring-cloud.
the class MappingSpannerReadConverter method read.
/**
* Reads a single POJO from a Spanner row.
* @param type the type of POJO
* @param source the Spanner row
* @param includeColumns the columns to read. If null then all columns will be read.
* @param <R> the type of the POJO.
* @return the POJO
*/
public <R> R read(Class<R> type, Struct source, Set<String> includeColumns) {
boolean readAllColumns = includeColumns == null;
R object = instantiate(type);
SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(type);
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(object);
persistentEntity.doWithProperties((PropertyHandler<SpannerPersistentProperty>) spannerPersistentProperty -> {
String columnName = spannerPersistentProperty.getColumnName();
try {
if ((!readAllColumns && !includeColumns.contains(columnName)) || source.isNull(columnName)) {
return;
}
} catch (IllegalArgumentException e) {
throw new SpannerDataException("Unable to read column from Spanner results: " + columnName, e);
}
Class propType = spannerPersistentProperty.getType();
boolean valueSet;
if (ConversionUtils.isIterableNonByteArrayType(propType)) {
valueSet = attemptReadIterableValue(spannerPersistentProperty, source, columnName, accessor);
} else {
Class sourceType = this.spannerColumnTypeToJavaTypeMapping.get(source.getColumnType(columnName));
if (sourceType != null && canConvert(sourceType, propType)) {
valueSet = attemptReadSingleItemValue(spannerPersistentProperty, source, sourceType, columnName, accessor);
} else {
valueSet = false;
}
}
if (!valueSet) {
throw new SpannerDataException(String.format("The value in column with name %s" + " could not be converted to the corresponding property in the entity." + " The property's type is %s.", columnName, propType));
}
});
return object;
}
Aggregations