Subversion Repositories SmartDukaan

Rev

Rev 3561 | Rev 5945 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

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 in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.model.v1.catalog.InventoryServiceException;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.OrderStatus;
import in.shop2020.serving.utils.FormattingUtils;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.TransactionClient;

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;
    List<String> orderDate = new ArrayList<String>();
        
    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);
            Collections.reverse(orders);
        } catch (Exception e)   {
                logger.error("Not able to get order information from service.");
        }
                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 (InventoryServiceException 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(brand.trim())){
                addition = 18;
        }
        cd.add(Calendar.MONTH, addition);
        SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
        return dateformat.format(cd.getTime());
    }
}