Subversion Repositories SmartDukaan

Rev

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