r37 - in trunk: res/values res/values-fr src/fr/ifremer/wlo/models/referentials/imports src/fr/ifremer/wlo/preferences
Author: kmorin Date: 2014-01-28 22:06:06 +0100 (Tue, 28 Jan 2014) New Revision: 37 Url: http://forge.codelutin.com/projects/wlo/repository/revisions/37 Log: fixes #4205 G?\195?\169rer les erreurs lors des imports de r?\195?\169f?\195?\169rentiels Modified: trunk/res/values-fr/strings.xml trunk/res/values/strings.xml trunk/src/fr/ifremer/wlo/models/referentials/imports/ImportUtil.java trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java Modified: trunk/res/values/strings.xml =================================================================== --- trunk/res/values/strings.xml 2014-01-28 18:42:42 UTC (rev 36) +++ trunk/res/values/strings.xml 2014-01-28 21:06:06 UTC (rev 37) @@ -101,6 +101,7 @@ <string name="preferences_importing_referential">Importing referential data</string> <string name="import_new_element_number">%s new items imported</string> + <string name="import_error">An error occurred during the import.\nCheck that the header names of the file you chose are correct.</string> <!-- Contexts --> <string name="add_context">Create a new context</string> Modified: trunk/res/values-fr/strings.xml =================================================================== --- trunk/res/values-fr/strings.xml 2014-01-28 18:42:42 UTC (rev 36) +++ trunk/res/values-fr/strings.xml 2014-01-28 21:06:06 UTC (rev 37) @@ -96,6 +96,7 @@ <string name="preferences_importing_referential">Import des données du référentiel en cours</string> <string name="import_new_element_number">%s nouveaux éléments importés</string> + <string name="import_error">Une erreur s\'est produite durant l\'import.\nVérifiez que les entêtes du fichier que vous avez choisi sont corrects.</string> <!-- Contexts --> <string name="add_context">Créer un nouveau contexte</string> Modified: trunk/src/fr/ifremer/wlo/models/referentials/imports/ImportUtil.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/ImportUtil.java 2014-01-28 18:42:42 UTC (rev 36) +++ trunk/src/fr/ifremer/wlo/models/referentials/imports/ImportUtil.java 2014-01-28 21:06:06 UTC (rev 37) @@ -24,19 +24,20 @@ * #L% */ +import android.app.AlertDialog; import android.content.Context; import android.util.Log; -import android.widget.Toast; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import fr.ifremer.wlo.R; +import fr.ifremer.wlo.models.BaseModel; import fr.ifremer.wlo.models.referentials.HasCode; import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.storage.WloSqlOpenHelper; -import fr.ifremer.wlo.models.BaseModel; +import org.apache.commons.collections.CollectionUtils; import org.nuiton.csv.Import; import org.nuiton.csv.ImportModel; @@ -44,7 +45,6 @@ import java.io.FileNotFoundException; import java.io.InputStream; import java.util.Collection; -import java.util.List; /** * @author kmorin <kmorin@codelutin.com> @@ -238,7 +238,23 @@ Import<M> importer = Import.newImport(importModel, inputStream); Collection<M> models = Lists.newArrayList(importer.iterator()); - if (!models.isEmpty()) { + try { + importer = Import.newImport(importModel, inputStream); + models = Lists.newArrayList(importer.iterator()); + + } catch (ClassCastException e) { + // cf issue #4205 + // nuiton-i18n provocks an error when trying to get the error message + Log.e(TAG, "error during import", e); + throw e; + + } finally { + if (importer != null) { + importer.close(); + } + } + + if (CollectionUtils.isNotEmpty(models)) { Predicate<M> filter; //if the models have a code attribute, check M first = models.iterator().next(); @@ -272,7 +288,6 @@ soh.close(); } - importer.close(); return models.size(); } } Modified: trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-01-28 18:42:42 UTC (rev 36) +++ trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-01-28 21:06:06 UTC (rev 37) @@ -250,80 +250,99 @@ Log.d(TAG, path); final int updatedNb; final int newNb; - switch (requestCode) { - case REQUEST_IMPORT_AGES: - newNb = ImportUtil.importAges(context, path); - updatedNb = DataCache.getAllAges(context).size(); - break; - case REQUEST_IMPORT_COMMERCIAL_SPECIES: - newNb = ImportUtil.importCommercialSpecies(context, path); - updatedNb = DataCache.getAllCommercialSpecies(context).size(); - break; - case REQUEST_IMPORT_GENDERS: - newNb = ImportUtil.importGenders(context, path); - updatedNb = DataCache.getAllGenders(context).size(); - break; - case REQUEST_IMPORT_LOCATIONS: - newNb = ImportUtil.importLocations(context, path); - updatedNb = DataCache.getAllLocations(context).size(); - break; - case REQUEST_IMPORT_MATURITIES: - newNb = ImportUtil.importMaturities(context, path); - updatedNb = DataCache.getAllMaturities(context).size(); - break; - case REQUEST_IMPORT_MENSURATIONS: - newNb = ImportUtil.importMensurations(context, path); - updatedNb = DataCache.getAllMensurations(context).size(); - break; - case REQUEST_IMPORT_METIERS: - newNb = ImportUtil.importMetiers(context, path); - updatedNb = DataCache.getAllMetiers(context).size(); - break; - case REQUEST_IMPORT_PRESENTATIONS: - newNb = ImportUtil.importPresentations(context, path); - updatedNb = DataCache.getAllPresentations(context).size(); - break; - case REQUEST_IMPORT_SCIENTIFIC_SPECIES: - newNb = ImportUtil.importScientificSpecies(context, path); - updatedNb = DataCache.getAllScientificSpecies(context).size(); - break; - case REQUEST_IMPORT_STATES: - newNb = ImportUtil.importStates(context, path); - updatedNb = DataCache.getAllStates(context).size(); - break; - case REQUEST_IMPORT_VESSELS: - newNb = ImportUtil.importVessels(context, path); - updatedNb = DataCache.getAllVessels(context).size(); - break; - default: - newNb = 0; - updatedNb = 0; - } - dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { - @Override - public void onDismiss(DialogInterface dialog) { - String key = requestCodeByPrefKey.inverse().get(requestCode); - Preference filePicker = findPreference(key); - filePicker.setSummary(getString(R.string.preferences_import_summary, updatedNb)); + try { + switch (requestCode) { + case REQUEST_IMPORT_AGES: + newNb = ImportUtil.importAges(context, path); + updatedNb = DataCache.getAllAges(context).size(); + break; + case REQUEST_IMPORT_COMMERCIAL_SPECIES: + newNb = ImportUtil.importCommercialSpecies(context, path); + updatedNb = DataCache.getAllCommercialSpecies(context).size(); + break; + case REQUEST_IMPORT_GENDERS: + newNb = ImportUtil.importGenders(context, path); + updatedNb = DataCache.getAllGenders(context).size(); + break; + case REQUEST_IMPORT_LOCATIONS: + newNb = ImportUtil.importLocations(context, path); + updatedNb = DataCache.getAllLocations(context).size(); + break; + case REQUEST_IMPORT_MATURITIES: + newNb = ImportUtil.importMaturities(context, path); + updatedNb = DataCache.getAllMaturities(context).size(); + break; + case REQUEST_IMPORT_MENSURATIONS: + newNb = ImportUtil.importMensurations(context, path); + updatedNb = DataCache.getAllMensurations(context).size(); + break; + case REQUEST_IMPORT_METIERS: + newNb = ImportUtil.importMetiers(context, path); + updatedNb = DataCache.getAllMetiers(context).size(); + break; + case REQUEST_IMPORT_PRESENTATIONS: + newNb = ImportUtil.importPresentations(context, path); + updatedNb = DataCache.getAllPresentations(context).size(); + break; + case REQUEST_IMPORT_SCIENTIFIC_SPECIES: + newNb = ImportUtil.importScientificSpecies(context, path); + updatedNb = DataCache.getAllScientificSpecies(context).size(); + break; + case REQUEST_IMPORT_STATES: + newNb = ImportUtil.importStates(context, path); + updatedNb = DataCache.getAllStates(context).size(); + break; + case REQUEST_IMPORT_VESSELS: + newNb = ImportUtil.importVessels(context, path); + updatedNb = DataCache.getAllVessels(context).size(); + break; + default: + newNb = 0; + updatedNb = 0; + } - Toast.makeText(context, context.getString(R.string.import_new_element_number, newNb), Toast.LENGTH_SHORT).show(); + if (backupFile) { + File importedFile = new File(path); + String copyFileName = String.format("%1$tY%1$tm%1$td-%1$tH%1$tM%1$tS-", new Date()) + importedFile.getName(); + File dir = Environment.getExternalStoragePublicDirectory(".wlo"); + File file = new File(dir, copyFileName); + try { + FileUtils.copyInputStreamToFile(new FileInputStream(path), file); + } catch (IOException e) { + Log.e(TAG, "Error while copying the file", e); + } } - }); - if (backupFile) { - File importedFile = new File(path); - String copyFileName = String.format("%1$tY%1$tm%1$td-%1$tH%1$tM%1$tS-", new Date()) + importedFile.getName(); - File dir = Environment.getExternalStoragePublicDirectory(".wlo"); - File file = new File(dir, copyFileName); - try { - FileUtils.copyInputStreamToFile(new FileInputStream(path), file); - } catch (IOException e) { - Log.e(TAG, "Error while copying the file", e); - } + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + dialog.dismiss(); + String key = requestCodeByPrefKey.inverse().get(requestCode); + Preference filePicker = findPreference(key); + filePicker.setSummary(getString(R.string.preferences_import_summary, updatedNb)); + + Toast.makeText(context, context.getString(R.string.import_new_element_number, newNb), Toast.LENGTH_SHORT).show(); + } + }); + + } catch (Exception e) { + // cf issue #4205 + // nuiton-i18n provocks an error when trying to get the error message + Log.e(TAG, "error during import", e); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + dialog.dismiss(); + new AlertDialog.Builder(context) + .setMessage(R.string.import_error) + .setNeutralButton(android.R.string.ok, UIUtils.getCancelClickListener()) + .create().show(); + } + }); + } - dialog.dismiss(); } }).start(); }
participants (1)
-
kmorin@users.forge.codelutin.com