| Line 38... |
Line 38... |
| 38 |
import java.util.*;
|
38 |
import java.util.*;
|
| 39 |
import java.util.stream.Collectors;
|
39 |
import java.util.stream.Collectors;
|
| 40 |
import java.util.zip.ZipEntry;
|
40 |
import java.util.zip.ZipEntry;
|
| 41 |
import java.util.zip.ZipOutputStream;
|
41 |
import java.util.zip.ZipOutputStream;
|
| 42 |
|
42 |
|
| - |
|
43 |
import static org.springframework.util.FileCopyUtils.BUFFER_SIZE;
|
| - |
|
44 |
|
| 43 |
@Controller
|
45 |
@Controller
|
| 44 |
@Transactional(rollbackFor = Throwable.class)
|
46 |
@Transactional(rollbackFor = Throwable.class)
|
| 45 |
public class PurchaseController {
|
47 |
public class PurchaseController {
|
| 46 |
|
48 |
|
| 47 |
private static final Logger LOGGER = LogManager.getLogger(PurchaseController.class);
|
49 |
private static final Logger LOGGER = LogManager.getLogger(PurchaseController.class);
|
| Line 136... |
Line 138... |
| 136 |
|
138 |
|
| 137 |
for (String invoice : invoiceNumbers) {
|
139 |
for (String invoice : invoiceNumbers) {
|
| 138 |
List<Order> orders = invoiceOrdersMap.get(invoice);
|
140 |
List<Order> orders = invoiceOrdersMap.get(invoice);
|
| 139 |
String invoicePath = this.getInvoicePath(orders.get(0));
|
141 |
String invoicePath = this.getInvoicePath(orders.get(0));
|
| 140 |
HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
|
142 |
HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
|
| 141 |
ZipEntry zipEntry = new ZipEntry(invoice + ".pdf");
|
- |
|
| 142 |
zipOut.putNextEntry(zipEntry);
|
- |
|
| 143 |
IOUtils.copy(response.getEntity().getContent(), zipOut);
|
143 |
this.addFileToZip(zipOut, response.getEntity().getContent(), invoice + ".pdf");
|
| 144 |
response.getEntity().getContent().close();
|
- |
|
| 145 |
zipOut.closeEntry();
|
- |
|
| 146 |
}
|
144 |
}
|
| - |
|
145 |
zipOut.close();
|
| 147 |
byte[] byteArray = fos.toByteArray();
|
146 |
byte[] byteArray = fos.toByteArray();
|
| 148 |
headers.set("Content-Type", ContentType.APPLICATION_OCTET_STREAM.getMimeType());
|
147 |
headers.set("Content-Type", ContentType.APPLICATION_OCTET_STREAM.getMimeType());
|
| 149 |
headers.set("Content-disposition", "attachment; filename=invoices.zip");
|
148 |
headers.set("Content-disposition", "attachment; filename=invoices.zip");
|
| 150 |
headers.setContentLength(byteArray.length);
|
149 |
headers.setContentLength(byteArray.length);
|
| 151 |
zipOut.close();
|
- |
|
| 152 |
final InputStream inputStream = new ByteArrayInputStream(fos.toByteArray());
|
150 |
final InputStream inputStream = new ByteArrayInputStream(fos.toByteArray());
|
| 153 |
return new ResponseEntity<>(new InputStreamResource(inputStream), headers, HttpStatus.OK);
|
151 |
return new ResponseEntity<>(new InputStreamResource(inputStream), headers, HttpStatus.OK);
|
| 154 |
}
|
152 |
}
|
| 155 |
}
|
153 |
}
|
| 156 |
|
154 |
|
| Line 159... |
Line 157... |
| 159 |
throw new ProfitMandiBusinessException("Invalid Invoice", invoiceNumber, "Please check with your manager");
|
157 |
throw new ProfitMandiBusinessException("Invalid Invoice", invoiceNumber, "Please check with your manager");
|
| 160 |
}
|
158 |
}
|
| 161 |
return null;
|
159 |
return null;
|
| 162 |
}
|
160 |
}
|
| 163 |
|
161 |
|
| - |
|
162 |
private void addFileToZip(ZipOutputStream outZip, InputStream inputStream, String fileName) throws Exception {
|
| - |
|
163 |
byte data[] = new byte[BUFFER_SIZE];
|
| - |
|
164 |
ZipEntry entry = new ZipEntry(fileName);
|
| - |
|
165 |
outZip.putNextEntry(entry);
|
| - |
|
166 |
IOUtils.copy(inputStream, outZip);
|
| - |
|
167 |
outZip.closeEntry();
|
| - |
|
168 |
}
|
| - |
|
169 |
|
| 164 |
private String getInvoicePath(Order order) {
|
170 |
private String getInvoicePath(Order order) {
|
| 165 |
LocalDateTime billTime = order.getBillingTimestamp();
|
171 |
LocalDateTime billTime = order.getBillingTimestamp();
|
| 166 |
String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
|
172 |
String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
|
| 167 |
String filename = dirPath + File.separator + order.getInvoiceNumber() + ".pdf";
|
173 |
String filename = dirPath + File.separator + order.getInvoiceNumber() + ".pdf";
|
| 168 |
return filename;
|
174 |
return filename;
|