Subversion Repositories SmartDukaan

Rev

Rev 2904 | Rev 3105 | Go to most recent revision | Details | Compare with Previous | 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;
4
import in.shop2020.thrift.clients.CatalogServiceClient;
749 chandransh 5
import in.shop2020.thrift.clients.HelperServiceClient;
6
import in.shop2020.utils.LogisticsUser;
679 chandransh 7
 
8
import java.io.ByteArrayOutputStream;
756 chandransh 9
import java.io.File;
10
import java.io.FileInputStream;
679 chandransh 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
 
1075 chandransh 18
import javax.servlet.ServletContext;
679 chandransh 19
import javax.servlet.ServletOutputStream;
20
import javax.servlet.http.HttpServletRequest;
21
import javax.servlet.http.HttpServletResponse;
749 chandransh 22
import javax.servlet.http.HttpSession;
679 chandransh 23
 
24
import org.apache.struts2.interceptor.ServletRequestAware;
25
import org.apache.struts2.interceptor.ServletResponseAware;
1075 chandransh 26
import org.apache.struts2.util.ServletContextAware;
3062 chandransh 27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
679 chandransh 29
 
2904 chandransh 30
/**
31
 * Allows executives of courier companies to login and download courier details
32
 * report which they then use to upload into their database.
33
 * 
34
 * @author Chandranshu
35
 * 
36
 */
