Subversion Repositories SmartDukaan

Rev

Rev 3062 | Rev 4788 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.controllers;

import in.shop2020.support.services.CourierDetailsGenerator;
import in.shop2020.support.services.CourierDetailsReportMerger;
import in.shop2020.support.services.ManifestGenerator;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ManifestController implements ServletResponseAware, ServletRequestAware {
        
    private static Logger logger = LoggerFactory.getLogger(ManifestController.class);
    
        //FIXME: Read this configuration from the config client
        private String courierDetailsPath = "/CourierDetailReports";
        private long warehouseId;
        private long providerId;
        private boolean isCod;
        
        private HttpServletRequest request;
        private HttpServletResponse response;

        public HttpHeaders index(){
                this.warehouseId = Long.parseLong(request.getParameter("warehouseID"));
                this.providerId = Long.parseLong(request.getParameter("providerID"));
                try {
                    this.isCod = Boolean.parseBoolean(request.getParameter("isCod"));
                } catch (Exception e) {
                    this.isCod = false;
                }
                
                logger.debug("Warehouse Id is:  " + warehouseId);
                logger.debug("Provider Id is: " + providerId);
                logger.debug("Cod is: " + isCod);
                
                Calendar date = new GregorianCalendar();
                int year = date.get(Calendar.YEAR);
                int month = date.get(Calendar.MONTH) +1;
                int day = date.get(Calendar.DAY_OF_MONTH);
                
                String fileNameSuffix = "-" + warehouseId + "-"+ providerId + "-" + year + "-" + month + "-" + day;
                String mergedFileNameSuffix = "-" + 0 + "-"+ providerId + "-" + year + "-" + month + "-" + day;
                if(isCod){
                    fileNameSuffix = "cod" + fileNameSuffix ;
                        mergedFileNameSuffix = "cod" + mergedFileNameSuffix;
                }
                else{
                    fileNameSuffix = "prepaid" + fileNameSuffix;
                        mergedFileNameSuffix = "prepaid" + mergedFileNameSuffix;
                }
                
                ManifestGenerator manifestGenerator = new ManifestGenerator();
                ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(warehouseId, providerId, isCod);
                response.setContentType("application/pdf");
                
                CourierDetailsGenerator courierDetailsGenerator = new CourierDetailsGenerator();
                ByteArrayOutputStream baosXLS = courierDetailsGenerator.generateCourierDetails(warehouseId, providerId, isCod);
                try {
                        String fileName = courierDetailsPath + "/courier-details-" + fileNameSuffix + ".xls";
                        FileOutputStream f = new FileOutputStream(fileName);
                        baosXLS.writeTo(f);
                        f.close();
                } catch (FileNotFoundException e) {
                        logger.error("Unable to create the courier details file", e);
                } catch (IOException e) {
                    logger.error("Unable to create the courier details file", e);
                }
                
                // FIXME This is not a useful way. We need to fix it asap. This is done just to give ease to courier company.
                CourierDetailsReportMerger merger = new CourierDetailsReportMerger();
                try {
                        FileOutputStream f = new FileOutputStream(courierDetailsPath + "/courier-details-" + mergedFileNameSuffix + ".xls");
                        Map<Long, String> warehouseIdFileNames = new HashMap<Long, String>();
                        
                        String p1;
                        if(isCod){
                            p1 = "cod";
                        }
                        else{
                            p1 = "prepaid";
                        }
                        String fName;
                        fName = courierDetailsPath + "/courier-details-" + p1 + "-" + 1 + "-"+ providerId + "-" + year + "-" + month + "-" + day + ".xls";
                        warehouseIdFileNames.put(1L, fName);
                        fName = courierDetailsPath + "/courier-details-" + p1 + "-" + 2 + "-"+ providerId + "-" + year + "-" + month + "-" + day + ".xls";
                        warehouseIdFileNames.put(2L, fName);
                        fName = courierDetailsPath + "/courier-details-" + p1 + "-" + 5 + "-"+ providerId + "-" + year + "-" + month + "-" + day + ".xls";
                        warehouseIdFileNames.put(5L, fName);
                        ByteArrayOutputStream binXLS = merger.mergeCourierDetailsReports(warehouseIdFileNames, 1, true);
                        binXLS.writeTo(f);
                        f.close();
                } catch (FileNotFoundException e) {
                        logger.error("Error while creating the Courier Details report", e);
                } catch (IOException e) {
                        logger.error("IO error while writing the Courier Details report", e);
                }
                
                
                response.setHeader("Content-disposition", "inline; filename=manifest-" + fileNameSuffix + ".pdf" );
                
                ServletOutputStream sos;
                try {
                        sos = response.getOutputStream();
                        baos.writeTo(sos);
                        sos.flush();
                } catch (IOException e) {
                    logger.error("Unable to stream the manifest file", e);
                }       
                return new DefaultHttpHeaders("lsuccess");
        }
        
        @Override
        public void setServletResponse(HttpServletResponse response) {
                this.response  = response;
        }

        @Override
        public void setServletRequest(HttpServletRequest request) {
                this.request = request;
        }
}