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
 
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;
756 chandransh 11
import java.io.File;
12
import java.io.FileInputStream;
679 chandransh 13
import java.io.IOException;
14
import java.util.Calendar;
15
import java.util.GregorianCalendar;
754 chandransh 16
import java.util.HashMap;
17
import java.util.List;
18
import java.util.Map;
679 chandransh 19
 
20
import javax.servlet.ServletOutputStream;
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpServletResponse;
749 chandransh 23
import javax.servlet.http.HttpSession;
679 chandransh 24
 
25
import org.apache.struts2.interceptor.ServletRequestAware;
26
import org.apache.struts2.interceptor.ServletResponseAware;
27
import org.apache.struts2.rest.DefaultHttpHeaders;
28
import org.apache.struts2.rest.HttpHeaders;
29
 
30
public class CourierDetailsController implements ServletResponseAware,
31
		ServletRequestAware {
749 chandransh 32
	private String id;
756 chandransh 33
 
34
	//FIXME: Read this configuration from the config client
35
	private String courierDetailsPath = "/CourierDetailReports";
36
 
679 chandransh 37
	private HttpServletRequest request;
38
	private HttpServletResponse response;
749 chandransh 39
	private HttpSession session;
679 chandransh 40
 
749 chandransh 41
	public String index(){
42
		if(getSessionUserName()==null)
43
			return "authfail";
44
		else
45
			return "authsuccess";
46
	}
47
 
48
	// Handler for POST /courier-details
49
	public String create(){
50
		String username = request.getParameter("username");
51
		String password = request.getParameter("password");
52
		try{
53
			HelperServiceClient helperServiceClient = new HelperServiceClient();
54
			in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
55
			LogisticsUser user = client.authenticateLogisticsUser(username, password);
56
			session.setAttribute("username", user.getUsername());
57
			session.setAttribute("providerId", Long.valueOf(user.getProviderId()));
58
		}catch(Exception e){
679 chandransh 59
			e.printStackTrace();
749 chandransh 60
			return "authfail";
61
		}
62
		return "authsuccess";
679 chandransh 63
	}
64
 
749 chandransh 65
	// Handler for GET /courier-details/<warehouseId>
66
	public String show(){
67
		try {
68
			long warehouseId = Long.parseLong(getId());
69
			long providerId = ((Long)session.getAttribute("providerId")).longValue();
70
 
71
			System.out.println("Warehouse Id is:  " + warehouseId);
72
			System.out.println("Provider Id is: " + providerId);
73
 
74
			response.setContentType("application/vnd.ms-excel");
75
 
76
			Calendar date = new GregorianCalendar();
77
			int year = date.get(Calendar.YEAR);
78
			int month = date.get(Calendar.MONTH) +1;
79
			int day = date.get(Calendar.DAY_OF_MONTH);
756 chandransh 80
			String fileName = courierDetailsPath + "/courier-details-"+warehouseId+"-"+ providerId + "-"+year+"-"+ month+"-" + day +".xls";
749 chandransh 81
			response.setHeader("Content-disposition", "inline; filename=courier-details-"+warehouseId+"-"+ providerId + "-"+year+"-"+ month+"-" + day +".xls" );
82
 
83
			ServletOutputStream sos;
84
			try {
756 chandransh 85
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
86
				baos.write(getBytesFromFile(new File(fileName)));
749 chandransh 87
				sos = response.getOutputStream();
88
				baos.writeTo(sos);
89
				sos.flush();
90
			} catch (IOException e) {
91
				e.printStackTrace();
92
			}	
93
			return "authsuccess";
94
		}catch(NumberFormatException nfe){
95
			nfe.printStackTrace();
96
		}
97
		return "authfail";
98
	}
99
 
756 chandransh 100
	// Returns the contents of the file in a byte array.
101
	public static byte[] getBytesFromFile(File file) throws IOException {
102
	    FileInputStream is = new FileInputStream(file);
103
 
104
	    // Get the size of the file
105
	    long length = file.length();
106
 
107
	    // You cannot create an array using a long type.
108
	    // It needs to be an int type.
109
	    // Before converting to an int type, check
110
	    // to ensure that file is not larger than Integer.MAX_VALUE.
111
	    if (length > Integer.MAX_VALUE) {
112
	        // File is too large
113
	    }
114
 
115
	    // Create the byte array to hold the data
116
	    byte[] bytes = new byte[(int)length];
117
 
118
	    // Read in the bytes
119
	    int offset = 0;
120
	    int numRead = 0;
121
	    while (offset < bytes.length
122
	           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
123
	        offset += numRead;
124
	    }
125
 
126
	    // Ensure all the bytes have been read in
127
	    if (offset < bytes.length) {
128
	        throw new IOException("Could not completely read file "+file.getName());
129
	    }
130
 
131
	    // Close the input stream and return bytes
132
	    is.close();
133
	    return bytes;
134
	}
135
 
679 chandransh 136
	@Override
137
	public void setServletResponse(HttpServletResponse response) {
138
		this.response  = response;
139
	}
140
 
141
	@Override
142
	public void setServletRequest(HttpServletRequest request) {
143
		this.request = request;
749 chandransh 144
		this.session = request.getSession();
679 chandransh 145
	}
146
 
749 chandransh 147
	public String getId(){
148
		return id;
149
	}
150
 
151
	public void setId(String id){
152
		this.id = id;
153
	}
154
 
155
	public String getSessionUserName(){
156
		return (String) session.getAttribute("username");
157
	}
754 chandransh 158
 
159
	public Map<Long, String> getWarehouses(){
160
		Map<Long, String> warehouseMap = new HashMap<Long, String>();
161
		try{
162
			CatalogServiceClient csc = new CatalogServiceClient();
163
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
164
			List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);
165
			for(Warehouse warehouse : warehouses){
166
				warehouseMap.put(warehouse.getId(), warehouse.getDisplayName());
167
			}
168
		}catch(Exception e){
169
			e.printStackTrace();
170
		}
171
		return warehouseMap;
172
	}
749 chandransh 173
 
679 chandransh 174
}