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.archetype.function.party.PartyFunctionsTestCase.java

/**
 * Tests the {@link PartyFunctions#getPatientReferralVet(Act)} and
 * {@link PartyFunctions#getPatientReferralVet(ExpressionContext)} methods.
 *//*from  www.  j a  v  a 2 s . c  o  m*/
@Test
public void testActGetPatientReferralVet() {
    Party patient = TestHelper.createPatient();
    Party vet = TestHelper.createSupplierVet();
    EntityBean bean = new EntityBean(patient);
    bean.addRelationship(PatientArchetypes.REFERRED_FROM, vet);
    save(patient, vet);
    Act act = (Act) create("act.customerEstimationItem");

    JXPathContext ctx = createContext(act);
    assertNull(ctx.getValue("party:getPatientReferralVet()")); // invokes getPatientReferralVet(ExpressionContext)
    assertNull(ctx.getValue("party:getPatientReferralVet(.)")); // invokes getPatientReferralVet(Act)

    ActBean actBean = new ActBean(act);
    actBean.addNodeParticipation("patient", patient);

    assertEquals(vet, ctx.getValue("party:getPatientReferralVet()"));
    assertEquals(vet, ctx.getValue("party:getPatientReferralVet(.)"));
}

From source file:org.openvpms.archetype.function.party.PartyFunctionsTestCase.java

/**
 * Tests the {@link PartyFunctions#getPatientReferralVet(Party)} and
 * {@link PartyFunctions#getPatientReferralVet(ExpressionContext)} methods.
 *//*from w  ww .  j a  va2s . com*/
@Test
public void testGetPatientReferralVet() {
    Party patient = TestHelper.createPatient();
    Party vet = TestHelper.createSupplierVet();

    JXPathContext ctx = createContext(patient);

    // verify that if the patient can't be resolved, null is returned
    assertNull(ctx.getValue("party:getPatientReferralVet()")); // invokes getPatientReferralVet(ExpressionContext)
    assertNull(ctx.getValue("party:getPatientReferralVet(.)")); // invokes getPatientReferralVet(Party)

    EntityBean bean = new EntityBean(patient);
    bean.addRelationship(PatientArchetypes.REFERRED_TO, vet);
    assertEquals(vet, ctx.getValue("party:getPatientReferralVet()"));
    assertEquals(vet, ctx.getValue("party:getPatientReferralVet(.)"));
}

From source file:org.openvpms.archetype.function.party.PartyFunctionsTestCase.java

/**
 * Tests the {@link PartyFunctions#getPatientReferralVetPractice(Act)} and
 * {@link PartyFunctions#getPatientReferralVetPractice(ExpressionContext)} methods.
 *//*  w  ww. j  a  va 2s. c om*/
@Test
public void testActGetPatientReferralVetPractice() {
    Party patient = TestHelper.createPatient();
    Party vet = TestHelper.createSupplierVet();
    Party practice = TestHelper.createSupplierVetPractice();

    // create relationships between the patient, vet, and vet practice
    EntityBean bean = new EntityBean(patient);
    bean.addRelationship(PatientArchetypes.REFERRED_FROM, vet);
    EntityBean practiceBean = new EntityBean(practice);
    practiceBean.addNodeRelationship("veterinarians", vet);
    save(patient, vet, practice);

    Act act = (Act) create("act.customerEstimationItem");
    JXPathContext ctx = createContext(act);

    // verify that if the patient can't be resolved, null is returned
    assertNull(ctx.getValue("party:getPatientReferralVetPractice()"));
    // invokes getPatientReferralVetPractice(ExpressionContext)

    assertNull(ctx.getValue("party:getPatientReferralVetPractice(.)"));
    // invokes getPatientReferralVetPractice(Act)

    // add the patient to the act, and verify the practice can be retrieved
    ActBean actBean = new ActBean(act);
    actBean.addNodeParticipation("patient", patient);

    assertEquals(practice, ctx.getValue("party:getPatientReferralVetPractice()"));
    assertEquals(practice, ctx.getValue("party:getPatientReferralVetPractice(.)"));
}

