Subversion Repositories SmartDukaan

Rev

Rev 3125 | Rev 3293 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3125 Rev 3213
Line 1... Line 1...
1
package in.shop2020.support.controllers;
1
package in.shop2020.support.controllers;
2
 
2
 
3
import in.shop2020.model.v1.order.LineItem;
3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.model.v1.order.Order;
5
import in.shop2020.model.v1.order.OrderStatus;
5
import in.shop2020.model.v1.order.OrderStatus;
-
 
6
import in.shop2020.model.v1.order.TransactionServiceException;
6
import in.shop2020.model.v1.user.Affiliate;
7
import in.shop2020.model.v1.user.Affiliate;
7
import in.shop2020.model.v1.user.MasterAffiliate;
8
import in.shop2020.model.v1.user.MasterAffiliate;
8
import in.shop2020.model.v1.user.TrackLog;
9
import in.shop2020.model.v1.user.TrackLog;
-
 
10
import in.shop2020.model.v1.user.UserAffiliateException;
9
import in.shop2020.model.v1.user.UserContextService.Client;
11
import in.shop2020.model.v1.user.UserContextService.Client;
10
import in.shop2020.payments.Payment;
12
import in.shop2020.payments.Payment;
-
 
13
import in.shop2020.payments.PaymentException;
11
import in.shop2020.thrift.clients.PaymentClient;
14
import in.shop2020.thrift.clients.PaymentClient;
12
import in.shop2020.thrift.clients.TransactionClient;
15
import in.shop2020.thrift.clients.TransactionClient;
13
import in.shop2020.thrift.clients.UserClient;
16
import in.shop2020.thrift.clients.UserClient;
14
 
17
 
15
import java.io.ByteArrayOutputStream;
18
import java.io.ByteArrayOutputStream;
Line 36... Line 39...
36
import org.apache.poi.ss.usermodel.Sheet;
39
import org.apache.poi.ss.usermodel.Sheet;
37
import org.apache.poi.ss.usermodel.Workbook;
40
import org.apache.poi.ss.usermodel.Workbook;
38
import org.apache.poi.ss.util.CellRangeAddress;
41
import org.apache.poi.ss.util.CellRangeAddress;
39
import org.apache.struts2.interceptor.ServletRequestAware;
42
import org.apache.struts2.interceptor.ServletRequestAware;
40
import org.apache.struts2.interceptor.ServletResponseAware;
43
import org.apache.struts2.interceptor.ServletResponseAware;
-
 
44
import org.apache.thrift.TException;
-
 
45
import org.slf4j.Logger;
-
 
46
import org.slf4j.LoggerFactory;
41
 
47
 
42
/**
48
/**
43
 * Provides reports about successful registrations and orders place through an
49
 * Provides reports about successful registrations and orders place through an
44
 * affiliate.
50
 * affiliate.
45
 * 
51
 * 
46
 * @author Vikas Malik
52
 * @author Vikas Malik
47
 * 
53
 * 
48
 */
54
 */
49
public class AffiliateController implements ServletResponseAware, ServletRequestAware{
55
public class AffiliateController implements ServletResponseAware, ServletRequestAware{
50
 
56
 
-
 
57
    private static Logger logger = LoggerFactory.getLogger(AffiliateController.class);
-
 
58
    
51
	private HttpServletResponse response;
59
	private HttpServletResponse response;
52
	private HttpServletRequest request;
60
	private HttpServletRequest request;
53
	
61
	
54
	private String errorMsg = "";
62
	private String errorMsg = "";
55
	private List<MasterAffiliate> masterAffiliates;
63
	private List<MasterAffiliate> masterAffiliates;
Line 69... Line 77...
69
        try {
77
        try {
70
        	UserClient userContextServiceClient = new UserClient();
78
        	UserClient userContextServiceClient = new UserClient();
71
            Client userClient = userContextServiceClient.getClient();
79
            Client userClient = userContextServiceClient.getClient();
72
            masterAffiliates = userClient.getAllMasterAffiliates();
80
            masterAffiliates = userClient.getAllMasterAffiliates();
73
        } catch (Exception e) {
81
        } catch (Exception e) {
74
            e.printStackTrace();
82
            logger.error("Error while getting all affiliates", e);
75
        }
83
        }
76
        return "report";
84
        return "report";
77
    }
85
    }
78
	
86
	
