Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4687 mandeep.dh 1
/**
2
 * 
3
 */
4
 
5
package in.shop2020.inventory.controllers;
6
 
7
import in.shop2020.model.v1.catalog.InventoryServiceException;
8
import in.shop2020.model.v1.catalog.Vendor;
9
import in.shop2020.purchase.PurchaseOrder;
10
import in.shop2020.purchase.PurchaseService.Client;
11
import in.shop2020.purchase.PurchaseServiceException;
12
import in.shop2020.purchase.Supplier;
13
import in.shop2020.thrift.clients.CatalogClient;
14
import in.shop2020.thrift.clients.PurchaseClient;
15
 
16
import java.io.BufferedInputStream;
17
import java.io.File;
18
import java.io.FileInputStream;
19
import java.io.FileNotFoundException;
20
import java.io.FileOutputStream;
21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.util.Calendar;
24
import java.util.List;
25
 
26
import javax.servlet.ServletOutputStream;
27
import javax.servlet.http.HttpServletResponse;
28
 
29
import org.apache.struts2.convention.annotation.Result;
30
import org.apache.struts2.convention.annotation.Results;
31
import org.apache.thrift.TException;
32
import org.apache.thrift.transport.TTransportException;
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35
 
36
@SuppressWarnings("serial")
37
@Results({ @Result(name = "index", location = "raisePO-index.vm"),
38
        @Result(name = "result", location = "raisePO-result.vm") })
