Fixed skip error on import csv
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@90842 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
11af64e3ce
commit
37fbd70b60
|
@ -3,6 +3,7 @@
|
|||
*/
|
||||
package org.gcube.portlets.user.td.gwtservice.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
@ -14,6 +15,8 @@ import java.util.Map;
|
|||
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import net.sf.csv4j.ParseException;
|
||||
|
||||
import org.gcube.application.framework.core.session.ASLSession;
|
||||
import org.gcube.common.homelibrary.home.Home;
|
||||
import org.gcube.common.homelibrary.home.HomeLibrary;
|
||||
|
@ -1221,7 +1224,23 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements
|
|||
FileUploadSession fileUploadSession,
|
||||
CSVImportSession csvImportSession) throws TDGWTServiceException {
|
||||
logger.debug("File Storage Access");
|
||||
|
||||
logger.debug("CSVImportSession skip:"+csvImportSession.isSkipInvalidLines());
|
||||
|
||||
if(csvImportSession.isSkipInvalidLines()){
|
||||
try {
|
||||
fileUploadSession.setCsvFile(CSVFileUtil.skipError(fileUploadSession.getCsvFile(), fileUploadSession.getParserConfiguration()));
|
||||
} catch (ParseException e) {
|
||||
logger.debug("Error parsing file for skip:"+e.getMessage());
|
||||
throw new TDGWTServiceException("Error parsing file for skip: "+e.getMessage());
|
||||
|
||||
} catch (IOException e) {
|
||||
logger.debug("Error IOException skipping file error: "+e.getMessage());
|
||||
throw new TDGWTServiceException("Error IOException skipping file error: "+e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
FilesStorage filesStorage = new FilesStorage();
|
||||
|
||||
String fileUrlOnStorage = filesStorage.storageCSVTempFile(user,
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
*/
|
||||
package org.gcube.portlets.user.td.gwtservice.shared.csv;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
@ -21,6 +22,7 @@ import java.util.Collections;
|
|||
import java.util.List;
|
||||
|
||||
import net.sf.csv4j.CSVReader;
|
||||
import net.sf.csv4j.CSVWriter;
|
||||
import net.sf.csv4j.ParseException;
|
||||
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.file.HeaderPresence;
|
||||
|
@ -76,7 +78,45 @@ public class CSVFileUtil {
|
|||
CSVReader csvReader = new CSVReader(fileReader, parserConfiguration.getDelimiter(), parserConfiguration.getComment());
|
||||
return csvReader;
|
||||
}
|
||||
|
||||
|
||||
public static File skipError(File inCSVFile, CSVParserConfiguration config) throws ParseException, IOException
|
||||
{
|
||||
return skipError(inCSVFile, config.getCharset(), config.getDelimiter(), config.getComment());
|
||||
}
|
||||
|
||||
|
||||
public static File skipError(File inCSVFile, Charset charset, char delimiter, char comment) throws ParseException, IOException
|
||||
{
|
||||
File outCSVFile = File.createTempFile("import", "csv");
|
||||
|
||||
outCSVFile.deleteOnExit();
|
||||
|
||||
BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(inCSVFile), charset));
|
||||
BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outCSVFile), charset));
|
||||
CSVWriter csvWriter = new CSVWriter(fileWriter,delimiter,comment);
|
||||
CSVReader csvReader = new CSVReader(fileReader, delimiter, comment);
|
||||
|
||||
List<String> line=null;
|
||||
|
||||
do{
|
||||
try{
|
||||
line= csvReader.readLine();
|
||||
|
||||
}catch(ParseException exception)
|
||||
{
|
||||
logger.debug("Skip line:"+line.toString());
|
||||
continue;
|
||||
}
|
||||
csvWriter.writeLine(line);
|
||||
|
||||
} while (line!=null);
|
||||
|
||||
csvReader.close();
|
||||
csvWriter.close();
|
||||
return outCSVFile;
|
||||
}
|
||||
|
||||
public static ArrayList<CSVRowError> checkCSV(File csvFile, CSVParserConfiguration config, long errorsLimit) throws ParseException, IOException
|
||||
{
|
||||
return checkCSV(csvFile, config.getCharset(), config.getDelimiter(), config.getComment(), errorsLimit);
|
||||
|
@ -89,6 +129,7 @@ public class CSVFileUtil {
|
|||
|
||||
Reader fileReader = new InputStreamReader(new FileInputStream(csvFile), charset);
|
||||
CSVReader csvReader = new CSVReader(fileReader, delimiter, comment);
|
||||
|
||||
long count = -1;
|
||||
long fields = -1;
|
||||
|
||||
|
|
Loading…
Reference in New Issue