Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
3019 rajveer 1
package in.shop2020.serving.controllers;
2
 
8274 amit.gupta 3
import in.shop2020.model.v1.catalog.BrandInfo;
4
import in.shop2020.model.v1.catalog.CatalogService.Client;
5
import in.shop2020.model.v1.catalog.CatalogServiceException;
6
import in.shop2020.model.v1.order.Order;
7
import in.shop2020.model.v1.order.OrderStatus;
8
import in.shop2020.model.v1.order.TransactionService;
9
import in.shop2020.serving.utils.FormattingUtils;
10
import in.shop2020.thrift.clients.CatalogClient;
11
import in.shop2020.thrift.clients.TransactionClient;
12
 
6915 anupam.sin 13
import java.nio.ByteBuffer;
3019 rajveer 14
import java.text.SimpleDateFormat;
15
import java.util.ArrayList;
3022 rajveer 16
import java.util.Arrays;
3019 rajveer 17
import java.util.Calendar;
18
import java.util.Collections;
19
import java.util.Date;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
 
6915 anupam.sin 24
import javax.servlet.ServletOutputStream;
25
 
3019 rajveer 26
import org.apache.log4j.Logger;
27
import org.apache.struts2.convention.annotation.InterceptorRef;
28
import org.apache.struts2.convention.annotation.InterceptorRefs;
29
import org.apache.struts2.convention.annotation.Result;
30
import org.apache.struts2.convention.annotation.Results;
3950 rajveer 31
import org.apache.thrift.TException;
3019 rajveer 32
 
3561 rajveer 33
 
3019 rajveer 34
/**
35
 * @author rajveer
36
 *
37
 */
38
 
39
@InterceptorRefs({
40
    @InterceptorRef("myDefault"),
41
    @InterceptorRef("login")
42
})
43
 
44
@Results({
45
    @Result(name="redirect", type="redirectAction", 
46
    		params = {"actionName" , "login"})
47
})
48
public class MyPurchasesController extends BaseController {
49
 
50
	private static final long serialVersionUID = 1L;
51
	private static Logger logger = Logger.getLogger(Class.class);
3022 rajveer 52
	private static final List<OrderStatus> statuses = Arrays.asList(new OrderStatus[] {OrderStatus.DELIVERY_SUCCESS});
53
 
3019 rajveer 54
    private FormattingUtils formattingUtils = new FormattingUtils();
55
 
56
    Map<Long, String> providerNames = new HashMap<Long, String>();
57
    List<Order> orders = null;
8274 amit.gupta 58
    Map<String, BrandInfo> brandInfo = null;
3019 rajveer 59
    List<String> orderDate = new ArrayList<String>();
6915 anupam.sin 60
 
61
    private String orderId;
3019 rajveer 62
 
3126 rajveer 63
    CatalogClient csc = null;
3019 rajveer 64
    Client client = null;
65
	public MyPurchasesController() {
66
		super();
67
		try {
3126 rajveer 68
			csc = new CatalogClient();
3019 rajveer 69
			client = csc.getClient();
70
		} catch (Exception e) {
71
			logger.error("Unable to get catalog service client", e);
72
		}
73
	}
74
 
75
	public String index(){
3126 rajveer 76
        TransactionClient transactionServiceClient = null;
3019 rajveer 77
        in.shop2020.model.v1.order.TransactionService.Client orderClient = null;
78
        try{
3126 rajveer 79
            transactionServiceClient = new TransactionClient();
3019 rajveer 80
            orderClient = transactionServiceClient.getClient();
3022 rajveer 81
            orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0, (new Date()).getTime(), statuses);
8274 amit.gupta 82
            brandInfo = client.getBrandInfo();
3019 rajveer 83
            Collections.reverse(orders);
84
        } catch (Exception e)   {
85
        	logger.error("Not able to get order information from service.");
86
        }
87
		return "index";
88
    }
6915 anupam.sin 89
 
