| 25162 |
amit.gupta |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
|
|
3 |
import java.io.IOException;
|
|
|
4 |
import java.io.InputStream;
|
|
|
5 |
import java.io.OutputStream;
|
|
|
6 |
import java.time.LocalDateTime;
|
|
|
7 |
import java.util.Properties;
|
|
|
8 |
import java.util.Vector;
|
|
|
9 |
|
|
|
10 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
11 |
import org.springframework.stereotype.Service;
|
|
|
12 |
|
|
|
13 |
import com.itextpdf.text.Document;
|
|
|
14 |
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
15 |
import com.jcraft.jsch.ChannelSftp;
|
|
|
16 |
import com.jcraft.jsch.ChannelSftp.LsEntry;
|
|
|
17 |
import com.jcraft.jsch.JSch;
|
|
|
18 |
import com.jcraft.jsch.JSchException;
|
|
|
19 |
import com.jcraft.jsch.Session;
|
|
|
20 |
import com.jcraft.jsch.SftpException;
|
|
|
21 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
|
|
22 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
23 |
|
|
|
24 |
@Service
|
|
|
25 |
public class PurchaseInvoiceService {
|
|
|
26 |
|
|
|
27 |
@Autowired
|
|
|
28 |
PurchaseRepository purchaseRepository;
|
|
|
29 |
|
|
|
30 |
@Autowired
|
|
|
31 |
OrderRepository orderRepository;
|
|
|
32 |
|
|
|
33 |
//private static final String remoteHost = "192.168.149.148";
|
|
|
34 |
private static final String remoteHost = "support.shop2020.in";
|
|
|
35 |
private static final String invoicepath = "/SaholicInvoices/";
|
|
|
36 |
|
|
|
37 |
private static final String passowrd = "spic@2015support";
|
|
|
38 |
|
|
|
39 |
public void getInvoices(int fofoId, LocalDateTime startDate, LocalDateTime endDate, OutputStream outputStream)
|
|
|
40 |
throws Exception {
|
|
|
41 |
String invoicePath = String.format("%s%d-%d/%d", invoicepath, startDate.getYear(), startDate.getMonth().getValue() - 1,
|
|
|
42 |
fofoId);
|
|
|
43 |
Session session = createSession("root", remoteHost, 22, passowrd);
|
|
|
44 |
this.copyRemoteToLocal(session, invoicePath, outputStream);
|
|
|
45 |
Document document = new Document();
|
|
|
46 |
document.setMargins(0, 0, 25, 0);
|
|
|
47 |
PdfWriter.getInstance(document, outputStream);
|
|
|
48 |
document.open();
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
private Session createSession(String user, String host, int port, String password) {
|
|
|
52 |
try {
|
|
|
53 |
JSch jsch = new JSch();
|
|
|
54 |
|
|
|
55 |
Properties config = new java.util.Properties();
|
|
|
56 |
config.put("StrictHostKeyChecking", "no");
|
|
|
57 |
|
|
|
58 |
Session session = jsch.getSession(user, host, port);
|
|
|
59 |
session.setPassword(passowrd);
|
|
|
60 |
session.setConfig(config);
|
|
|
61 |
session.connect();
|
|
|
62 |
|
|
|
63 |
return session;
|
|
|
64 |
} catch (JSchException e) {
|
|
|
65 |
System.out.println(e);
|
|
|
66 |
return null;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
private void copyRemoteToLocal(Session session, String filePath, OutputStream outputStream)
|
|
|
71 |
throws JSchException, IOException, SftpException {
|
|
|
72 |
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
|
|
|
73 |
// Once you've opened an sftp channel, there are methods to read a remote file
|
|
|
74 |
// which let you access the file's content as an InputStream. You can convert
|
|
|
75 |
// that to a Reader if you need to read line-by-line:
|
|
|
76 |
Vector fileVector = sftp.ls(filePath);
|
|
|
77 |
fileVector.stream().forEach(x -> System.out.println(((LsEntry) x).getFilename()));
|
|
|
78 |
// InputStream stream = sftp.get("/some/file");
|
|
|
79 |
/*
|
|
|
80 |
* try { BufferedReader br = new BufferedReader(new InputStreamReader(stream));
|
|
|
81 |
* // read from br } finally { stream.close(); }
|
|
|
82 |
*/
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public static int checkAck(InputStream in) throws IOException {
|
|
|
86 |
int b = in.read();
|
|
|
87 |
// b may be 0 for success,
|
|
|
88 |
// 1 for error,
|
|
|
89 |
// 2 for fatal error,
|
|
|
90 |
// -1
|
|
|
91 |
if (b == 0)
|
|
|
92 |
return b;
|
|
|
93 |
if (b == -1)
|
|
|
94 |
return b;
|
|
|
95 |
|
|
|
96 |
if (b == 1 || b == 2) {
|
|
|
97 |
StringBuffer sb = new StringBuffer();
|
|
|
98 |
int c;
|
|
|
99 |
do {
|
|
|
100 |
c = in.read();
|
|
|
101 |
sb.append((char) c);
|
|
|
102 |
} while (c != '\n');
|
|
|
103 |
if (b == 1) { // error
|
|
|
104 |
System.out.print(sb.toString());
|
|
|
105 |
}
|
|
|
106 |
if (b == 2) { // fatal error
|
|
|
107 |
System.out.print(sb.toString());
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
return b;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
}
|