Subversion Repositories SmartDukaan

Rev

Rev 5711 | Rev 8628 | Go to most recent revision | Details | Compare with Previous | 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",
5768 mandeep.dh 85
                    "Category",
5711 mandeep.dh 86
                    "Brand",
87
                    "Model Name",
88
                    "Model Number",
89
                    "Color",
90
                    "Fresh",
91
                    "1-2 week",
92
                    "2-3 week",
93
                    "3-4 week",
5768 mandeep.dh 94
                    "4+ week",
95
                    "1+ week",
96
                    "1+ week cost",
97
                    "All",
98
                    "All cost" }, '\t'));
5711 mandeep.dh 99
 
100
            for (InventoryAge item : inventoryAge) {
101
                bufferedWriter.newLine();
102
 
103
                bufferedWriter.write(StringUtils.join(
104
                        new String[] {
105
                                String.valueOf(item.getItemId()),
5768 mandeep.dh 106
                                item.getCategory(),
5711 mandeep.dh 107
                                item.getBrand(),
108
                                item.getModelName(),
109
                                item.getModelNumber(),
110
                                item.getColor(),
111
                                String.valueOf(item.getFreshCount()),
112
                                String.valueOf(item.getOneToTwoCount()),
113
                                String.valueOf(item.getTwoToThreeCount()),
114
                                String.valueOf(item.getThreeToFourCount()),
5768 mandeep.dh 115
                                String.valueOf(item.getFourPlusCount()),
116
                                String.valueOf(item.getOnePlusCount()),
117
                                String.valueOf(item.getOnePlusCost()),
118
                                String.valueOf(item.getZeroPlusCount()),
119
                                String.valueOf(item.getZeroPlusCost())}, '\t'));
5711 mandeep.dh 120
            }
121
 
122
            bufferedWriter.close();
123
            return file;
124
        } catch (Exception e) {
125
            return null;
126
        }
127
    }
128
}