Example usage for javax.xml.bind JAXBElement toString

List of usage examples for javax.xml.bind JAXBElement toString

Introduction

In this page you can find the example usage for javax.xml.bind JAXBElement toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:nz.co.senanque.base.ObjectTest.java

@SuppressWarnings("unused")
@Test//from  w ww .j ava  2s.  c o  m
public void test1() throws Exception {
    ValidationSession validationSession = m_validationEngine.createSession();

    // create a customer
    Customer customer = m_customerDAO.createCustomer();
    validationSession.bind(customer);
    Invoice invoice = new Invoice();
    invoice.setDescription("test invoice");
    customer.getInvoices().add(invoice);
    boolean exceptionFound = false;
    try {
        customer.setName("ttt");
    } catch (ValidationException e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);

    final ObjectMetadata customerMetadata = validationSession.getMetadata(customer);
    final FieldMetadata customerTypeMetadata = customerMetadata.getFieldMetadata(Customer.CUSTOMERTYPE);

    assertFalse(customerTypeMetadata.isActive());
    assertFalse(customerTypeMetadata.isReadOnly());
    assertFalse(customerTypeMetadata.isRequired());

    customer.setName("aaaab");
    assertTrue(customerTypeMetadata.isActive());
    assertTrue(customerTypeMetadata.isReadOnly());
    assertTrue(customerTypeMetadata.isRequired());
    exceptionFound = false;
    try {
        customer.setCustomerType("B");
    } catch (Exception e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);
    exceptionFound = false;
    try {
        customer.setCustomerType("XXX");
    } catch (Exception e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);
    customer.setBusiness(IndustryType.AG);
    customer.setAmount(new Double(500.99));
    final long id = m_customerDAO.save(customer);
    log.info(id);

    // fetch customer back
    customer = m_customerDAO.getCustomer(id);
    final int invoiceCount = customer.getInvoices().size();
    validationSession.bind(customer);
    invoice = new Invoice();
    invoice.setDescription("test invoice2");
    customer.getInvoices().add(invoice);
    m_customerDAO.save(customer);

    // fetch customer again
    customer = m_customerDAO.getCustomer(id);
    customer.toString();
    validationSession.bind(customer);
    final Invoice inv = customer.getInvoices().get(0);
    customer.getInvoices().remove(inv);

    //ObjectMetadata metadata = validationSession.getMetadata(customer);
    ObjectMetadata metadata = customer.getMetadata();
    org.junit.Assert.assertEquals("this is a description",
            metadata.getFieldMetadata(Customer.NAME).getDescription());
    List<ChoiceBase> choices = metadata.getFieldMetadata(Customer.BUSINESS).getChoiceList();
    assertEquals(1, choices.size());
    List<ChoiceBase> choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    assertEquals(2, choices2.size());
    choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    assertEquals(2, choices2.size());

    customer.setName("aab");
    choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    assertEquals(6, choices2.size());

    // Convert customer to XML
    QName qname = new QName("http://www.example.org/sandbox", "Session");
    JAXBElement<Session> sessionJAXB = new JAXBElement<Session>(qname, Session.class, new Session());
    sessionJAXB.getValue().getCustomers().add(customer); //??This fails to actually add
    StringWriter marshallWriter = new StringWriter();
    Result marshallResult = new StreamResult(marshallWriter);
    m_marshaller.marshal(sessionJAXB, marshallResult);
    marshallWriter.flush();
    String result = marshallWriter.getBuffer().toString().trim();
    String xml = result.replaceAll("\\Qhttp://www.example.org/sandbox\\E", "http://www.example.org/sandbox");
    log.info(xml);

    // Convert customer back to objects
    SAXBuilder builder = new SAXBuilder();
    org.jdom.Document resultDOM = builder.build(new StringReader(xml));
    @SuppressWarnings("unchecked")
    JAXBElement<Session> request = (JAXBElement<Session>) m_unmarshaller.unmarshal(new JDOMSource(resultDOM));
    validationSession = m_validationEngine.createSession();
    validationSession.bind(request.getValue());
    assertEquals(3, validationSession.getProxyCount());
    List<Customer> customers = request.getValue().getCustomers();
    assertEquals(1, customers.size());
    customers.clear();
    assertEquals(1, validationSession.getProxyCount());
    request.toString();
    validationSession.close();
}

