Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1901 vikas 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.model.v1.order.TransactionServiceException;
4
import in.shop2020.model.v1.user.Affiliate;
5
import in.shop2020.model.v1.user.MasterAffiliate;
6
import in.shop2020.model.v1.user.TrackLog;
7
import in.shop2020.model.v1.user.Tracker;
8
import in.shop2020.model.v1.user.UserContextService.Client;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
10
 
11
import java.io.ByteArrayOutputStream;
12
import java.io.IOException;
13
import java.text.ParseException;
14
import java.util.Date;
15
import java.util.HashMap;
16
import java.util.LinkedList;
17
import java.util.List;
18
import java.util.Map;
19
 
20
import javax.servlet.ServletOutputStream;
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpServletResponse;
23
 
24
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
25
import org.apache.poi.ss.usermodel.Cell;
26
import org.apache.poi.ss.usermodel.CellStyle;
27
import org.apache.poi.ss.usermodel.CreationHelper;
28
import org.apache.poi.ss.usermodel.Font;
29
import org.apache.poi.ss.usermodel.Row;
30
import org.apache.poi.ss.usermodel.Sheet;
31
import org.apache.poi.ss.usermodel.Workbook;
32
import org.apache.poi.ss.util.CellRangeAddress;
33
import org.apache.struts2.interceptor.ServletRequestAware;
34
import org.apache.struts2.interceptor.ServletResponseAware;
35
 
