Subversion Repositories SmartDukaan

Rev

Rev 1884 | Rev 4209 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
676 chandransh 1
package in.shop2020.support.controllers;
2
 
756 chandransh 3
import in.shop2020.support.services.CourierDetailsGenerator;
676 chandransh 4
import in.shop2020.support.services.ManifestGenerator;
5
 
6
import java.io.ByteArrayOutputStream;
756 chandransh 7
import java.io.FileNotFoundException;
8
import java.io.FileOutputStream;
676 chandransh 9
import java.io.IOException;
10
import java.util.Calendar;
11
import java.util.GregorianCalendar;
12
 
13
import javax.servlet.ServletOutputStream;
14
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
16
 
17
import org.apache.struts2.interceptor.ServletRequestAware;
18
import org.apache.struts2.interceptor.ServletResponseAware;
19
import org.apache.struts2.rest.DefaultHttpHeaders;
20
import org.apache.struts2.rest.HttpHeaders;
3062 chandransh 21
import org.slf4j.Logger;
22
import org.slf4j.LoggerFactory;
676 chandransh 23
 
24
public class ManifestController implements ServletResponseAware, ServletRequestAware {
756 chandransh 25
 
3062 chandransh 26
    private static Logger logger = LoggerFactory.getLogger(ManifestController.class);
27
 
756 chandransh 28
	//FIXME: Read this configuration from the config client
29
	private String courierDetailsPath = "/CourierDetailReports";
676 chandransh 30
	private long warehouseId;
31
	private long providerId;
3062 chandransh 32
	private boolean isCod;
33
 
676 chandransh 34
	private HttpServletRequest request;
35
	private HttpServletResponse response;
36
 
37
	public HttpHeaders index(){
38
		this.warehouseId = Long.parseLong(request.getParameter("warehouseID"));
39
		this.providerId = Long.parseLong(request.getParameter("providerID"));
3062 chandransh 40
		try {
41
		    this.isCod = Boolean.parseBoolean(request.getParameter("isCod"));
42
		} catch (Exception e) {
43
		    this.isCod = false;
44
		}
676 chandransh 45
 
3062 chandransh 46
		logger.debug("Warehouse Id is:  " + warehouseId);
47
		logger.debug("Provider Id is: " + providerId);
48
		logger.debug("Cod is: " + isCod);
676 chandransh 49
 
50
		Calendar date = new GregorianCalendar();
51
		int year = date.get(Calendar.YEAR);
52
		int month = date.get(Calendar.MONTH) +1;
53
		int day = date.get(Calendar.DAY_OF_MONTH);
54
 
3062 chandransh 55
		String fileNameSuffix = "-" + warehouseId + "-"+ providerId + "-" + year + "-" + month + "-" + day;
56
		if(isCod)
57
		    fileNameSuffix = "cod" + fileNameSuffix ;
58
		else
59
		    fileNameSuffix = "prepaid" + fileNameSuffix;
60
 
756 chandransh 61
		ManifestGenerator manifestGenerator = new ManifestGenerator();
3062 chandransh 62
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(warehouseId, providerId, isCod);
756 chandransh 63
		response.setContentType("application/pdf");
64
 
65
		CourierDetailsGenerator courierDetailsGenerator = new CourierDetailsGenerator();
3062 chandransh 66
		ByteArrayOutputStream baosXLS = courierDetailsGenerator.generateCourierDetails(warehouseId, providerId, isCod);
756 chandransh 67
		try {
3062 chandransh 68
			String fileName = courierDetailsPath + "/courier-details-" + fileNameSuffix + ".xls";
756 chandransh 69
			FileOutputStream f = new FileOutputStream(fileName);
70
			baosXLS.writeTo(f);
71
			f.close();
72
		} catch (FileNotFoundException e) {
3062 chandransh 73
			logger.error("Unable to create the courier details file", e);
756 chandransh 74
		} catch (IOException e) {
3062 chandransh 75
		    logger.error("Unable to create the courier details file", e);
756 chandransh 76
		}
77
 
3062 chandransh 78
		response.setHeader("Content-disposition", "inline; filename=manifest-" + fileNameSuffix + ".pdf" );
676 chandransh 79
 
80
		ServletOutputStream sos;
81
		try {
82
			sos = response.getOutputStream();
83
			baos.writeTo(sos);
84
			sos.flush();
85
		} catch (IOException e) {
3062 chandransh 86
		    logger.error("Unable to stream the manifest file", e);
676 chandransh 87
		}	
88
		return new DefaultHttpHeaders("lsuccess");
89
	}
90
 
91
	@Override
92
	public void setServletResponse(HttpServletResponse response) {
93
		this.response  = response;
94
	}
95
 
96
	@Override
97
	public void setServletRequest(HttpServletRequest request) {
98
		this.request = request;
99
	}
100
}