Search in sources :

Example 1 with DescriptorEventAdapter

use of org.eclipse.persistence.descriptors.DescriptorEventAdapter in project eclipselink by eclipse-ee4j.

the class ObjectPersistenceRuntimeXMLProject method buildFunctionExpressionDescriptor.

protected ClassDescriptor buildFunctionExpressionDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(FunctionExpression.class);
    descriptor.setDefaultRootElement("function-expression");
    descriptor.getInheritancePolicy().setParentClass(Expression.class);
    // A function's base is always its first child so not persisted,
    // Is fixed up to be its first child or new expression builder if no children.
    // Child value expressions need their backpointer to their local base set,
    // this is not persisted so must be hooked back up after loading.
    descriptor.getEventManager().addListener(new DescriptorEventAdapter() {

        @Override
        public void postBuild(DescriptorEvent event) {
            FunctionExpression expression = (FunctionExpression) event.getObject();
            for (int index = 0; index < expression.getChildren().size(); index++) {
                Expression child = expression.getChildren().get(index);
                if (child.isValueExpression()) {
                    child.setLocalBase(new ExpressionBuilder());
                }
            }
            if (expression.getChildren().size() > 0) {
                expression.setBaseExpression(expression.getChildren().get(0));
            } else {
                expression.setBaseExpression(new ExpressionBuilder());
            }
        }
    });
    XMLDirectMapping operatorMapping = new XMLDirectMapping();
    operatorMapping.setAttributeName("operator");
    ExpressionOperatorConverter operatorConverter = new ExpressionOperatorConverter();
    operatorConverter.addConversionValue("like", ExpressionOperator.getOperator(ExpressionOperator.Like));
    operatorConverter.addConversionValue("notLike", ExpressionOperator.getOperator(ExpressionOperator.NotLike));
    operatorConverter.addConversionValue("not", ExpressionOperator.getOperator(ExpressionOperator.Not));
    operatorConverter.addConversionValue("isNull", ExpressionOperator.getOperator(ExpressionOperator.IsNull));
    operatorConverter.addConversionValue("notNull", ExpressionOperator.getOperator(ExpressionOperator.NotNull));
    operatorConverter.addConversionValue("ascending", ExpressionOperator.getOperator(ExpressionOperator.Ascending));
    operatorConverter.addConversionValue("descending", ExpressionOperator.getOperator(ExpressionOperator.Descending));
    // These are platform specific so not on operator.
    operatorConverter.addConversionValue("upper", new ExpressionOperator(ExpressionOperator.ToUpperCase, new ArrayList<>(0)));
    operatorConverter.addConversionValue("lower", new ExpressionOperator(ExpressionOperator.ToLowerCase, new ArrayList<>(0)));
    // Aggregate functions
    operatorConverter.addConversionValue("count", ExpressionOperator.getOperator(ExpressionOperator.Count));
    operatorConverter.addConversionValue("sum", ExpressionOperator.getOperator(ExpressionOperator.Sum));
    operatorConverter.addConversionValue("average", ExpressionOperator.getOperator(ExpressionOperator.Average));
    operatorConverter.addConversionValue("maximum", ExpressionOperator.getOperator(ExpressionOperator.Maximum));
    operatorConverter.addConversionValue("minimum", ExpressionOperator.getOperator(ExpressionOperator.Minimum));
    // standardDeviation is platform specific.
    operatorConverter.addConversionValue("standardDeviation", new ExpressionOperator(ExpressionOperator.StandardDeviation, new ArrayList<>(0)));
    operatorConverter.addConversionValue("variance", new ExpressionOperator(ExpressionOperator.Variance, new ArrayList<>(0)));
    operatorConverter.addConversionValue("distinct", ExpressionOperator.getOperator(ExpressionOperator.Distinct));
    operatorMapping.setConverter(operatorConverter);
    operatorMapping.setXPath("@function");
    descriptor.addMapping(operatorMapping);
    XMLCompositeCollectionMapping childrenMapping = new XMLCompositeCollectionMapping();
    childrenMapping.useCollectionClass(NonSynchronizedVector.class);
    childrenMapping.setAttributeName("children");
    childrenMapping.setReferenceClass(Expression.class);
    childrenMapping.setXPath(getPrimaryNamespaceXPath() + "arguments/" + getPrimaryNamespaceXPath() + "argument");
    descriptor.addMapping(childrenMapping);
    return descriptor;
}
Also used : ExpressionOperatorConverter(org.eclipse.persistence.internal.expressions.ExpressionOperatorConverter) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) Expression(org.eclipse.persistence.expressions.Expression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) DescriptorEventAdapter(org.eclipse.persistence.descriptors.DescriptorEventAdapter) ArrayList(java.util.ArrayList) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator)

