Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
992 varun.gupt 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.model.v1.catalog.Warehouse;
4
import in.shop2020.model.v1.order.LineItem;
5
import in.shop2020.model.v1.order.Order;
6
import in.shop2020.model.v1.order.OrderStatus;
7
import in.shop2020.model.v1.order.TransactionServiceException;
8
import in.shop2020.thrift.clients.CatalogServiceClient;
9
import in.shop2020.thrift.clients.TransactionServiceClient;
10
 
11
import java.text.DateFormat;
12
import java.text.SimpleDateFormat;
13
import java.text.ParseException;
14
 
15
import java.io.ByteArrayOutputStream;
16
import java.io.IOException;
17
import java.util.ArrayList;
18
import java.util.Calendar;
19
import java.util.Date;
20
import java.util.GregorianCalendar;
21
import java.util.List;
22
 
23
import javax.servlet.ServletOutputStream;
24
import javax.servlet.http.HttpServletRequest;
25
import javax.servlet.http.HttpServletResponse;
26
 
27
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
28
import org.apache.poi.ss.usermodel.Cell;
29
import org.apache.poi.ss.usermodel.CellStyle;
30
import org.apache.poi.ss.usermodel.Font;
31
import org.apache.poi.ss.usermodel.Row;
32
import org.apache.poi.ss.usermodel.Sheet;
33
import org.apache.poi.ss.usermodel.Workbook;
34
import org.apache.poi.ss.util.CellRangeAddress;
35
import org.apache.struts2.interceptor.ServletRequestAware;
36
import org.apache.struts2.interceptor.ServletResponseAware;
37
import org.apache.struts2.rest.DefaultHttpHeaders;
38
import org.apache.struts2.rest.HttpHeaders;
39
 
40
/**
41
 * 
42
 * @author Varun Gupta
43
 * @version 1.0
44
 * @description HotspotReconciliationController handles requests to generate a reconciliation
45
 * report in XLS format for all the transactions with HotSpot within a given date range.
46
 * 
47
 */
48
public class HotspotReconciliationController implements ServletResponseAware, ServletRequestAware {
49
 
1075 chandransh 50
	//FIXME: Read this configuration from the config server
1884 chandransh 51
	//private String hotspotReconciliationReportPath = "/HotspotReports";
992 varun.gupt 52
 
53
	private HttpServletRequest request;
54
	private HttpServletResponse response;
55
 
56
	private String id;
57
 
58
	public HotspotReconciliationController(){
59
 
60
	}
61
 
62
	public HttpHeaders index()	{
63
		return new DefaultHttpHeaders("report");
64
	}
65
 
66
	public HttpHeaders show(){
67
		return new DefaultHttpHeaders("report");
68
	}
69
 
70
	// Handles the POST request (Form Submission)
71
	public HttpHeaders create(){
72
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
73
		Date startDate = null;
74
		Date endDate = null;
75
 
76
		try	{
77
			//Formatting Form input parameters
78
			startDate = dateFormat.parse(request.getParameter("start"));
79
			endDate = dateFormat.parse(request.getParameter("end"));
80
 
81
			CatalogServiceClient csc = new CatalogServiceClient();
82
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
83
			List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);
84
 
85
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
86
			in.shop2020.model.v1.order.TransactionService.Client client = transactionServiceClient.getClient();
87
 
88
			List <Order> orders = new ArrayList<Order>();
89
 
90
			//Retrieving all the orders across all the warehouses
91
			for(Warehouse warehouse : warehouses)	{
1066 varun.gupt 92
				orders.addAll(client.getOrdersByBillingDate(OrderStatus.BILLED, startDate.getTime(), endDate.getTime(), warehouse.getId()));
992 varun.gupt 93
			}
1066 varun.gupt 94
			System.out.println("Total number of Orders: " + orders.size());
992 varun.gupt 95
 
96
			// Preparing XLS file for output
97
			response.setContentType("application/vnd.ms-excel");
98
 
99
			Calendar date = new GregorianCalendar();
100
			int year = date.get(Calendar.YEAR);
101
			int month = date.get(Calendar.MONTH) +1;
102
			int day = date.get(Calendar.DAY_OF_MONTH);
103
 
104
			response.setHeader("Content-disposition", "inline; filename=hotspot-reconciliation-" + "-" + year + "-" + month + "-" + day + ".xls");
105
 
106
			ServletOutputStream sos;
107
			try {
108
				ByteArrayOutputStream baos = getSpreadSheetData(orders, startDate, endDate);
109
				sos = response.getOutputStream();
110
				baos.writeTo(sos);
111
				sos.flush();
112
			} catch (IOException e) {
113
				e.printStackTrace();
114
			}
115
 
116
		} catch (ParseException e)	{
117
			e.printStackTrace();
118
		} catch (TransactionServiceException e)	{
119
			e.printStackTrace();
120
		} catch (Exception e)	{
121
			e.printStackTrace();
122
		} finally	{
123
			System.out.println(startDate.getTime() + "  |  " + endDate.getTime());
124
		}
125
		return new DefaultHttpHeaders("report");
126
	}
