Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1597 ankur.sing 1
package in.shop2020.support.controllers;
2
 
1886 ankur.sing 3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
5
import in.shop2020.model.v1.order.OrderStatus;
6
import in.shop2020.model.v1.order.TransactionServiceException;
1941 ankur.sing 7
import in.shop2020.support.utils.ReportsUtils;
3125 rajveer 8
import in.shop2020.thrift.clients.PaymentClient;
9
import in.shop2020.thrift.clients.TransactionClient;
10
import in.shop2020.thrift.clients.UserClient;
1597 ankur.sing 11
 
20779 amit.gupta 12
import java.io.InputStream;
1891 ankur.sing 13
import java.text.DateFormat;
14
import java.text.SimpleDateFormat;
20779 amit.gupta 15
import java.util.ArrayList;
1891 ankur.sing 16
import java.util.Calendar;
20779 amit.gupta 17
import java.util.Date;
1731 ankur.sing 18
import java.util.List;
19
 
1941 ankur.sing 20
import javax.servlet.ServletContext;
20788 amit.gupta 21
import javax.servlet.ServletOutputStream;
1941 ankur.sing 22
import javax.servlet.http.HttpServletRequest;
20788 amit.gupta 23
import javax.servlet.http.HttpServletResponse;
1941 ankur.sing 24
import javax.servlet.http.HttpSession;
25
 
20790 amit.gupta 26
import org.apache.axis.utils.ByteArrayOutputStream;
20779 amit.gupta 27
import org.apache.poi.ss.usermodel.Row;
28
import org.apache.poi.ss.usermodel.Sheet;
29
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
1891 ankur.sing 30
import org.apache.struts2.convention.annotation.InterceptorRef;
31
import org.apache.struts2.convention.annotation.InterceptorRefs;
3936 chandransh 32
import org.apache.struts2.convention.annotation.Result;
33
import org.apache.struts2.convention.annotation.Results;
1941 ankur.sing 34
import org.apache.struts2.interceptor.ServletRequestAware;
20788 amit.gupta 35
import org.apache.struts2.interceptor.ServletResponseAware;
1941 ankur.sing 36
import org.apache.struts2.util.ServletContextAware;
1886 ankur.sing 37
import org.apache.thrift.TException;
3071 chandransh 38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
1597 ankur.sing 40
 
20788 amit.gupta 41
import com.opensymphony.xwork2.ActionSupport;
42
 
