Subversion Repositories SmartDukaan

Rev

Rev 2000 | Go to most recent revision | Details | 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)) {
75
                for (Tracker tracker : userClient.getTrackersByAffiliate(aff.getId())) {
76
                    for (TrackLog tracklog : userClient.getTrackLogsByTracker(tracker.getId())) {
77
                        Map<String, String> contentRow = new HashMap<String, String>();
78
                        contentRow.put("mAffiliate", mAffiliate.getName());
79
                        contentRow.put("affiliate", aff.getName());
80
                        contentRow.put("affiliateUrl", aff.getUrl());
81
                        contentRow.put("date", String.valueOf(tracklog.getAddedOn()));
82
                        contentRow.put("event", tracklog.getEvent());
83
                        contentRow.put("url", tracklog.getUrl());
84
                        contentRow.put("data", tracklog.getData());
85
                        contentRows.add(contentRow);
86
                    }
87
                }
88
            }
89
 
90
            // Preparing XLS file for output
91
            response.setContentType("application/vnd.ms-excel");
92
 
93
            response.setHeader("Content-disposition", "inline; filename=user-communications" + ".xls");
94
 
95
            ServletOutputStream sos;
96
            try {
97
                ByteArrayOutputStream baos = getSpreadSheetData(contentRows, mAffiliate);
98
                sos = response.getOutputStream();
99
                baos.writeTo(sos);
100
                sos.flush();
101
            } catch (IOException e) {
102
                errorMsg = "Failed to write to response.";
103
                e.printStackTrace();
104
            }
105
 
106
        } catch (ParseException e)  {
107
            errorMsg = e.getMessage();
108
            e.printStackTrace();
109
        } catch (TransactionServiceException e) {
110
            errorMsg = e.getMessage();
111
            e.printStackTrace();
112
        } catch (Exception e)   {
113
            errorMsg = e.getMessage();
114
            e.printStackTrace();
115
        }
116
        return null;
117
	}
118
 
119
	// Prepares the XLS worksheet object and fills in the data with proper formatting
120
	private ByteArrayOutputStream getSpreadSheetData(List<Map<String, String>> contentRows, MasterAffiliate mAffiliate)
121
	{
122
	    ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
123
 
124
		Workbook wb = new HSSFWorkbook();
125
 
126
	    Font font = wb.createFont();
127
	    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
128
	    CellStyle style = wb.createCellStyle();
129
	    style.setFont(font);
130
 
131
	    CreationHelper createHelper = wb.getCreationHelper();
132
	    CellStyle dateCellStyle = wb.createCellStyle();
133
	    dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY HH:MM"));
134
 
135
	    createAffiliateSheet(contentRows, mAffiliate, wb, style, dateCellStyle);
136
 
137
		// Write the workbook to the output stream
138
		try {
139
			wb.write(baosXLS);
140
			baosXLS.close();
141
		} catch (IOException e) {
142
			e.printStackTrace();
143
		}		
144
		return baosXLS;
145
	}
146
 
147
	private void createAffiliateSheet(
148
	        List<Map<String, String>> contentRows, 
149
			MasterAffiliate mAffiliate, Workbook wb, 
150
			CellStyle style, CellStyle dateCellStyle) 
151
	{
152
		// Affiliate SHEET
153
	    Sheet affSheet = wb.createSheet("Affiliates Report");
154
	    short affSerialNo = 0;
155
 
156
	    Row affTitleRow = affSheet.createRow(affSerialNo ++);
157
	    Cell affTitleCell = affTitleRow.createCell(0);
158
	    affTitleCell.setCellValue(mAffiliate.getName() + " Affiliates Report");
159
	    affTitleCell.setCellStyle(style);
160
 
161
	    affSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
162
 
163
	    affSheet.createRow(affSerialNo ++);
164
 
165
	    Row affHeaderRow = affSheet.createRow(affSerialNo++);
166
	    affHeaderRow.createCell(0).setCellValue("Affiliate");
167
	    affHeaderRow.createCell(1).setCellValue("Affiliate Url");
168
	    affHeaderRow.createCell(2).setCellValue("Date");
169
	    affHeaderRow.createCell(3).setCellValue("Event");
170
	    affHeaderRow.createCell(4).setCellValue("Url");
171
	    affHeaderRow.createCell(5).setCellValue("Data");
172
	    for (int i=0; i<6 ;i++) {
173
	        affHeaderRow.getCell(i).setCellStyle(style);
174
	    }
175
 
176
		for( Map<String, String> contentRow : contentRows) {
177
			affSerialNo++;
178
			Row commContentRow = affSheet.createRow(affSerialNo);
179
 
180
			commContentRow.createCell(0).setCellValue(contentRow.get("affiliate"));
181
			commContentRow.createCell(1).setCellValue(contentRow.get("affiliateUrl"));
182
			commContentRow.createCell(2).setCellValue(new Date(Long.parseLong(contentRow.get("date"))));
183
			commContentRow.getCell(2).setCellStyle(dateCellStyle);
184
            commContentRow.createCell(3).setCellValue(contentRow.get("event"));
185
			commContentRow.createCell(4).setCellValue(contentRow.get("url"));
186
			commContentRow.createCell(5).setCellValue(contentRow.get("data"));
187
		}
188
        for (int i = 0; i<6; i++) {
189
            affSheet.autoSizeColumn(i);
190
        }
191
	}
192
 
193
	public String getErrorMsg() {
194
		return errorMsg;
195
	}
196
 
197
	public List<MasterAffiliate> getMasterAffiliates() {
198
        return masterAffiliates;
199
    }
200
}