Search in sources :

Example 11 with ManagedViewTypeImplementor

use of com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor in project blaze-persistence by Blazebit.

the class ProxyFactory method createToString.

private CtMethod createToString(ManagedViewTypeImplementor<?> managedViewType, CtClass cc, boolean idBased, CtField[] fields) throws NotFoundException, CannotCompileException {
    ConstPool cp = cc.getClassFile2().getConstPool();
    MethodInfo method = new MethodInfo(cp, "toString", "()" + Descriptor.of("java.lang.String"));
    method.setAccessFlags(AccessFlag.PUBLIC);
    CtMethod m = CtMethod.make(method, cc);
    StringBuilder sb = new StringBuilder();
    sb.append('{');
    if (idBased) {
        ViewTypeImplementor<?> viewType = (ViewTypeImplementor<?>) managedViewType;
        MethodAttribute<?, ?> idAttribute = viewType.getIdAttribute();
        sb.append("\treturn \"").append(managedViewType.getJavaType().getSimpleName()).append("(").append(idAttribute.getName()).append(" = \" + $0.").append(idAttribute.getName()).append(" + \")\";\n");
    } else {
        int sizeEstimate = managedViewType.getJavaType().getSimpleName().length() + 2;
        for (int i = 0; i < fields.length; i++) {
            // 5 is the amount of chars for the format
            // 10 is the amount of chars that we assume is needed to represent a value on average
            sizeEstimate += fields[i].getName().length() + 5 + 10;
        }
        sb.append("\tStringBuilder sb = new StringBuilder(").append(sizeEstimate).append(");\n");
        sb.append("\tsb.append(\"").append(managedViewType.getJavaType().getSimpleName()).append("(\");\n");
        if (fields.length != 0) {
            sb.append("\tsb.append(\"").append(fields[0].getName()).append(" = \").append($0.").append(fields[0].getName()).append(");\n");
            for (int i = 1; i < fields.length; i++) {
                sb.append("\tsb.append(\", \");\n");
                sb.append("\tsb.append(\"").append(fields[i].getName()).append(" = \").append($0.").append(fields[i].getName()).append(");\n");
            }
        }
        sb.append("\tsb.append(\")\");\n");
        sb.append("\treturn sb.toString();\n");
    }
    sb.append('}');
    m.setBody(sb.toString());
    return m;
}
Also used : ConstPool(javassist.bytecode.ConstPool) ViewTypeImplementor(com.blazebit.persistence.view.impl.metamodel.ViewTypeImplementor) ManagedViewTypeImplementor(com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor) MethodInfo(javassist.bytecode.MethodInfo) CtMethod(javassist.CtMethod)

Example 12 with ManagedViewTypeImplementor

use of com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor in project blaze-persistence by Blazebit.

the class EntityViewManagerImpl method getChangeModel.

@Override
@SuppressWarnings("unchecked")
public <T> SingularChangeModel<T> getChangeModel(T entityView) {
    if (!(entityView instanceof DirtyStateTrackable)) {
        throw new IllegalArgumentException("Change model can only be accessed for updatable entity views that use dirty tracking! Switch to the LAZY or PARTIAL FlushMode instead!");
    }
    DirtyStateTrackable updatableProxy = (DirtyStateTrackable) entityView;
    Class<?> entityViewClass = updatableProxy.$$_getEntityViewClass();
    ManagedViewTypeImplementor<DirtyStateTrackable> viewType = (ManagedViewTypeImplementor<DirtyStateTrackable>) metamodel.managedViewOrError(entityViewClass);
    EntityViewUpdater updater = getUpdater(null, viewType, null, null, null);
    return (SingularChangeModel<T>) new ViewChangeModel<>(viewType, updatableProxy, updater.getDirtyChecker());
}
Also used : DirtyStateTrackable(com.blazebit.persistence.view.spi.type.DirtyStateTrackable) SingularChangeModel(com.blazebit.persistence.view.change.SingularChangeModel) ManagedViewTypeImplementor(com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor) EntityViewUpdater(com.blazebit.persistence.view.impl.update.EntityViewUpdater)

Example 13 with ManagedViewTypeImplementor

use of com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor in project blaze-persistence by Blazebit.

the class Mappers method forViewConvertToViewAttributeMapping.

