Subversion Repositories SmartDukaan

Rev

Rev 3364 | Rev 4386 | 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;
3364 chandransh 4
import in.shop2020.support.utils.FileUtils;
3125 rajveer 5
import in.shop2020.thrift.clients.CatalogClient;
6
import in.shop2020.thrift.clients.HelperClient;
749 chandransh 7
import in.shop2020.utils.LogisticsUser;
679 chandransh 8
 
9
import java.io.ByteArrayOutputStream;
756 chandransh 10
import java.io.File;
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{
3125 rajveer 65
			HelperClient helperServiceClient = new HelperClient();
749 chandransh 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){
3105 chandransh 71
			logger.error("Error authenticating the user " + username, e);
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());
4209 rajveer 81
			if(warehouseId == 1){
82
				warehouseId = 0;
83
			}
749 chandransh 84
			long providerId = ((Long)session.getAttribute("providerId")).longValue();
3062 chandransh 85
			boolean isCod;
86
			try {
87
	            isCod = Boolean.parseBoolean(request.getParameter("isCod"));
88
	        } catch (Exception e) {
89
	            isCod = false;
90
	        }
3105 chandransh 91
			logger.info("Download request for " + (isCod ? "COD" : "Prepaid") + " Courier Details report of warehouse Id: " + warehouseId + " and provider Id:" + providerId);
749 chandransh 92
 
3062 chandransh 93
			String deliveryType = "prepaid";
94
			if(isCod)
95
			    deliveryType = "cod";
96
 
749 chandransh 97
			response.setContentType("application/vnd.ms-excel");
98
 
99
			Calendar date = new GregorianCalendar();
1075 chandransh 100
			date.add(Calendar.DAY_OF_MONTH, daysToSubtract);
749 chandransh 101
			int year = date.get(Calendar.YEAR);
102
			int month = date.get(Calendar.MONTH) +1;
103
			int day = date.get(Calendar.DAY_OF_MONTH);
3062 chandransh 104
			String fileName = courierDetailsPath + "/courier-details-" + deliveryType + "-" + warehouseId + "-" + providerId + "-" + year + "-" + month + "-" + day +".xls";
105
			response.setHeader("Content-disposition", "inline; filename=courier-details-" + deliveryType + "-" + warehouseId + "-" + providerId + "-" + year + "-"+ month + "-" + day +".xls" );
749 chandransh 106
 
107
			ServletOutputStream sos;
108
			try {
756 chandransh 109
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
3364 chandransh 110
				baos.write(FileUtils.getBytesFromFile(new File(fileName)));
749 chandransh 111
				sos = response.getOutputStream();
112
				baos.writeTo(sos);
113
				sos.flush();
114
			} catch (IOException e) {
3062 chandransh 115
				logger.error("Unable to stream the courier details report", e);
749 chandransh 116
			}	
117
			return "authsuccess";
118
		}catch(NumberFormatException nfe){
3062 chandransh 119
			logger.error("Unable to parse the warehouse id", nfe);
749 chandransh 120
		}
121
		return "authfail";
122
	}
3105 chandransh 123
 
124
    /**
125
     * Sets the daysToSubtract to -2 and then invokes the standard show() handler.
126
     * Should be used to view day before yesterday's courier details report.
127
     * 
128
     * @return the same string tokens as show
129
     */
1075 chandransh 130
	public String dayBefore(){
131
		daysToSubtract = -2;
132
		return show();
133
	}
3105 chandransh 134
 
135
    /**
136
     * Sets the daysToSubtract to -1 and then invokes the standard show()
137
     * handler. Should be used to view yesterday's courier details report.
138
     * 
139
     * @return the same string tokens as show
140
     */
1075 chandransh 141
	public String yesterday(){
142
		daysToSubtract = -1;
143
		return show();
144
	}
3105 chandransh 145
 
146
    /**
147
     * Sets the daysToSubtract to 0 and then invokes the standard show()
148
     * handler. Should be used to view today's courier details report.
149
     * 
150
     * @return the same string tokens as show
151
     */
1075 chandransh 152
	public String today(){
153
		daysToSubtract = 0;
154
		return show();
155
	}
3364 chandransh 156
 
679 chandransh 157
	@Override
1075 chandransh 158
	public void setServletContext(ServletContext context) {
159
		this.context = context;
160
	}
161
 
162
	@Override
679 chandransh 163
	public void setServletResponse(HttpServletResponse response) {
164
		this.response  = response;
165
	}
166
 
167
	@Override
168
	public void setServletRequest(HttpServletRequest request) {
169
		this.request = request;
749 chandransh 170
		this.session = request.getSession();
679 chandransh 171
	}
172
 
749 chandransh 173
	public String getId(){
174
		return id;
175
	}
176
 
177
	public void setId(String id){
178
		this.id = id;
179
	}
180
 
181
	public String getSessionUserName(){
182
		return (String) session.getAttribute("username");
183
	}
3105 chandransh 184
 
185
    /**
186
     * Gets the list of all warehouses and maps the warehouse ids to their
187
     * display name.
188
     * 
189
     * @return the mapping of warehouse if to its display namee
190
     */
754 chandransh 191
	public Map<Long, String> getWarehouses(){
192
		Map<Long, String> warehouseMap = new HashMap<Long, String>();
193
		try{
3125 rajveer 194
			CatalogClient csc = new CatalogClient();
754 chandransh 195
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
196
			List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);
197
			for(Warehouse warehouse : warehouses){
4209 rajveer 198
				// FIXME Remove the hardcoding
199
				if(warehouse.getId() == 2 || warehouse.getId() == 5){
200
					continue;
201
				}
754 chandransh 202
				warehouseMap.put(warehouse.getId(), warehouse.getDisplayName());
203
			}
204
		}catch(Exception e){
3105 chandransh 205
			logger.error("Error getting the list of warehouses", e);
754 chandransh 206
		}
207
		return warehouseMap;
208
	}
749 chandransh 209
 
1075 chandransh 210
	public String getServletContextPath(){
211
		return context.getContextPath();
212
	}
679 chandransh 213
}