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