Final DemoServices Java Class File

The following code shows the complete DemoServices.java file for the purchase extension sample.

package com.matrixx.rsgateway.configuration;

import com.matrixx.datacontainer.mdc.DemoRequestSubscriberPurchase;
import com.matrixx.datacontainer.mdc.DemoResponseSubscriberPurchase;
import com.matrixx.datacontainer.mdc.MtxPurchasedOfferData;
import com.matrixx.datacontainer.mdc.MtxRequestSubscriberPurchaseOffer;
import com.matrixx.datacontainer.mdc.MtxResponsePurchase;
import com.matrixx.datacontainer.mdc.MtxSubscriberSearchData;
import com.matrixx.datacontainer.mdc.SubscriberQuery;
import com.matrixx.datacontainer.mdc.SubscriberResponse;
import com.matrixx.datacontainer.rest.JsonStubs;
import com.matrixx.datacontainer.rest.RestQueryTerm;
import com.matrixx.platform.JsonObject;
import com.matrixx.platform.MatrixxContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.matrixx.platform.LogUtils.ENTER;
import static com.matrixx.platform.LogUtils.ERROR;
import static com.matrixx.platform.LogUtils.LEAVE;

import org.springframework.stereotype.Service;

@Service
public class DemoServices extends JsonStubs {
    /**
     * logger for errors and warnings
     */
    private final Logger m_logger = LoggerFactory.getLogger(getClass());


    private SubscriberResponse query(String route, RestQueryTerm term, SubscriberQuery req)
        throws Exception
    {
        SubscriberResponse rsp = new SubscriberResponse();
        JsonObject json = new JsonObject();

        service_subscriber_query(route, json, term, req, rsp);
        return rsp;
    }
    private MtxResponsePurchase purchase(String route, MtxSubscriberSearchData   searchData,
        MtxPurchasedOfferData     offer)
    {
        MtxRequestSubscriberPurchaseOffer purchaseRequest
            = new MtxRequestSubscriberPurchaseOffer();
        MtxResponsePurchase response = null;
        purchaseRequest.setSubscriberSearchData(searchData);
        purchaseRequest.appendOfferRequestArray(offer);
        response = m_api.subscriberPurchaseOffer(route, purchaseRequest);
        return response;
    }

    public int queryAndPurchase(
        RestQueryTerm searchTerm,
        DemoRequestSubscriberPurchase input,
        DemoResponseSubscriberPurchase output)
    {
        SubscriberQuery subQry = new SubscriberQuery();
        MtxPurchasedOfferData offer = input.getOffer();
        SubscriberResponse pre = null;
        MtxResponsePurchase purchase = null;
        SubscriberResponse post = null;
        MtxSubscriberSearchData searchData = null;

        int rc = 0;
        ENTER(m_logger, "queryAndPurchase");
        try {
            String route = getRoute(MatrixxContext.getRequest());
            pre = query(route, searchTerm, subQry);
            output.setPrePurchase(pre);
            if (pre.getResult() == 0) {
                searchData = subQry.getSubscriberSearchData();
                purchase = purchase(route, searchData, offer);
                output.setPurchase(purchase);
                if (purchase.getResult() == 0) {
                    post = query(route, searchTerm, subQry);
                    output.setPostPurchase(post);
                    output.setResult(post.getResult());
                    output.setResultText(post.getResultText());
                }
                else {
                    output.setResult(purchase.getResult());
                    output.setResultText(purchase.getResultText());
                }
            }
            else {
                output.setResult(pre.getResult());
                output.setResultText(pre.getResultText());
            }
        }
        catch(Exception ex) {
            String m = ex.getMessage();
            output.setResult(-1L);
            output.setResultText(m);
            ERROR(getClass(), "queryAndPurchase: " + m, ex);
        }
        return LEAVE(rc, m_logger, "queryAndPurchase");
    }
}