Example 2 with DescriptorEventAdapter

use of org.eclipse.persistence.descriptors.DescriptorEventAdapter in project eclipselink by eclipse-ee4j.

the class EventListenerCollectionTest method setup.

@Override
public void setup() {
    project = new EmployeeProject();
    ClassDescriptor descriptor = project.getDescriptors().get(Employee.class);
    descriptor.getEventManager().addListener(new DescriptorEventAdapter());
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DescriptorEventAdapter(org.eclipse.persistence.descriptors.DescriptorEventAdapter) EmployeeProject(org.eclipse.persistence.testing.models.employee.relational.EmployeeProject)

Example 3 with DescriptorEventAdapter

use of org.eclipse.persistence.descriptors.DescriptorEventAdapter in project eclipselink by eclipse-ee4j.

the class InvalidDescriptorEventCodeTest method test.

@Override
public void test() {
    try {
        DescriptorEventManager eManager = new DescriptorEventManager();
        DescriptorEvent event = new DescriptorEvent(this);
        DescriptorEventAdapter listener = new DescriptorEventAdapter();
        event.setEventCode(-1);
        eManager.addListener(listener);
        eManager.notifyListeners(event);
    } catch (EclipseLinkException exception) {
        caughtException = exception;
    }
}
Also used : DescriptorEventManager(org.eclipse.persistence.descriptors.DescriptorEventManager) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) DescriptorEventAdapter(org.eclipse.persistence.descriptors.DescriptorEventAdapter) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent)

Example 4 with DescriptorEventAdapter

use of org.eclipse.persistence.descriptors.DescriptorEventAdapter in project eclipselink by eclipse-ee4j.

the class AdvancedJPAJunitTest method testRelationshipReadDuringClone.

