Subversion Repositories SmartDukaan

Rev

Rev 1884 | Rev 4209 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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