Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.inventory.controllers;

import in.shop2020.model.v1.order.Order;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.thrift.clients.TransactionClient;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletOutputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PriceDropController extends BaseController{
        
        private TransactionClient tsc;
        private in.shop2020.model.v1.order.TransactionService.Client tClient;
        
        private InventoryClient ic;
        private in.shop2020.model.v1.inventory.InventoryService.Client iClient;
        
        private static Log logger = LogFactory.getLog(PurchaseReportController.class);
        private String closingDate;
        private int itemId;
    public int getItemId() {
                return itemId;
        }

        public void setItemId(int itemId) {
                this.itemId = itemId;
        }

        private final DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
    
    public String index() {
        return INDEX;
    }
    
    public String downloadImeis() throws Exception {
        long closingDateLong;
                try {
                        closingDateLong = sdf.parse(this.closingDate).getTime();
                } catch (Exception e) {
                        this.addActionError("Invalid date format");
                        return INDEX;
                }
                tsc = new TransactionClient();
                tClient = tsc.getClient();
                
                List<Order> inTransitOrders;

                inTransitOrders = tClient.getInTransitOrdersOnDateByItemId(closingDateLong, this.itemId);
                
                Set<Long> warehouseSet = new HashSet<Long>();
                for(Order o: inTransitOrders) {
                        warehouseSet.add(o.getFulfilmentWarehouseId());
                }
                
                ic = new InventoryClient();
                iClient = ic.getClient();
                
                Map<Long, String> warehouseNameMap = new HashMap<Long, String>(); 
                for (Long warehouseId : warehouseSet) {
                        warehouseNameMap.put(warehouseId, iClient.getWarehouseName(warehouseId));
                }

                byte[] buffer = null;
        File file = createFile(inTransitOrders, warehouseNameMap);
        buffer = new byte[(int) file.length()];
        InputStream input = null;
        try {
            int totalBytesRead = 0;
            input = new BufferedInputStream(new FileInputStream(file));
            while (totalBytesRead < buffer.length) {
                int bytesRemaining = buffer.length - totalBytesRead;
                // input.read() returns -1, 0, or more :
                int bytesRead = input.read(buffer, totalBytesRead,
                        bytesRemaining);
                if (bytesRead > 0) {
                    totalBytesRead = totalBytesRead + bytesRead;
                }
            }
            /*
             * the above style is a bit tricky: it places bytes into the
             * 'buffer' array; 'buffer' is an output parameter; the while
             * loop usually has a single iteration only.
             */
        } finally {
            input.close();
        }

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "inline; filename="
                + file.getName());

        ServletOutputStream sos = response.getOutputStream();
        sos.write(buffer);
        sos.flush();
                
        return null;
    }

        private File createFile(List<Order> inTransitOrders, Map<Long, String> warehouseNameMap) {
                try {
                String tmpDir = System.getProperty("java.io.tmpdir");
            File file = new File(tmpDir + "/IntransitIMEIS-" + this.closingDate + this.itemId
                    + ".xls");
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
                    file));
            bufferedWriter.write(StringUtils.join(new String[] { "Order Id",
                    "Vendor Name", "Brand", "ModelName", "ModelNumber", "Color", "UnitPrice", "IMEI"},
                    '\t'));

            for (Order order : inTransitOrders) {
                bufferedWriter.newLine();
                in.shop2020.model.v1.order.LineItem lineitem = order.getLineitems().get(0);
                for(String serialNumber : lineitem.getSerial_number().split(",")){
                        bufferedWriter.write(StringUtils.join(
                                        new String[] {
                                                        String.valueOf(order.getId()),
                                                        warehouseNameMap.get(order.getFulfilmentWarehouseId()),
                                                        lineitem.getBrand(),
                                                        lineitem.getModel_name(),
                                                        lineitem.getModel_number(),
                                                        lineitem.getColor(),
                                                        String.valueOf(lineitem.getUnit_price()),
                                                        serialNumber,
                                                        order.getStatusDescription() }, '\t'));
                }
            }

            bufferedWriter.close();
            return file;
        } catch (Exception e) {
            return null;
        }
        }

        public void setClosingDate(String closingDate) {
                this.closingDate = closingDate;
        }

        public String getClosingDate() {
                return closingDate;
        }
}