Subversion Repositories SmartDukaan

Rev

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

Rev 3213 Rev 3293
Line 9... Line 9...
9
import in.shop2020.model.v1.user.TrackLog;
9
import in.shop2020.model.v1.user.TrackLog;
10
import in.shop2020.model.v1.user.UserAffiliateException;
10
import in.shop2020.model.v1.user.UserAffiliateException;
11
import in.shop2020.model.v1.user.UserContextService.Client;
11
import in.shop2020.model.v1.user.UserContextService.Client;
12
import in.shop2020.payments.Payment;
12
import in.shop2020.payments.Payment;
13
import in.shop2020.payments.PaymentException;
13
import in.shop2020.payments.PaymentException;
-
 
14
import in.shop2020.support.utils.ReportsUtils;
14
import in.shop2020.thrift.clients.PaymentClient;
15
import in.shop2020.thrift.clients.PaymentClient;
15
import in.shop2020.thrift.clients.TransactionClient;
16
import in.shop2020.thrift.clients.TransactionClient;
16
import in.shop2020.thrift.clients.UserClient;
17
import in.shop2020.thrift.clients.UserClient;
17
 
18
 
18
import java.io.ByteArrayOutputStream;
19
import java.io.ByteArrayOutputStream;
19
import java.io.IOException;
20
import java.io.IOException;
-
 
21
import java.text.DateFormat;
-
 
22
import java.text.ParseException;
-
 
23
import java.text.SimpleDateFormat;
20
import java.util.ArrayList;
24
import java.util.ArrayList;
-
 
25
import java.util.Calendar;
21
import java.util.Date;
26
import java.util.Date;
22
import java.util.HashMap;
27
import java.util.HashMap;
23
import java.util.HashSet;
28
import java.util.HashSet;
24
import java.util.LinkedList;
29
import java.util.LinkedList;
25
import java.util.List;
30
import java.util.List;
Line 27... Line 32...
27
import java.util.Set;
32
import java.util.Set;
28
 
33
 
29
import javax.servlet.ServletOutputStream;
34
import javax.servlet.ServletOutputStream;
30
import javax.servlet.http.HttpServletRequest;
35
import javax.servlet.http.HttpServletRequest;
31
import javax.servlet.http.HttpServletResponse;
36
import javax.servlet.http.HttpServletResponse;
-
 
37
import javax.servlet.http.HttpSession;
32
 
38
 
33
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
39
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
34
import org.apache.poi.ss.usermodel.Cell;
40
import org.apache.poi.ss.usermodel.Cell;
35
import org.apache.poi.ss.usermodel.CellStyle;
41
import org.apache.poi.ss.usermodel.CellStyle;
36
import org.apache.poi.ss.usermodel.CreationHelper;
42
import org.apache.poi.ss.usermodel.CreationHelper;
37
import org.apache.poi.ss.usermodel.Font;
43
import org.apache.poi.ss.usermodel.Font;
38
import org.apache.poi.ss.usermodel.Row;
44
import org.apache.poi.ss.usermodel.Row;
39
import org.apache.poi.ss.usermodel.Sheet;
45
import org.apache.poi.ss.usermodel.Sheet;
40
import org.apache.poi.ss.usermodel.Workbook;
46
import org.apache.poi.ss.usermodel.Workbook;
41
import org.apache.poi.ss.util.CellRangeAddress;
47
import org.apache.poi.ss.util.CellRangeAddress;
-
 
48
import org.apache.struts2.convention.annotation.InterceptorRef;
-
 
49
import org.apache.struts2.convention.annotation.InterceptorRefs;
42
import org.apache.struts2.interceptor.ServletRequestAware;
50
import org.apache.struts2.interceptor.ServletRequestAware;
43
import org.apache.struts2.interceptor.ServletResponseAware;
51
import org.apache.struts2.interceptor.ServletResponseAware;
44
import org.apache.thrift.TException;
52
import org.apache.thrift.TException;
45
import org.slf4j.Logger;
53
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
54
import org.slf4j.LoggerFactory;
Line 50... Line 58...
50
 * affiliate.
