View Javadoc
1   package net.sumaris.importation.service;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core Importation
6    * %%
7    * Copyright (C) 2018 - 2019 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  import net.sumaris.core.config.SumarisConfiguration;
26  import net.sumaris.core.dao.technical.schema.DatabaseTableEnum;
27  import net.sumaris.core.dao.technical.schema.SumarisDatabaseMetadata;
28  import net.sumaris.core.util.Files;
29  import net.sumaris.importation.dao.DataLoaderDao;
30  import net.sumaris.importation.exception.FileValidationException;
31  import net.sumaris.importation.util.csv.CSVFileReader;
32  import net.sumaris.importation.util.csv.FileReader;
33  import static net.sumaris.importation.service.vo.DataLoadError.*;
34  import net.sumaris.importation.service.vo.DataLoadError;
35  import org.apache.commons.lang3.ArrayUtils;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.springframework.beans.factory.annotation.Autowired;
39  import org.springframework.stereotype.Service;
40  
41  import java.io.File;
42  import java.io.IOException;
43  
44  @Service("dataLoaderService")
45  public class DataLoaderServiceImpl implements DataLoaderService {
46  
47  	protected static final Logger log = LoggerFactory.getLogger(DataLoaderServiceImpl.class);
48  
49  	@Autowired
50  	protected SumarisConfiguration config;
51  
52  	@Autowired
53  	protected SumarisDatabaseMetadata databaseMetadata;
54  
55  	@Autowired
56  	protected DataLoaderDao dao;
57  
58  	@Override
59  	public void remove(DatabaseTableEnum table, String[] filteredColumns, Object[] filteredValues) {
60  
61  		if (ArrayUtils.isNotEmpty(filteredColumns) || ArrayUtils.isNotEmpty(filteredValues)) {
62  			dao.removeData(table, filteredColumns, filteredValues);
63  		} else {
64  			dao.removeData(table, null, null);
65  		}
66  	}
67  
68  	@Override
69  	public void load(File inputFile, DatabaseTableEnum table, boolean validate) throws IOException,
70  			FileValidationException {
71  		DataLoadError[] errors;
72  		Files.checkExists(inputFile);
73  
74  		// Validate if need
75  		if (validate) {
76  			validate(inputFile, table);
77  		}
78  
79  		// Creating a new reader
80  		FileReader reader = new CSVFileReader(inputFile, true);
81  
82  		try {
83  			errors = dao.load(reader, table);
84  
85  			// Stop if result KO
86  			if (hasErrorOrFatal(errors)) throw new FileValidationException(errors);
87  		}
88  		finally {
89  			reader.close();
90  		}
91  	}
92  
93  	@Override
94  	public void validate(File inputFile, DatabaseTableEnum table) throws IOException, FileValidationException {
95  		Files.checkExists(inputFile);
96  
97  		FileReader reader = new CSVFileReader(inputFile, true);
98  
99  		try {
100 			DataLoadError[] errors = dao.validate(reader, table);
101 			// Stop if result KO
102 			if (hasErrorOrFatal(errors)) throw new FileValidationException(errors);
103 		}
104 		finally {
105 			reader.close();
106 		}
107 	}
108 
109 	/* -- protected methods -- */
110 
111 	protected boolean hasErrorOrFatal(DataLoadError[] validationDataLoadErrors){
112 		if (validationDataLoadErrors != null && validationDataLoadErrors.length > 0) {
113 			for (DataLoadError error : validationDataLoadErrors) {
114 				if (error.getErrorType() == ErrorType.ERROR
115 						|| error.getErrorType() == ErrorType.FATAL) {
116 					return true;
117 				}
118 			}
119 		}
120 		return false;
121 	}
122 }