39
public class RaisePOController extends BaseController {
40
 
41
    private static Logger log = LoggerFactory
42
            .getLogger(RaisePOController.class);
43
 
44
    private File purchaseOrderDetails;
45
    private String vendorId;
46
    private String result;
47
    private List<Vendor> vendors;
48
 
49
    private HttpServletResponse response;
50
 
51
    public String index() {
52
        CatalogClient csc;
53
        try {
54
            csc = new CatalogClient();
55
            in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = csc
56
                    .getClient();
57
            setVendors(catalogClient.getAllVendors());
58
        } catch (TTransportException e) {
59
            e.printStackTrace();
60
            setResult(e.getMessage());
61
        } catch (TException e) {
62
            e.printStackTrace();
63
            setResult(e.getMessage());
64
        }
65
        return "index";
66
    }
67
 
68
    /*
69
     * MAIN METHOD TO TEST THE CODE
70
     * 
71
     * public static void main(String[] args) { RaisePOController rpc = new
72
     * RaisePOController(); rpc.setPurchaseOrderDetails(new
73
     * File("/home/anupam/Desktop/po.xls")); rpc.setVendorId("3"); rpc.create();
74
     * System.out.println(rpc.getResult()); }
75
     */
76
 
77
    public String create() {
78
        try {
79
            /*
80
             * Save the uploaded file to disk
81
             */
82
            FileInputStream is = new FileInputStream(purchaseOrderDetails);
83
            String filename = "/tmp/po-"
84
                    + Calendar.getInstance().getTime().toString() + ".xls";
85
            File f = new File(filename);
86
            FileOutputStream fos = null;
87
            fos = new FileOutputStream(f);
88
            byte[] buf = new byte[4096];
89
            int bytesRead;
90
 
91
            while ((bytesRead = is.read(buf)) != -1) {
92
                fos.write(buf, 0, bytesRead);
93
            }
94
            fos.close();
95
 
96
            int vendor = Integer.parseInt(vendorId);
97
            CatalogClient csc = new CatalogClient();
98
            in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = csc
99
                    .getClient();
100
            setVendors(catalogClient.getAllVendors());
101
            if (vendor < 1 || vendor > vendors.size()) {
102
                setResult("Select a valid Vendor");
103
                return "result";
104
            }
105
            /*
106
             * Try to create the purchase order for given vendor
107
             */
108
 
109
            Long purchaseOrderId = null;
110
            String output = catalogClient
111
                    .processPurchaseOrder(filename, vendor);
112
 
113
            /*
114
             * If PO is generated then create the PDF receipt for it
115
             */
116
            if (output.contains("POId:")) {
117
                String[] arr = output.split(":");
118
                purchaseOrderId = Long.parseLong(arr[1]);
119
                PurchaseClient warehouseServiceClient = new PurchaseClient();
120
                Client client = warehouseServiceClient.getClient();
121
                PurchaseOrder purchaseOrder = client
122
                        .getPurchaseOrder(purchaseOrderId);
123
                Supplier supplier = client.getSupplier(purchaseOrder
124
                        .getSupplierId());
125
                String pdfFilename = in.shop2020.inventory.service.PdfPoSheetGenerator
126
                        .generatePdfSheet(purchaseOrder, supplier);
127
 
128
                /*
129
                 * Put pdf file in a buffer to send it in response
130
                 */
131
                File file = new File(pdfFilename);
132
                byte[] buffer = new byte[(int) file.length()];
133
                try {
134
                    InputStream input = null;
135
                    try {
136
                        int totalBytesRead = 0;
137
                        input = new BufferedInputStream(new FileInputStream(
138
                                file));
139
                        while (totalBytesRead < buffer.length) {
140
                            int bytesRemaining = buffer.length - totalBytesRead;
141
                            // input.read() returns -1, 0, or more :
142
                            int bytesRead1 = input.read(buffer, totalBytesRead,
143
                                    bytesRemaining);
144
                            if (bytesRead1 > 0) {
145
                                totalBytesRead = totalBytesRead + bytesRead1;
146
                            }
147
                        }
148
                        /*
149
                         * the above style is a bit tricky: it places bytes into
150
                         * the 'buffer' array; 'buffer' is an output parameter;
151
                         * the while loop usually has a single iteration only.
152
                         */
153
                    } finally {
154
                        input.close();
155
                    }
156
                } catch (FileNotFoundException ex) {
157
                    log.info("FILE NOT FOUND : " + pdfFilename, ex);
158
                    setResult(ex.getMessage());
159
                } catch (IOException ex) {
160
                    log.info("FILE NOT FOUND : " + pdfFilename);
161
                    setResult(ex.getMessage());
162
                }
163
                response.setHeader("Content-disposition",
164
                        "attachment; filename=" + pdfFilename);
165
                /*
166
                 * Send the pdf file in response
167
                 */
168
                ServletOutputStream sos;
169
                sos = response.getOutputStream();
170
                sos.write(buffer);
171
                sos.flush();
172
            }
173
 
174
        } catch (TTransportException e) {
175
            e.printStackTrace();
176
            setResult(e.getMessage());
177
 
178
        } catch (NumberFormatException e) {
179
            e.printStackTrace();
180
            setResult(e.getMessage());
181
 
182
        } catch (TException e) {
183
            e.printStackTrace();
184
            setResult(e.getMessage());
185
 
186
        } catch (InventoryServiceException e) {
187
            setResult(e.getMessage());
188
 
189
        } catch (IOException e) {
190
            log.error("Error occured");
191
            setResult(e.getMessage());
192
        } catch (PurchaseServiceException e) {
193
            setResult(e.getMessage());
194
        }
195
        return "result";
196
    }
197
 
198
    public File getPurchaseOrderDetails() {
199
        return purchaseOrderDetails;
200
    }
201
 
202
    public void setPurchaseOrderDetails(File purchaseOrderDetails) {
203
        this.purchaseOrderDetails = purchaseOrderDetails;
204
    }
205
 
206
    public String getVendorId() {
207
        return vendorId;
208
    }
209
 
210
    public void setVendorId(String vendorId) {
211
        this.vendorId = vendorId;
212
    }
213
 
214
    public void setResult(String result) {
215
        this.result = result;
216
    }
217
 
218
    public String getResult() {
219
        return result;
220
    }
221
 
222
    public void setServletResponse(HttpServletResponse response) {
223
        this.response = response;
224
    }
225
 
226
    public List<Vendor> getVendors() {
227
        return vendors;
228
    }
229
 
230
    public void setVendors(List<Vendor> vendors) {
231
        this.vendors = vendors;
232
    }
233
 
234
}