package org.openlca.io.simapro.csv.input;

import java.io.File;

import org.openlca.core.database.IDatabase;
import org.openlca.io.FileImport;
import org.openlca.simapro.csv.SimaProCSV;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.EventBus;

public class SimaProCsvImport implements FileImport {

	private Logger log = LoggerFactory.getLogger(getClass());
	private boolean canceled = false;
	private final IDatabase database;
	private final File file;
	private EventBus eventBus;

	public SimaProCsvImport(IDatabase database, File file) {
		this.database = database;
		this.file = file;
	}

	public IDatabase getDatabase() {
		return database;
	}

	public EventBus getEventBus() {
		return eventBus;
	}

	public boolean isCanceled() {
		return canceled;
	}

	@Override
	public void cancel() {
		this.canceled = true;
	}

	@Override
	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
	}

	@Override
	public void run() {
		log.trace("import SimaPro CSV file {}", file);
		try {
			log.trace("extract reference data");
			SpRefIndexHandler refDataHandler = new SpRefIndexHandler();
			SimaProCSV.parse(file, refDataHandler);
			SpRefDataIndex index = refDataHandler.getIndex();
			log.trace("sync. reference data");
			RefDataSync sync = new RefDataSync(index, database);
			RefData refData = sync.run();
			log.trace("import processes");
			ProcessHandler processHandler = new ProcessHandler(database,
					refData);
			SimaProCSV.parse(file, processHandler);
		} catch (Exception e) {
			log.error("SimaPro CSV import failed");
		}
	}

}

@@ -485,21 +485,24 @@
 nal File
+%5B%5D
  file
+s
 ;%0A%09priva
@@ -573,21 +573,24 @@
 se, File
+%5B%5D
  file
+s
 ) %7B%0A%09%09th
@@ -628,72 +628,17 @@
 file
+s
  = file
-;%0A%09%7D%0A%0A%09public IDatabase getDatabase() %7B%0A%09%09return database
+s
 ;%0A%09%7D
@@ -933,16 +933,108 @@
 run() %7B%0A
+%09%09if (files == null %7C%7C files.length == 0)%0A%09%09%09return;%0A%09%09try %7B%0A%09%09%09for (File file : files) %7B%0A%09%09
 %09%09log.tr
@@ -1075,23 +1075,16 @@
 file);%0A%09
-%09try %7B%0A
 %09%09%09log.t
@@ -1115,16 +1115,17 @@
 data%22);%0A
+%09
 %09%09%09SpRef
@@ -1178,24 +1178,25 @@
 ndler();%0A%09%09%09
+%09
 SimaProCSV.p
@@ -1223,16 +1223,17 @@
 ndler);%0A
+%09
 %09%09%09SpRef
@@ -1276,24 +1276,25 @@
 Index();%0A%09%09%09
+%09
 log.trace(%22s
@@ -1316,16 +1316,17 @@
 data%22);%0A
+%09
 %09%09%09RefDa
@@ -1376,16 +1376,17 @@
 se);%0A%09%09%09
+%09
 RefData 
@@ -1407,16 +1407,17 @@
 .run();%0A
+%09
 %09%09%09log.t
@@ -1442,16 +1442,17 @@
 sses%22);%0A
+%09
 %09%09%09Proce
@@ -1512,16 +1512,17 @@
 e,%0A%09%09%09%09%09
+%09
 refData)
@@ -1523,16 +1523,17 @@
 fData);%0A
+%09
 %09%09%09SimaP
@@ -1567,16 +1567,21 @@
 ndler);%0A
+%09%09%09%7D%0A
 %09%09%7D catc

support multiple files in SimaPro CSV import