1 package net.sumaris.core.service.file;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.google.common.base.Preconditions;
27 import net.sumaris.core.dao.technical.schema.DatabaseTableEnum;
28 import net.sumaris.core.dao.technical.schema.SumarisHibernateColumnMetadata;
29 import net.sumaris.core.dao.technical.schema.SumarisTableMetadata;
30 import net.sumaris.core.vo.ErrorType;
31 import net.sumaris.core.vo.file.ValidationErrorVO;
32 import org.apache.commons.lang3.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Service;
36
37 import java.io.File;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40
41 @Service("fileImportService")
42 public class FileImportServiceImpl implements FileImportService {
43
44 private static final Logger log = LoggerFactory.getLogger(FileImportServiceImpl.class);
45
46
47
48
49 @Override
50 public void importFile(int userId, File inputFile, DatabaseTableEnum table, String country, boolean validate) throws IOException,
51 FileValidationException {
52 Preconditions.checkNotNull(inputFile);
53
54 if (inputFile.exists() == false) {
55 throw new FileNotFoundException("File not exists: " + inputFile.getAbsolutePath());
56 }
57
58 if (StringUtils.isNotBlank(country)) {
59
60 } else {
61
62 }
63
64 ValidationErrorVO[] errors = new ValidationErrorVO[0];
65
66
67
68 if (errors != null && errors.length > 0) {
69 boolean hasErrorOrFatal = false;
70 for (ValidationErrorVO error : errors) {
71 if (error.getErrorType() == ErrorType.ERROR
72 || error.getErrorType() == ErrorType.FATAL) {
73 hasErrorOrFatal = true;
74 break;
75 }
76 }
77 if (hasErrorOrFatal) {
78 throw new FileValidationException(errors);
79 }
80 }
81 }
82
83 @Override
84 public ValidationErrorVO[] validateFile(int userId, File inputFile, DatabaseTableEnum table) throws IOException {
85
86 if (inputFile.exists() == false) {
87 throw new FileNotFoundException("File not exists: " + inputFile.getAbsolutePath());
88 }
89
90 return null;
91
92
93 }
94
95 protected String getLogPrefix(SumarisTableMetadata table, SumarisHibernateColumnMetadata colMeta, int lineNumber) {
96 StringBuilder sb = new StringBuilder();
97 sb.append("[").append(table.getName());
98 if (colMeta != null) {
99 sb.append(".").append(colMeta.getName());
100 }
101 if (lineNumber != -1) {
102 sb.append(" / ").append(lineNumber);
103 }
104 sb.append("] ");
105 return sb.toString();
106
107 }
108 }