use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.
the class ViewMapper method createAccessor.
private ObjectMapper createAccessor(ManagedViewType<S> sourceType, ManagedViewType<T> targetType, boolean ignoreMissing, EntityViewKindMapping entityViewKindMapping, EntityViewManager entityViewManager, ProxyFactory proxyFactory, MethodAttribute<? super T, ?> targetAttribute, String prefix, Map<String, Key<Object, Object>> subMappers) {
String newPrefix;
if (prefix.isEmpty()) {
newPrefix = targetAttribute.getName();
} else {
newPrefix = prefix + "." + targetAttribute.getName();
}
Type<?> attributeType;
MappingConstructorImpl<?> constructor = null;
Boolean maybeMarkNew = entityViewKindMapping == EntityViewKindMapping.MARK_NEW ? null : false;
Key<Object, Object> subMapperKey = subMappers.get(newPrefix);
if (subMapperKey == Key.EXCLUDE_MARKER) {
return null;
} else if (subMapperKey != null) {
ignoreMissing = subMapperKey.ignoreMissing;
maybeMarkNew = subMapperKey.markNew;
}
// Try to find a source attribute
MethodAttribute<?, ?> sourceAttribute;
AttributeAccessor accessor;
if (sourceType == null) {
sourceAttribute = null;
if (targetAttribute.getMappingType() == Attribute.MappingType.PARAMETER) {
return new ParameterObjectMapper(((MappingAttribute<?, ?>) targetAttribute).getMapping());
}
accessor = Accessors.forEntityMapping((EntityViewManagerImpl) entityViewManager, targetAttribute);
if (accessor == null) {
if (ignoreMissing) {
return null;
}
throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type is missing in source type!", targetType);
}
} else {
sourceAttribute = sourceType.getAttribute(targetAttribute.getName());
if (sourceAttribute == null) {
if (targetAttribute.getMappingType() == Attribute.MappingType.PARAMETER) {
return new ParameterObjectMapper(((MappingAttribute<?, ?>) targetAttribute).getMapping());
}
// Optionally ignore missing attributes
if (ignoreMissing) {
return null;
}
throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type is missing in source type!", sourceType, targetType);
}
accessor = Accessors.forViewAttribute(null, sourceAttribute, true);
}
// Handle conversion from one type to another
if (targetAttribute.isCollection()) {
if ((sourceAttribute != null) && (targetAttribute.getConvertedJavaType() != sourceAttribute.getConvertedJavaType())) {
throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type has a different plural type than in source type!", sourceType, targetType);
}
PluralAttribute<?, ?, ?> targetPluralAttr = (PluralAttribute<?, ?, ?>) targetAttribute;
Type<?> elementType = (sourceAttribute == null) ? null : ((PluralAttribute<?, ?, ?>) sourceAttribute).getElementType();
ViewMapper<Object, Object> valueMapper = null;
attributeType = targetPluralAttr.getElementType();
if (subMapperKey != null) {
attributeType = subMapperKey.targetType;
constructor = subMapperKey.targetConstructor;
}
if (targetAttribute.isSubview()) {
valueMapper = createViewMapper(elementType, attributeType, constructor, ignoreMissing, maybeMarkNew, entityViewManager, proxyFactory, newPrefix, subMappers);
} else if ((sourceType != null) && (targetPluralAttr.getElementType() != elementType)) {
throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type has a different element type than in source type!", sourceType, targetType);
}
boolean needsDirtyTracker = ((AbstractAttribute<?, ?>) targetAttribute).needsDirtyTracker();
if (targetPluralAttr.getCollectionType() == PluralAttribute.CollectionType.MAP) {
MapAttribute<?, ?, ?> targetMapAttr = (MapAttribute<?, ?, ?>) targetAttribute;
Type<?> keyType = (sourceAttribute == null) ? null : ((MapAttribute<?, ?, ?>) sourceAttribute).getKeyType();
ViewMapper<Object, Object> keyMapper = null;
if (targetMapAttr.isKeySubview()) {
String newKeyPrefix = "KEY(" + newPrefix + ")";
Key<Object, Object> keySubMapperKey = subMappers.get(newKeyPrefix);
if (keySubMapperKey == Key.EXCLUDE_MARKER) {
keyMapper = null;
} else {
constructor = null;
Type<?> keyTargetType = targetMapAttr.getKeyType();
Boolean maybeMarkNewKey = entityViewKindMapping == EntityViewKindMapping.MARK_NEW ? null : false;
if (subMapperKey != null) {
constructor = keySubMapperKey.targetConstructor;
keyTargetType = keySubMapperKey.targetType;
ignoreMissing = keySubMapperKey.ignoreMissing;
maybeMarkNewKey = keySubMapperKey.markNew;
}
keyMapper = createViewMapper(keyType, keyTargetType, constructor, ignoreMissing, maybeMarkNewKey, entityViewManager, proxyFactory, newPrefix, subMappers);
}
} else if ((sourceType != null) && (targetMapAttr.getKeyType() != keyType)) {
throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type has a different key type than in source type!", sourceType, targetType);
}
MapInstantiatorImplementor<?, ?> mapInstantiator = ((AbstractAttribute<?, ?>) targetAttribute).getMapInstantiator();
return new MapObjectMapper(accessor, needsDirtyTracker, entityViewKindMapping != EntityViewKindMapping.MARK_NEW, mapInstantiator, keyMapper, valueMapper);
} else {
CollectionInstantiatorImplementor<?, ?> collectionInstantiator = ((AbstractAttribute<?, ?>) targetAttribute).getCollectionInstantiator();
return new CollectionObjectMapper(accessor, needsDirtyTracker, entityViewKindMapping != EntityViewKindMapping.MARK_NEW, collectionInstantiator, valueMapper);
}
} else if (targetAttribute.isSubview()) {
attributeType = ((SingularAttribute<?, ?>) targetAttribute).getType();
if (subMapperKey != null) {
attributeType = subMapperKey.targetType;
constructor = subMapperKey.targetConstructor;
}
Type<?> type = (sourceAttribute == null) ? null : ((SingularAttribute<?, ?>) sourceAttribute).getType();
ViewMapper<Object, Object> mapper = createViewMapper(type, attributeType, constructor, ignoreMissing, maybeMarkNew, entityViewManager, proxyFactory, newPrefix, subMappers);
return new AttributeObjectMapper(accessor, mapper);
} else if ((sourceAttribute != null) && (targetAttribute.getConvertedJavaType() != sourceAttribute.getConvertedJavaType())) {
throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type has a different type than in source type!", sourceType, targetType);
} else {
return new PassthroughObjectMapper(accessor);
}
}
use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.
the class EntityViewBuilderBaseImpl method withSetBuilder.
private <E> EntityViewNestedBuilder<E, ? extends BuilderType, ?> withSetBuilder(AbstractAttribute<?, ?> attr) {
checkAttribute(attr, SetAttribute.class, "Set");
checkType(attr.getElementType(), "element", attr);
Map<ManagedViewType<? extends E>, String> inheritanceSubtypeMappings = ((PluralAttribute<?, ?, E>) attr).getElementInheritanceSubtypeMappings();
return new EntityViewNestedBuilderImpl<>(evm, (ManagedViewTypeImplementor<E>) attr.getElementType(), inheritanceSubtypeMappings, optionalParameters, (BuilderType) this, new CollectionEntityViewBuilderListener(getCollection(attr)));
}
use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.
the class EntityViewBuilderBaseImpl method withBuilder.
@Override
public <K, V> EntityViewNestedBuilder<V, ? extends BuilderType, ?> withBuilder(MapAttribute<ViewType, K, V> attribute, K key) {
if (key == null) {
throw new IllegalArgumentException("Illegal null key!");
}
AbstractAttribute<?, ?> attr = getAttribute(attribute);
checkAttribute(attr, MapAttribute.class, "Map");
Type<?> keyType = ((MapAttribute<?, ?, ?>) attr).getKeyType();
checkType(attr.getElementType(), "element", attr);
key = getValue(keyType, key);
Map<ManagedViewType<? extends V>, String> inheritanceSubtypeMappings = ((PluralAttribute<?, ?, V>) attr).getElementInheritanceSubtypeMappings();
return new EntityViewNestedBuilderImpl<>(evm, (ManagedViewTypeImplementor<V>) attr.getElementType(), inheritanceSubtypeMappings, optionalParameters, (BuilderType) this, new MapEntityViewBuilderListener(getMap(attr), key));
}
use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.
the class EntityViewBuilderBaseImpl method withListBuilder.
public <E> EntityViewNestedBuilder<E, ? extends BuilderType, ?> withListBuilder(AbstractAttribute<?, ?> attr, int index) {
checkAttribute(attr, ListAttribute.class, "List");
checkType(attr.getElementType(), "element", attr);
Map<ManagedViewType<? extends E>, String> inheritanceSubtypeMappings = ((PluralAttribute<?, ?, E>) attr).getElementInheritanceSubtypeMappings();
return new EntityViewNestedBuilderImpl<>(evm, (ManagedViewTypeImplementor<E>) attr.getElementType(), inheritanceSubtypeMappings, optionalParameters, (BuilderType) this, new ListEntityViewBuilderListener((List<Object>) getCollection(attr), index));
}
use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.
the class EntityViewBuilderBaseImpl method withBuilder.
@Override
public <E> EntityViewNestedBuilder<E, ? extends BuilderType, ?> withBuilder(ListAttribute<ViewType, E> attribute) {
AbstractAttribute<?, ?> attr = getAttribute(attribute);
checkType(attr.getElementType(), "element", attr);
Map<ManagedViewType<? extends E>, String> inheritanceSubtypeMappings = ((PluralAttribute<?, ?, E>) attr).getElementInheritanceSubtypeMappings();
return new EntityViewNestedBuilderImpl<>(evm, (ManagedViewTypeImplementor<E>) attr.getElementType(), inheritanceSubtypeMappings, optionalParameters, (BuilderType) this, new CollectionEntityViewBuilderListener(getCollection(attr)));
}
Aggregations