79
	public String create()	{
87
	public String create()	{
Line 115... Line 123...
115
                            long paymentId = Long.parseLong(tracklog.getData().replaceAll("\\(*\\)", ""));
123
                            long paymentId = Long.parseLong(tracklog.getData().replaceAll("\\(*\\)", ""));
116
                            if (Payments.contains(paymentId)) {
124
                            if (Payments.contains(paymentId)) {
117
                                continue;
125
                                continue;
118
                            }
126
                            }
119
                            Payments.add(paymentId);
127
                            Payments.add(paymentId);
120
                            Payment payment = paymentClient
128
                            Payment payment = paymentClient.getPayment(paymentId);
121
                                    .getPayment(paymentId);
-
 
122
                            List<Order> orders = transactionServiceClient
129
                            List<Order> orders = transactionServiceClient.getClient().getOrdersForTransaction(
123
                                    .getClient().getOrdersForTransaction(
-
 
124
                                            payment.getMerchantTxnId(),
130
                                            payment.getMerchantTxnId(),
125
                                            payment.getUserId());
131
                                            payment.getUserId());
126
                            contentRow.put("amount",
-
 
127
                                    Double.toString(payment.getAmount()));
132
                            contentRow.put("amount", Double.toString(payment.getAmount()));
128
                            String itemString = "";
133
                            String itemString = "";
129
                            for (Order order : orders) {
134
                            for (Order order : orders) {
130
                                if (order.getStatus() == OrderStatus.DELIVERY_SUCCESS) {
135
                                if (order.getStatus() == OrderStatus.DELIVERY_SUCCESS) {
131
                                    orderCompleted = true;
136
                                    orderCompleted = true;
132
                                }
137
                                }
Line 138... Line 143...
138
                                            + item.getQuantity() + ", "
143
                                            + item.getQuantity() + ", "
139
                                            + item.getTotal_price() + "), ";
144
                                            + item.getTotal_price() + "), ";
140
                                }
145
                                }
141
                                contentRow.put("items", itemString.replaceAll("null", ""));
146
                                contentRow.put("items", itemString.replaceAll("null", ""));
142
                            }
147
                            }
-
 
148
                        } catch (PaymentException e) {
-
 
149
                            logger.error("Error while getting payment info", e);
-
 
150
                        } catch (TransactionServiceException e) {
-
 
151
                            logger.error("Error while getting order info", e);
143
                        } catch (Exception e) {
152
                        } catch (TException e) {
-
 
153
                            logger.error("Unable to get order or payment info", e);
144
                            e.printStackTrace();
154
                        } catch (NullPointerException e) {
-
 
155
                            logger.error("Unexpected exception", e);
145
                        }
156
                        }
146
                    }
157
                    }
147
                    contentRow.put("data", tracklog.getData());
158
                    contentRow.put("data", tracklog.getData());
148
                    if (orderCompleted) {
159
                    if (orderCompleted) {
149
                        contentRows.add(contentRow);
160
                        contentRows.add(contentRow);
Line 161... Line 172...
161
                ByteArrayOutputStream baos = getSpreadSheetData(contentRows);
172
                ByteArrayOutputStream baos = getSpreadSheetData(contentRows);
162
                sos = response.getOutputStream();
173
                sos = response.getOutputStream();
163
                baos.writeTo(sos);
174
                baos.writeTo(sos);
164
                sos.flush();
175
                sos.flush();
165
            } catch (IOException e) {
176
            } catch (IOException e) {
-
 
177
                logger.error("Unable to stream the affiliates report", e);
166
                errorMsg = "Failed to write to response.";
178
                errorMsg = "Failed to write to response.";
167
                e.printStackTrace();
-
 
168
            }
179
            }
-
 
180
        } catch (UserAffiliateException e) {
-
 
181
            logger.error("Error while getting affiliated users", e);
-
 
182
            errorMsg = e.getMessage();
-
 
183
        } catch (TException e) {
-
 
184
            logger.error("Unable to get affiliated users", e);
-
 
185
            errorMsg = e.getMessage();
169
        } catch (Exception e)   {
186
        } catch (Exception e) {
-
 
187
            logger.error("Unexpected exception", e);
170
            errorMsg = e.getMessage();
188
            errorMsg = e.getMessage();
171
            e.printStackTrace();
-
 
172
        }
189
        }
173
        return null;
190
        return null;
174
	}
191
	}
175
	
192
	
176
	// Prepares the XLS worksheet object and fills in the data with proper formatting
193
	// Prepares the XLS worksheet object and fills in the data with proper formatting
Line 194... Line 211...
194
		// Write the workbook to the output stream
211
		// Write the workbook to the output stream
195
		try {
212
		try {
196
			wb.write(baosXLS);
213
			wb.write(baosXLS);
197
			baosXLS.close();
214
			baosXLS.close();
198
		} catch (IOException e) {
215
		} catch (IOException e) {
199
			e.printStackTrace();
216
			logger.error("Unable to get the byte array for the affiliate report", e);
200
		}		
217
		}		
201
		return baosXLS;
218
		return baosXLS;
202
	}
219
	}
203
	
220
	
204
	private void createAffiliateSheet(List<Map<String, String>> contentRows, Workbook wb, CellStyle style, CellStyle dateCellStyle) 
221
	private void createAffiliateSheet(List<Map<String, String>> contentRows, Workbook wb, CellStyle style, CellStyle dateCellStyle)