public void testRelationshipReadDuringClone() {
    EntityManager em = createEntityManager();
    AbstractSession session = getServerSession();
    ClassDescriptor departmentDesc = session.getDescriptor(Department.class);
    DescriptorEventAdapter listener = new DescriptorEventAdapter() {

        @Override
        public void postClone(DescriptorEvent event) {
            ((Department) event.getObject()).getEquipment().size();
        }
    };
    departmentDesc.getDescriptorEventManager().addListener(listener);
    em.createQuery("SELECT e from Equipment e where e.department is not null").getResultList();
    departmentDesc.getDescriptorEventManager().removeListener(listener);
    departmentDesc.getDescriptorEventManager().initialize(session);
    closeEntityManager(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DescriptorEventAdapter(org.eclipse.persistence.descriptors.DescriptorEventAdapter) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 5 with DescriptorEventAdapter

use of org.eclipse.persistence.descriptors.DescriptorEventAdapter in project eclipselink by eclipse-ee4j.

the class ObjectPersistenceRuntimeXMLProject method buildRelationExpressionDescriptor.

protected ClassDescriptor buildRelationExpressionDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(RelationExpression.class);
    descriptor.setDefaultRootElement("relation-expression");
    descriptor.getInheritancePolicy().setParentClass(Expression.class);
    // Child value expressions need their backpointer to their local base set,
    // this is not persisted so must be hooked back up after loading.
    descriptor.getEventManager().addListener(new DescriptorEventAdapter() {

        @Override
        public void postBuild(DescriptorEvent event) {
            RelationExpression expression = (RelationExpression) event.getObject();
            if ((expression.getFirstChild() != null) && (expression.getSecondChild() != null)) {
                if (expression.getSecondChild().isValueExpression()) {
                    expression.getSecondChild().setLocalBase(expression.getFirstChild());
                }
                if (expression.getFirstChild().isValueExpression()) {
                    expression.getFirstChild().setLocalBase(expression.getSecondChild());
                }
            }
        }
    });
    XMLDirectMapping operatorMapping = new XMLDirectMapping();
    operatorMapping.setAttributeName("operator");
    ObjectTypeConverter operatorConverter = new ObjectTypeConverter();
    operatorConverter.addConversionValue("equal", ExpressionOperator.getOperator(ExpressionOperator.Equal));
    operatorConverter.addConversionValue("notEqual", ExpressionOperator.getOperator(ExpressionOperator.NotEqual));
    operatorConverter.addConversionValue("like", ExpressionOperator.getOperator(ExpressionOperator.Like));
    operatorConverter.addConversionValue("notLike", ExpressionOperator.getOperator(ExpressionOperator.NotLike));
    operatorConverter.addConversionValue("greaterThan", ExpressionOperator.getOperator(ExpressionOperator.GreaterThan));
    operatorConverter.addConversionValue("greaterThanEqual", ExpressionOperator.getOperator(ExpressionOperator.GreaterThanEqual));
    operatorConverter.addConversionValue("lessThan", ExpressionOperator.getOperator(ExpressionOperator.LessThan));
    operatorConverter.addConversionValue("lessThanEqual", ExpressionOperator.getOperator(ExpressionOperator.LessThanEqual));
    operatorMapping.setConverter(operatorConverter);
    operatorMapping.setXPath("@operator");
    descriptor.addMapping(operatorMapping);
    XMLCompositeObjectMapping leftMapping = new XMLCompositeObjectMapping();
    leftMapping.setAttributeName("firstChild");
    leftMapping.setGetMethodName("getFirstChild");
    leftMapping.setSetMethodName("setFirstChild");
    leftMapping.setReferenceClass(Expression.class);
    leftMapping.setXPath(getPrimaryNamespaceXPath() + "left");
    descriptor.addMapping(leftMapping);
    XMLCompositeObjectMapping rightMapping = new XMLCompositeObjectMapping();
    rightMapping.setAttributeName("secondChild");
    rightMapping.setGetMethodName("getSecondChild");
    rightMapping.setSetMethodName("setSecondChild");
    rightMapping.setReferenceClass(Expression.class);
    rightMapping.setXPath(getPrimaryNamespaceXPath() + "right");
    descriptor.addMapping(rightMapping);
    return descriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) DescriptorEventAdapter(org.eclipse.persistence.descriptors.DescriptorEventAdapter) ObjectTypeConverter(org.eclipse.persistence.mappings.converters.ObjectTypeConverter) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Aggregations

DescriptorEventAdapter (org.eclipse.persistence.descriptors.DescriptorEventAdapter)8 DescriptorEvent (org.eclipse.persistence.descriptors.DescriptorEvent)7 ArrayList (java.util.ArrayList)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 RelationExpression (org.eclipse.persistence.internal.expressions.RelationExpression)2 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)2 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)2 EntityManager (jakarta.persistence.EntityManager)1 CacheIndex (org.eclipse.persistence.descriptors.CacheIndex)1 DescriptorEventManager (org.eclipse.persistence.descriptors.DescriptorEventManager)1 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)1 Expression (org.eclipse.persistence.expressions.Expression)1 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)1 ExpressionOperator (org.eclipse.persistence.expressions.ExpressionOperator)1 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)1 ExpressionOperatorConverter (org.eclipse.persistence.internal.expressions.ExpressionOperatorConverter)1 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)1 FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)1 LogicalExpression (org.eclipse.persistence.internal.expressions.LogicalExpression)1 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)1