Subversion Repositories SmartDukaan

Rev

Rev 3019 | Rev 3126 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3019 rajveer 1
package in.shop2020.serving.controllers;
2
 
3
import java.text.SimpleDateFormat;
4
import java.util.ArrayList;
3022 rajveer 5
import java.util.Arrays;
3019 rajveer 6
import java.util.Calendar;
7
import java.util.Collections;
8
import java.util.Date;
9
import java.util.HashMap;
10
import java.util.List;
11
import java.util.Map;
12
 
13
import in.shop2020.model.v1.catalog.InventoryService.Client;
14
import in.shop2020.model.v1.catalog.InventoryServiceException;
15
import in.shop2020.model.v1.order.Order;
16
import in.shop2020.model.v1.order.OrderStatus;
17
import in.shop2020.serving.utils.FormattingUtils;
18
import in.shop2020.thrift.clients.CatalogServiceClient;
19
import in.shop2020.thrift.clients.TransactionServiceClient;
20
 
21
import org.apache.log4j.Logger;
22
import org.apache.struts2.convention.annotation.InterceptorRef;
23
import org.apache.struts2.convention.annotation.InterceptorRefs;
24
import org.apache.struts2.convention.annotation.Result;
25
import org.apache.struts2.convention.annotation.Results;
26
import org.apache.thrift.TException;
27
 
28
/**
29
 * @author rajveer
30
 *
31
 */
32
 
33
@InterceptorRefs({
34
    @InterceptorRef("myDefault"),
35
    @InterceptorRef("login")
36
})
37
 
38
@Results({
39
    @Result(name="redirect", type="redirectAction", 
40
    		params = {"actionName" , "login"})
41
})
42
public class MyPurchasesController extends BaseController {
43
 
44
	private static final long serialVersionUID = 1L;
45
	private static Logger logger = Logger.getLogger(Class.class);
3022 rajveer 46
	private static final List<OrderStatus> statuses = Arrays.asList(new OrderStatus[] {OrderStatus.DELIVERY_SUCCESS});
47
 
3019 rajveer 48
    private FormattingUtils formattingUtils = new FormattingUtils();
49
 
50
    Map<Long, String> providerNames = new HashMap<Long, String>();
51
    List<Order> orders = null;
52
    List<String> orderDate = new ArrayList<String>();
53
 
54
    CatalogServiceClient csc = null;
55
    Client client = null;
56
	public MyPurchasesController() {
57
		super();
58
		try {
59
			csc = new CatalogServiceClient();
60
			client = csc.getClient();
61
		} catch (Exception e) {
62
			logger.error("Unable to get catalog service client", e);
63
		}
64
	}
65
 
66
	public String index(){
67
        TransactionServiceClient transactionServiceClient = null;
68
        in.shop2020.model.v1.order.TransactionService.Client orderClient = null;
69
        try{
70
            transactionServiceClient = new TransactionServiceClient();
71
            orderClient = transactionServiceClient.getClient();
3022 rajveer 72
            orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0, (new Date()).getTime(), statuses);
3019 rajveer 73
            Collections.reverse(orders);
74
        } catch (Exception e)   {
75
        	logger.error("Not able to get order information from service.");
76
        }
77
		return "index";
78
    }
79
 
80
	public String formatPrice(double price)    {
81
        return formattingUtils.formatPrice(price);
82
    }
83
 
84
    public List<Order> getOrders()  {
85
        return orders;
86
    }
87
 
88
    public List<String> getOrderDate()  {
89
        return orderDate;
90
    }
91
 
92
    public String formatDate(long timestamp){
93
    	Date date = new Date(timestamp);
94
    	SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
95
    	return dateformat.format(date);
96
    }
97
 
98
	public String getWarrantyDate(long timestamp, String brand){
99
    	Calendar cd = Calendar.getInstance();
100
    	cd.setTimeInMillis(timestamp);
101
    	int addition = 12;
102
    	if(brand.equalsIgnoreCase(brand.trim())){
103
    		addition = 18;
104
    	}
105
    	cd.add(Calendar.MONTH, addition);
106
    	SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
107
    	return dateformat.format(cd.getTime());
108
    }
109
 
110
	public long getCatalogId(long itemId){
111
		if(client != null){
112
			try {
113
				return client.getItem(itemId).getCatalogItemId();
114
			} catch (InventoryServiceException e) {
115
				logger.error("Not able to get item information from service.", e);
116
			} catch (TException e) {
117
				logger.error("Not able to get item information from service.", e);
118
			}
119
		}
120
		return 0;
121
	}
122
}