Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

import in.shop2020.model.v1.catalog.BrandInfo;
import in.shop2020.model.v1.catalog.CatalogService.Client;
import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.OrderStatus;
import in.shop2020.model.v1.order.TransactionService;
import in.shop2020.serving.utils.FormattingUtils;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.TransactionClient;

import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.thrift.TException;


/**
 * @author rajveer
 *
 */

@InterceptorRefs({
    @InterceptorRef("myDefault"),
    @InterceptorRef("login")
})

@Results({
    @Result(name="redirect", type="redirectAction", 
                params = {"actionName" , "login"})
})
public class MyPurchasesController extends BaseController {

        private static final long serialVersionUID = 1L;
        private static Logger logger = Logger.getLogger(Class.class);
        private static final List<OrderStatus> statuses = Arrays.asList(new OrderStatus[] {OrderStatus.DELIVERY_SUCCESS});
        
    private FormattingUtils formattingUtils = new FormattingUtils();

    Map<Long, String> providerNames = new HashMap<Long, String>();
    List<Order> orders = null;
    Map<String, BrandInfo> brandInfo = null;
    List<String> orderDate = new ArrayList<String>();
    
    private String orderId;
        
    CatalogClient csc = null;
    Client client = null;
        public MyPurchasesController() {
                super();
                try {
                        csc = new CatalogClient();
                        client = csc.getClient();
                } catch (Exception e) {
                        logger.error("Unable to get catalog service client", e);
                }
        }
    
        public String index(){
        TransactionClient transactionServiceClient = null;
        in.shop2020.model.v1.order.TransactionService.Client orderClient = null;
        try{
            transactionServiceClient = new TransactionClient();
            orderClient = transactionServiceClient.getClient();
            orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0, (new Date()).getTime(), statuses);
            brandInfo = client.getBrandInfo();
            Collections.reverse(orders);
        } catch (Exception e)   {
                logger.error("Not able to get order information from service.");
        }
                return "index";
    }
        
        public String downloadPolicyDoc() {
            ByteBuffer buffer = null;
        try {
            if (orderId == null || orderId.isEmpty()) {
                logger.error("Order Id is null or empty");
                return "index";
            }
            TransactionClient transactionServiceClient = new TransactionClient();
            TransactionService.Client orderClient = transactionServiceClient.getClient();
            buffer = orderClient.getDocument(1, Long.parseLong(orderId));
            if(!buffer.hasArray()) {
                logger.error("The policy doc is not backed by an accessible byte buffer");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        response.setHeader("Content-disposition", "inline; filename=" + orderId + "-policy.pdf");

        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            sos.write(buffer.array());
            sos.flush();
        } catch (Exception e) {
            System.out.println("Unable to stream the manifest file");
        }
        return "index";
        }
    
        public String downloadInvoice() {
            ByteBuffer buffer = null;
        try {
            if (orderId == null || orderId.isEmpty()) {
                logger.error("Order Id is null or empty");
                return "index";
            }
            TransactionClient transactionServiceClient = new TransactionClient();
            TransactionService.Client orderClient = transactionServiceClient.getClient();
            buffer = orderClient.retrieveInvoice(Long.parseLong(orderId), userinfo.getUserId());
            if(!buffer.hasArray()) {
                logger.error("The invoice does not found");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        response.setHeader("Content-disposition", "inline; filename=invoice-" + orderId + ".pdf");

        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            sos.write(buffer.array());
            sos.flush();
        } catch (Exception e) {
            System.out.println("Unable to stream the invoice file");
        }
        return "index";
        }

        public String formatPrice(double price)    {
        return formattingUtils.formatPrice(price);
    }
        
    public List<Order> getOrders()  {
        return orders;
    }
    
    public List<String> getOrderDate()  {
        return orderDate;
    }
    
    public String formatDate(long timestamp){
        Date date = new Date(timestamp);
        SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
        return dateformat.format(date);
    }
    
    public long getCatalogId(long itemId){
        long catalogId = 0;
        try {
                catalogId = csc.getClient().getItem(itemId).getCatalogItemId();
                } catch (CatalogServiceException e) {
                        logger.error("Unable to get item information." + e);
                } catch (TException e) {
                        logger.error("Unable to get item information." + e);
                }
                return catalogId;
    }
    
        public String getWarrantyDate(long timestamp, String brand){
        Calendar cd = Calendar.getInstance();
        cd.setTimeInMillis(timestamp);
        int addition = 12;
        if(brand.equalsIgnoreCase("blackberry"))        {
                addition = 18;
        }
        cd.add(Calendar.MONTH, addition);
        SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
        return dateformat.format(cd.getTime());
    }
        
        public String getInsuranceExpiryDate(long timestamp, long insurer){
            if(insurer == 0) {
                return "N/A";
            }
        Calendar cd = Calendar.getInstance();
        cd.setTimeInMillis(timestamp);
        int addition = 12;
        cd.add(Calendar.MONTH, addition);
        SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
        return dateformat.format(cd.getTime());
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
    
    public String getSupportUrl(String brand){
        if (brandInfo.get(brand) != null){
                return brandInfo.get(brand).getServiceCenterLocatorUrl(); 
        }else {
                return null;
        }
    }
}