Subversion Repositories SmartDukaan

Rev

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

package com.spice.profitmandi.common.util;

import com.spice.profitmandi.common.enumuration.ContentType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tika.Tika;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

public class FileUtil {

        private static final Logger LOGGER = LogManager.getLogger(FileUtil.class);

        public static ContentType detectFileType(File file) throws ProfitMandiBusinessException {
                Tika tika = new Tika();

                // detecting the file type using detect method
                String filetype;
                try {
                        filetype = tika.detect(file);
                } catch (IOException e) {
                        e.printStackTrace();
                        throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1001");
                }
                if (filetype.contains("pdf")) {
                        return ContentType.PDF;
                } else if (filetype.contains("jpg") | filetype.contains("jpeg")) {
                        return ContentType.JPEG;
                } else if (filetype.contains("png")) {
                        return ContentType.PNG;
                } else {
                        throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1002");
                }
        }

        public static void main(String[] args) {
                // detectFileType(new File("/hsps-docs/1499163441532"));
                // System.out.println("he");
        }

        public static ByteArrayOutputStream getCSVByteStream(List<String> headers, List<List<?>> rows) throws Exception {
                try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                PrintStream writer = new PrintStream(baos);
                                CSVPrinter csvPrinter = new CSVPrinter(writer,
                                                CSVFormat.EXCEL.withTrim().withHeader(headers.toArray(new String[] {})));) {
                        for (List<?> row : rows) {
                                csvPrinter.printRecord(row);
                        }
                        return baos;
                }
        }

        public static ByteArrayOutputStream getCSVByteStreamWithMultiHeaders(List<List<String>> headers, List<List<?>> rows) throws Exception {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try (PrintStream writer = new PrintStream(baos);
                         CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL.withTrim())) {

                        // Print all header rows manually
                        for (List<String> headerRow : headers) {
                                csvPrinter.printRecord(headerRow);
                        }

                        // Print all data rows
                        for (List<?> row : rows) {
                                csvPrinter.printRecord(row);
                        }
                }
                return baos;
        }


        public static List<CSVRecord> readFile(MultipartFile file) throws IOException, ProfitMandiBusinessException {

                CSVParser parser = new CSVParser(new InputStreamReader(file.getInputStream()), CSVFormat.DEFAULT);
                List<CSVRecord> records = parser.getRecords();
                LOGGER.info("records" + records);
                LOGGER.info("parser" + parser);
                if (records.size() < 2) {
                        parser.close();
                        throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
                }
                // Remove header
                records.remove(0);
                parser.close();
                return records;

        }

        public static List<CSVRecord> readFile(File file) throws IOException, ProfitMandiBusinessException {
                FileReader filereader = new FileReader(file);
                CSVParser parser = new CSVParser(filereader, CSVFormat.DEFAULT);
                List<CSVRecord> records = parser.getRecords();
                LOGGER.info("records" + records);
                LOGGER.info("parser" + parser);
                if (records.size() < 2) {
                        parser.close();
                        throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
                }
                // Remove header
                records.remove(0);
                parser.close();
                return records;

        }
}