From source file:org.openvpms.archetype.function.party.PartyFunctionsTestCase.java

/**
 * Tests the {@link PartyFunctions#getPatientReferralVetPractice(Party)} and
 * {@link PartyFunctions#getPatientReferralVetPractice(ExpressionContext)} methods.
 *///w  ww .  j av a  2  s. c  o m
@Test
public void testGetPatientReferralVetPractice() {
    Party patient = TestHelper.createPatient();
    Party vet = TestHelper.createSupplierVet();
    Party practice = TestHelper.createSupplierVetPractice();

    JXPathContext ctx = createContext(patient);

    // verify that if the vet can't be resolved, null is returned
    assertNull(ctx.getValue("party:getPatientReferralVetPractice()"));
    // invokes getPatientReferralVetPractice(ExpressionContext)

    assertNull(ctx.getValue("party:getPatientReferralVetPractice(.)"));
    // invokes getPatientReferralVetPractice(Party)

    // create relationships between the patient, vet, and vet practice
    EntityBean bean = new EntityBean(patient);
    bean.addRelationship(PatientArchetypes.REFERRED_TO, vet);
    EntityBean practiceBean = new EntityBean(practice);
    practiceBean.addNodeRelationship("veterinarians", vet);
    save(patient, vet, practice);

    // verify the practice can be retrieved
    assertEquals(practice, ctx.getValue("party:getPatientReferralVetPractice()"));
    assertEquals(practice, ctx.getValue("party:getPatientReferralVetPractice(.)"));
}

From source file:org.openvpms.archetype.function.party.PartyFunctionsTestCase.java

/**
 * Tests the {@link PartyFunctions#getAccountBalance} methods.
 *//*from w  w  w  . j  ava 2  s .c o m*/
@Test
public void testGetAccountBalance() {
    Party customer = TestHelper.createCustomer();
    Party patient = TestHelper.createPatient(customer);
    BigDecimal total = BigDecimal.valueOf(100);

    JXPathContext ctx1 = createContext(customer);
    checkEquals(BigDecimal.ZERO, (BigDecimal) ctx1.getValue("party:getAccountBalance(.)"));

    List<FinancialAct> invoice = FinancialTestHelper.createChargesInvoice(total, customer, patient,
            TestHelper.createProduct(), ActStatus.POSTED);
    save(invoice);

    checkEquals(total, (BigDecimal) ctx1.getValue("party:getAccountBalance(.)"));

    JXPathContext ctx2 = createContext(invoice.get(1));
    checkEquals(total, (BigDecimal) ctx2.getValue("party:getAccountBalance(.)"));
}

From source file:org.openvpms.archetype.function.reminder.ReminderFunctionsTestCase.java

/**
 * Tests the {@link ReminderFunctions#getReminders} methods.
 *
 * @param context  the jxpath context. Either a customer or an act with a customer participation
 * @param customer the customer/*w w  w.  jav a 2  s .com*/
 */
@SuppressWarnings("unchecked")
private void checkGetReminders(Object context, Party customer) {
    final int count = 10;
    Entity reminderType = ReminderTestHelper.createReminderType();

    Calendar calendar = new GregorianCalendar();

    // backdate the calendar 5 days. When excluding overdue reminders, reminders dated prior to the current date
    // will be ignored.
    calendar.add(Calendar.DAY_OF_YEAR, -5);
    for (int i = 0; i < count; ++i) {
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        Date dueDate = calendar.getTime();
        Party patient = TestHelper.createPatient(customer);
        ReminderTestHelper.createReminderWithDueDate(patient, reminderType, dueDate);
    }

    JXPathContext ctx = createContext(context);

    // get reminders excluding any reminders prior to the current date
    List<Act> reminders1 = (List<Act>) ctx.getValue("reminder:getReminders(., 1, 'YEARS')");
    assertEquals(6, reminders1.size());

    // get all reminders (i.e., including overdue)
    List<Act> reminders2 = (List<Act>) ctx.getValue("reminder:getReminders(., 12, 'MONTHS', 'true')");
    assertEquals(count, reminders2.size());
}

