{"id":2640,"date":"2013-01-07T13:28:47","date_gmt":"2013-01-07T18:28:47","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=2640"},"modified":"2016-08-01T08:41:21","modified_gmt":"2016-08-01T13:41:21","slug":"example-of-creating-an-order-in-atg","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2013\/01\/07\/example-of-creating-an-order-in-atg\/","title":{"rendered":"Example of creating an order in ATG"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<p>However, if you want to have a better understanding of all components and the flow of the order process, this can be useful.<\/p>\n<p>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).<\/p>\n<p>Focus on the flow, and improve it where you can.<\/p>\n<p>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 &#8220;under the hood&#8221;.<\/p>\n<p>We start with a droplet to print a list of SKU&#8217;s on a page.  A droplet is just a servlet in ATG that generates content which you can include on a page.<\/p>\n<p>UPDATE 6\/20\/2013 &#8211; I added all the code, including the droplet, form handler, and JSP&#8217;s.<\/p>\n<pre lang=\"java\">\r\nimport java.io.*;\r\nimport javax.servlet.*;\r\nimport atg.servlet.*;\r\nimport atg.nucleus.*;\r\nimport atg.adapter.gsa.*;\r\nimport atg.repository.*;\r\nimport atg.repository.rql.*;\r\n\r\npublic class ListSkus extends DynamoServlet {\r\n  public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response) throws ServletException, IOException {\r\n    try {\r\n      GSARepository r = (GSARepository)Nucleus.getGlobalNucleus().resolveName(\"\/atg\/commerce\/catalog\/ProductCatalog\");\r\n      RepositoryView view = r.getView(\"sku\");\r\n      RqlStatement rql = RqlStatement.parseRqlStatement(\"listPrice > 0\");\r\n      Object params[] = new Object[0];\r\n      RepositoryItem[] answer = rql.executeQuery(view,params);\r\n      if (answer != null) {\r\n        for(int i =0; i < answer.length; i++) {\r\n          request.setParameter(\"items\", answer[i]);\r\n          request.serviceParameter(\"output\", request, response);\r\n        }\r\n      }\r\n      else {\r\n        request.setParameter(\"items\", \"No SKU's found\");\r\n        request.serviceParameter(\"output\", request, response);\r\n      }\r\n    }\r\n    catch (Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>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.<\/p>\n<p>In our case, we will take the SKU and quantity entered by a user and create a record in the database.<\/p>\n<pre lang=\"java\">\r\nimport java.io.*;\r\nimport javax.servlet.*;\r\n\r\nimport atg.adapter.gsa.*;\r\nimport atg.commerce.order.*;\r\nimport atg.commerce.order.purchase.*;\r\nimport atg.commerce.pricing.*;\r\nimport atg.commerce.profile.*;\r\nimport atg.core.util.*;\r\nimport atg.nucleus.*;\r\nimport atg.droplet.*;\r\nimport atg.servlet.*;\r\nimport atg.repository.*;\r\nimport atg.repository.rql.*;\r\nimport atg.service.pipeline.*;\r\n\r\npublic class orderRunner extends GenericFormHandler {\r\n\r\n  String sku;\r\n  String product;\r\n  int quantity;\r\n\r\n  public orderRunner() {}\r\n\r\n  public void setQuantity(int quantity) {\r\n    this.quantity = quantity;\r\n  }\r\n\r\n  public int getQuantity() {\r\n    return quantity;\r\n  }\r\n\r\n  public void setSku(String sku) {\r\n    this.sku = sku;\r\n  }\r\n\r\n  public String getSku() {\r\n    return sku;\r\n  }\r\n\r\n  public void setProduct(String product) {\r\n    String s = product.substring(product.indexOf(\":\") + 1);\r\n    this.product = s.replace(\"]\",\"\");\r\n  }\r\n\r\n  public String getProduct() {\r\n    return product;\r\n  }\r\n\r\n  public boolean handleSave(DynamoHttpServletRequest pRequest,\r\n                            DynamoHttpServletResponse pResponse) throws ServletException, IOException {\r\n    try {\r\n      OrderManager om = (OrderManager)Nucleus.getGlobalNucleus().resolveName(\"\/atg\/commerce\/order\/OrderManager\");\r\n      CommerceItemManager cim = (CommerceItemManager)Nucleus.getGlobalNucleus().resolveName(\"\/atg\/commerce\/order\/CommerceItemManager\");\r\n      ShippingGroupManager st = (ShippingGroupManager)Nucleus.getGlobalNucleus().resolveName(\"\/atg\/commerce\/order\/ShippingGroupManager\");\r\n      PricingTools pt = (PricingTools)Nucleus.getGlobalNucleus().resolveName(\"\/atg\/commerce\/pricing\/PricingTools\");\r\n      CommerceProfileTools pft = (CommerceProfileTools)Nucleus.getGlobalNucleus().resolveName(\"\/atg\/userprofiling\/ProfileTools\");\r\n\r\n      String profileId = \"470000\";\r\n      Order order = om.createOrder(profileId);\r\n\r\n      \/\/\/sku chosen must have populated list_price in dcs_sku table\r\n      CommerceItem item1 = cim.createCommerceItem(getSku(),getProduct(),getQuantity());\r\n      cim.addItemToOrder(order, item1);\r\n\r\n      GSARepository r = (GSARepository)Nucleus.getGlobalNucleus().resolveName(\"\/atg\/userprofiling\/ProfileAdapterRepository\");\r\n\r\n      RepositoryView view = r.getView(\"contactInfo\");\r\n      RqlStatement rql = RqlStatement.parseRqlStatement(\"ownerId = ?0\");\r\n      Object params[] = new Object[1];\r\n      params[0] = new String(profileId);\r\n      RepositoryItem[] answer = rql.executeQuery(view,params);\r\n      myContactInfo rci = null;\r\n      if (answer != null) {\r\n        for (int i=0; i<answer.length; i++) {\r\n          MutableRepository mr = (MutableRepository)answer[i].getRepository();\r\n          MutableRepositoryItem mri = mr.getItemForUpdate(answer[i].getRepositoryId(),answer[i].getItemDescriptor().getItemDescriptorName());\r\n          rci = new myContactInfo(mri);\r\n        }\r\n      }\r\n      if (isLoggingDebug()) {\r\n        try {\r\n          vlogDebug(rci.getFirstName());\r\n          vlogDebug(rci.getLastName());\r\n          vlogDebug(rci.getAddress1());\r\n          vlogDebug(rci.getAddress2());\r\n          vlogDebug(rci.getAddress3());\r\n          vlogDebug(rci.getState());\r\n          vlogDebug(rci.getPhoneNumber());\r\n          vlogDebug(rci.getCity());\r\n          vlogDebug(rci.getPostalCode());\r\n          vlogDebug(rci.getCountry());\r\n        }\r\n        catch (Exception e) {\r\n          e.printStackTrace();\r\n        }\r\n      }\r\n\r\n      view = r.getView(\"user\");\r\n      rql = RqlStatement.parseRqlStatement(\"id = ?0\");\r\n      answer = rql.executeQuery(view,params);\r\n\r\n      HardgoodShippingGroup sg = (HardgoodShippingGroup)order.getShippingGroups().get(0);\r\n      sg.setShippingAddress(rci);\r\n      cim.addItemQuantityToShippingGroup(order, item1.getId(), sg.getId(), getQuantity());\r\n      if (isLoggingDebug())\r\n        vlogDebug(sg.toString());\r\n\r\n      PaymentGroup pg = (PaymentGroup)order.getPaymentGroups().get(0);\r\n      om.addRemainingOrderAmountToPaymentGroup(order, pg.getId());\r\n\r\n      pft.copyCreditCardToPaymentGroup(\"Real card\", (CreditCard)pg, answer[0], new java.util.Locale(\"en\"));\r\n      pt.priceOrderTotal(order);\r\n\r\n      if (isLoggingDebug()) {\r\n        vlogDebug(\"Payment method = \" + pg.getPaymentMethod());\r\n        vlogDebug(\"Pricing info = \" + pt.toString());\r\n        vlogDebug(\"CreditCard = \" + pft.getDefaultCreditCard(answer[0]));\r\n      }\r\n\r\n      PipelineResult pr = om.processOrder(order);\r\n      if (pr.hasErrors()) {\r\n        Object[] keys = pr.getErrorKeys();\r\n        for (int i = 0; i < keys.length; i++) {\r\n          System.out.println(\"ERROR = \" + pr.getError(keys[i]));\r\n        }\r\n        pResponse.sendRedirect(\"orderError.jsp\");\r\n        return false;\r\n      }\r\n      return true;\r\n    }\r\n    catch (Exception e) {\r\n      e.printStackTrace();\r\n      return false;\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>Once we have compiled our components, we build simple JSP's to use them.<\/p>\n<p>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...<\/p>\n<pre lang=\"java\">\r\n<dsp:importbean bean=\"\/atg\/dynamo\/droplet\/ForEach\"\/>\r\n<dsp:page>\r\n\r\n<html>\r\n<head><title>Products for sale<\/title><\/head>\r\n<body>\r\n\r\n  <table border=\"3\">\r\n    <dsp:droplet name=\"\/ListSkus\">\r\n      <dsp:oparam name=\"output\">\r\n        <tr>\r\n          <td>\r\n            <dsp:a href=\"ProcessOrder.jsp\"><dsp:valueof param=\"items.displayName\"\/>\r\n              <dsp:param name=\"sku\" param=\"items.id\" \/>\r\n              <dsp:param name=\"product\" param=\"items.parentProducts\" \/>\r\n            <\/dsp:a>\r\n          <\/td>\r\n          <td>$<dsp:valueof param=\"items.listPrice\"\/><\/td>\r\n        <\/tr>\r\n      <\/dsp:oparam>\r\n    <\/dsp:droplet>\r\n  <\/table>\r\n\r\n<\/body>\r\n<\/html>\r\n\r\n<\/dsp:page>\r\n<\/pre>\n<p>...and the page to which we submit...<\/p>\n<pre lang=\"java\">\r\n<dsp:page>\r\n  <dsp:importbean bean=\"orderRunner\"\/>\r\n  <dsp:form name=\"OptionalName\" action=\"handleForm.jsp\" >\r\n    SKU:<dsp:input bean=\"orderRunner.sku\" type=\"text\" value=\"${param.sku}\"\/><br\/>\r\n    Product:<dsp:input bean=\"orderRunner.product\" type=\"text\" value=\"${param.product}\"\/><br\/>\r\n    Quantity:<dsp:input bean=\"orderRunner.quantity\" type=\"text\"\/><br\/>\r\n    <dsp:input bean=\"orderRunner.save\" type=\"submit\" value=\"Place Order\"><\/dsp:input>\r\n    <dsp:input bean=\"orderRunner.cancel\" type=\"submit\" value=\"Clear\"><\/dsp:input>\r\n  <\/dsp:form>\r\n<\/dsp:page>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2013\/01\/07\/example-of-creating-an-order-in-atg\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[37,38,24,25],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2640"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2640"}],"version-history":[{"count":16,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2640\/revisions"}],"predecessor-version":[{"id":5559,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2640\/revisions\/5559"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2640"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}