2017 June Release

Comprehensive Java examplePermanent link for this heading

The following example demonstrates the implementation of a use case for importing customer orders from an XML document. The XML document is stored in a structure of compound type COOSYSTEM@1.1:Content which is passed to the use case as an input parameter. An order object is created in Fabasoft Folio for each of the order records found in the XML document. Afterwards, the orders’ properties are populated with data from the XML records. Finally, the new order objects are added to the customer’s list of orders.

Example

XML File

<?xml version="1.0" encoding="ISO-8859-1"?>
<orders>
  <order>
    <orderid>1000251</orderid>
    <orderdate>22.01.2010</orderdate>
    <orderpositions>
      <orderposition>
        <productid>J82-K32-629</productid>
        <quantity>10</quantity>
      </orderposition>
      <orderposition>
        <productid>A72-G53-882</productid>
        <quantity>2</quantity>
      </orderposition>
    </orderpositions>
  </order>
  <order>
    <orderid>1000252</orderid>
    <orderdate>23.01.2010</orderdate>
    <orderpositions>
      <orderposition>
        <productid>B03-X53-341</productid>
        <quantity>1</quantity>
      </orderposition>
    </orderpositions>
  </order>
</orders>

app.ducx Use Case Language

usecase ImportOrders(Content xmlcontent) {
  variant Person {
    impl = java:APPDUCXSAMPLE.Person.ImportOrders;
  }
}

Java Source Code

package APPDUCXSAMPLE;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import static APPDUCXSAMPLE.generated.DUCX.coort;

import APPDUCXSAMPLE.generated.DUCX;
import
APPDUCXSAMPLE.generated.DUCXImplementation;

import APPDUCXSAMPLE.generated.COOSYSTEM_1_1.structs.Content;

import APPDUCXSAMPLE.generated.APPDUCXSAMPLE_200_300.APPDUCXSAMPLE_200_300;
import APPDUCXSAMPLE.generated.APPDUCXSAMPLE_200_300.classes.Order;
import APPDUCXSAMPLE.generated.APPDUCXSAMPLE_200_300.classes.Product;
import APPDUCXSAMPLE.generated.APPDUCXSAMPLE_200_300.structs.OrderPosition;

import CooLib.CooObject;
import CooLib.CooContent;

public class Person extends APPDUCXSAMPLE.generated.FSCFOLIO_1_1001.
  classes.Person {
    public Person(CooObject obj) {
      super(obj);
  }

  private String getXMLValue(Node parentnode, String tagname) {
    Element elmnt = (Element) parentnode;
    NodeList nodelst = elmnt.getElementsByTagName(tagname);
    Element childelmnt = (Element) nodelst.item(0);
    return childelmnt.getChildNodes().item(0).getNodeValue();
  }

  @DUCXImplementation("APPDUCXSAMPLE@200.200:ImportOrders")
  public void ImportOrders(final Content xmlcontent) throws Exception {
    try {
      // Build XML DOM from XML content
      CooContent content = xmlcontent.COOSYSTEM_1_1_contcontent;
      File file = new File(content.GetFile("", false));
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(file);
      doc.getDocumentElement().normalize();

      NodeList ordernodelst = doc.getElementsByTagName("order");

      for (int i = 0; i < ordernodelst.getLength(); i++) {
        // Initialize the list of order positions
        ArrayList<OrderPosition> poslst = new ArrayList<OrderPosition>();

        Node ordernode = ordernodelst.item(i);

        if (ordernode.getNodeType() == Node.ELEMENT_NODE) {
          // Retrieve order date from XML
          String orderDatestr = getXMLValue(ordernode, "orderdate");
          SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy");
          Date orderdate = dateformat.parse(orderDatestr);

          // Retrieve list of order positions from XML
          Element orderelmnt = (Element) ordernode;
          Element orderpositionselmnt = (Element) orderelmnt.
            getElementsByTagName("orderpositions").item(0);
          NodeList orderpositionnodelst = orderpositionselmnt.
            getElementsByTagName("orderposition");

          // Iterate through order positions (an order may consist of one
          // or more order positions)

          for (int j = 0; j < orderpositionnodelst.getLength(); j++) {
            Node orderposnode = orderpositionnodelst.item(j);

            if (orderposnode.getNodeType() == Node.ELEMENT_NODE) {
              // Retrieve product ID for current order position
              String productid = getXMLValue(orderposnode, "productid");

              // Retrieve quantity
              Long orderquantity = new Long(getXMLValue(orderposnode,
                "quantity"));

              // Find existing product object matching the product ID
              // in Fabasoft Folio

              CooObject[] orderproducts = coort.SearchObjects(DUCX.
                cootx, "LIMIT 1 SELECT objname FROM APPDUCXSAMPLE@200.200:
                Product WHERE .
APPDUCXSAMPLE@200.200:productid = \"" +
                productid + "\""
);

              if (orderproducts.length > 0) {
                Product orderproduct = Product.from(orderproducts[0]);

                if (orderproduct.isValid() && orderquantity != null) {
                  // Create new order position item and add it to the
                  // list of order positions

                  OrderPosition item = OrderPosition.create();
                  item.APPDUCXSAMPLE_200_300_product = orderproduct;
                  item.APPDUCXSAMPLE_200_300_quantity = orderquantity;
                  poslst.add(item);
                }
                else {
                  coort.SetError(APPDUCXSAMPLE_200_300.
                  getErrorMessage_InvalidOrder());
                }
              }
              else {
                coort.SetError(APPDUCXSAMPLE_200_300.
                getErrorMessage_ProductNotFound());
              }
            }
          }

          // Create new order object in Fabasoft Folio
          Order order = Order.create();

          // Set order date and positions to order object
          order.APPDUCXSAMPLE_200_300_orderdate = orderdate;
          order.APPDUCXSAMPLE_200_300_orderpositions = poslst;

          // Add the new order to the customer's list of orders
          order.COODESK_1_1_ShareObject(null, null, APPDUCXSAMPLE_200_300.
            getProperty_customerorders(), this);
        }
      }
    } catch (Exception e) {
      coort.Trace("Exception occurred: " + e.getMessage());
      throw e;
    }
  }
}