Example of creating an order in ATG

I spent a lot of time understanding the plumbing of the Pipeline, OrderManager, etc. As I have indicated in the past, simple startup examples of ATG development are hard to find.

The out of the box form handlers work far better, and eliminate much of what is below. In other words, what is below is not even close to what should be in production.

However, if you want to have a better understanding of all components and the flow of the order process, this can be useful.

Much of it is hardcoded, but functionally works. It needs transaction management as well as synchronization code (my understanding is operations on orders should always be synchronized, which makes sense).

Focus on the flow, and improve it where you can.

Once again, the form handlers supplied by ATG do all of this far better, but this was a great way for me to have a better understanding of how everything works “under the hood”.

We start with a droplet to print a list of SKU’s on a page. A droplet is just a servlet in ATG that generates content which you can include on a page.

UPDATE 6/20/2013 – I added all the code, including the droplet, form handler, and JSP’s.

import java.io.*;
import javax.servlet.*;
import atg.servlet.*;
import atg.nucleus.*;
import atg.adapter.gsa.*;
import atg.repository.*;
import atg.repository.rql.*;

public class ListSkus extends DynamoServlet {
  public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response) throws ServletException, IOException {
    try {
      GSARepository r = (GSARepository)Nucleus.getGlobalNucleus().resolveName("/atg/commerce/catalog/ProductCatalog");
      RepositoryView view = r.getView("sku");
      RqlStatement rql = RqlStatement.parseRqlStatement("listPrice > 0");
      Object params[] = new Object[0];
      RepositoryItem[] answer = rql.executeQuery(view,params);
      if (answer != null) {
        for(int i =0; i < answer.length; i++) {
          request.setParameter("items", answer[i]);
          request.serviceParameter("output", request, response);
        }
      }
      else {
        request.setParameter("items", "No SKU's found");
        request.serviceParameter("output", request, response);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

We then compile a form handler class. A form handler is just a component that takes action based on specific user input. For example, it can save a record, redirect a user to another page, etc.

In our case, we will take the SKU and quantity entered by a user and create a record in the database.

import java.io.*;
import javax.servlet.*;

import atg.adapter.gsa.*;
import atg.commerce.order.*;
import atg.commerce.order.purchase.*;
import atg.commerce.pricing.*;
import atg.commerce.profile.*;
import atg.core.util.*;
import atg.nucleus.*;
import atg.droplet.*;
import atg.servlet.*;
import atg.repository.*;
import atg.repository.rql.*;
import atg.service.pipeline.*;

public class orderRunner extends GenericFormHandler {

  String sku;
  String product;
  int quantity;

  public orderRunner() {}

  public void setQuantity(int quantity) {
    this.quantity = quantity;
  }

  public int getQuantity() {
    return quantity;
  }

  public void setSku(String sku) {
    this.sku = sku;
  }

  public String getSku() {
    return sku;
  }

  public void setProduct(String product) {
    String s = product.substring(product.indexOf(":") + 1);
    this.product = s.replace("]","");
  }

  public String getProduct() {
    return product;
  }

  public boolean handleSave(DynamoHttpServletRequest pRequest,
                            DynamoHttpServletResponse pResponse) throws ServletException, IOException {
    try {
      OrderManager om = (OrderManager)Nucleus.getGlobalNucleus().resolveName("/atg/commerce/order/OrderManager");
      CommerceItemManager cim = (CommerceItemManager)Nucleus.getGlobalNucleus().resolveName("/atg/commerce/order/CommerceItemManager");
      ShippingGroupManager st = (ShippingGroupManager)Nucleus.getGlobalNucleus().resolveName("/atg/commerce/order/ShippingGroupManager");
      PricingTools pt = (PricingTools)Nucleus.getGlobalNucleus().resolveName("/atg/commerce/pricing/PricingTools");
      CommerceProfileTools pft = (CommerceProfileTools)Nucleus.getGlobalNucleus().resolveName("/atg/userprofiling/ProfileTools");

      String profileId = "470000";
      Order order = om.createOrder(profileId);

      ///sku chosen must have populated list_price in dcs_sku table
      CommerceItem item1 = cim.createCommerceItem(getSku(),getProduct(),getQuantity());
      cim.addItemToOrder(order, item1);

      GSARepository r = (GSARepository)Nucleus.getGlobalNucleus().resolveName("/atg/userprofiling/ProfileAdapterRepository");

      RepositoryView view = r.getView("contactInfo");
      RqlStatement rql = RqlStatement.parseRqlStatement("ownerId = ?0");
      Object params[] = new Object[1];
      params[0] = new String(profileId);
      RepositoryItem[] answer = rql.executeQuery(view,params);
      myContactInfo rci = null;
      if (answer != null) {
        for (int i=0; i

Once we have compiled our components, we build simple JSP's to use them.

We start with the page that will list our SKU's, as well as a DSP href tag that will push our choice to a JSP that calls our form handler...





Products for sale


  
      
        
$

...and the page to which we submit...


  
  
    SKU:
Product:
Quantity:

7 comments for “Example of creating an order in ATG

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.