Subversion Repositories SmartDukaan

Rev

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