127
 
128
	// Prepares the XLS worksheet object and fills in the data with proper formatting
129
	private ByteArrayOutputStream getSpreadSheetData(List <Order> orders, Date startDate, Date endDate)	{
130
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
131
 
132
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
133
 
134
	    Workbook wb = new HSSFWorkbook();
135
	    Sheet sheet = wb.createSheet("new sheet");
136
	    short serialNo = 0;
137
 
138
	    // Create the header row and put all the titles in it. Rows are 0 based.
139
	    Font font = wb.createFont();
140
	    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
141
	    CellStyle style = wb.createCellStyle();
142
	    style.setFont(font);
143
 
144
	    Row titleRow = sheet.createRow(serialNo ++);
145
	    Cell titleCell = titleRow.createCell(0);
146
	    titleCell.setCellValue("HotSpot Reconciliation Report (" + dateFormat.format(startDate) + " - " + dateFormat.format(endDate) + ")");
147
	    titleCell.setCellStyle(style);
148
 
149
	    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
150
 
151
	    sheet.createRow(serialNo ++);
152
 
153
	    Row headerRow = sheet.createRow(serialNo ++);
154
	    headerRow.createCell(0).setCellValue("Billing Number");
155
	    headerRow.createCell(1).setCellValue("Billing Date");
156
	    headerRow.createCell(2).setCellValue("Brand");
157
	    headerRow.createCell(3).setCellValue("Model Name");
158
	    headerRow.createCell(4).setCellValue("Model Number");
159
	    headerRow.createCell(5).setCellValue("Color");
160
	    headerRow.createCell(6).setCellValue("Transfer Price");
161
 
162
	    double totalTransferPrice = 0.0;
163
 
164
	    for(int i = 0; i < orders.size(); i ++)	{
165
	    	Order order = orders.get(i);
166
			serialNo ++;
167
	    	Row contentRow = sheet.createRow(serialNo);
168
 
169
		    LineItem lineItem = order.getLineitems().get(0);
170
		    double transferPrice = lineItem.getTransfer_price();
171
		    totalTransferPrice += transferPrice;
172
 
173
		    contentRow.createCell(0).setCellValue(order.getInvoice_number());
174
		    contentRow.createCell(1).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));
175
		    contentRow.createCell(2).setCellValue(getValueForEmptyString(lineItem.getBrand()));
176
		    contentRow.createCell(3).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
177
		    contentRow.createCell(4).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
178
		    contentRow.createCell(5).setCellValue(getValueForEmptyString(lineItem.getColor()));
179
		    contentRow.createCell(6).setCellValue(transferPrice);
180
	    }
181
	    sheet.createRow(serialNo ++);
182
    	Row contentRow = sheet.createRow(serialNo);
183
    	contentRow.createCell(0).setCellValue("Total Transfer Price");
184
    	contentRow.createCell(6).setCellValue(totalTransferPrice);
185
    	sheet.addMergedRegion(new CellRangeAddress(serialNo, serialNo, 0, 5));
186
 
187
		// Write the workbook to the output stream
188
		try {
189
			wb.write(baosXLS);
190
			baosXLS.close();
191
		} catch (IOException e) {
192
			e.printStackTrace();
193
		}		
194
		return baosXLS;
195
	}
196
 
197
	public String getId(){
198
		return id;
199
	}
200
 
201
	public void setId(String id){
202
		this.id = id;
203
	}
204
 
205
	@Override
206
	public void setServletRequest(HttpServletRequest request) {
207
		this.request = request;
208
	}
209
 
210
	@Override
211
	public void setServletResponse(HttpServletResponse response) {
212
		this.response  = response;
213
	}
214
 
215
	private String getValueForEmptyString(String s){
216
		if(s==null || s.equals(""))
217
			return "-";
218
		else
219
			return s; 
220
	}
221
}