Subversion Repositories SmartDukaan

Rev

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