Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
5711 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.inventory.controllers;
5
 
6
import in.shop2020.thrift.clients.WarehouseClient;
7
import in.shop2020.warehouse.InventoryAge;
8
import in.shop2020.warehouse.WarehouseService.Client;
9
 
10
import java.io.BufferedInputStream;
11
import java.io.BufferedWriter;
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileWriter;
15
import java.io.InputStream;
16
import java.util.Date;
17
import java.util.List;
18
 
19
import javax.servlet.ServletOutputStream;
20
 
21
import org.apache.commons.lang.StringUtils;
22
import org.apache.commons.logging.Log;
23
import org.apache.commons.logging.LogFactory;
24
 
25
/**
26
 * @author mandeep
27
 *
28
 */
29
public class InventoryAgeController extends BaseController {
30
    private static Log logger = LogFactory.getLog(InventoryAgeController.class);
31
 
32
    public String index() {
33
        try {
34
            Client client = new WarehouseClient().getClient();
35
            List<InventoryAge> inventoryAge = client.getInventoryAge();
36
            byte[] buffer = null;
37
            File file = createFile(inventoryAge);
38
            buffer = new byte[(int) file.length()];
39
            InputStream input = null;
40
            try {
41
                int totalBytesRead = 0;
42
                input = new BufferedInputStream(new FileInputStream(file));
43
                while (totalBytesRead < buffer.length) {
44
                    int bytesRemaining = buffer.length - totalBytesRead;
45
                    // input.read() returns -1, 0, or more :
46
                    int bytesRead = input.read(buffer, totalBytesRead,
47
                            bytesRemaining);
48
                    if (bytesRead > 0) {
49
                        totalBytesRead = totalBytesRead + bytesRead;
50
                    }
51
                }
52
                /*
53
                 * the above style is a bit tricky: it places bytes into the
54
                 * 'buffer' array; 'buffer' is an output parameter; the while
55
                 * loop usually has a single iteration only.
56
                 */
57
            } finally {
58
                input.close();
59
            }
60
 
61
            response.setContentType("application/vnd.ms-excel");
62
            response.setHeader("Content-disposition", "inline; filename="
63
                    + file.getName());
64
 
65
            ServletOutputStream sos = response.getOutputStream();
66
            sos.write(buffer);
67
            sos.flush();            
68
        }
69
        catch (Exception e) {
70
        }
71
 
72
        return null;
73
    }
74
 
75
    /**
76
     * @param inventoryAge
77
     * @return
78
     */
79
    private File createFile(List<InventoryAge> inventoryAge) {
80
        try {
81
            File file = new File("InventoryAge.xls");
82
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
83
            bufferedWriter.write(StringUtils.join(new String[] {
84
                    "Item Id",
85
                    "Brand",
86
                    "Model Name",
87
                    "Model Number",
88
                    "Color",
89
                    "Fresh",
90
                    "1-2 week",
91
                    "2-3 week",
92
                    "3-4 week",
93
                    "4+ week"}, '\t'));
94
 
95
            for (InventoryAge item : inventoryAge) {
96
                bufferedWriter.newLine();
97
 
98
                bufferedWriter.write(StringUtils.join(
99
                        new String[] {
100
                                String.valueOf(item.getItemId()),
101
                                item.getBrand(),
102
                                item.getModelName(),
103
                                item.getModelNumber(),
104
                                item.getColor(),
105
                                String.valueOf(item.getFreshCount()),
106
                                String.valueOf(item.getOneToTwoCount()),
107
                                String.valueOf(item.getTwoToThreeCount()),
108
                                String.valueOf(item.getThreeToFourCount()),
109
                                String.valueOf(item.getFourPlusCount())}, '\t'));
110
            }
111
 
112
            bufferedWriter.close();
113
            return file;
114
        } catch (Exception e) {
115
            return null;
116
        }
117
    }
118
}