Subversion Repositories SmartDukaan

Rev

Rev 3062 | Rev 4410 | 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;
4209 rajveer 4
import in.shop2020.support.services.CourierDetailsReportMerger;
676 chandransh 5
import in.shop2020.support.services.ManifestGenerator;
6
 
7
import java.io.ByteArrayOutputStream;
756 chandransh 8
import java.io.FileNotFoundException;
9
import java.io.FileOutputStream;
676 chandransh 10
import java.io.IOException;
11
import java.util.Calendar;
12
import java.util.GregorianCalendar;
4209 rajveer 13
import java.util.HashMap;
14
import java.util.Map;
676 chandransh 15
 
16
import javax.servlet.ServletOutputStream;
17
import javax.servlet.http.HttpServletRequest;
18
import javax.servlet.http.HttpServletResponse;
19
 
20
import org.apache.struts2.interceptor.ServletRequestAware;
21
import org.apache.struts2.interceptor.ServletResponseAware;
22
import org.apache.struts2.rest.DefaultHttpHeaders;
23
import org.apache.struts2.rest.HttpHeaders;
3062 chandransh 24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
676 chandransh 26
 
27
public class ManifestController implements ServletResponseAware, ServletRequestAware {
756 chandransh 28
 
3062 chandransh 29
    private static Logger logger = LoggerFactory.getLogger(ManifestController.class);
30
 
756 chandransh 31
	//FIXME: Read this configuration from the config client
32
	private String courierDetailsPath = "/CourierDetailReports";
676 chandransh 33
	private long warehouseId;
34
	private long providerId;
3062 chandransh 35
	private boolean isCod;
36
 
676 chandransh 37
	private HttpServletRequest request;
38
	private HttpServletResponse response;
39
 
40
	public HttpHeaders index(){
41
		this.warehouseId = Long.parseLong(request.getParameter("warehouseID"));
42
		this.providerId = Long.parseLong(request.getParameter("providerID"));
3062 chandransh 43
		try {
44
		    this.isCod = Boolean.parseBoolean(request.getParameter("isCod"));
45
		} catch (Exception e) {
46
		    this.isCod = false;
47
		}
676 chandransh 48
 
3062 chandransh 49
		logger.debug("Warehouse Id is:  " + warehouseId);
50
		logger.debug("Provider Id is: " + providerId);
51
		logger.debug("Cod is: " + isCod);
676 chandransh 52
 
53
		Calendar date = new GregorianCalendar();
54
		int year = date.get(Calendar.YEAR);
55
		int month = date.get(Calendar.MONTH) +1;
56
		int day = date.get(Calendar.DAY_OF_MONTH);
57
 
3062 chandransh 58
		String fileNameSuffix = "-" + warehouseId + "-"+ providerId + "-" + year + "-" + month + "-" + day;
4209 rajveer 59
		String mergedFileNameSuffix = "-" + 0 + "-"+ providerId + "-" + year + "-" + month + "-" + day;
60
		if(isCod){
3062 chandransh 61
		    fileNameSuffix = "cod" + fileNameSuffix ;
4209 rajveer 62
			mergedFileNameSuffix = "cod" + mergedFileNameSuffix;
63
		}
64
		else{
3062 chandransh 65
		    fileNameSuffix = "prepaid" + fileNameSuffix;
4209 rajveer 66
			mergedFileNameSuffix = "prepaid" + mergedFileNameSuffix;
67
		}
3062 chandransh 68
 
756 chandransh 69
		ManifestGenerator manifestGenerator = new ManifestGenerator();
3062 chandransh 70
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(warehouseId, providerId, isCod);
756 chandransh 71
		response.setContentType("application/pdf");
72
 
73
		CourierDetailsGenerator courierDetailsGenerator = new CourierDetailsGenerator();
3062 chandransh 74
		ByteArrayOutputStream baosXLS = courierDetailsGenerator.generateCourierDetails(warehouseId, providerId, isCod);
756 chandransh 75
		try {
3062 chandransh 76
			String fileName = courierDetailsPath + "/courier-details-" + fileNameSuffix + ".xls";
756 chandransh 77
			FileOutputStream f = new FileOutputStream(fileName);
78
			baosXLS.writeTo(f);
79
			f.close();
80
		} catch (FileNotFoundException e) {
3062 chandransh 81
			logger.error("Unable to create the courier details file", e);
756 chandransh 82
		} catch (IOException e) {
3062 chandransh 83
		    logger.error("Unable to create the courier details file", e);
756 chandransh 84
		}
85
 
4209 rajveer 86
		// FIXME This is not a useful way. We need to fix it asap. This is done just to give ease to courier company.
87
		CourierDetailsReportMerger merger = new CourierDetailsReportMerger();
88
		try {
89
			FileOutputStream f = new FileOutputStream(courierDetailsPath + "/courier-details-" + mergedFileNameSuffix + ".xls");
90
			Map<Long, String> warehouseIdFileNames = new HashMap<Long, String>();
91
 
92
			String p1;
93
			if(isCod){
94
			    p1 = "cod";
95
			}
96
			else{
97
			    p1 = "prepaid";
98
			}
99
			String fName;
100
			fName = courierDetailsPath + "/courier-details-" + p1 + "-" + 1 + "-"+ providerId + "-" + year + "-" + month + "-" + day + ".xls";
101
			warehouseIdFileNames.put(1L, fName);
102
			fName = courierDetailsPath + "/courier-details-" + p1 + "-" + 2 + "-"+ providerId + "-" + year + "-" + month + "-" + day + ".xls";
103
			warehouseIdFileNames.put(2L, fName);
104
			fName = courierDetailsPath + "/courier-details-" + p1 + "-" + 5 + "-"+ providerId + "-" + year + "-" + month + "-" + day + ".xls";
105
			warehouseIdFileNames.put(5L, fName);
106
			ByteArrayOutputStream binXLS = merger.mergeCourierDetailsReports(warehouseIdFileNames, 1, true);
107
			binXLS.writeTo(f);
108
			f.close();
109
		} catch (FileNotFoundException e) {
110
			logger.error("Error while creating the Courier Details report", e);
111
		} catch (IOException e) {
112
			logger.error("IO error while writing the Courier Details report", e);
113
		}
114
 
115
 
3062 chandransh 116
		response.setHeader("Content-disposition", "inline; filename=manifest-" + fileNameSuffix + ".pdf" );
676 chandransh 117
 
118
		ServletOutputStream sos;
119
		try {
120
			sos = response.getOutputStream();
121
			baos.writeTo(sos);
122
			sos.flush();
123
		} catch (IOException e) {
3062 chandransh 124
		    logger.error("Unable to stream the manifest file", e);
676 chandransh 125
		}	
126
		return new DefaultHttpHeaders("lsuccess");
127
	}
128
 
129
	@Override
130
	public void setServletResponse(HttpServletResponse response) {
131
		this.response  = response;
132
	}
133
 
134
	@Override
135
	public void setServletRequest(HttpServletRequest request) {
136
		this.request = request;
137
	}
138
}