Subversion Repositories SmartDukaan

Rev

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