69 lines
2.1 KiB
Java
69 lines
2.1 KiB
Java
package org.gcube.application.perform.service.engine.impl;
|
|
|
|
import java.io.FileReader;
|
|
import java.io.Reader;
|
|
import java.sql.PreparedStatement;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map.Entry;
|
|
|
|
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVParser;
|
|
import org.apache.commons.csv.CSVRecord;
|
|
import org.gcube.application.perform.service.engine.model.DBQueryDescriptor;
|
|
import org.gcube.application.perform.service.engine.model.importer.ImportRoutineDescriptor;
|
|
import org.gcube.application.perform.service.engine.model.importer.ImportedTable;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class ImportedTableManager {
|
|
|
|
private static final Logger log= LoggerFactory.getLogger(ImportedTableManager.class);
|
|
|
|
private static List<ImportedTable> tables;
|
|
|
|
|
|
public void loadImportedData(ImportRoutineDescriptor desc) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private static final long parse(String path, String description, ImportRoutineDescriptor routine) {
|
|
Reader in = new FileReader(path);
|
|
CSVParser parser= CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in);
|
|
try {
|
|
log.debug("Parsing file {} : {} ",description,path);
|
|
// Extract CSV Schema
|
|
ArrayList<String> csvSchema=new ArrayList<String>();
|
|
for(Entry<String,Integer> entry : parser.getHeaderMap().entrySet()) {
|
|
csvSchema.add(entry.getValue(), entry.getKey());
|
|
}
|
|
|
|
log.debug("CSV Schema is {} ",csvSchema);
|
|
|
|
long counter=0l;
|
|
//Get the right table
|
|
for(ImportedTable table:tables) {
|
|
if(table.matchesSchema(csvSchema)) {
|
|
log.debug("Mathing table is {} ",table.getTableName());
|
|
Query query=table.getQuery();
|
|
PreparedStatement psInsert=query.prepare(conn);
|
|
log.debug("Reading csvLines");
|
|
for(CSVRecord record:parser) {
|
|
DBQueryDescriptor desc=table.getSetRow(record.toMap(), routine.getId(), routine.getFarmId());
|
|
query.fill(psInsert, desc);
|
|
counter+=psInsert.executeUpdate();
|
|
}
|
|
log.debug("Inserted {} lines into {} for routine {} [FARM ID {}]",counter,table.getTableName(),routine.getId(),routine.getFarmId());
|
|
}
|
|
}
|
|
}finally {
|
|
parser.close();
|
|
in.close();
|
|
}
|
|
|
|
}
|
|
}
|