|
|
|
|
@ -3,6 +3,7 @@ package net.pokeranalytics.android.util.csv |
|
|
|
|
import android.os.Handler |
|
|
|
|
import android.os.Looper |
|
|
|
|
import io.realm.Realm |
|
|
|
|
import net.pokeranalytics.android.model.realm.Import |
|
|
|
|
import org.apache.commons.csv.CSVFormat |
|
|
|
|
import org.apache.commons.csv.CSVParser |
|
|
|
|
import org.apache.commons.csv.CSVRecord |
|
|
|
|
@ -25,7 +26,7 @@ interface ImportDelegate { |
|
|
|
|
* When finding a descriptor, the CSVImporter then continue to parse the file and delegates the parsing of each row |
|
|
|
|
* to the CSVDescriptor |
|
|
|
|
*/ |
|
|
|
|
open class CSVImporter(istream: InputStream) { |
|
|
|
|
open class CSVImporter(istream: InputStream, filePath: String? = null) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The object being notified of the import progress |
|
|
|
|
@ -50,7 +51,7 @@ open class CSVImporter(istream: InputStream) { |
|
|
|
|
/** |
|
|
|
|
* The path of the CSV file |
|
|
|
|
*/ |
|
|
|
|
private var path: String? = null |
|
|
|
|
private var path: String? = filePath |
|
|
|
|
/** |
|
|
|
|
* The InputStream containing a file content |
|
|
|
|
*/ |
|
|
|
|
@ -107,13 +108,18 @@ open class CSVImporter(istream: InputStream) { |
|
|
|
|
|
|
|
|
|
realm.beginTransaction() |
|
|
|
|
|
|
|
|
|
val import = realm.copyToRealm(Import()) |
|
|
|
|
this.path?.let { |
|
|
|
|
import.fileName = it |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parser.forEachIndexed { index, record -> |
|
|
|
|
|
|
|
|
|
// Timber.d("line $index") |
|
|
|
|
this.notifyDelegate() |
|
|
|
|
|
|
|
|
|
if (this.currentDescriptor == null) { // find descriptor |
|
|
|
|
this.currentDescriptor = this.findDescriptor(record, realm) |
|
|
|
|
this.currentDescriptor = this.findDescriptor(realm, record, import) |
|
|
|
|
|
|
|
|
|
if (this.currentDescriptor == null) { |
|
|
|
|
|
|
|
|
|
@ -176,13 +182,12 @@ open class CSVImporter(istream: InputStream) { |
|
|
|
|
/** |
|
|
|
|
* Search for a descriptor in the list of managed formats |
|
|
|
|
*/ |
|
|
|
|
private fun findDescriptor(record: CSVRecord, realm: Realm): CSVDescriptor? { |
|
|
|
|
private fun findDescriptor(realm: Realm, record: CSVRecord, import: Import): CSVDescriptor? { |
|
|
|
|
|
|
|
|
|
val allCSVDescriptors = ProductCSVDescriptors.all |
|
|
|
|
Timber.d(allCSVDescriptors.toString()) |
|
|
|
|
allCSVDescriptors.forEach { descriptor -> |
|
|
|
|
Timber.d("descriptor = $descriptor, record = $record") |
|
|
|
|
if (descriptor.matches(record)) { |
|
|
|
|
descriptor.import = import |
|
|
|
|
descriptor.mapCustomField(record, realm) |
|
|
|
|
this.currentDescriptor = descriptor |
|
|
|
|
Timber.d("Identified source: ${descriptor.source}") |
|
|
|
|
|