Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
22124 ashik.ali 1
package com.spice.profitmandi.common.util;
2
 
30728 amit.gupta 3
import com.spice.profitmandi.common.enumuration.ContentType;
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23951 amit.gupta 5
import org.apache.commons.csv.CSVFormat;
29598 tejbeer 6
import org.apache.commons.csv.CSVParser;
23951 amit.gupta 7
import org.apache.commons.csv.CSVPrinter;
29598 tejbeer 8
import org.apache.commons.csv.CSVRecord;
23951 amit.gupta 9
import org.apache.commons.io.output.ByteArrayOutputStream;
10
import org.apache.logging.log4j.LogManager;
11
import org.apache.logging.log4j.Logger;
22124 ashik.ali 12
import org.apache.tika.Tika;
29598 tejbeer 13
import org.springframework.web.multipart.MultipartFile;
22124 ashik.ali 14
 
30728 amit.gupta 15
import java.io.*;
16
import java.util.List;
22124 ashik.ali 17
 
18
public class FileUtil {
23951 amit.gupta 19
 
20
	private static final Logger LOGGER = LogManager.getLogger(FileUtil.class);
21
 
22
	public static ContentType detectFileType(File file) throws ProfitMandiBusinessException {
22124 ashik.ali 23
		Tika tika = new Tika();
23951 amit.gupta 24
 
25
		// detecting the file type using detect method
26
		String filetype;
22124 ashik.ali 27
		try {
28
			filetype = tika.detect(file);
29
		} catch (IOException e) {
30
			e.printStackTrace();
23780 ashik.ali 31
			throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1001");
22124 ashik.ali 32
		}
23951 amit.gupta 33
		if (filetype.contains("pdf")) {
34
			return ContentType.PDF;
35
		} else if (filetype.contains("jpg") | filetype.contains("jpeg")) {
36
			return ContentType.JPEG;
37
		} else if (filetype.contains("png")) {
38
			return ContentType.PNG;
39
		} else {
40
			throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1002");
41
		}
22124 ashik.ali 42
	}
23951 amit.gupta 43
 
44
	public static void main(String[] args) {
45
		// detectFileType(new File("/hsps-docs/1499163441532"));
46
		// System.out.println("he");
22124 ashik.ali 47
	}
23951 amit.gupta 48
 
25840 amit.gupta 49
	public static ByteArrayOutputStream getCSVByteStream(List<String> headers, List<List<?>> rows) throws Exception {
23951 amit.gupta 50
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
51
				PrintStream writer = new PrintStream(baos);
52
				CSVPrinter csvPrinter = new CSVPrinter(writer,
53
						CSVFormat.EXCEL.withTrim().withHeader(headers.toArray(new String[] {})));) {
25840 amit.gupta 54
			for (List<?> row : rows) {
23951 amit.gupta 55
				csvPrinter.printRecord(row);
56
			}
57
			return baos;
58
		}
59
	}
29598 tejbeer 60
 
61
	public static List<CSVRecord> readFile(MultipartFile file) throws IOException, ProfitMandiBusinessException {
62
 
63
		CSVParser parser = new CSVParser(new InputStreamReader(file.getInputStream()), CSVFormat.DEFAULT);
64
		List<CSVRecord> records = parser.getRecords();
65
		LOGGER.info("records" + records);
66
		LOGGER.info("parser" + parser);
67
		if (records.size() < 2) {
68
			parser.close();
69
			throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
70
		}
71
		// Remove header
72
		records.remove(0);
73
		parser.close();
74
		return records;
75
 
76
	}
30728 amit.gupta 77
 
78
	public static List<CSVRecord> readFile(File file) throws IOException, ProfitMandiBusinessException {
79
		FileReader filereader = new FileReader(file);
80
		CSVParser parser = new CSVParser(filereader, CSVFormat.DEFAULT);
81
		List<CSVRecord> records = parser.getRecords();
82
		LOGGER.info("records" + records);
83
		LOGGER.info("parser" + parser);
84
		if (records.size() < 2) {
85
			parser.close();
86
			throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
87
		}
88
		// Remove header
89
		records.remove(0);
90
		parser.close();
91
		return records;
92
 
93
	}
22124 ashik.ali 94
}