From source file:nz.co.senanque.sandbox.ObjectTest.java

@Test
public void test1() throws Exception {
    @SuppressWarnings("unused")
    Object en = IndustryType.fromValue("Ag");
    ValidationSession validationSession = m_validationEngine.createSession();

    // create a customer
    Customer customer = m_customerDAO.createCustomer();
    validationSession.bind(customer);// w  ww .  j a  v a 2s. co  m
    Invoice invoice = new Invoice();
    invoice.setDescription("test invoice");
    invoice.setTestBoolean(true);
    customer.getInvoices().add(invoice);
    boolean exceptionFound = false;
    try {
        customer.setName("ttt");
    } catch (ValidationException e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);

    final ObjectMetadata customerMetadata = validationSession.getMetadata(customer);
    @SuppressWarnings("unused")
    final FieldMetadata customerTypeMetadata = customerMetadata.getFieldMetadata(Customer.CUSTOMERTYPE);

    //        assertFalse(customerTypeMetadata.isActive());
    //        assertFalse(customerTypeMetadata.isReadOnly());
    //        assertFalse(customerTypeMetadata.isRequired());

    customer.setName("aaaab");
    exceptionFound = false;
    try {
        customer.setCustomerType("B");
    } catch (Exception e) {
        exceptionFound = true;
    }
    //        assertTrue(exceptionFound);
    exceptionFound = false;
    try {
        customer.setCustomerType("XXX");
    } catch (Exception e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);
    customer.setBusiness(IndustryType.AG);
    customer.setAmount(new Double(500.99));
    final long id = m_customerDAO.save(customer);
    log.info("{}", id);

    // fetch customer back
    customer = m_customerDAO.getCustomer(id);
    @SuppressWarnings("unused")
    final int invoiceCount = customer.getInvoices().size();
    validationSession.bind(customer);
    invoice = new Invoice();
    ValidationUtils.setDefaults(invoice);
    invoice.setDescription("test invoice2");
    invoice.setTestBoolean(true);
    customer.getInvoices().add(invoice);
    assertEquals("xyz", invoice.getTestDefault());
    assertEquals("Ag", invoice.getTestEnumDefault().value());
    m_customerDAO.save(customer);

    // fetch customer again
    customer = m_customerDAO.getCustomer(id);
    customer.toString();
    final Invoice inv0 = customer.getInvoices().get(0);
    assertTrue(inv0.isTestBoolean());

    validationSession.bind(customer);
    final Invoice inv = customer.getInvoices().get(0);
    assertTrue(inv.isTestBoolean());
    customer.getInvoices().remove(inv);

    //ObjectMetadata metadata = validationSession.getMetadata(customer);
    ObjectMetadata metadata = customer.getMetadata();
    assertEquals("this is a description", metadata.getFieldMetadata(Customer.NAME).getDescription());
    assertEquals("ABC", metadata.getFieldMetadata(Customer.NAME).getPermission());
    @SuppressWarnings("unused")
    List<ChoiceBase> choices = metadata.getFieldMetadata(Customer.BUSINESS).getChoiceList();
    //        assertEquals(1,choices.size());
    List<ChoiceBase> choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    //        assertEquals(1,choices2.size());
    choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    //        assertEquals(1,choices2.size());

    for (ChoiceBase choice : choices2) {
        System.out.println(choice.getDescription());
    }

    customer.setName("aab");
    choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    //        assertEquals(6,choices2.size());

    // Convert customer to XML
    QName qname = new QName("http://www.example.org/sandbox", "Session");
    JAXBElement<Session> sessionJAXB = new JAXBElement<Session>(qname, Session.class, new Session());
    sessionJAXB.getValue().getCustomers().add(customer); //??This fails to actually add
    StringWriter marshallWriter = new StringWriter();
    Result marshallResult = new StreamResult(marshallWriter);
    m_marshaller.marshal(sessionJAXB, marshallResult);
    marshallWriter.flush();
    String result = marshallWriter.getBuffer().toString().trim();
    String xml = result.replaceAll("\\Qhttp://www.example.org/sandbox\\E", "http://www.example.org/sandbox");
    log.info("{}", xml);

    // Convert customer back to objects
    SAXBuilder builder = new SAXBuilder();
    org.jdom.Document resultDOM = builder.build(new StringReader(xml));
    @SuppressWarnings("unchecked")
    JAXBElement<Session> request = (JAXBElement<Session>) m_unmarshaller.unmarshal(new JDOMSource(resultDOM));
    validationSession = m_validationEngine.createSession();
    validationSession.bind(request.getValue());
    assertEquals(3, validationSession.getProxyCount());
    List<Customer> customers = request.getValue().getCustomers();
    assertEquals(1, customers.size());
    assertTrue(customers.get(0).getInvoices().get(0).isTestBoolean());
    customers.clear();
    assertEquals(1, validationSession.getProxyCount());
    request.toString();
    validationSession.close();
}