20779 amit.gupta 43
@InterceptorRefs({ @InterceptorRef("defaultStack"), @InterceptorRef("login") })
44
@Results({ @Result(name = "authfail", type = "redirectAction", params = {
20788 amit.gupta 45
		"actionName", "reports" })
20780 amit.gupta 46
})
20788 amit.gupta 47
public class StatisticsController implements ServletRequestAware, ServletResponseAware,
20779 amit.gupta 48
		ServletContextAware {
1597 ankur.sing 49
 
20779 amit.gupta 50
	private static Logger logger = LoggerFactory
51
			.getLogger(StatisticsController.class);
20780 amit.gupta 52
 
1630 ankur.sing 53
 
20788 amit.gupta 54
	private HttpServletResponse response;
20780 amit.gupta 55
	private String type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
56
 
57
	public String getType() {
58
		return type;
59
	}
60
 
20779 amit.gupta 61
	private HttpServletRequest request;
62
	private HttpSession session;
63
	private ServletContext context;
1891 ankur.sing 64
 
65
	private long noOfRegisterUsers;
66
	private long noOfOrders;
67
	private long noOfCustomers;
20779 amit.gupta 68
	private int quantity;
1891 ankur.sing 69
	private double maxOrderAmount;
70
	private double minOrderAmount;
71
	private double maxPaymentAmount;
72
	private double minPaymentAmount;
73
	private List<Double> paymentAmountRange;
74
	private List<Double> orderAmountRange;
75
	private List<Order> validOrders;
76
 
20779 amit.gupta 77
	private String my;
78
 
79
	private UserClient usc;
80
	private in.shop2020.model.v1.user.UserContextService.Client uclient;
81
 
82
	private TransactionClient tsc;
83
	private in.shop2020.model.v1.order.TransactionService.Client tClient;
84
 
85
	private PaymentClient psc;
86
	private in.shop2020.payments.PaymentService.Client pClient;
87
 
88
	private final DateFormat formatter = new SimpleDateFormat(
89
			"EEE, dd-MMM-yyyy hh:mm a");
90
 
91
	public StatisticsController() {
92
		try {
93
			usc = new UserClient();
94
			uclient = usc.getClient();
95
 
96
			tsc = new TransactionClient();
97
			tClient = tsc.getClient();
98
 
99
			psc = new PaymentClient();
100
			pClient = psc.getClient();
101
		} catch (Exception e) {
102
			logger.error(
103
					"Error connecting to one of the user, order or payment service",
104
					e);
105
		}
1891 ankur.sing 106
	}
20779 amit.gupta 107
 
1941 ankur.sing 108
	public String index() {
20779 amit.gupta 109
		if (!ReportsUtils.canAccessReport(
110
				(Long) session.getAttribute(ReportsUtils.ROLE),
111
				request.getServletPath())) {
1891 ankur.sing 112
			return "authfail";
113
		}
20779 amit.gupta 114
		getStats();
115
		return "authsuccess";
116
 
117
		/*
118
		 * if(getSessionUserName()==null) { return "authfail"; } else {
119
		 * if(!canAccessReport()) { return "exception"; } getStats(); return
120
		 * "authsuccess"; }
121
		 */
1891 ankur.sing 122
	}
20779 amit.gupta 123
 
1891 ankur.sing 124
	private void getStats() {
20779 amit.gupta 125
		// try {
126
		// noOfRegisterUsers = uclient.getUserCount(UserType.USER);
127
		// } catch(Exception e){
128
		// logger.error("Error while getting the no. of registered users", e);
129
		// }
130
 
3071 chandransh 131
		try {
20779 amit.gupta 132
			// noOfOrders = tClient.getValidOrderCount();
133
			// noOfCustomers =
134
			// tClient.getNoOfCustomersWithSuccessfulTransaction();
135
			// orderAmountRange = tClient.getValidOrdersAmountRange();
136
			// minOrderAmount = orderAmountRange.get(0);
137
			// maxOrderAmount = orderAmountRange.get(1);
138
			//
20783 amit.gupta 139
			validOrders = tClient.getValidOrders(50, false);
20779 amit.gupta 140
		} catch (Exception e) {
141
			logger.error("Error while getting order statistics", e);
3071 chandransh 142
		}
20779 amit.gupta 143
 
3071 chandransh 144
		try {
20779 amit.gupta 145
			paymentAmountRange = pClient.getSuccessfulPaymentsAmountRange();
146
			minPaymentAmount = paymentAmountRange.get(0);
147
			maxPaymentAmount = paymentAmountRange.get(1);
148
		} catch (Exception e) {
149
			logger.error("Error while getting payment statistics", e);
150
		}
1891 ankur.sing 151
	}
20779 amit.gupta 152
 
1891 ankur.sing 153
	public long getNoOfRegisterUsers() {
154
		return noOfRegisterUsers;
155
	}
1886 ankur.sing 156
 
1891 ankur.sing 157
	public long getNoOfOrders() {
158
		return noOfOrders;
159
	}
20779 amit.gupta 160
 
1891 ankur.sing 161
	public long getNoOfCustomers() {
162
		return noOfCustomers;
163
	}
1886 ankur.sing 164
 
1891 ankur.sing 165
	public double getMaxOrderAmount() {
166
		return maxOrderAmount;
167
	}
1886 ankur.sing 168
 
1891 ankur.sing 169
	public double getMinOrderAmount() {
170
		return minOrderAmount;
171
	}
1886 ankur.sing 172
 
1891 ankur.sing 173
	public double getMaxPaymentAmount() {
174
		return maxPaymentAmount;
175
	}
1886 ankur.sing 176
 
1891 ankur.sing 177
	public double getMinPaymentAmount() {
178
		return minPaymentAmount;
179
	}
1886 ankur.sing 180
 
20779 amit.gupta 181
	public List<Order> getValidOrders() {
182
		return validOrders;
183
	}
1941 ankur.sing 184
 
20779 amit.gupta 185
	public LineItem getItem(Order order) throws TransactionServiceException,
186
			TException {
187
		LineItem lItem = order.getLineitems().get(0);
188
		return lItem;
189
	}
1941 ankur.sing 190
 
20779 amit.gupta 191
	public String getOrderStatusString(OrderStatus status) {
192
		return status.getDescription();
193
	}
194
 
195
	public String getDateTime(long milliseconds) {
196
		Calendar cal = Calendar.getInstance();
197
		cal.setTimeInMillis(milliseconds);
198
		return formatter.format(cal.getTime());
199
	}
200
 
201
	@Override
202
	public void setServletRequest(HttpServletRequest req) {
203
		this.request = req;
204
		this.session = req.getSession();
205
	}
206
 
207
	@Override
208
	public void setServletContext(ServletContext context) {
209
		this.context = context;
210
	}
211
 
212
	public String getServletContextPath() {
213
		return context.getContextPath();
214
	}
215
 
216
	public void setQuantity(int quantity) {
217
		this.quantity = quantity;
218
	}
219
 
220
	public int getQuantity() {
221
		return quantity;
222
	}
223
 
224
	public String download() throws Exception {
225
		InputStream is = getClass().getClassLoader().getResourceAsStream("orderformat.xlsx");
20787 amit.gupta 226
		logger.info("is stream is " + (is==null?"null" : "not null"));
20779 amit.gupta 227
		XSSFWorkbook workbook = new XSSFWorkbook(is);
20790 amit.gupta 228
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
20779 amit.gupta 229
		Sheet sheet = workbook.getSheetAt(0);
230
		workbook.setSheetName(0, this.getMy());
231
		DateFormat df = new SimpleDateFormat("MMM, yyyy");
232
		Date fromDate = df.parse(this.getMy());
233
 
234
		Calendar toDateCal = Calendar.getInstance();
235
		toDateCal.add(Calendar.MONTH, 1);
236
		List<Order> orders = tClient.getAllOrders(new ArrayList<OrderStatus>(), fromDate.getTime(), toDateCal.getTime().getTime(), 0);
237
		int rowNum=2;
238
		for(Order o : orders){
239
			Row row = sheet.createRow(rowNum);
240
			int i=0;
241
			row.createCell(++i).setCellValue(o.getId());
242
			row.createCell(++i).setCellValue(new Date(o.getCreated_timestamp()));
243
 
244
			++i;
245
			row.createCell(++i).setCellValue(new Date(o.getVerification_timestamp()));
246
			++i;
247
 
248
			row.createCell(++i).setCellValue(o.getInvoice_number());
249
			row.createCell(++i).setCellValue(o.getBilled_by());
250
			++i;
251
			row.createCell(++i).setCellValue(new Date(o.getBilling_timestamp()));
20783 amit.gupta 252
			row.createCell(++i).setCellValue(new Date(o.getShipping_timestamp()));
20779 amit.gupta 253
			row.createCell(++i).setCellValue(o.getAirwaybill_no());
254
			row.createCell(++i).setCellValue(o.getLogistics_provider_id());
255
			++i;
256
			row.createCell(++i).setCellValue(new Date(o.getDelivery_timestamp()));
257
			row.createCell(++i).setCellValue(o.getLineitems().get(0).getQuantity());
258
			row.createCell(++i).setCellValue(o.getTotal_amount() + o.getShippingCost());
259
			if(o.getStatus().equals(OrderStatus.DELIVERY_SUCCESS)) {
260
				row.createCell(++i).setCellValue(o.getTotal_amount() + o.getShippingCost());
261
			}else {
262
				++i;
263
			}
264
			if(o.isCod()) {
265
				row.createCell(++i).setCellValue("COD");
266
			} else {
267
				row.createCell(++i).setCellValue("PREPAID");
268
			}
269
			row.createCell(++i).setCellValue(o.getStatusDescription());
270
			row.createCell(++i).setCellValue(o.getCustomer_city());
271
 
272
		}
20788 amit.gupta 273
		ServletOutputStream os = response.getOutputStream();
20790 amit.gupta 274
		workbook.write(bos);
275
		bos.writeTo(os);
20788 amit.gupta 276
		String filename=this.getMy() + "-order.xlsx";
20789 amit.gupta 277
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
20788 amit.gupta 278
		response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
279
 
20779 amit.gupta 280
 
20788 amit.gupta 281
		return ActionSupport.NONE;
20779 amit.gupta 282
	}
283
 
284
	public void setMy(String my) {
285
		this.my = my;
286
	}
287
 
288
	public String getMy() {
289
		return my;
290
	}
20788 amit.gupta 291
 
292
	@Override
293
	public void setServletResponse(HttpServletResponse arg0) {
294
		this.response = arg0;
295
 
296
	}
1597 ankur.sing 297
}