679 chandransh 37
public class CourierDetailsController implements ServletResponseAware,
1075 chandransh 38
		ServletRequestAware, ServletContextAware {
3062 chandransh 39
 
40
    private static Logger logger = LoggerFactory.getLogger(CourierDetailsController.class);
41
 
749 chandransh 42
	private String id;
1075 chandransh 43
	private int daysToSubtract;
756 chandransh 44
 
45
	//FIXME: Read this configuration from the config client
46
	private String courierDetailsPath = "/CourierDetailReports";
47
 
1075 chandransh 48
	private ServletContext context;
679 chandransh 49
	private HttpServletRequest request;
50
	private HttpServletResponse response;
749 chandransh 51
	private HttpSession session;
679 chandransh 52
 
749 chandransh 53
	public String index(){
54
		if(getSessionUserName()==null)
55
			return "authfail";
56
		else
57
			return "authsuccess";
58
	}
59
 
60
	// Handler for POST /courier-details
61
	public String create(){
62
		String username = request.getParameter("username");
63
		String password = request.getParameter("password");
64
		try{
65
			HelperServiceClient helperServiceClient = new HelperServiceClient();
66
			in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
67
			LogisticsUser user = client.authenticateLogisticsUser(username, password);
68
			session.setAttribute("username", user.getUsername());
69
			session.setAttribute("providerId", Long.valueOf(user.getProviderId()));
70
		}catch(Exception e){
679 chandransh 71
			e.printStackTrace();
749 chandransh 72
			return "authfail";
73
		}
74
		return "authsuccess";
679 chandransh 75
	}
76
 
749 chandransh 77
	// Handler for GET /courier-details/<warehouseId>
78
	public String show(){
79
		try {
80
			long warehouseId = Long.parseLong(getId());
81
			long providerId = ((Long)session.getAttribute("providerId")).longValue();
3062 chandransh 82
			boolean isCod;
83
			try {
84
	            isCod = Boolean.parseBoolean(request.getParameter("isCod"));
85
	        } catch (Exception e) {
86
	            isCod = false;
87
	        }
749 chandransh 88
			System.out.println("Warehouse Id is:  " + warehouseId);
89
			System.out.println("Provider Id is: " + providerId);
90
 
3062 chandransh 91
			String deliveryType = "prepaid";
92
			if(isCod)
93
			    deliveryType = "cod";
94
 
749 chandransh 95
			response.setContentType("application/vnd.ms-excel");
96
 
97
			Calendar date = new GregorianCalendar();
1075 chandransh 98
			date.add(Calendar.DAY_OF_MONTH, daysToSubtract);
749 chandransh 99
			int year = date.get(Calendar.YEAR);
100
			int month = date.get(Calendar.MONTH) +1;
101
			int day = date.get(Calendar.DAY_OF_MONTH);
3062 chandransh 102
			String fileName = courierDetailsPath + "/courier-details-" + deliveryType + "-" + warehouseId + "-" + providerId + "-" + year + "-" + month + "-" + day +".xls";
103
			response.setHeader("Content-disposition", "inline; filename=courier-details-" + deliveryType + "-" + warehouseId + "-" + providerId + "-" + year + "-"+ month + "-" + day +".xls" );
749 chandransh 104
 
105
			ServletOutputStream sos;
106
			try {
756 chandransh 107
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
108
				baos.write(getBytesFromFile(new File(fileName)));
749 chandransh 109
				sos = response.getOutputStream();
110
				baos.writeTo(sos);
111
				sos.flush();
112
			} catch (IOException e) {
3062 chandransh 113
				logger.error("Unable to stream the courier details report", e);
749 chandransh 114
			}	
115
			return "authsuccess";
116
		}catch(NumberFormatException nfe){
3062 chandransh 117
			logger.error("Unable to parse the warehouse id", nfe);
749 chandransh 118
		}
119
		return "authfail";
120
	}
121
 
1075 chandransh 122
	public String dayBefore(){
123
		daysToSubtract = -2;
124
		return show();
125
	}
126
 
127
	public String yesterday(){
128
		daysToSubtract = -1;
129
		return show();
130
	}
131
 
132
	public String today(){
133
		daysToSubtract = 0;
134
		return show();
135
	}
136
 
756 chandransh 137
	// Returns the contents of the file in a byte array.
138
	public static byte[] getBytesFromFile(File file) throws IOException {
139
	    FileInputStream is = new FileInputStream(file);
140
 
141
	    // Get the size of the file
142
	    long length = file.length();
143
 
144
	    // You cannot create an array using a long type.
145
	    // It needs to be an int type.
146
	    // Before converting to an int type, check
147
	    // to ensure that file is not larger than Integer.MAX_VALUE.
148
	    if (length > Integer.MAX_VALUE) {
149
	        // File is too large
150
	    }
151
 
152
	    // Create the byte array to hold the data
153
	    byte[] bytes = new byte[(int)length];
154
 
155
	    // Read in the bytes
156
	    int offset = 0;
157
	    int numRead = 0;
158
	    while (offset < bytes.length
159
	           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
160
	        offset += numRead;
161
	    }
162
 
163
	    // Ensure all the bytes have been read in
164
	    if (offset < bytes.length) {
165
	        throw new IOException("Could not completely read file "+file.getName());
166
	    }
167
 
168
	    // Close the input stream and return bytes
169
	    is.close();
170
	    return bytes;
171
	}
172
 
679 chandransh 173
	@Override
1075 chandransh 174
	public void setServletContext(ServletContext context) {
175
		this.context = context;
176
	}
177
 
178
	@Override
679 chandransh 179
	public void setServletResponse(HttpServletResponse response) {
180
		this.response  = response;
181
	}
182
 
183
	@Override
184
	public void setServletRequest(HttpServletRequest request) {
185
		this.request = request;
749 chandransh 186
		this.session = request.getSession();
679 chandransh 187
	}
188
 
749 chandransh 189
	public String getId(){
190
		return id;
191
	}
192
 
193
	public void setId(String id){
194
		this.id = id;
195
	}
196
 
197
	public String getSessionUserName(){
198
		return (String) session.getAttribute("username");
199
	}
754 chandransh 200
 
201
	public Map<Long, String> getWarehouses(){
202
		Map<Long, String> warehouseMap = new HashMap<Long, String>();
203
		try{
204
			CatalogServiceClient csc = new CatalogServiceClient();
205
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
206
			List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);
207
			for(Warehouse warehouse : warehouses){
208
				warehouseMap.put(warehouse.getId(), warehouse.getDisplayName());
209
			}
210
		}catch(Exception e){
211
			e.printStackTrace();
212
		}
213
		return warehouseMap;
214
	}
749 chandransh 215
 
1075 chandransh 216
	public String getServletContextPath(){
217
		return context.getContextPath();
218
	}
679 chandransh 219
}