From source file:org.opencds.vmr.v1_0.mappings.in.BuildCDSInputFactLists.java

public static String buildFactLists(JAXBElement<org.opencds.vmr.v1_0.schema.CDSInput> cdsInput, Date evalTime,
        Map<String, List<?>> allFactLists, TimingDataKM timingDataKM) throws InvalidDriDataFormatExceptionFault,
        UnrecognizedLanguageExceptionFault, RequiredDataNotProvidedExceptionFault,
        UnsupportedLanguageExceptionFault, UnrecognizedScopedEntityExceptionFault, EvaluationExceptionFault,
        InvalidTimeZoneOffsetExceptionFault, DSSRuntimeExceptionFault {
    MappingUtility mu = new MappingUtility();
    String focalPersonId = null;/* ww  w  .ja  v  a  2 s. c  o m*/
    log.debug("buildFactLists");

    try {

        String subjectPersonId = null;
        FactLists factLists = new FactLists();
        factLists.clearAllFactLists();
        EvalTime evalTimeFact = new EvalTime();
        evalTimeFact.setEvalTimeValue(evalTime);
        factLists.evalTimeList.add(evalTimeFact);

        if ((cdsInput.getValue() == null)) {
            throw new InvalidDriDataFormatExceptionFault("Error: No payload within the CDSInput.");
        } else {

            try {

                CDSInput internalCDSInput = new CDSInput();
                CDSInputMapper.pullIn(cdsInput.getValue(), internalCDSInput, mu);
                factLists.internalCDSInputList.add(internalCDSInput);

                focalPersonId = internalCDSInput.getFocalPersonId(); //for later reference, never changes for the entire VMR...
                log.debug("BuildCDSInputFactLists for focalPersonId=" + focalPersonId);
                FocalPersonId focalPersonIdFact = new FocalPersonId();
                focalPersonIdFact.setId(focalPersonId);
                factLists.focalPersonIdList.add(focalPersonIdFact);
                subjectPersonId = focalPersonId; //same as focal person initially, but will change for each "otherEvaluatedPerson" entry
            } catch (Exception e) {
                String unknownError = e.getMessage();
                e.printStackTrace();
                throw new InvalidDriDataFormatExceptionFault(
                        "Unknown error initializing BuildCDSInputFactLists: " + unknownError
                                + ", therefore unable to complete unmarshalling input Semantic Payload: "
                                + cdsInput.toString());
            }

            org.opencds.vmr.v1_0.schema.VMR vmrInput = cdsInput.getValue().getVmrInput();

            VMR internalVMR = new VMR();
            VMRMapper.pullIn(vmrInput, internalVMR, mu);
            factLists.internalVMRList.add(internalVMR);

            // ================= Patient =======================//
            if (vmrInput.getPatient() != null) {
                org.opencds.vmr.v1_0.schema.EvaluatedPerson inputPatient = vmrInput.getPatient();
                oneEvaluatedPerson(inputPatient, evalTime, subjectPersonId, focalPersonId, factLists);
            }

            // ================= OtherEvaluatedPersons =======================//
            if ((vmrInput.getOtherEvaluatedPersons() != null)
                    && (vmrInput.getOtherEvaluatedPersons().getEvaluatedPerson() != null)
                    && (vmrInput.getOtherEvaluatedPersons().getEvaluatedPerson().size() > 0)) {
                List<org.opencds.vmr.v1_0.schema.EvaluatedPerson> input = vmrInput.getOtherEvaluatedPersons()
                        .getEvaluatedPerson();
                for (org.opencds.vmr.v1_0.schema.EvaluatedPerson eachOtherEvaluatedPerson : input) {
                    subjectPersonId = MappingUtility.iI2FlatId(eachOtherEvaluatedPerson.getId());
                    oneEvaluatedPerson(eachOtherEvaluatedPerson, evalTime, subjectPersonId, focalPersonId,
                            factLists);
                }
            }

            // ================= EvaluatedPersonRelationships (for family history relationships, where relatedPersons also have their own vMR) =======================//
            if ((vmrInput.getEvaluatedPersonRelationships() != null)
                    && (vmrInput.getEvaluatedPersonRelationships().getEvaluatedPersonRelationship() != null)
                    && (vmrInput.getEvaluatedPersonRelationships().getEvaluatedPersonRelationship()
                            .size() > 0)) {
                List<org.opencds.vmr.v1_0.schema.EntityRelationship> input = vmrInput
                        .getEvaluatedPersonRelationships().getEvaluatedPersonRelationship();
                for (org.opencds.vmr.v1_0.schema.EntityRelationship each : input) {
                    EvaluatedPersonRelationship internalEvaluatedPersonRelationship = new EvaluatedPersonRelationshipMapper()
                            .pullIn(each, mu);
                    factLists.internalEvaluatedPersonRelationshipList.add(internalEvaluatedPersonRelationship);
                }
            }
            // ================= End of Building all Internal FactLists =======================//            

            // ================= Populate OpenCDS Concepts through Post-Processing of Internal VMR =======================//
            try {
                timingDataKM.setFinishBuildFactListsTime(new AtomicLong(System.nanoTime()));

                ConceptLists conceptLists = new ConceptLists();
                conceptLists.clearAllConceptLists();
                BuildOpenCDSConceptLists.buildConceptLists(factLists, conceptLists, allFactLists);
            } catch (Exception e) {
                String err = e.getMessage();
                e.printStackTrace();
                throw new InvalidDriDataFormatExceptionFault(
                        "BuildOpenCDSConceptLists threw error: " + err + "; " + e);
            }
            // ================= End Populating Code Concepts =======================//

            // ================= Begin Populating AllFactLists object =======================//             
            factLists.populateAllFactLists(allFactLists);
            // ================= End Populating AllFactLists object =======================//             
        }

    } catch (ImproperUsageException e) {
        String err = e.getMessage();
        e.printStackTrace();
        throw new InvalidDriDataFormatExceptionFault("ImproperUsageException error in BuildCDSInputFactLists: "
                + err + ", therefore unable to complete unmarshalling input Semantic Payload: "
                + cdsInput.toString());
    } catch (DataFormatException e) {
        String err = e.getMessage();
        e.printStackTrace();
        throw new InvalidDriDataFormatExceptionFault("DataFormatException error in BuildCDSInputFactLists: "
                + err + ", therefore unable to complete unmarshalling input Semantic Payload: "
                + cdsInput.toString());
    } catch (InvalidDataException e) {
        String err = e.getMessage();
        e.printStackTrace();
        throw new InvalidDriDataFormatExceptionFault("InvalidDataException error in BuildCDSInputFactLists: "
                + err + ", therefore unable to complete unmarshalling input Semantic Payload: "
                + cdsInput.toString());
    }

    /**
     * The actual output of the mapping is in allFactLists, which is a live I-O input parameter
     */
    log.debug("buildFactLists completed for " + focalPersonId);
    return focalPersonId;

}