public static <S, T> Mapper<S, T> forViewConvertToViewAttributeMapping(EntityViewManagerImpl evm, ViewType<S> sourceViewType, ViewType<T> targetViewType, String mappedBy, Mapper<S, T> additionalMapper) {
    List<Mapper<S, T>> mappers = new ArrayList<>();
    AttributeAccessor entityAccessor = Accessors.forEntityMapping(evm, sourceViewType.getEntityClass(), ((MappingAttribute<?, ?>) sourceViewType.getIdAttribute()).getMapping());
    ExpressionFactory ef = evm.getCriteriaBuilderFactory().getService(ExpressionFactory.class);
    for (MethodAttribute<?, ?> attribute : targetViewType.getAttributes()) {
        if (attribute.isUpdatable() && attribute instanceof MappingAttribute<?, ?> && attribute instanceof SingularAttribute<?, ?>) {
            if (mappedBy.equals(((MappingAttribute) attribute).getMapping())) {
                ViewType<?> attributeType = (ViewType<?>) ((SingularAttribute<?, ?>) attribute).getType();
                Type<?> attributeViewIdType = ((SingularAttribute<?, ?>) attributeType.getIdAttribute()).getType();
                EntityTupleizer entityTupleizer = null;
                ObjectBuilder<?> idViewBuilder = null;
                if (attributeViewIdType instanceof ManagedViewType<?>) {
                    entityTupleizer = new DefaultEntityTupleizer(evm, (ManagedViewType<?>) attributeViewIdType);
                    idViewBuilder = (ObjectBuilder<Object>) evm.getTemplate(new MacroConfigurationExpressionFactory(ef, ef.getDefaultMacroConfiguration()), (ManagedViewTypeImplementor<?>) attributeViewIdType, null, null, new MutableViewJpqlMacro(), null, new MutableEmbeddingViewJpqlMacro(), 0).createObjectBuilder(null, null, null, 0, false, false);
                }
                mappers.add(new ReferenceViewAttributeMapper<S, T>(evm, entityAccessor, attributeType.getJavaType(), entityTupleizer, Accessors.forMutableViewAttribute(evm, attribute), idViewBuilder));
            }
        }
    }
    if (mappers.isEmpty()) {
        return additionalMapper;
    }
    if (additionalMapper != null) {
        mappers.add(additionalMapper);
    }
    return new CompositeMapper<>(mappers.toArray(new Mapper[mappers.size()]));
}
Also used : DefaultEntityTupleizer(com.blazebit.persistence.view.impl.update.DefaultEntityTupleizer) EntityTupleizer(com.blazebit.persistence.view.impl.entity.EntityTupleizer) ArrayList(java.util.ArrayList) MappingAttribute(com.blazebit.persistence.view.metamodel.MappingAttribute) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) DefaultEntityTupleizer(com.blazebit.persistence.view.impl.update.DefaultEntityTupleizer) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) ManagedViewTypeImplementor(com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) SingularAttribute(com.blazebit.persistence.view.metamodel.SingularAttribute) AttributeAccessor(com.blazebit.persistence.view.impl.accessor.AttributeAccessor) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ViewType(com.blazebit.persistence.view.metamodel.ViewType)

Aggregations

ManagedViewTypeImplementor (com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor)13 ManagedViewType (com.blazebit.persistence.view.metamodel.ManagedViewType)8 ViewType (com.blazebit.persistence.view.metamodel.ViewType)8 MapAttribute (com.blazebit.persistence.view.metamodel.MapAttribute)6 ViewTypeImplementor (com.blazebit.persistence.view.impl.metamodel.ViewTypeImplementor)5 MappingAttribute (com.blazebit.persistence.view.metamodel.MappingAttribute)5 PluralAttribute (com.blazebit.persistence.view.metamodel.PluralAttribute)5 AttributeAccessor (com.blazebit.persistence.view.impl.accessor.AttributeAccessor)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 EntityMetamodel (com.blazebit.persistence.parser.EntityMetamodel)3 ExtendedManagedType (com.blazebit.persistence.spi.ExtendedManagedType)3 EmbeddableUpdaterBasedViewToEntityMapper (com.blazebit.persistence.view.impl.entity.EmbeddableUpdaterBasedViewToEntityMapper)3 LoadOrPersistViewToEntityMapper (com.blazebit.persistence.view.impl.entity.LoadOrPersistViewToEntityMapper)3 UpdaterBasedViewToEntityMapper (com.blazebit.persistence.view.impl.entity.UpdaterBasedViewToEntityMapper)3 ViewToEntityMapper (com.blazebit.persistence.view.impl.entity.ViewToEntityMapper)3 AbstractMethodAttribute (com.blazebit.persistence.view.impl.metamodel.AbstractMethodAttribute)3 CorrelationMultisetTupleElementMapper (com.blazebit.persistence.view.impl.objectbuilder.mapper.CorrelationMultisetTupleElementMapper)3 BasicType (com.blazebit.persistence.view.metamodel.BasicType)3 Expression (com.blazebit.persistence.parser.expression.Expression)2