Subversion Repositories SmartDukaan

Rev

Rev 5768 | Rev 10689 | 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",
8628 amar.kumar 95
                    "3+ month",
96
                    "6+ month",
5768 mandeep.dh 97
                    "1+ week",
98
                    "1+ week cost",
99
                    "All",
100
                    "All cost" }, '\t'));
5711 mandeep.dh 101
 
102
            for (InventoryAge item : inventoryAge) {
103
                bufferedWriter.newLine();
104
 
105
                bufferedWriter.write(StringUtils.join(
106
                        new String[] {
107
                                String.valueOf(item.getItemId()),
5768 mandeep.dh 108
                                item.getCategory(),
5711 mandeep.dh 109
                                item.getBrand(),
110
                                item.getModelName(),
111
                                item.getModelNumber(),
112
                                item.getColor(),
113
                                String.valueOf(item.getFreshCount()),
114
                                String.valueOf(item.getOneToTwoCount()),
115
                                String.valueOf(item.getTwoToThreeCount()),
116
                                String.valueOf(item.getThreeToFourCount()),
5768 mandeep.dh 117
                                String.valueOf(item.getFourPlusCount()),
8628 amar.kumar 118
                                String.valueOf(item.getThreeMonthPlusCount()),
119
                                String.valueOf(item.getSixMonthPlusCount()),
5768 mandeep.dh 120
                                String.valueOf(item.getOnePlusCount()),
121
                                String.valueOf(item.getOnePlusCost()),
122
                                String.valueOf(item.getZeroPlusCount()),
123
                                String.valueOf(item.getZeroPlusCost())}, '\t'));
5711 mandeep.dh 124
            }
125
 
126
            bufferedWriter.close();
127
            return file;
128
        } catch (Exception e) {
129
            return null;
130
        }
131
    }
132
}