View Javadoc
1   package net.sumaris.core.service.file;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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  	//@Autowired
47  	//protected FileImportDao fileImportDao;
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  			//fileImportDao.removeData(userId, table, new String[] { "COUNTRY_CODE_VESSEL" }, new String[] { country });
60  		} else {
61  			//fileImportDao.removeData(userId, table, null, null);
62  		}
63  
64  		ValidationErrorVO[] errors = new ValidationErrorVO[0];
65  		// FIXME: call dao
66  		// ValidationErrorVO[] errors = fileImportDao.importFile(userId, inputFile, table, validate);
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  		//FIXME
92  		//return fileImportDao.validateFile(userId, inputFile, table);
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 }