Subversion Repositories SmartDukaan

Rev

Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.Properties;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;

@Service
public class PurchaseInvoiceService {

        @Autowired
        PurchaseRepository purchaseRepository;

        @Autowired
        OrderRepository orderRepository;

        //private static final String remoteHost = "192.168.149.148";
        private static final String remoteHost = "support.shop2020.in";
        private static final String invoicepath = "/SaholicInvoices/";

        private static final String passowrd = "spic@2015support";

        public void getInvoices(int fofoId, LocalDateTime startDate, LocalDateTime endDate, OutputStream outputStream)
                        throws Exception {
                String invoicePath = String.format("%s%d-%d/%d", invoicepath, startDate.getYear(), startDate.getMonth().getValue() - 1,
                                fofoId);
                Session session = createSession("root", remoteHost, 22, passowrd);
                this.copyRemoteToLocal(session, invoicePath, outputStream);
                Document document = new Document();
                document.setMargins(0, 0, 25, 0);
                PdfWriter.getInstance(document, outputStream);
                document.open();
        }

        private Session createSession(String user, String host, int port, String password) {
                try {
                        JSch jsch = new JSch();

                        Properties config = new java.util.Properties();
                        config.put("StrictHostKeyChecking", "no");

                        Session session = jsch.getSession(user, host, port);
                        session.setPassword(passowrd);
                        session.setConfig(config);
                        session.connect();

                        return session;
                } catch (JSchException e) {
                        System.out.println(e);
                        return null;
                }
        }

        private void copyRemoteToLocal(Session session, String filePath, OutputStream outputStream)
                        throws JSchException, IOException, SftpException {
                ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
                // Once you've opened an sftp channel, there are methods to read a remote file
                // which let you access the file's content as an InputStream. You can convert
                // that to a Reader if you need to read line-by-line:
                Vector fileVector = sftp.ls(filePath);
                fileVector.stream().forEach(x -> System.out.println(((LsEntry) x).getFilename()));
                // InputStream stream = sftp.get("/some/file");
                /*
                 * try { BufferedReader br = new BufferedReader(new InputStreamReader(stream));
                 * // read from br } finally { stream.close(); }
                 */
        }

        public static int checkAck(InputStream in) throws IOException {
                int b = in.read();
                // b may be 0 for success,
                // 1 for error,
                // 2 for fatal error,
                // -1
                if (b == 0)
                        return b;
                if (b == -1)
                        return b;

                if (b == 1 || b == 2) {
                        StringBuffer sb = new StringBuffer();
                        int c;
                        do {
                                c = in.read();
                                sb.append((char) c);
                        } while (c != '\n');
                        if (b == 1) { // error
                                System.out.print(sb.toString());
                        }
                        if (b == 2) { // fatal error
                                System.out.print(sb.toString());
                        }
                }
                return b;
        }

}