Example usage for org.apache.commons.jxpath JXPathContext getValue

List of usage examples for org.apache.commons.jxpath JXPathContext getValue

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext getValue.

Prototype

public abstract Object getValue(String xpath);

Source Link

Document

Evaluates the xpath and returns the resulting object.

Usage

From source file:org.openvpms.component.business.service.archetype.ArchetypeService.java

/**
 * Iterate through the {@link NodeDescriptor}s and set all derived values.
 * Do it recursively./*from  ww w. j  a  v a 2s.c o m*/
 *
 * @param context the context object
 * @param nodes   a list of node descriptors
 * @throws ArchetypeServiceException
 */
private void deriveValues(JXPathContext context, Map<String, NodeDescriptor> nodes) {
    for (NodeDescriptor node : nodes.values()) {
        if (node.isDerived()) {
            try {
                Object value = context.getValue(node.getDerivedValue());
                context.getPointer(node.getPath()).setValue(value);
            } catch (Exception exception) {
                throw new ArchetypeServiceException(ArchetypeServiceException.ErrorCode.FailedToDeriveValue,
                        exception, node.getName(), node.getPath());
            }
        }

        // if this node contains other nodes then make a recursive call
        if (node.getNodeDescriptors().size() > 0) {
            deriveValues(context, node.getNodeDescriptors());
        }
    }
}

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Tests behaviour where an intermediate node doesn't exist.
 *//*ww  w.j  ava  2  s .co m*/
@Test
public void testIMObjectMissingReference() {
    ActBean act = createAct("act.customerEstimation");
    JXPathContext context = JXPathHelper.newContext(act.getAct());
    assertNull(context.getValue("openvpms:get(., 'customer.entity.firstName')"));
}

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Tests behaviour where an intermediate node doesn't exist.
 *///from  ww w.j  ava2 s.  co m
@Test
public void testPropertySetMissingReference() {
    ActBean act = createAct("act.customerEstimation");
    ObjectSet set = new ObjectSet();
    set.set("act", act.getAct());
    JXPathContext context = JXPathHelper.newContext(set);
    assertNull(context.getValue("openvpms:get(.,'act.customer.entity.firstName')"));
}

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Tests behaviour where an invalid node name is supplied.
 *///w w w.  j  a  va 2  s.  c o  m
@Test
public void testInvalidNode() {
    Party party = createCustomer();
    ActBean act = createAct("act.customerEstimation");
    act.setParticipant("participation.customer", party);
    JXPathContext context = JXPathHelper.newContext(act.getAct());

    // root node followed by invalid node
    try {
        context.getValue("openvpms:get(., 'customer.invalidNode')");
        fail("expected PropertyResolverException to be thrown");
    } catch (JXPathInvalidAccessException exception) {
        checkException(exception, InvalidProperty);
    }

    // intermediate node followed by invalid node
    try {
        context.getValue("openvpms:get(., 'customer.entity.invalidNode')");
        fail("expected PropertyResolverException to be thrown");
    } catch (JXPathInvalidAccessException exception) {
        checkException(exception, InvalidProperty);
    }

    // leaf node followed by invalid node
    try {
        context.getValue("openvpms:get(., 'startTime.displayName')");
        fail("expected PropertyResolverException to be thrown");
    } catch (JXPathInvalidAccessException exception) {
        checkException(exception, InvalidObject);
    }

    // invalid node with default value
    Object value = context.getValue("openvpms:get(., 'invalidNode', 'default')");
    assertEquals("default", value);
}

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Tests behaviour where an invalid property name is supplied.
 *///from  w ww .  j ava2 s  .  com
@Test
public void testInvalidProperty() {
    Party party = createCustomer();
    ActBean act = createAct("act.customerEstimation");
    act.setParticipant("participation.customer", party);
    ObjectSet set = new ObjectSet();
    set.set("act", act.getAct());
    JXPathContext context = JXPathHelper.newContext(set);

    // invalid property name
    try {
        context.getValue("openvpms:get(., 'nonexistentprop')");
        fail("expected PropertyResolverException to be thrown");
    } catch (JXPathInvalidAccessException exception) {
        checkException(exception, InvalidObject);
    }

    // root node followed by invalid node
    try {
        context.getValue("openvpms:get(., 'act.customer.invalidNode')");
        fail("expected PropertyResolverException to be thrown");
    } catch (JXPathInvalidAccessException exception) {
        checkException(exception, InvalidProperty);
    }

    // intermediate node followed by invalid node
    try {
        context.getValue("openvpms:get(., 'act.customer.entity.invalidNode')");
        fail("expected PropertyResolverException to be thrown");
    } catch (JXPathInvalidAccessException exception) {
        checkException(exception, InvalidProperty);
    }

    // leaf node followed by invalid node
    try {
        context.getValue("openvpms:get(., 'act.startTime.displayName')");
        fail("expected PropertyResolverException to be thrown");
    } catch (JXPathInvalidAccessException exception) {
        checkException(exception, InvalidObject);
    }

    // invalid node with default value
    Object value = context.getValue("openvpms:get(., 'act.invalidNode', 'default')");
    assertEquals("default", value);
}

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Tests openvpms:get() methods, when a {@code null} is supplied.
 *//*from  ww w  .j a va2s  .c o  m*/
