Rev 2042 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import in.shop2020.model.v1.order.TransactionServiceException;import in.shop2020.model.v1.user.Affiliate;import in.shop2020.model.v1.user.MasterAffiliate;import in.shop2020.model.v1.user.TrackLog;import in.shop2020.model.v1.user.Tracker;import in.shop2020.model.v1.user.UserContextService.Client;import in.shop2020.thrift.clients.UserContextServiceClient;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.text.ParseException;import java.util.Date;import java.util.HashMap;import java.util.LinkedList;import java.util.List;import java.util.Map;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;public class AffiliateController implements ServletResponseAware, ServletRequestAware{private HttpServletResponse response;private HttpServletRequest request;private String errorMsg = "";private List<MasterAffiliate> masterAffiliates;@Overridepublic void setServletResponse(HttpServletResponse res) {this.response = res;}@Overridepublic void setServletRequest(HttpServletRequest req) {this.request = req;}public String index() {try {UserContextServiceClient userContextServiceClient = new UserContextServiceClient();Client userClient = userContextServiceClient.getClient();masterAffiliates = userClient.getAllMasterAffiliates();} catch (Exception e) {e.printStackTrace();}return "report";}public String create() {try {long mAfId = Long.parseLong(request.getParameter("masterAffiliate"));UserContextServiceClient userContextServiceClient = new UserContextServiceClient();Client userClient = userContextServiceClient.getClient();List<Map<String, String>> contentRows = new LinkedList<Map<String,String>>();MasterAffiliate mAffiliate = userClient.getMasterAffiliateById(mAfId);for (Affiliate aff : userClient.getAffiliatesByMasterAffiliate(mAfId)) {for (Tracker tracker : userClient.getTrackersByAffiliate(aff.getId())) {for (TrackLog tracklog : userClient.getTrackLogsByTracker(tracker.getId())) {Map<String, String> contentRow = new HashMap<String, String>();contentRow.put("mAffiliate", mAffiliate.getName());contentRow.put("affiliate", aff.getName());contentRow.put("affiliateUrl", aff.getUrl());contentRow.put("date", String.valueOf(tracklog.getAddedOn()));contentRow.put("event", tracklog.getEvent());contentRow.put("url", tracklog.getUrl());contentRow.put("data", tracklog.getData());contentRows.add(contentRow);}}}// Preparing XLS file for outputresponse.setContentType("application/vnd.ms-excel");response.setHeader("Content-disposition", "inline; filename=user-communications" + ".xls");ServletOutputStream sos;try {ByteArrayOutputStream baos = getSpreadSheetData(contentRows, mAffiliate);sos = response.getOutputStream();baos.writeTo(sos);sos.flush();} catch (IOException e) {errorMsg = "Failed to write to response.";e.printStackTrace();}} catch (ParseException e) {errorMsg = e.getMessage();e.printStackTrace();} catch (TransactionServiceException e) {errorMsg = e.getMessage();e.printStackTrace();} catch (Exception e) {errorMsg = e.getMessage();e.printStackTrace();}return null;}// Prepares the XLS worksheet object and fills in the data with proper formattingprivate ByteArrayOutputStream getSpreadSheetData(List<Map<String, String>> contentRows, MasterAffiliate mAffiliate){ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();Workbook wb = new HSSFWorkbook();Font font = wb.createFont();font.setBoldweight(Font.BOLDWEIGHT_BOLD);CellStyle style = wb.createCellStyle();style.setFont(font);CreationHelper createHelper = wb.getCreationHelper();CellStyle dateCellStyle = wb.createCellStyle();dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY HH:MM"));createAffiliateSheet(contentRows, mAffiliate, wb, style, dateCellStyle);// Write the workbook to the output streamtry {wb.write(baosXLS);baosXLS.close();} catch (IOException e) {e.printStackTrace();}return baosXLS;}private void createAffiliateSheet(List<Map<String, String>> contentRows,MasterAffiliate mAffiliate, Workbook wb,CellStyle style, CellStyle dateCellStyle){// Affiliate SHEETSheet affSheet = wb.createSheet("Affiliates Report");short affSerialNo = 0;Row affTitleRow = affSheet.createRow(affSerialNo ++);Cell affTitleCell = affTitleRow.createCell(0);affTitleCell.setCellValue(mAffiliate.getName() + " Affiliates Report");affTitleCell.setCellStyle(style);affSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));affSheet.createRow(affSerialNo ++);Row affHeaderRow = affSheet.createRow(affSerialNo++);affHeaderRow.createCell(0).setCellValue("Affiliate");affHeaderRow.createCell(1).setCellValue("Affiliate Url");affHeaderRow.createCell(2).setCellValue("Date");affHeaderRow.createCell(3).setCellValue("Event");affHeaderRow.createCell(4).setCellValue("Url");affHeaderRow.createCell(5).setCellValue("Data");for (int i=0; i<6 ;i++) {affHeaderRow.getCell(i).setCellStyle(style);}for( Map<String, String> contentRow : contentRows) {affSerialNo++;Row commContentRow = affSheet.createRow(affSerialNo);commContentRow.createCell(0).setCellValue(contentRow.get("affiliate"));commContentRow.createCell(1).setCellValue(contentRow.get("affiliateUrl"));commContentRow.createCell(2).setCellValue(new Date(Long.parseLong(contentRow.get("date"))));commContentRow.getCell(2).setCellStyle(dateCellStyle);commContentRow.createCell(3).setCellValue(contentRow.get("event"));commContentRow.createCell(4).setCellValue(contentRow.get("url"));commContentRow.createCell(5).setCellValue(contentRow.get("data"));}for (int i = 0; i<6; i++) {affSheet.autoSizeColumn(i);}}public String getErrorMsg() {return errorMsg;}public List<MasterAffiliate> getMasterAffiliates() {return masterAffiliates;}}