Change the Implementation Method

In this task, you change the implementation method to query the subscriber before an offer purchase, purchase the offer, query the subscriber after the purchase, and add logging functions to help trace through the code.

Procedure

  1. Edit the DemoServices.java file.
  2. Add the following code to change the implementation method.
    0001 |  public int queryAndPurchase(
    0002 |    RestQueryTerm searchTerm,
    0003 |    DemoRequestSubscriberPurchase input,
    0004 |    DemoResponseSubscriberPurchase output)
    0005 |  {
    0006 |    SubscriberQuery subQry = new SubscriberQuery();
    0007 |    MtxPurchasedOfferData offer = input.getOffer();
    0008 |    SubscriberResponse pre = null;
    0009 |    MtxResponsePurchase purchase = null;
    0010 |    SubscriberResponse post = null;
    0011 |    MtxSubscriberSearchData searchData = null;
    0012 | 	
    0013 |    int rc = 0;
    0014 |    ENTER(m_logger, "queryAndPurchase");
    0015 |    try {
    0016 |        String route = getRoute(MatrixxContext.getRequest());
    0017 |        pre = query(route, searchTerm, subQry);
    0018 |        output.setPrePurchase(pre);
    0019 |        if (pre.getResult() == 0) {
    0020 |            searchData = subQry.getSubscriberSearchData();
    0021 |            purchase = purchase(route, searchData, offer);
    0022 |            output.setPurchase(purchase);
    0023 |            if (purchase.getResult() == 0) {
    0024 |                post = query(route, searchTerm, subQry);
    0025 |                output.setPostPurchase(post);
    0026 |                output.setResult(post.getResult());
    0027 |                output.setResultText(post.getResultText());
    0028 |            }
    0029 |            else {
    0030 |                output.setResult(purchase.getResult());
    0031 |                output.setResultText(purchase.getResultText());
    0032 |            }
    0033 |          }
    0034 |          else {
    0035 |              output.setResult(pre.getResult());
    0036 |              output.setResultText(pre.getResultText());
    0037 |          }
    0038 |      }
    0039 |      catch(Exception ex) {
    0040 |          String m = ex.getMessage();
    0041 |          output.setResult(-1L);
    0042 |          output.setResultText(m);
    0043 |          ERROR(getClass(), "queryAndPurchase: " + m, ex);
    0044 |      }
    0045 |      return LEAVE(rc, m_logger, "queryAndPurchase");
    0046 |  }
    0047 | 	}
    
    Note the following:
    • Lines 6-11 — Define local variables.
    • Line 14 and line 45 — Use logging functions that aid in tracing through the code.
    • Line 17 — Performs the pre-purchase subscriber query.
    • Line 21 — Purchases the offer.
    • Line 24 — Performs a post-purchase subscriber query.
  3. Save the file.

What to do next

Add the required imports to the file.