@Test
public void testNullObject() {
    ActBean act = createAct("act.customerEstimation");
    JXPathContext context = JXPathHelper.newContext(act.getObject());
    assertNull(context.getValue("openvpms:get(openvpms:get(.,'customer.entity'), 'foo')"));
    assertEquals("Foo",
            context.getValue("openvpms:get(openvpms:get(.,'customer.entity'), 'invalidNode', 'Foo')"));
}

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Tests the openvpms:set() function.//from  www. j a va 2s . c  o  m
 */
@Test
public void testSet() {
    Party party = createCustomer();
    JXPathContext context = JXPathHelper.newContext(party);
    context.getValue("openvpms:set(., 'lastName', 'testSet')");
    party = (Party) get(party.getObjectReference());
    assertNotNull(party);
    IMObjectBean bean = new IMObjectBean(party);
    assertEquals("testSet", bean.getString("lastName"));
}

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Tests the openvpms:lookup() function.
 *//*from  www  .j  a  v a  2s  .  c o m*/
@Test
public void testLookup() {
    Party party = createCustomer();
    ActBean act = createAct("act.customerEstimation");
    act.setStatus("IN_PROGRESS");
    Participation participation = act.setParticipant("participation.customer", party);

    JXPathContext context = JXPathHelper.newContext(act.getAct());

    checkLookup("In Progress", "status", context);
    checkLookup("Mr", "customer.entity.title", context);

    // test invalid node
    try {
        context.getValue("openvpms:lookup(., 'displayName')");
        fail("expected PropertyResolverException to be thrown");
    } catch (JXPathInvalidAccessException exception) {
        checkException(exception, InvalidProperty);
    }

    // test invalid node with default
    Object value = context.getValue("openvpms:lookup(., 'displayName', 'default')");
    assertEquals("default", value);

    // test null
    Party unsavedCustomer = createCustomer(false);
    participation.setEntity(unsavedCustomer.getObjectReference());
    assertNull(context.getValue("openvpms:lookup(openvpms:get(.,'customer.entity'), 'title')"));

    assertEquals("Foo", context.getValue("openvpms:lookup(openvpms:get(.,'customer.entity'), 'title', 'Foo')"));
}

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Tests the openvpms:defaultLookup function.
 *///from  w w  w.  j  a  va 2s.  c o  m
@Test
public void testDefaultLookup() {
    // ensure existing lookups are non-default and create some new ones,
    // making the last one created the default.
    Collection<Lookup> lookups = LookupServiceHelper.getLookupService().getLookups("lookup.staff");
    for (Lookup lookup : lookups) {
        if (lookup.isDefaultLookup()) {
            lookup.setDefaultLookup(false);
            save(lookup);
        }
    }
    Lookup lookup = null;
    for (int i = 0; i < 10; ++i) {
        lookup = (Lookup) create("lookup.staff");
        lookup.setCode("CODE" + Math.random());
        lookup.setName(lookup.getCode());
        lookup.setDescription(lookup.getCode());
        save(lookup);
    }
    assertNotNull(lookup);
    lookup.setDefaultLookup(true);
    save(lookup);

    Party party = createCustomer();
    JXPathContext context = JXPathHelper.newContext(party);
    Object value = context.getValue("openvpms:defaultLookup(.,'classifications')");
    assertNotNull(value);
    assertTrue(value instanceof Lookup);
    assertEquals(lookup, value);
}

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Verifies that an expression evaluates to the expected result.
 *
 * @param expected the expected result//  w  ww . ja v a  2  s  . c  o  m
 * @param node     the exoression node
 * @param context  the context
 */
private void checkEquals(String expected, String node, JXPathContext context) {
    String expression = "openvpms:get(., '" + node + "')";
    assertEquals(expected, context.getValue(expression));
}