58
 * affiliate.
51
 * 
59
 * 
52
 * @author Vikas Malik
60
 * @author Vikas Malik
53
 * 
61
 * 
54
 */
62
 */
-
 
63
@InterceptorRefs({
-
 
64
    @InterceptorRef("defaultStack"),
-
 
65
    @InterceptorRef("login")
-
 
66
})
55
public class AffiliateController implements ServletResponseAware, ServletRequestAware{
67
public class AffiliateController implements ServletRequestAware, ServletResponseAware{
56
 
68
 
57
    private static Logger logger = LoggerFactory.getLogger(AffiliateController.class);
69
    private static Logger log = LoggerFactory.getLogger(AffiliateController.class);
58
    
70
    
59
	private HttpServletResponse response;
71
	private HttpServletResponse response;
60
	private HttpServletRequest request;
72
	private HttpServletRequest request;
-
 
73
	private HttpSession session;
61
	
74
    
62
	private String errorMsg = "";
75
	private String errorMsg = "";
63
	private List<MasterAffiliate> masterAffiliates;
76
	private List<MasterAffiliate> masterAffiliates;
64
	
77
	
65
	@Override
78
	@Override
66
	public void setServletResponse(HttpServletResponse res) {
79
	public void setServletResponse(HttpServletResponse res) {
Line 68... Line 81...
68
	}
81
	}
69
 
82
 
70
	@Override
83
	@Override
71
    public void setServletRequest(HttpServletRequest req) {
84
    public void setServletRequest(HttpServletRequest req) {
72
	    this.request = req;
85
	    this.request = req;
-
 
86
        this.session = req.getSession();
73
    }
87
    }
74
	
88
	
75
	public String index()  {
89
	public String index()  {
-
 
90
	    log.info(request.getServletPath());
-
 
91
        if (!ReportsUtils.canAccessReport(
-
 
92
                (Long) session.getAttribute(ReportsUtils.ROLE),
-
 
93
                request.getServletPath())) 
-
 
94
        {
-
 
95
            return "exception";
76
	    
96
        }
77
        try {
97
        try {
78
        	UserClient userContextServiceClient = new UserClient();
98
        	UserClient userContextServiceClient = new UserClient();
79
            Client userClient = userContextServiceClient.getClient();
99
            Client userClient = userContextServiceClient.getClient();
80
            masterAffiliates = userClient.getAllMasterAffiliates();
100
            masterAffiliates = userClient.getAllMasterAffiliates();
81
        } catch (Exception e) {
101
        } catch (Exception e) {
82
            logger.error("Error while getting all affiliates", e);
102
            log.error("Error while getting all affiliates", e);
83
        }
103
        }
84
        return "report";
104
        return "report";
85
    }
105
    }
86
	
106
	
87
	public String create()	{
107
	public String create()	{
88
	    try   {
108
	    try   {
89
	        long mAfId = Long.parseLong(request.getParameter("masterAffiliate"));
109
	        long mAfId = Long.parseLong(request.getParameter("masterAffiliate"));
-
 
110
	        String filename;
-
 
111
	        String startDateStr = request.getParameter("startDate");
-
 
112
	        String endDateStr = request.getParameter("endDate");
-
 
113
	        
-
 
114
	        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
-
 
115
	        Date startDate = null, endDate = null;
-
 
116
	        try {
-
 
117
	            startDate = df.parse(startDateStr);
-
 
118
	            endDate = df.parse(endDateStr);
-
 
119
	            Calendar cal = Calendar.getInstance();
-
 
120
	            cal.setTime(endDate);
-
 
121
	            cal.add(Calendar.DATE, 1);
-
 
122
	            endDate.setTime(cal.getTimeInMillis());
-
 
123
	        } catch (ParseException pe) {
-
 
124
	            errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
-
 
125
	            return "report";
-
 
126
	        }
-
 
127
	        
90
	        UserClient userContextServiceClient = new UserClient();
128
	        UserClient userContextServiceClient = new UserClient();
91
            PaymentClient paymentServiceClient = new PaymentClient();
129
            PaymentClient paymentServiceClient = new PaymentClient();
92
            TransactionClient transactionServiceClient = new TransactionClient();
130
            TransactionClient transactionServiceClient = new TransactionClient();
93
            
131
            
94
            Client userClient = userContextServiceClient.getClient();
132
            Client userClient = userContextServiceClient.getClient();
Line 96... Line 134...
96
            
134
            
97
            Set<Long> Payments = new HashSet<Long>();
135
            Set<Long> Payments = new HashSet<Long>();
98
            List<Map<String, String>> contentRows = new LinkedList<Map<String,String>>();
136
            List<Map<String, String>> contentRows = new LinkedList<Map<String,String>>();
99
            List<Affiliate> affiliates;
137
            List<Affiliate> affiliates;
100
            if (mAfId == -1) {
138
            if (mAfId == -1) {
-
 
139
                filename = "All";
101
                affiliates = new ArrayList<Affiliate>();
140
                affiliates = new ArrayList<Affiliate>();
102
                for(MasterAffiliate mAffiliate : userClient.getAllMasterAffiliates()) {
141
                for(MasterAffiliate mAffiliate : userClient.getAllMasterAffiliates()) {
103
                    affiliates.addAll(userClient.getAffiliatesByMasterAffiliate(mAffiliate.getId()));
142
                    affiliates.addAll(userClient.getAffiliatesByMasterAffiliate(mAffiliate.getId()));
104
                }
143
                }
105
            }
144
            }
106
            else {
145
            else {
107
                affiliates = userClient.getAffiliatesByMasterAffiliate(mAfId);
146
                affiliates = userClient.getAffiliatesByMasterAffiliate(mAfId);
-
 
147
                filename = userClient.getMasterAffiliateById(mAfId).getName();
108
            }
148
            }
109
            
149
            
110
            for (Affiliate aff : affiliates) {
150
            for (Affiliate aff : affiliates) {
111
                MasterAffiliate mAffiliate = userClient.getMasterAffiliateById(aff.getMasterAffiliateId());
151
                MasterAffiliate mAffiliate = userClient.getMasterAffiliateById(aff.getMasterAffiliateId());
112
                for (TrackLog tracklog : userClient.getTrackLogsByAffiliate(aff.getId())) {
152
                for (TrackLog tracklog : userClient.getTrackLogsByAffiliate(aff.getId(), startDate.getTime(), endDate.getTime())) {
113
                    boolean orderCompleted = false;
153
                    boolean orderCompleted = false;
114
                    Map<String, String> contentRow = new HashMap<String, String>();
154
                    Map<String, String> contentRow = new HashMap<String, String>();
115
                    contentRow.put("mAffiliate", mAffiliate.getName());
155
                    contentRow.put("mAffiliate", mAffiliate.getName());
116
                    contentRow.put("affiliate", aff.getName());
156
                    contentRow.put("affiliate", aff.getName());
117
                    contentRow.put("affiliateUrl", aff.getUrl());
157
                    contentRow.put("affiliateUrl", aff.getUrl());
Line 144... Line 184...
144
                                            + item.getTotal_price() + "), ";
184
                                            + item.getTotal_price() + "), ";
145
                                }
185
                                }
146
                                contentRow.put("items", itemString.replaceAll("null", ""));
186
                                contentRow.put("items", itemString.replaceAll("null", ""));
147
                            }
187
                            }
148
                        } catch (PaymentException e) {
188
                        } catch (PaymentException e) {
149
                            logger.error("Error while getting payment info", e);
189
                            log.error("Error while getting payment info", e);
150
                        } catch (TransactionServiceException e) {
190
                        } catch (TransactionServiceException e) {
151
                            logger.error("Error while getting order info", e);
191
                            log.error("Error while getting order info", e);
152
                        } catch (TException e) {
192
                        } catch (TException e) {
153
                            logger.error("Unable to get order or payment info", e);
193
                            log.error("Unable to get order or payment info", e);
154
                        } catch (NullPointerException e) {
194
                        } catch (NullPointerException e) {
155
                            logger.error("Unexpected exception", e);
195
                            log.error("Unexpected exception", e);
156
                        }
196
                        }
157
                    }
197
                    }
158
                    contentRow.put("data", tracklog.getData());
198
                    contentRow.put("data", tracklog.getData());
159
                    if (orderCompleted) {
199
                    if (orderCompleted) {
160
                        contentRows.add(contentRow);
200
                        contentRows.add(contentRow);
Line 163... Line 203...
163
            }
203
            }
164
             
204
             
165
            // Preparing XLS file for output
205
            // Preparing XLS file for output
166
            response.setContentType("application/vnd.ms-excel");
206
            response.setContentType("application/vnd.ms-excel");
167
            
207
            
168
            response.setHeader("Content-disposition", "inline; filename=affiliate-report" + ".xls");
208
            response.setHeader("Content-disposition", "inline; filename=" + filename + "-affiliate-report" + ".xls");
169
            
209
            
170
            ServletOutputStream sos;
210
            ServletOutputStream sos;
171
            try {
211
            try {
172
                ByteArrayOutputStream baos = getSpreadSheetData(contentRows);
212
                ByteArrayOutputStream baos = getSpreadSheetData(contentRows);
173
                sos = response.getOutputStream();
213
                sos = response.getOutputStream();
174
                baos.writeTo(sos);
214
                baos.writeTo(sos);
175
                sos.flush();
215
                sos.flush();
176
            } catch (IOException e) {
216
            } catch (IOException e) {
177
                logger.error("Unable to stream the affiliates report", e);
217
                log.error("Unable to stream the affiliates report", e);
178
                errorMsg = "Failed to write to response.";
218
                errorMsg = "Failed to write to response.";
179
            }
219
            }
180
        } catch (UserAffiliateException e) {
220
        } catch (UserAffiliateException e) {
181
            logger.error("Error while getting affiliated users", e);
221
            log.error("Error while getting affiliated users", e);
182
            errorMsg = e.getMessage();
222
            errorMsg = e.getMessage();
183
        } catch (TException e) {
223
        } catch (TException e) {
184
            logger.error("Unable to get affiliated users", e);
224
            log.error("Unable to get affiliated users", e);
185
            errorMsg = e.getMessage();
225
            errorMsg = e.getMessage();
186
        } catch (Exception e) {
226
        } catch (Exception e) {
187
            logger.error("Unexpected exception", e);
227
            log.error("Unexpected exception", e);
188
            errorMsg = e.getMessage();
228
            errorMsg = e.getMessage();
189
        }
229
        }
190
        return null;
230
        return null;
191
	}
231
	}
192
	
232
	
Line 211... Line 251...
211
		// Write the workbook to the output stream
251
		// Write the workbook to the output stream
212
		try {
252
		try {
213
			wb.write(baosXLS);
253
			wb.write(baosXLS);
214
			baosXLS.close();
254
			baosXLS.close();
215
		} catch (IOException e) {
255
		} catch (IOException e) {
216
			logger.error("Unable to get the byte array for the affiliate report", e);
256
			log.error("Unable to get the byte array for the affiliate report", e);
217
		}		
257
		}		
218
		return baosXLS;
258
		return baosXLS;
219
	}
259
	}
220
	
260
	
221
	private void createAffiliateSheet(List<Map<String, String>> contentRows, Workbook wb, CellStyle style, CellStyle dateCellStyle) 
261
	private void createAffiliateSheet(List<Map<String, String>> contentRows, Workbook wb, CellStyle style, CellStyle dateCellStyle)