From source file:org.openvpms.archetype.function.reminder.ReminderFunctionsTestCase.java

/**
 * Invokes the reminder:getDocumentFormReminder() jxpath function with the supplied context.
 *
 * @param context the context object// w w w  .ja  v a2 s.c  o  m
 * @return the resulting reminder act. May be {@code null}
 */
private Act getDocumentFormReminder(Object context) {
    JXPathContext ctx = createContext(context);
    return (Act) ctx.getValue("reminder:getDocumentFormReminder(.)");
}

From source file:org.openvpms.component.business.domain.im.archetype.descriptor.NodeDescriptor.java

/**
 * Derive the node value for the specified {@link NodeDescriptor}. If the
 * node does not support derived value or the value cannot be derived then
 * raise an exception.//from   w ww.j  av a2s  . c o  m
 *
 * @param imobj the {@link IMObject}
 * @throws FailedToDeriveValueException if the node is not declared to support dervied value or
 *                                      the value cannot be derived correctly.
 */
public void deriveValue(IMObject imobj) {
    if (!isDerived()) {
        throw new FailedToDeriveValueException(FailedToDeriveValueException.ErrorCode.DerivedValueUnsupported,
                new Object[] { getName() });
    }

    // attempt to derive the value
    try {
        JXPathContext context = JXPathHelper.newContext(imobj);
        Object value = context.getValue(this.getDerivedValue());
        context.getPointer(this.getPath()).setValue(value);
    } catch (Exception exception) {
        throw new FailedToDeriveValueException(FailedToDeriveValueException.ErrorCode.FailedToDeriveValue,
                new Object[] { getName(), getPath(), getDerivedValue() }, exception);
    }
}

From source file:org.openvpms.component.business.domain.im.archetype.descriptor.NodeDescriptor.java

/**
 * Returns the value of this node given the specified context.
 *
 * @param context the context to use//from  w w  w.  j  a  v a  2  s  . c o m
 * @return Object
 *         the returned object
 */
public Object getValue(JXPathContext context) {
    Object value = null;
    if (context != null) {
        if (isDerived()) {
            value = context.getValue(getDerivedValue());
        } else {
            if (isCollection()) {
                value = getChildren((IMObject) context.getContextBean());
            } else {
                value = context.getValue(getPath());
            }
        }
    }

    return value;
}

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

/**
 * Creates a node in the context, populating any default value.
 *
 * @param context the jxpath context/*from w  w  w.ja  v  a2 s. com*/
 * @param node    the node to create
 * @throws ArchetypeServiceException if the create fails
 */
private void create(JXPathContext context, NodeDescriptor node) {
    if (log.isDebugEnabled()) {
        log.debug("Attempting to create path " + node.getPath() + " for node " + node.getName());
    }

    context.getVariables().declareVariable("node", node);
    context.createPath(node.getPath());

    String expression = node.getDefaultValue();
    if (!StringUtils.isEmpty(expression)) {
        if (log.isDebugEnabled()) {
            log.debug("evaluating default value expression for node " + node.getName() + " path "
                    + node.getPath() + " and expression " + expression);
        }
        Object value = context.getValue(expression);
        IMObject object = (IMObject) context.getContextBean();
        if (node.isCollection()) {
            if (value != null) {
                if (Collection.class.isAssignableFrom(value.getClass())) {
                    for (Object v : (Collection) value) {
                        node.addChildToCollection(object, v);
                    }
                } else {
                    node.addChildToCollection(object, value);
                }
            }
        } else {
            node.setValue(object, value);
        }
    }
}