90
	public String downloadPolicyDoc() {
91
	    ByteBuffer buffer = null;
92
        try {
93
            if (orderId == null || orderId.isEmpty()) {
94
                logger.error("Order Id is null or empty");
95
                return "index";
96
            }
97
            TransactionClient transactionServiceClient = new TransactionClient();
98
            TransactionService.Client orderClient = transactionServiceClient.getClient();
99
            buffer = orderClient.getDocument(1, Long.parseLong(orderId));
100
            if(!buffer.hasArray()) {
101
                logger.error("The policy doc is not backed by an accessible byte buffer");
102
            }
103
        } catch (Exception e) {
104
            System.out.println(e.getMessage());
105
        }
106
        response.setHeader("Content-disposition", "inline; filename=" + orderId + "-policy.pdf");
107
 
108
        ServletOutputStream sos;
109
        try {
110
            sos = response.getOutputStream();
111
            sos.write(buffer.array());
112
            sos.flush();
113
        } catch (Exception e) {
114
            System.out.println("Unable to stream the manifest file");
115
        }
116
        return "index";
117
	}
3019 rajveer 118
 
7077 rajveer 119
	public String downloadInvoice() {
120
	    ByteBuffer buffer = null;
121
        try {
122
            if (orderId == null || orderId.isEmpty()) {
123
                logger.error("Order Id is null or empty");
124
                return "index";
125
            }
126
            TransactionClient transactionServiceClient = new TransactionClient();
127
            TransactionService.Client orderClient = transactionServiceClient.getClient();
7081 rajveer 128
            buffer = orderClient.retrieveInvoice(Long.parseLong(orderId), userinfo.getUserId());
7077 rajveer 129
            if(!buffer.hasArray()) {
130
                logger.error("The invoice does not found");
131
            }
132
        } catch (Exception e) {
133
            System.out.println(e.getMessage());
134
        }
135
        response.setHeader("Content-disposition", "inline; filename=invoice-" + orderId + ".pdf");
136
 
137
        ServletOutputStream sos;
138
        try {
139
            sos = response.getOutputStream();
140
            sos.write(buffer.array());
141
            sos.flush();
142
        } catch (Exception e) {
143
            System.out.println("Unable to stream the invoice file");
144
        }
145
        return "index";
146
	}
147
 
3019 rajveer 148
	public String formatPrice(double price)    {
149
        return formattingUtils.formatPrice(price);
150
    }
151
 
152
    public List<Order> getOrders()  {
153
        return orders;
154
    }
155
 
156
    public List<String> getOrderDate()  {
157
        return orderDate;
158
    }
159
 
160
    public String formatDate(long timestamp){
161
    	Date date = new Date(timestamp);
162
    	SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
163
    	return dateformat.format(date);
164
    }
165
 
3950 rajveer 166
    public long getCatalogId(long itemId){
167
    	long catalogId = 0;
168
    	try {
169
    		catalogId = csc.getClient().getItem(itemId).getCatalogItemId();
5945 mandeep.dh 170
		} catch (CatalogServiceException e) {
3950 rajveer 171
			logger.error("Unable to get item information." + e);
172
		} catch (TException e) {
173
			logger.error("Unable to get item information." + e);
174
		}
175
		return catalogId;
176
    }
177
 
3019 rajveer 178
	public String getWarrantyDate(long timestamp, String brand){
179
    	Calendar cd = Calendar.getInstance();
180
    	cd.setTimeInMillis(timestamp);
181
    	int addition = 12;
4275 varun.gupt 182
    	if(brand.equalsIgnoreCase("blackberry"))	{
3019 rajveer 183
    		addition = 18;
184
    	}
185
    	cd.add(Calendar.MONTH, addition);
186
    	SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
187
    	return dateformat.format(cd.getTime());
188
    }
6903 anupam.sin 189
 
190
	public String getInsuranceExpiryDate(long timestamp, long insurer){
191
	    if(insurer == 0) {
192
	        return "N/A";
193
	    }
194
        Calendar cd = Calendar.getInstance();
195
        cd.setTimeInMillis(timestamp);
196
        int addition = 12;
197
        cd.add(Calendar.MONTH, addition);
198
        SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
199
        return dateformat.format(cd.getTime());
200
    }
6915 anupam.sin 201
 
202
    public String getOrderId() {
203
        return orderId;
204
    }
205
 
206
    public void setOrderId(String orderId) {
207
        this.orderId = orderId;
208
    }
8274 amit.gupta 209
 
210
    public String getSupportUrl(String brand){
211
    	if (brandInfo.get(brand) != null){
212
    		return brandInfo.get(brand).getServiceCenterLocatorUrl(); 
213
    	}else {
214
    		return null;
215
    	}
216
    }
3019 rajveer 217
}