Subversion Repositories SmartDukaan

Rev

Rev 23081 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23069 amit.gupta 1
package in.shop2020.inventory.controllers;
2
 
3
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.thrift.clients.InventoryClient;
5
import in.shop2020.thrift.clients.TransactionClient;
6
 
7
import java.io.BufferedInputStream;
8
import java.io.BufferedWriter;
9
import java.io.File;
10
import java.io.FileInputStream;
11
import java.io.FileWriter;
12
import java.io.InputStream;
13
import java.text.DateFormat;
14
import java.text.SimpleDateFormat;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.Set;
20
 
21
import javax.servlet.ServletOutputStream;
22
 
23
import org.apache.commons.lang.StringUtils;
24
import org.apache.commons.logging.Log;
25
import org.apache.commons.logging.LogFactory;
26
 
27
public class PriceDropController extends BaseController{
28
 
29
	private TransactionClient tsc;
30
	private in.shop2020.model.v1.order.TransactionService.Client tClient;
31
 
32
	private InventoryClient ic;
33
	private in.shop2020.model.v1.inventory.InventoryService.Client iClient;
34
 
35
	private static Log logger = LogFactory.getLog(PurchaseReportController.class);
36
	private String closingDate;
37
	private int itemId;
38
    public int getItemId() {
39
		return itemId;
40
	}
41
 
42
	public void setItemId(int itemId) {
43
		this.itemId = itemId;
44
	}
45
 
46
	private final DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
47
 
48
 
49
    public String index() {
50
    	return INDEX;
51
    }
52
 
53
    public String downloadImeis() throws Exception {
54
    	long closingDateLong;
55
		try {
56
			closingDateLong = sdf.parse(this.closingDate).getTime();
57
		} catch (Exception e) {
58
			this.addActionError("Invalid date format");
59
			return INDEX;
60
		}
61
		tsc = new TransactionClient();
62
		tClient = tsc.getClient();
63
 
64
		List<Order> inTransitOrders;
65
 
66
		inTransitOrders = tClient.getInTransitOrdersOnDateByItemId(closingDateLong, this.itemId);
67
 
68
		Set<Long> warehouseSet = new HashSet<Long>();
69
		for(Order o: inTransitOrders) {
70
			warehouseSet.add(o.getFulfilmentWarehouseId());
71
		}
72
 
73
		ic = new InventoryClient();
74
		iClient = ic.getClient();
75
 
76
		Map<Long, String> warehouseNameMap = new HashMap<Long, String>(); 
77
		for (Long warehouseId : warehouseSet) {
78
			warehouseNameMap.put(warehouseId, iClient.getWarehouseName(warehouseId));
79
		}
80
 
81
		byte[] buffer = null;
82
        File file = createFile(inTransitOrders, warehouseNameMap);
83
        buffer = new byte[(int) file.length()];
84
        InputStream input = null;
85
        try {
86
            int totalBytesRead = 0;
87
            input = new BufferedInputStream(new FileInputStream(file));
88
            while (totalBytesRead < buffer.length) {
89
                int bytesRemaining = buffer.length - totalBytesRead;
90
                // input.read() returns -1, 0, or more :
91
                int bytesRead = input.read(buffer, totalBytesRead,
92
                        bytesRemaining);
93
                if (bytesRead > 0) {
94
                    totalBytesRead = totalBytesRead + bytesRead;
95
                }
96
            }
97
            /*
98
             * the above style is a bit tricky: it places bytes into the
99
             * 'buffer' array; 'buffer' is an output parameter; the while
100
             * loop usually has a single iteration only.
101
             */
102
        } finally {
103
            input.close();
104
        }
105
 
106
        response.setContentType("application/vnd.ms-excel");
107
        response.setHeader("Content-disposition", "inline; filename="
108
                + file.getName());
109
 
110
        ServletOutputStream sos = response.getOutputStream();
111
        sos.write(buffer);
112
        sos.flush();
113
 
114
    	return null;
115
    }
116
 
117
	private File createFile(List<Order> inTransitOrders, Map<Long, String> warehouseNameMap) {
118
		try {
119
        	String tmpDir = System.getProperty("java.io.tmpdir");
120
            File file = new File(tmpDir + "/IntransitIMEIS-" + this.closingDate + this.itemId
121
                    + ".xls");
122
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
123
                    file));
124
            bufferedWriter.write(StringUtils.join(new String[] { "Order Id",
125
                    "Vendor Name", "Brand", "ModelName", "ModelNumber", "Color", "UnitPrice", "IMEI"},
126
                    '\t'));
127
 
128
            for (Order order : inTransitOrders) {
129
                bufferedWriter.newLine();
130
                in.shop2020.model.v1.order.LineItem lineitem = order.getLineitems().get(0);
131
                for(String serialNumber : lineitem.getSerial_number().split(",")){
132
                	bufferedWriter.write(StringUtils.join(
133
                			new String[] {
134
                					String.valueOf(order.getId()),
135
                					warehouseNameMap.get(order.getFulfilmentWarehouseId()),
136
                					lineitem.getBrand(),
137
                					lineitem.getModel_name(),
138
                					lineitem.getModel_number(),
139
                					lineitem.getColor(),
140
                					String.valueOf(lineitem.getUnit_price()),
141
                					serialNumber,
142
                					order.getStatusDescription() }, '\t'));
143
                }
144
            }
145
 
146
            bufferedWriter.close();
147
            return file;
148
        } catch (Exception e) {
149
            return null;
150
        }
151
	}
152
 
153
	public void setClosingDate(String closingDate) {
154
		this.closingDate = closingDate;
155
	}
156
 
157
	public String getClosingDate() {
158
		return closingDate;
159
	}
160
}