Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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