com.emc.smartcomm.UregApplication.java Source code

Java tutorial

Introduction

Here is the source code for com.emc.smartcomm.UregApplication.java

Source

/*******************************************************************************
* Copyright (c) 2014 Emc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the EMC License
* which accompanies this distribution, and is available at
* http://www.legal.emc.com
*
* Contributors:
* EMC. - initial API and implementation
*******************************************************************************/

package com.emc.smartcomm;

import java.io.InputStream;
import java.sql.Timestamp;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import com.emc.smartcomm.bean.BabProtocolRequest;
import com.emc.smartcomm.bean.BabProtocolResponse;
import com.emc.smartcomm.bean.GrsiRequest;
import com.emc.smartcomm.bean.GrsiResponse;
import com.emc.smartcomm.bean.SmartRegister;
import com.emc.smartcomm.bean.ValidateRegRequest;
import com.emc.smartcomm.bean.ValidateRegResponse;
import com.emc.smartcomm.domain.LogTransaction;
import com.emc.smartcomm.mock.PopulateData;
import com.emc.smartcomm.mock.PrepareLog;
import com.emc.smartcomm.service.LogTransactionService;
import com.emc.smartcomm.util.ExternalFileWriter;
import com.emc.smartcomm.util.FileReader;

/**
 * Component that pushes messages to the respective Queue
 *  
 * @author Barun
 */
@Component
public class UregApplication {

    @Autowired
    private LogTransactionService logTransactionService;
    @Resource(name = "uregRequest")
    private AmqpTemplate uregTemplate;
    @Resource(name = "grsiRequest")
    private AmqpTemplate grsiTemplate;
    @Resource(name = "validateRegRequest")
    private AmqpTemplate validateRegTemplate;
    @Resource(name = "babRequest")
    private AmqpTemplate babTemplate;
    @Resource(name = "rechargeRequest")
    private AmqpTemplate soapRechargeTemplate;

    static final Logger logger = LoggerFactory.getLogger(UregApplication.class);

    /**
     * @param register A SmartRegister instance 
     */
    public void uregApplication(SmartRegister register) {

        logger.info("Processing UREG Tranasctional Flow:");
        GrsiRequest greq = PopulateData.setGrsiRequest();
        GrsiResponse grsiResponse = PopulateData.setGrsiResponse();
        BabProtocolRequest breq = PopulateData.setBabRequest();
        BabProtocolResponse bres = PopulateData.setBabResponse();
        ValidateRegRequest vreg = PopulateData.setValidateRegRequest();
        ValidateRegResponse vres = PopulateData.setValidateRegResponse();
        greq.setSvcFlag(register.getChannel());
        String msg = register.toString();
        String path = "/appl/LogTransaction.txt";

        Timestamp txStartTime = PrepareLog.getCurrentTimeStamp();
        StopWatch sw = new StopWatch("UREG Transaction");
        sw.start("UREG Transaction");
        logger.debug("UREG Transaction Initiated:{}", txStartTime);
        StopWatch sw0 = new StopWatch("UREG REQUEST");
        sw0.start("UREG REQUEST");
        uregTemplate.convertAndSend(msg);
        sw0.stop();
        logger.debug(sw0.prettyPrint());
        logger.debug(sw0.shortSummary());

        StopWatch sw1 = new StopWatch("GRSI Request");
        sw1.start("GRSI Request");
        grsiTemplate.convertAndSend(greq);
        sw1.stop();
        logger.debug(sw1.prettyPrint());
        logger.debug(sw1.shortSummary());

        if ("PVS".equals(grsiResponse.getsx12())) // || "BAB".equals(grsiResponse.getsx13()))
        {
            StopWatch sw2 = new StopWatch("Validate Request:");
            sw2.start("Validate Request:");
            String validateRegText = vreg.toString();
            validateRegTemplate.convertAndSend(validateRegText);
            sw2.stop();
            logger.debug(sw2.prettyPrint());
            logger.debug(sw2.shortSummary());
        }

        if ("PPC".equals(grsiResponse.getsx03())) {

            StopWatch sw3 = new StopWatch("BAB Request");
            sw3.start("BAB Request:");
            babTemplate.convertAndSend("bab.Request", breq.toString());
            sw3.stop();
            logger.debug(sw3.prettyPrint());
            logger.debug(sw3.shortSummary());
        }

        grsiResponse.setsx03("NSN");
        if ("NSN".equals(grsiResponse.getsx03())) {

            InputStream is = getClass().getResourceAsStream("/mock/SOAPProtocolRecharge.txt");
            String message = FileReader.readFile(is);
            StopWatch sw4 = new StopWatch("SOAP Recharge Request: ");
            sw4.start("SOAP Recharge Request:");
            soapRechargeTemplate.convertAndSend(message);
            sw4.stop();
            logger.debug(sw4.prettyPrint());
            logger.debug(sw4.shortSummary());

        }

        Timestamp txEndTime = PrepareLog.getCurrentTimeStamp();
        logger.debug("Persisting Transaction log in gemxd and oracle");
        LogTransaction logTransaction = PrepareLog.prepareLog(greq, grsiResponse, breq, bres, vreg, vres);
        logTransaction.setTxnStartTime(txStartTime);
        logTransaction.setTxnEndTime(txEndTime);
        StopWatch sw5 = new StopWatch("Transaction Persistence: ");
        sw5.start("Transaction Persistence:");
        logTransactionService.logTransaction(logTransaction);
        sw5.stop();
        logger.debug(sw5.prettyPrint());
        logger.debug(sw5.shortSummary());
        ExternalFileWriter.writeToFile(path, PopulateData.populateLog());

        sw.stop();
        logger.debug(sw.prettyPrint());
        logger.debug(sw.shortSummary());
        logger.debug("UREG Transaction is Completed:{}", txEndTime);
        logger.info("UREG Transaction TimeSpan:{}", (txEndTime.getTime() - txStartTime.getTime()));

    }
}