36
public class AffiliateController implements ServletResponseAware, ServletRequestAware{
37
 
38
	private HttpServletResponse response;
39
	private HttpServletRequest request;
40
 
41
	private String errorMsg = "";
42
	private List<MasterAffiliate> masterAffiliates;
43
 
44
	@Override
45
	public void setServletResponse(HttpServletResponse res) {
46
		this.response = res;
47
	}
48
 
49
	@Override
50
    public void setServletRequest(HttpServletRequest req) {
51
	    this.request = req;
52
    }
53
 
54
	public String index()  {
55
 
56
        try {
57
            UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
58
            Client userClient = userContextServiceClient.getClient();
59
            masterAffiliates = userClient.getAllMasterAffiliates();
60
        } catch (Exception e) {
61
            e.printStackTrace();
62
        }
63
        return "report";
64
    }
65
 
66
	public String create()	{
67
	    try   {
68
	        long mAfId = Long.parseLong(request.getParameter("masterAffiliate"));
69
            UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
70
            Client userClient = userContextServiceClient.getClient();
71
 
72
            List<Map<String, String>> contentRows = new LinkedList<Map<String,String>>(); 
73
            MasterAffiliate mAffiliate = userClient.getMasterAffiliateById(mAfId);
74
            for (Affiliate aff : userClient.getAffiliatesByMasterAffiliate(mAfId)) {
2000 vikas 75
                for (TrackLog tracklog : userClient.getTrackLogsByAffiliate(aff.getId())) {
76
                    Map<String, String> contentRow = new HashMap<String, String>();
77
                    contentRow.put("mAffiliate", mAffiliate.getName());
78
                    contentRow.put("affiliate", aff.getName());
79
                    contentRow.put("affiliateUrl", aff.getUrl());
80
                    contentRow.put("date",String.valueOf(tracklog.getAddedOn()));
81
                    contentRow.put("event", tracklog.getEvent());
82
                    contentRow.put("url", tracklog.getUrl());
83
                    contentRow.put("data", tracklog.getData());
84
                    contentRows.add(contentRow);
1901 vikas 85
                }
86
            }
87
 
88
            // Preparing XLS file for output
89
            response.setContentType("application/vnd.ms-excel");
90
 
91
            response.setHeader("Content-disposition", "inline; filename=user-communications" + ".xls");
92
 
93
            ServletOutputStream sos;
94
            try {
95
                ByteArrayOutputStream baos = getSpreadSheetData(contentRows, mAffiliate);
96
                sos = response.getOutputStream();
97
                baos.writeTo(sos);
98
                sos.flush();
99
            } catch (IOException e) {
100
                errorMsg = "Failed to write to response.";
101
                e.printStackTrace();
102
            }
103
 
104
        } catch (ParseException e)  {
105
            errorMsg = e.getMessage();
106
            e.printStackTrace();
107
        } catch (TransactionServiceException e) {
108
            errorMsg = e.getMessage();
109
            e.printStackTrace();
110
        } catch (Exception e)   {
111
            errorMsg = e.getMessage();
112
            e.printStackTrace();
113
        }
114
        return null;
115
	}
116
 
117
	// Prepares the XLS worksheet object and fills in the data with proper formatting
118
	private ByteArrayOutputStream getSpreadSheetData(List<Map<String, String>> contentRows, MasterAffiliate mAffiliate)
119
	{
120
	    ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
121
 
122
		Workbook wb = new HSSFWorkbook();
123
 
124
	    Font font = wb.createFont();
125
	    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
126
	    CellStyle style = wb.createCellStyle();
127
	    style.setFont(font);
128
 
129
	    CreationHelper createHelper = wb.getCreationHelper();
130
	    CellStyle dateCellStyle = wb.createCellStyle();
131
	    dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY HH:MM"));
132
 
133
	    createAffiliateSheet(contentRows, mAffiliate, wb, style, dateCellStyle);
134
 
135
		// Write the workbook to the output stream
136
		try {
137
			wb.write(baosXLS);
138
			baosXLS.close();
139
		} catch (IOException e) {
140
			e.printStackTrace();
141
		}		
142
		return baosXLS;
143
	}
144
 
145
	private void createAffiliateSheet(
146
	        List<Map<String, String>> contentRows, 
147
			MasterAffiliate mAffiliate, Workbook wb, 
148
			CellStyle style, CellStyle dateCellStyle) 
149
	{
150
		// Affiliate SHEET
151
	    Sheet affSheet = wb.createSheet("Affiliates Report");
152
	    short affSerialNo = 0;
153
 
154
	    Row affTitleRow = affSheet.createRow(affSerialNo ++);
155
	    Cell affTitleCell = affTitleRow.createCell(0);
156
	    affTitleCell.setCellValue(mAffiliate.getName() + " Affiliates Report");
157
	    affTitleCell.setCellStyle(style);
158
 
159
	    affSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
160
 
161
	    affSheet.createRow(affSerialNo ++);
162
 
163
	    Row affHeaderRow = affSheet.createRow(affSerialNo++);
164
	    affHeaderRow.createCell(0).setCellValue("Affiliate");
165
	    affHeaderRow.createCell(1).setCellValue("Affiliate Url");
166
	    affHeaderRow.createCell(2).setCellValue("Date");
167
	    affHeaderRow.createCell(3).setCellValue("Event");
168
	    affHeaderRow.createCell(4).setCellValue("Url");
169
	    affHeaderRow.createCell(5).setCellValue("Data");
170
	    for (int i=0; i<6 ;i++) {
171
	        affHeaderRow.getCell(i).setCellStyle(style);
172
	    }
173
 
174
		for( Map<String, String> contentRow : contentRows) {
175
			affSerialNo++;
176
			Row commContentRow = affSheet.createRow(affSerialNo);
177
 
178
			commContentRow.createCell(0).setCellValue(contentRow.get("affiliate"));
179
			commContentRow.createCell(1).setCellValue(contentRow.get("affiliateUrl"));
180
			commContentRow.createCell(2).setCellValue(new Date(Long.parseLong(contentRow.get("date"))));
181
			commContentRow.getCell(2).setCellStyle(dateCellStyle);
182
            commContentRow.createCell(3).setCellValue(contentRow.get("event"));
183
			commContentRow.createCell(4).setCellValue(contentRow.get("url"));
184
			commContentRow.createCell(5).setCellValue(contentRow.get("data"));
185
		}
186
        for (int i = 0; i<6; i++) {
187
            affSheet.autoSizeColumn(i);
188
        }
189
	}
190
 
191
	public String getErrorMsg() {
192
		return errorMsg;
193
	}
194
 
195
	public List<MasterAffiliate> getMasterAffiliates() {
196
        return masterAffiliates;
197
    }
198
}