use of io.micronaut.data.model.runtime.RuntimePersistentProperty in project micronaut-data by micronaut-projects.
the class PersistentPropertyPath method getPropertyValue.
/**
* Gets property path value.
* (Only possible for runtime properties)
*
* @param bean The root bean
* @return The value
*/
public Object getPropertyValue(Object bean) {
if (!(property instanceof RuntimePersistentProperty)) {
throw new IllegalStateException("Expected runtime property!");
}
Object value = bean;
for (Association association : associations) {
RuntimePersistentProperty<?> property = (RuntimePersistentProperty) association;
BeanProperty beanProperty = property.getProperty();
value = beanProperty.get(value);
if (value == null) {
return null;
}
}
RuntimePersistentProperty<?> p = (RuntimePersistentProperty<?>) property;
if (value != null) {
BeanProperty beanProperty = p.getProperty();
value = beanProperty.get(value);
}
return value;
}
use of io.micronaut.data.model.runtime.RuntimePersistentProperty in project micronaut-data by micronaut-projects.
the class PersistentPropertyPath method setProperty.
private Object setProperty(List<Association> associations, RuntimePersistentProperty property, Object bean, Object value) {
if (associations.isEmpty()) {
BeanProperty beanProperty = property.getProperty();
return setProperty(beanProperty, bean, value);
}
Association association = associations.iterator().next();
RuntimePersistentProperty<?> p = (RuntimePersistentProperty) association;
BeanProperty beanProperty = p.getProperty();
Object prevBean = beanProperty.get(bean);
Object newBean = setProperty(associations.subList(1, associations.size()), property, prevBean, value);
if (prevBean != newBean) {
return setProperty(beanProperty, bean, newBean);
}
return bean;
}
use of io.micronaut.data.model.runtime.RuntimePersistentProperty in project micronaut-data by micronaut-projects.
the class AbstractSqlRepositoryOperations method idPropertiesWithValues.
private Stream<Map.Entry<PersistentProperty, Object>> idPropertiesWithValues(PersistentProperty property, Object value) {
Object propertyValue = ((RuntimePersistentProperty) property).getProperty().get(value);
if (property instanceof Embedded) {
Embedded embedded = (Embedded) property;
PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
return embeddedEntity.getPersistentProperties().stream().flatMap(prop -> idPropertiesWithValues(prop, propertyValue));
} else if (property instanceof Association) {
Association association = (Association) property;
if (association.isForeignKey()) {
return Stream.empty();
}
PersistentEntity associatedEntity = association.getAssociatedEntity();
PersistentProperty identity = associatedEntity.getIdentity();
if (identity == null) {
throw new IllegalStateException("Identity cannot be missing for: " + associatedEntity);
}
return idPropertiesWithValues(identity, propertyValue);
}
return Stream.of(new AbstractMap.SimpleEntry<>(property, propertyValue));
}
use of io.micronaut.data.model.runtime.RuntimePersistentProperty in project micronaut-data by micronaut-projects.
the class AbstractRepositoryOperations method getIdReader.
/**
* Obtain an ID reader for the given object.
*
* @param o The object
* @return The ID reader
*/
@NonNull
protected final RuntimePersistentProperty<Object> getIdReader(@NonNull Object o) {
Class<Object> type = (Class<Object>) o.getClass();
RuntimePersistentProperty beanProperty = idReaders.get(type);
if (beanProperty == null) {
RuntimePersistentEntity<Object> entity = getEntity(type);
RuntimePersistentProperty<Object> identity = entity.getIdentity();
if (identity == null) {
throw new DataAccessException("Entity has no ID: " + entity.getName());
}
beanProperty = identity;
idReaders.put(type, beanProperty);
}
return beanProperty;
}
use of io.micronaut.data.model.runtime.RuntimePersistentProperty in project micronaut-data by micronaut-projects.
the class PreparedQueryDBOperation method setParameters.
@Override
public <K, Cnt, PS> void setParameters(OpContext<Cnt, PS> context, Cnt connection, PS stmt, RuntimePersistentEntity<K> persistentEntity, K entity, Map<QueryParameterBinding, Object> previousValues) {
int index = context.shiftIndex(0);
Object[] parameterArray = preparedQuery.getParameterArray();
Argument[] parameterArguments = preparedQuery.getArguments();
for (QueryParameterBinding queryParameterBinding : preparedQuery.getQueryBindings()) {
Class<?> parameterConverter = queryParameterBinding.getParameterConverterClass();
Object value;
if (queryParameterBinding.getParameterIndex() != -1) {
value = resolveParameterValue(queryParameterBinding, parameterArray);
} else if (queryParameterBinding.isAutoPopulated()) {
String[] propertyPath = queryParameterBinding.getRequiredPropertyPath();
PersistentPropertyPath pp = persistentEntity.getPropertyPath(propertyPath);
if (pp == null) {
throw new IllegalStateException("Cannot find auto populated property: " + String.join(".", propertyPath));
}
RuntimePersistentProperty<?> persistentProperty = (RuntimePersistentProperty) pp.getProperty();
Object previousValue = null;
QueryParameterBinding previousPopulatedValueParameter = queryParameterBinding.getPreviousPopulatedValueParameter();
if (previousPopulatedValueParameter != null) {
if (previousPopulatedValueParameter.getParameterIndex() == -1) {
throw new IllegalStateException("Previous value parameter cannot be bind!");
}
previousValue = resolveParameterValue(previousPopulatedValueParameter, parameterArray);
}
value = context.getRuntimeEntityRegistry().autoPopulateRuntimeProperty(persistentProperty, previousValue);
value = context.convert(connection, value, persistentProperty);
parameterConverter = null;
} else {
throw new IllegalStateException("Invalid query [" + query + "]. Unable to establish parameter value for parameter at position: " + (index + 1));
}
DataType dataType = queryParameterBinding.getDataType();
List<Object> values = queryParameterBinding.isExpandable() ? expandValue(value, dataType) : Collections.singletonList(value);
if (values != null && values.isEmpty()) {
// Empty collections / array should always set at least one value
value = null;
values = null;
}
if (values == null) {
if (parameterConverter != null) {
int parameterIndex = queryParameterBinding.getParameterIndex();
Argument<?> argument = parameterIndex > -1 ? parameterArguments[parameterIndex] : null;
value = context.convert(parameterConverter, connection, value, argument);
}
context.setStatementParameter(stmt, index++, dataType, value, dialect);
} else {
for (Object v : values) {
if (parameterConverter != null) {
int parameterIndex = queryParameterBinding.getParameterIndex();
Argument<?> argument = parameterIndex > -1 ? parameterArguments[parameterIndex] : null;
v = context.convert(parameterConverter, connection, v, argument);
}
context.setStatementParameter(stmt, index++, dataType, v, dialect);
}
}
}
}
Aggregations