Subversion Repositories SmartDukaan

Rev

Rev 25162 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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