Subversion Repositories SmartDukaan

Rev

Rev 7343 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import in.shop2020.model.v1.order.HotspotStore;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.TransactionServiceException;
import in.shop2020.payments.PaymentException;
import in.shop2020.serving.utils.FormattingUtils;
import in.shop2020.thrift.clients.PaymentClient;
import in.shop2020.thrift.clients.TransactionClient;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.thrift.TException;

public class OrderSuccessController extends BaseController{

    /**
     * 
     */
    private static final long serialVersionUID = 1;
    private long userId;
    private long paymentId;
    private long txnId;
    private List<Order> orders = new ArrayList<Order>();
    private FormattingUtils formattingUtils = new FormattingUtils();
    
    
public String index() {
        
        String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
        if(loginStatus == null || !loginStatus.equals("TRUE")){
            return "authfail";
        }
        
        storeId = Long.parseLong((String) request.getSession().getAttribute("STORE_ID"));
        if(!hotspotStores.containsKey(storeId)){
            try{
                HotspotStore hotSpotStore = (new TransactionClient()).getClient().getHotspotStore(storeId, "");
                hotspotStores.put(storeId, hotSpotStore);
            } catch (Exception e) {
                log.error("Unable to get store", e);
            }
        }
    
        PaymentClient paymentServiceClient = null;
        TransactionClient transactionServiceClient = null;
        
        try {
            paymentServiceClient = new PaymentClient();
            transactionServiceClient = new TransactionClient();
        } catch (Exception e1) {
            // TODO Nothing to worry
            log.error("Unable to initialize the client for either payment or transaction or user service", e1);
        }
        
        try {
            txnId = paymentServiceClient.getClient().getPayment(paymentId).getMerchantTxnId();
            setOrders(transactionServiceClient.getClient().getOrdersForTransaction(txnId, userId));
        } catch (PaymentException e) {
            log.error("Payment service not responding. Payment id is" + paymentId, e);
        } catch (TException e) {
            log.error("Thrift service exception. Payment id is" + paymentId, e);
        } catch (TransactionServiceException e) {
            log.error("Transaction service exception. Payment id is" + paymentId, e);
        }
        
        return "index";
    }
    
    
    public long getPaymentId() {
        return paymentId;
    }
    public void setPaymentId(long paymentId) {
        this.paymentId = paymentId;
    }
    public long getUserId() {
        return userId;
    }
    public void setUserId(long userId) {
        this.userId = userId;
    }
    
    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }


    public List<Order> getOrders() {
        return orders;
    }
    
    public String formatPrice(double price)    {
        return formattingUtils.formatPrice(price);
    }
    
    public static String formatDate(long timestamp){
        SimpleDateFormat dateformat = new SimpleDateFormat("dd MMMM yyyy");
        return dateformat.format(new Date(timestamp));  
    }
}