Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
679 chandransh 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.support.services.CourierDetailsGenerator;
749 chandransh 4
import in.shop2020.thrift.clients.HelperServiceClient;
5
import in.shop2020.utils.HelperService;
6
import in.shop2020.utils.LogisticsUser;
679 chandransh 7
 
8
import java.io.ByteArrayOutputStream;
9
import java.io.IOException;
10
import java.util.Calendar;
11
import java.util.GregorianCalendar;
12
 
13
import javax.servlet.ServletOutputStream;
14
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
749 chandransh 16
import javax.servlet.http.HttpSession;
679 chandransh 17
 
18
import org.apache.struts2.interceptor.ServletRequestAware;
19
import org.apache.struts2.interceptor.ServletResponseAware;
20
import org.apache.struts2.rest.DefaultHttpHeaders;
21
import org.apache.struts2.rest.HttpHeaders;
22
 
23
public class CourierDetailsController implements ServletResponseAware,
24
		ServletRequestAware {
749 chandransh 25
	private String id;
26
	//private long warehouseId;
27
	//private long providerId;
679 chandransh 28
	private HttpServletRequest request;
29
	private HttpServletResponse response;
749 chandransh 30
	private HttpSession session;
679 chandransh 31
 
749 chandransh 32
	public String index(){
33
		if(getSessionUserName()==null)
34
			return "authfail";
35
		else
36
			return "authsuccess";
37
	}
38
 
39
	// Handler for POST /courier-details
40
	public String create(){
41
		String username = request.getParameter("username");
42
		String password = request.getParameter("password");
43
		try{
44
			HelperServiceClient helperServiceClient = new HelperServiceClient();
45
			in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
46
			LogisticsUser user = client.authenticateLogisticsUser(username, password);
47
			session.setAttribute("username", user.getUsername());
48
			session.setAttribute("providerId", Long.valueOf(user.getProviderId()));
49
		}catch(Exception e){
679 chandransh 50
			e.printStackTrace();
749 chandransh 51
			return "authfail";
52
		}
53
		return "authsuccess";
679 chandransh 54
	}
55
 
749 chandransh 56
	// Handler for GET /courier-details/<warehouseId>
57
	public String show(){
58
		try {
59
			long warehouseId = Long.parseLong(getId());
60
			long providerId = ((Long)session.getAttribute("providerId")).longValue();
61
 
62
			System.out.println("Warehouse Id is:  " + warehouseId);
63
			System.out.println("Provider Id is: " + providerId);
64
 
65
			CourierDetailsGenerator courierDetailsGenerator = new CourierDetailsGenerator();
66
			ByteArrayOutputStream baos = courierDetailsGenerator.generateCourierDetails(warehouseId, providerId);
67
			response.setContentType("application/vnd.ms-excel");
68
 
69
			Calendar date = new GregorianCalendar();
70
			int year = date.get(Calendar.YEAR);
71
			int month = date.get(Calendar.MONTH) +1;
72
			int day = date.get(Calendar.DAY_OF_MONTH);
73
 
74
			response.setHeader("Content-disposition", "inline; filename=courier-details-"+warehouseId+"-"+ providerId + "-"+year+"-"+ month+"-" + day +".xls" );
75
 
76
			ServletOutputStream sos;
77
			try {
78
				sos = response.getOutputStream();
79
				baos.writeTo(sos);
80
				sos.flush();
81
			} catch (IOException e) {
82
				e.printStackTrace();
83
			}	
84
			return "authsuccess";
85
		}catch(NumberFormatException nfe){
86
			nfe.printStackTrace();
87
		}
88
		return "authfail";
89
	}
90
 
679 chandransh 91
	@Override
92
	public void setServletResponse(HttpServletResponse response) {
93
		this.response  = response;
94
	}
95
 
96
	@Override
97
	public void setServletRequest(HttpServletRequest request) {
98
		this.request = request;
749 chandransh 99
		this.session = request.getSession();
679 chandransh 100
	}
101
 
749 chandransh 102
	public String getId(){
103
		return id;
104
	}
105
 
106
	public void setId(String id){
107
		this.id = id;
108
	}
109
 
110
	public String getSessionUserName(){
111
		return (String) session.getAttribute("username");
112
	}
113
 
679 chandransh 114
}