Subversion Repositories SmartDukaan

Rev

Rev 30192 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.jcraft.jsch.*;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.dao.entity.transaction.Order;
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class PurchaseInvoiceService {

    @Autowired
    PurchaseRepository purchaseRepository;

    @Autowired
    OrderRepository orderRepository;

    @Autowired
    RestClient restClient;

    //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(String fofoId, String invoiceNumber, OutputStream outputStream)
            throws Exception {
        List<Order> orders = orderRepository.selectByInvoiceNumber(invoiceNumber);
        if (orders.size() == 0) {
            throw new ProfitMandiBusinessException("Invalid invoice Number", invoiceNumber, "Invalid invoice number");
        }
        LocalDateTime billingDate = orders.get(0).getBillingTimestamp();
        String invoicePath = String.format("%s%d-%d/%d/%s.pdf", invoicepath, billingDate.getYear(), billingDate.getMonth().getValue() - 1,
                fofoId, invoiceNumber);
        Session session = createSession("root", remoteHost, 22, passowrd);
        try {
            this.copyRemoteToLocal(session, invoicePath, outputStream);
        } catch (Exception e) {

        }
        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.l;
                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;
    }

}