Subversion Repositories SmartDukaan

Rev

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