Subversion Repositories SmartDukaan

Rev

Rev 8406 | Rev 20780 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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