Subversion Repositories SmartDukaan

Rev

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