Author: kmorin Date: 2014-03-17 17:56:27 +0100 (Mon, 17 Mar 2014) New Revision: 76 Url: http://forge.codelutin.com/projects/wlo/repository/revisions/76 Log: fixes #4230 Mettre ?\195?\160 jour les imports existants si l'id est le m?\195?\170me fixes #4738 Exporter les r?\195?\169f?\195?\169rentiels Added: trunk/src/fr/ifremer/wlo/imports/WloAbstractImportExportModel.java trunk/src/fr/ifremer/wlo/utils/ImportExportUtil.java Removed: trunk/src/fr/ifremer/wlo/utils/ImportUtil.java Modified: trunk/AndroidManifest.xml trunk/res/values-fr/strings.xml trunk/res/values/strings.xml trunk/res/xml/preferences.xml trunk/src/fr/ifremer/wlo/MainActivity.java trunk/src/fr/ifremer/wlo/WloApplication.java trunk/src/fr/ifremer/wlo/imports/CalcifiedPartTakingRowModel.java trunk/src/fr/ifremer/wlo/imports/CommercialSpeciesRowModel.java trunk/src/fr/ifremer/wlo/imports/LocationRowModel.java trunk/src/fr/ifremer/wlo/imports/MensurationRowModel.java trunk/src/fr/ifremer/wlo/imports/MetierRowModel.java trunk/src/fr/ifremer/wlo/imports/PresentationRowModel.java trunk/src/fr/ifremer/wlo/imports/ScientificSpeciesRowModel.java trunk/src/fr/ifremer/wlo/imports/StateRowModel.java trunk/src/fr/ifremer/wlo/imports/VesselRowModel.java trunk/src/fr/ifremer/wlo/models/BaseModel.java trunk/src/fr/ifremer/wlo/models/referentials/CalcifiedPartTaking.java trunk/src/fr/ifremer/wlo/preferences/CategoryCreationAcivity.java trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java trunk/src/fr/ifremer/wlo/storage/WloSqlOpenHelper.java Modified: trunk/AndroidManifest.xml =================================================================== --- trunk/AndroidManifest.xml 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/AndroidManifest.xml 2014-03-17 16:56:27 UTC (rev 76) @@ -124,7 +124,7 @@ android:configChanges="orientation|screenSize|keyboardHidden"/> <activity android:name=".preferences.MultiSelectionActivity" - android:label="@string/favaorite_selection_title" + android:label="@string/favorite_selection_title" android:configChanges="orientation|screenSize|keyboardHidden"/> <activity android:name=".utils.filechooser.FileDialog" Modified: trunk/res/values/strings.xml =================================================================== --- trunk/res/values/strings.xml 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/res/values/strings.xml 2014-03-17 16:56:27 UTC (rev 76) @@ -26,6 +26,7 @@ <string name="delete">Delete</string> <string name="edit">Edit</string> <string name="form_comment">Comment</string> + <string name="error">Error</string> <!-- BigFin communication service --> <string name="bigfin_no_ichtyometer_connected_title">No ichtyometer connected</string> @@ -91,6 +92,7 @@ <string name="preferences_ichtyometer">Ichtyometer</string> <string name="preferences_referentials">Referentials</string> <string name="preferences_imports">Imports</string> + <string name="preferences_exports">Exports</string> <string name="preferences_imports">Favorites</string> <string name="preferences_import_summary">%s items currently</string> @@ -108,20 +110,51 @@ <string name="preferences_import_categories">Import categorizations</string> <string name="preferences_import_no_external_storage_title">External storage unavailable</string> - <string name="preferences_import_no_external_storage_message">No externat storage is available.\nWould you like to import the data without saving the source file?</string> + <string name="preferences_import_no_external_storage_message">No external storage is available.\nWould you like to import the data without saving the source file?</string> <string name="preferences_importing_referential">Importing referential data</string> + <string name="export_element_number">%s new items imported</string> + <string name="export_error">An error occurred during the import.\nCheck that the header names of the file you chose are correct.</string> + + <string name="preferences_export_summary">%s items</string> + <string name="preferences_export_commercial_species">Export commercial species</string> + <string name="preferences_export_locations">Export locations</string> + <string name="preferences_export_mensurations">Export mensurations</string> + <string name="preferences_export_metiers">Export metiers</string> + <string name="preferences_export_presentations">Export presentations</string> + <string name="preferences_export_scientific_species">Export scientific species</string> + <string name="preferences_export_states">Export states</string> + <string name="preferences_export_vessels">Export vessels</string> + <string name="preferences_export_categories">Export categorizations</string> + <string name="preferences_exporting_referential">Exporting referential data</string> + + <string name="preferences_export_commercial_species_default_name">commercial_species</string> + <string name="preferences_export_locations_default_name">locations</string> + <string name="preferences_export_mensurations_default_name">mensurations</string> + <string name="preferences_export_metiers_default_name">metiers</string> + <string name="preferences_export_presentations_default_name">presentations</string> + <string name="preferences_export_scientific_species_default_name">scientific_species</string> + <string name="preferences_export_states_default_name">states</string> + <string name="preferences_export_vessels_default_name">vessels</string> + + <string name="export_exported_element_number">%s items exported</string> + <string name="export_error">An error occurred during the export.</string> + <string name="preferences_favorites_commercial_species">Commercial species</string> <string name="preferences_favorites_scientific_species">Scientific species</string> - <string name="favaorite_selection_title">Favorites</string> + <string name="favorite_selection_title">Favorites</string> <string name="select_new_favorite">Select a new favorite</string> <string name="favorite_nb">%s favorites</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> - <string name="preferences_calcified_parts_takings">Calcified parts taking</string> <string name="preferences_import_calcified_parts_takings">Import algorithm settings</string> + <string name="preferences_export_calcified_parts_takings">Export algorithm settings</string> + <string name="preferences_export_calcified_parts_takings_default_name">calcified_takings</string> + <string name="preferences_import_calcified_parts_takings_null_species">A species code is null or is not in the species referential.</string> + <string name="preferences_import_calcified_parts_takings_overlapping">The ranges must not overlap.</string> + <string name="preferences_import_calcified_parts_takings_value_out_of_ranges">The ranges must cover every value.</string> + <string name="preferences_import_calcified_parts_different_size_steps">The size steps must be equals for a single species.</string> + <string name="preferences_import_calcified_parts_no_size_step">At least one row must have a size step for each species.</string> <string name="category_creation_title">New category</string> <string name="category_label">Label</string> Modified: trunk/res/values-fr/strings.xml =================================================================== --- trunk/res/values-fr/strings.xml 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/res/values-fr/strings.xml 2014-03-17 16:56:27 UTC (rev 76) @@ -24,6 +24,7 @@ <string name="delete">Supprimer</string> <string name="edit">Éditer</string> <string name="form_comment">Commentaire</string> + <string name="error">Erreur</string> <!-- BigFin communication service --> <string name="bigfin_no_ichtyometer_connected_title">Aucun ichtyomètre connecté</string> @@ -107,14 +108,45 @@ <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> + <string name="preferences_export_summary">%s éléments</string> + <string name="preferences_export_commercial_species">Exporter les espèces commerciales</string> + <string name="preferences_export_locations">Exporter les lieux</string> + <string name="preferences_export_mensurations">Exporter les mensurations</string> + <string name="preferences_export_metiers">Exporter les métiers</string> + <string name="preferences_export_presentations">Exporter les présentations</string> + <string name="preferences_export_scientific_species">Exporter les espèces scientifiques</string> + <string name="preferences_export_states">Exporter les états</string> + <string name="preferences_export_vessels">Exporter les navires</string> + <string name="preferences_export_categories">Export des catégorisations</string> + <string name="preferences_exporting_referential">Export des données du référentiel en cours</string> + + <string name="preferences_export_commercial_species_default_name">especes_commerciales</string> + <string name="preferences_export_locations_default_name">lieux</string> + <string name="preferences_export_mensurations_default_name">mensurations</string> + <string name="preferences_export_metiers_default_name">metiers</string> + <string name="preferences_export_presentations_default_name">presentations</string> + <string name="preferences_export_scientific_species_default_name">especes_scientifiques</string> + <string name="preferences_export_states_default_name">etats</string> + <string name="preferences_export_vessels_default_name">navires</string> + + <string name="export_element_number">%s éléments exportés</string> + <string name="export_error">Une erreur s\'est produite durant l\'export.</string> + <string name="preferences_favorites_commercial_species">Espèces commerciales</string> <string name="preferences_favorites_scientific_species">Espèces scientifiques</string> - <string name="favaorite_selection_title">Favoris</string> + <string name="favorite_selection_title">Favoris</string> <string name="select_new_favorite">Choisissez un nouveau favori</string> <string name="favorite_nb">%s favoris</string> <string name="preferences_calcified_parts_takings">Prélèvement des pièces calcifiées</string> <string name="preferences_import_calcified_parts_takings">Import des paramètres de l\'algorithme</string> + <string name="preferences_export_calcified_parts_takings">Export des paramètres de l\'algorithmes</string> + <string name="preferences_export_calcified_parts_takings_default_name">prelevement_pieces_calcifiees</string> + <string name="preferences_import_calcified_parts_takings_null_species">Un code d\'espèce est nul ou ne correspond à aucune espèce dans le référentiel.</string> + <string name="preferences_import_calcified_parts_takings_overlapping">Les intervalles ne doivent pas se chevaucher.</string> + <string name="preferences_import_calcified_parts_takings_value_out_of_ranges">Les intervalles doivent couvrir toutes les valeurs.</string> + <string name="preferences_import_calcified_parts_different_size_steps">Les pas de classe de taille doivent être égaux pour chaque espèce.</string> + <string name="preferences_import_calcified_parts_no_size_step">Au moins une ligne pour chaque espèce doit avoir un pas de classe de taille.</string> <string name="category_creation_title">Nouvelle catégorie</string> <string name="category_label">Libellé</string> Modified: trunk/res/xml/preferences.xml =================================================================== --- trunk/res/xml/preferences.xml 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/res/xml/preferences.xml 2014-03-17 16:56:27 UTC (rev 76) @@ -63,6 +63,30 @@ </PreferenceScreen> <PreferenceScreen + android:key="preferences_exports" + android:title="@string/preferences_exports" + android:persistent="false"> + + <Preference android:title="@string/preferences_export_commercial_species" + android:key="export_commercial_species"/> + <Preference android:title="@string/preferences_export_scientific_species" + android:key="export_scientific_species"/> + <Preference android:title="@string/preferences_export_locations" + android:key="export_locations"/> + <Preference android:title="@string/preferences_export_vessels" + android:key="export_vessels"/> + <Preference android:title="@string/preferences_export_metiers" + android:key="export_metiers"/> + <Preference android:title="@string/preferences_export_presentations" + android:key="export_presentations"/> + <Preference android:title="@string/preferences_export_states" + android:key="export_states"/> + <Preference android:title="@string/preferences_export_mensurations" + android:key="export_mensurations"/> + + </PreferenceScreen> + + <PreferenceScreen android:key="preferences_favorites" android:title="@string/preferences_favorites"> @@ -83,6 +107,9 @@ <Preference android:title="@string/preferences_import_calcified_parts_takings" android:key="import_calcified_parts_takings"/> + <Preference android:title="@string/preferences_export_calcified_parts_takings" + android:key="export_calcified_parts_takings"/> + </PreferenceCategory> <PreferenceCategory Modified: trunk/src/fr/ifremer/wlo/MainActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/MainActivity.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/MainActivity.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,7 +25,6 @@ */ import android.app.Activity; -import android.app.AlertDialog; import android.app.ProgressDialog; import android.bluetooth.BluetoothAdapter; import android.content.Context; @@ -42,13 +41,12 @@ import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; -import fr.ifremer.wlo.utils.ImportUtil; +import fr.ifremer.wlo.utils.ImportExportUtil; import fr.ifremer.wlo.models.categorization.CategoryModel; import fr.ifremer.wlo.preferences.SettingsActivity; import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.storage.Exporter; import fr.ifremer.wlo.storage.WloSqlOpenHelper; -import fr.ifremer.wlo.utils.UIUtils; import fr.ifremer.wlo.utils.UpdateCheckTask; import fr.ifremer.wlo.utils.filechooser.FileDialog; import fr.ifremer.wlo.utils.filechooser.SelectionMode; @@ -308,42 +306,42 @@ try { int i = 1; if (DataCache.getAllCommercialSpecies(context).isEmpty()) { - ImportUtil.importCommercialSpecies(context, getAssets().open("ref_import_commercial_species.csv")); + ImportExportUtil.importCommercialSpecies(context, getAssets().open("ref_import_commercial_species.csv")); } publishProgress(i++); if (DataCache.getAllLocations(context).isEmpty()) { - ImportUtil.importLocations(context, getAssets().open("ref_import_locations.csv")); + ImportExportUtil.importLocations(context, getAssets().open("ref_import_locations.csv")); } publishProgress(i++); if (DataCache.getAllMensurations(context).isEmpty()) { - ImportUtil.importMensurations(context, getAssets().open("ref_import_mensurations.csv")); + ImportExportUtil.importMensurations(context, getAssets().open("ref_import_mensurations.csv")); } publishProgress(i++); if (DataCache.getAllMetiers(context).isEmpty()) { - ImportUtil.importMetiers(context, getAssets().open("ref_import_metiers.csv")); + ImportExportUtil.importMetiers(context, getAssets().open("ref_import_metiers.csv")); } publishProgress(i++); if (DataCache.getAllPresentations(context).isEmpty()) { - ImportUtil.importPresentations(context, getAssets().open("ref_import_presentations.csv")); + ImportExportUtil.importPresentations(context, getAssets().open("ref_import_presentations.csv")); } publishProgress(i++); if (DataCache.getAllScientificSpecies(context).isEmpty()) { - ImportUtil.importScientificSpecies(context, getAssets().open("ref_import_scientific_species.csv")); + ImportExportUtil.importScientificSpecies(context, getAssets().open("ref_import_scientific_species.csv")); } publishProgress(i++); if (DataCache.getAllStates(context).isEmpty()) { - ImportUtil.importStates(context, getAssets().open("ref_import_states.csv")); + ImportExportUtil.importStates(context, getAssets().open("ref_import_states.csv")); } publishProgress(i++); if (DataCache.getAllVessels(context).isEmpty()) { - ImportUtil.importVessels(context, getAssets().open("ref_import_vessels.csv")); + ImportExportUtil.importVessels(context, getAssets().open("ref_import_vessels.csv")); } publishProgress(i++); @@ -362,9 +360,9 @@ maturityCategory.setLabel(getString(R.string.maturity)); soh.saveData(maturityCategory); - soh.saveData(ImportUtil.importQualitativeValues(ageCategory, getAssets().open("ref_import_ages.csv"))); - soh.saveData(ImportUtil.importQualitativeValues(genderCategory, getAssets().open("ref_import_genders.csv"))); - soh.saveData(ImportUtil.importQualitativeValues(maturityCategory, getAssets().open("ref_import_maturities.csv"))); + soh.saveData(ImportExportUtil.importQualitativeValues(ageCategory, getAssets().open("ref_import_ages.csv"))); + soh.saveData(ImportExportUtil.importQualitativeValues(genderCategory, getAssets().open("ref_import_genders.csv"))); + soh.saveData(ImportExportUtil.importQualitativeValues(maturityCategory, getAssets().open("ref_import_maturities.csv"))); soh.close(); } Modified: trunk/src/fr/ifremer/wlo/WloApplication.java =================================================================== --- trunk/src/fr/ifremer/wlo/WloApplication.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/WloApplication.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,25 +25,10 @@ */ import android.app.Application; -import android.app.DownloadManager; -import android.app.ProgressDialog; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.AsyncTask; -import android.util.Log; -import fr.ifremer.wlo.utils.ImportUtil; -import fr.ifremer.wlo.models.categorization.CategoryModel; -import fr.ifremer.wlo.storage.DataCache; -import fr.ifremer.wlo.storage.WloSqlOpenHelper; -import fr.ifremer.wlo.utils.UpdateCheckTask; import org.acra.ACRA; import org.acra.ReportingInteractionMode; import org.acra.annotation.ReportsCrashes; -import java.io.FileNotFoundException; -import java.io.IOException; - /** * @author Kevin Morin (Code Lutin) * @since x.x Modified: trunk/src/fr/ifremer/wlo/imports/CalcifiedPartTakingRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/CalcifiedPartTakingRowModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/imports/CalcifiedPartTakingRowModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -29,6 +29,7 @@ import com.google.common.collect.Maps; import fr.ifremer.wlo.models.referentials.CalcifiedPartTaking; import fr.ifremer.wlo.models.referentials.ScientificSpecies; +import org.nuiton.csv.ValueGetterSetter; import org.nuiton.csv.ValueSetter; import org.nuiton.csv.ext.AbstractImportExportModel; @@ -39,7 +40,7 @@ * @author kmorin <kmorin@codelutin.com> * @since 0.1 */ -public class CalcifiedPartTakingRowModel extends AbstractImportExportModel<CalcifiedPartTaking> { +public class CalcifiedPartTakingRowModel extends WloAbstractImportExportModel<CalcifiedPartTaking> { private static final String TAG = "CalcifiedPartTakingRowModel"; @@ -54,18 +55,22 @@ } }); - newMandatoryColumn("Espece_scientifique", new ValueSetter<CalcifiedPartTaking, String>() { + newColumnForImportExport("Espece_scientifique", new ValueGetterSetter<CalcifiedPartTaking, String>() { @Override public void set(CalcifiedPartTaking object, String value) throws Exception { - if (value == null) { - throw new NullPointerException("L'espece scientifique ne peut etre nulle"); - } + Log.d(TAG, "code expece scientific " + value); ScientificSpecies species = scientificSpeciesByCode.get(value); + Log.d(TAG, "expece scientific " + species); object.setParent(species); } + + @Override + public String get(CalcifiedPartTaking object) throws Exception { + return object.getParentId(); + } }); - newMandatoryColumn("Classe_debut", new ValueSetter<CalcifiedPartTaking, String>() { + newColumnForImportExport("Classe_debut", new ValueGetterSetter<CalcifiedPartTaking, String>() { @Override public void set(CalcifiedPartTaking object, String value) throws Exception { Integer size; @@ -77,9 +82,14 @@ Log.d(TAG, object + "setStartSize " + size); object.setStartSize(size); } + + @Override + public String get(CalcifiedPartTaking object) throws Exception { + return String.valueOf(object.getStartSize()); + } }); - newMandatoryColumn("Classe_fin", new ValueSetter<CalcifiedPartTaking, String>() { + newColumnForImportExport("Classe_fin", new ValueGetterSetter<CalcifiedPartTaking, String>() { @Override public void set(CalcifiedPartTaking object, String value) throws Exception { Integer size; @@ -91,27 +101,47 @@ Log.d(TAG, object + "setEndSize " + size); object.setEndSize(size); } + + @Override + public String get(CalcifiedPartTaking object) throws Exception { + return String.valueOf(object.getEndSize()); + } }); - newMandatoryColumn("Pas_de_classe", new ValueSetter<CalcifiedPartTaking, String>() { + newColumnForImportExport("Pas_de_classe", new ValueGetterSetter<CalcifiedPartTaking, String>() { @Override public void set(CalcifiedPartTaking object, String value) throws Exception { - Integer size = Integer.parseInt(value); + Integer size; + try { + size = Integer.parseInt(value); + } catch (NumberFormatException e) { + size = null; + } Log.d(TAG, object + "setSizeStep " + size); object.setSizeStep(size); } + + @Override + public String get(CalcifiedPartTaking object) throws Exception { + return String.valueOf(object.getSizeStep()); + } }); - newMandatoryColumn("Pas", new ValueSetter<CalcifiedPartTaking, String>() { + newColumnForImportExport("Pas", new ValueGetterSetter<CalcifiedPartTaking, String>() { @Override public void set(CalcifiedPartTaking object, String value) throws Exception { Integer size = Integer.parseInt(value); Log.d(TAG, object + "setStep " + size); object.setStep(size); } + + @Override + public String get(CalcifiedPartTaking object) throws Exception { + return String.valueOf(object.getStep()); + } }); - newMandatoryColumn("Arret", new ValueSetter<CalcifiedPartTaking, String>() { + newColumnForImportExport("Arret", new ValueGetterSetter<CalcifiedPartTaking, String>() { @Override public void set(CalcifiedPartTaking object, String value) throws Exception { Integer size; @@ -123,6 +153,11 @@ Log.d(TAG, object + "setStop " + size); object.setStop(size); } + + @Override + public String get(CalcifiedPartTaking object) throws Exception { + return String.valueOf(object.getStop()); + } }); } Modified: trunk/src/fr/ifremer/wlo/imports/CommercialSpeciesRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/CommercialSpeciesRowModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/imports/CommercialSpeciesRowModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,6 +25,7 @@ */ import fr.ifremer.wlo.models.referentials.CommercialSpecies; +import org.nuiton.csv.ValueGetterSetter; import org.nuiton.csv.ValueSetter; import org.nuiton.csv.ext.AbstractImportExportModel; @@ -32,55 +33,90 @@ * @author kmorin <kmorin@codelutin.com> * @since 0.1 */ -public class CommercialSpeciesRowModel extends AbstractImportExportModel<CommercialSpecies> { +public class CommercialSpeciesRowModel extends WloAbstractImportExportModel<CommercialSpecies> { public CommercialSpeciesRowModel(char separator) { super(separator); - newMandatoryColumn("ESPF_COD", new ValueSetter<CommercialSpecies, String>() { + newColumnForImportExport("ESPF_COD", new ValueGetterSetter<CommercialSpecies, String>() { @Override public void set(CommercialSpecies object, String value) throws Exception { object.setCode(value); } + + @Override + public String get(CommercialSpecies object) throws Exception { + return object.getCode(); + } }); - newMandatoryColumn("ESPF_ISSCAP", new ValueSetter<CommercialSpecies, String>() { + newColumnForImportExport("ESPF_ISSCAP", new ValueGetterSetter<CommercialSpecies, String>() { @Override public void set(CommercialSpecies object, String value) throws Exception { object.setIsscap(value); } + + @Override + public String get(CommercialSpecies object) throws Exception { + return object.getIsscap(); + } }); - newMandatoryColumn("ESPF_TAXON_COD", new ValueSetter<CommercialSpecies, String>() { + newColumnForImportExport("ESPF_TAXON_COD", new ValueGetterSetter<CommercialSpecies, String>() { @Override public void set(CommercialSpecies object, String value) throws Exception { object.setTaxonCode(value); } + + @Override + public String get(CommercialSpecies object) throws Exception { + return object.getTaxonCode(); + } }); - newMandatoryColumn("ESPF_SCI_LIB", new ValueSetter<CommercialSpecies, String>() { + newColumnForImportExport("ESPF_SCI_LIB", new ValueGetterSetter<CommercialSpecies, String>() { @Override public void set(CommercialSpecies object, String value) throws Exception { object.setScientificLabel(value); } + + @Override + public String get(CommercialSpecies object) throws Exception { + return object.getScientificLabel(); + } }); - newMandatoryColumn("ESPF_FRA_LIB", new ValueSetter<CommercialSpecies, String>() { + newColumnForImportExport("ESPF_FRA_LIB", new ValueGetterSetter<CommercialSpecies, String>() { @Override public void set(CommercialSpecies object, String value) throws Exception { object.setFrenchLabel(value); } + + @Override + public String get(CommercialSpecies object) throws Exception { + return object.getFrenchLabel(); + } }); - newMandatoryColumn("ESPF_FAMILLE", new ValueSetter<CommercialSpecies, String>() { + newColumnForImportExport("ESPF_FAMILLE", new ValueGetterSetter<CommercialSpecies, String>() { @Override public void set(CommercialSpecies object, String value) throws Exception { object.setFamily(value); } + + @Override + public String get(CommercialSpecies object) throws Exception { + return object.getFamily(); + } }); - newMandatoryColumn("ESPF_ORDRE", new ValueSetter<CommercialSpecies, String>() { + newColumnForImportExport("ESPF_ORDRE", new ValueGetterSetter<CommercialSpecies, String>() { @Override public void set(CommercialSpecies object, String value) throws Exception { object.setSpeciesOrder(value); } + + @Override + public String get(CommercialSpecies object) throws Exception { + return object.getSpeciesOrder(); + } }); - newMandatoryColumn("ESPF_ACT", new ValueSetter<CommercialSpecies, String>() { + newColumnForImportExport("ESPF_ACT", new ValueGetterSetter<CommercialSpecies, String>() { @Override public void set(CommercialSpecies object, String value) throws Exception { Integer iValue; @@ -91,6 +127,11 @@ } object.setActive(iValue); } + + @Override + public String get(CommercialSpecies object) throws Exception { + return String.valueOf(object.getActive()); + } }); } Modified: trunk/src/fr/ifremer/wlo/imports/LocationRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/LocationRowModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/imports/LocationRowModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,6 +25,7 @@ */ import fr.ifremer.wlo.models.referentials.Location; +import org.nuiton.csv.ValueGetterSetter; import org.nuiton.csv.ValueSetter; import org.nuiton.csv.ext.AbstractImportExportModel; @@ -32,29 +33,44 @@ * @author kmorin <kmorin@codelutin.com> * @since 0.1 */ -public class LocationRowModel extends AbstractImportExportModel<Location> { +public class LocationRowModel extends WloAbstractImportExportModel<Location> { public LocationRowModel(char separator) { super(separator); - newMandatoryColumn("TLIEU_LIB", new ValueSetter<Location, String>() { + newColumnForImportExport("TLIEU_LIB", new ValueGetterSetter<Location, String>() { @Override public void set(Location object, String value) throws Exception { object.setTypeLabel(value); } + + @Override + public String get(Location object) throws Exception { + return object.getTypeLabel(); + } }); - newMandatoryColumn("LIEU_COD", new ValueSetter<Location, String>() { + newColumnForImportExport("LIEU_COD", new ValueGetterSetter<Location, String>() { @Override public void set(Location object, String value) throws Exception { object.setCode(value); } + + @Override + public String get(Location object) throws Exception { + return object.getCode(); + } }); - newMandatoryColumn("LIEU_LIB", new ValueSetter<Location, String>() { + newColumnForImportExport("LIEU_LIB", new ValueGetterSetter<Location, String>() { @Override public void set(Location object, String value) throws Exception { object.setLabel(value); } + + @Override + public String get(Location object) throws Exception { + return object.getLabel(); + } }); } Modified: trunk/src/fr/ifremer/wlo/imports/MensurationRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/MensurationRowModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/imports/MensurationRowModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,6 +25,7 @@ */ import fr.ifremer.wlo.models.referentials.Mensuration; +import org.nuiton.csv.ValueGetterSetter; import org.nuiton.csv.ValueSetter; import org.nuiton.csv.ext.AbstractImportExportModel; @@ -32,23 +33,35 @@ * @author kmorin <kmorin@codelutin.com> * @since 0.1 */ -public class MensurationRowModel extends AbstractImportExportModel<Mensuration> { +public class MensurationRowModel extends WloAbstractImportExportModel<Mensuration> { public MensurationRowModel(char separator) { super(separator); - newMandatoryColumn("Type_Longueur_cod", new ValueSetter<Mensuration, String>() { + newColumnForImportExport("Type_Longueur_cod", new ValueGetterSetter<Mensuration, String>() { + @Override public void set(Mensuration object, String value) throws Exception { object.setCode(value); } + + @Override + public String get(Mensuration object) throws Exception { + return object.getCode(); + } }); - newMandatoryColumn("Type_Longueur_lib", new ValueSetter<Mensuration, String>() { + newColumnForImportExport("Type_Longueur_lib", new ValueGetterSetter<Mensuration, String>() { + @Override public void set(Mensuration object, String value) throws Exception { object.setLabel(value); } + + @Override + public String get(Mensuration object) throws Exception { + return object.getLabel(); + } }); } Modified: trunk/src/fr/ifremer/wlo/imports/MetierRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/MetierRowModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/imports/MetierRowModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,6 +25,7 @@ */ import fr.ifremer.wlo.models.referentials.Metier; +import org.nuiton.csv.ValueGetterSetter; import org.nuiton.csv.ValueSetter; import org.nuiton.csv.ext.AbstractImportExportModel; @@ -32,55 +33,90 @@ * @author kmorin <kmorin@codelutin.com> * @since 0.1 */ -public class MetierRowModel extends AbstractImportExportModel<Metier> { +public class MetierRowModel extends WloAbstractImportExportModel<Metier> { public MetierRowModel(char separator) { super(separator); - newMandatoryColumn("MET_ID", new ValueSetter<Metier, String>() { + newColumnForImportExport("MET_ID", new ValueGetterSetter<Metier, String>() { @Override public void set(Metier object, String value) throws Exception { object.setMetierId(value); } + + @Override + public String get(Metier object) throws Exception { + return object.getMetierId(); + } }); - newMandatoryColumn("MET_COD", new ValueSetter<Metier, String>() { + newColumnForImportExport("MET_COD", new ValueGetterSetter<Metier, String>() { @Override public void set(Metier object, String value) throws Exception { object.setCode(value); } + + @Override + public String get(Metier object) throws Exception { + return object.getCode(); + } }); - newMandatoryColumn("MET_LIB", new ValueSetter<Metier, String>() { + newColumnForImportExport("MET_LIB", new ValueGetterSetter<Metier, String>() { @Override public void set(Metier object, String value) throws Exception { object.setLabel(value); } + + @Override + public String get(Metier object) throws Exception { + return object.getLabel(); + } }); - newMandatoryColumn("MET_ENGIN_COD", new ValueSetter<Metier, String>() { + newColumnForImportExport("MET_ENGIN_COD", new ValueGetterSetter<Metier, String>() { @Override public void set(Metier object, String value) throws Exception { object.setGearCode(value); } + + @Override + public String get(Metier object) throws Exception { + return object.getGearCode(); + } }); - newMandatoryColumn("MET_ENGIN_LIB", new ValueSetter<Metier, String>() { + newColumnForImportExport("MET_ENGIN_LIB", new ValueGetterSetter<Metier, String>() { @Override public void set(Metier object, String value) throws Exception { object.setGearLabel(value); } + + @Override + public String get(Metier object) throws Exception { + return object.getGearLabel(); + } }); - newMandatoryColumn("MET_ESPECE_COD", new ValueSetter<Metier, String>() { + newColumnForImportExport("MET_ESPECE_COD", new ValueGetterSetter<Metier, String>() { @Override public void set(Metier object, String value) throws Exception { object.setSpeciesCode(value); } + + @Override + public String get(Metier object) throws Exception { + return object.getSpeciesCode(); + } }); - newMandatoryColumn("MET_ESPECE_LIB", new ValueSetter<Metier, String>() { + newColumnForImportExport("MET_ESPECE_LIB", new ValueGetterSetter<Metier, String>() { @Override public void set(Metier object, String value) throws Exception { object.setSpeciesLabel(value); } + + @Override + public String get(Metier object) throws Exception { + return object.getSpeciesLabel(); + } }); - newMandatoryColumn("MET_PECHE", new ValueSetter<Metier, String>() { + newColumnForImportExport("MET_PECHE", new ValueGetterSetter<Metier, String>() { @Override public void set(Metier object, String value) throws Exception { Integer iValue; @@ -91,8 +127,13 @@ } object.setFishing(iValue); } + + @Override + public String get(Metier object) throws Exception { + return String.valueOf(object.getFishing()); + } }); - newMandatoryColumn("MET_ACT", new ValueSetter<Metier, String>() { + newColumnForImportExport("MET_ACT", new ValueGetterSetter<Metier, String>() { @Override public void set(Metier object, String value) throws Exception { Integer iValue; @@ -103,6 +144,11 @@ } object.setActive(iValue); } + + @Override + public String get(Metier object) throws Exception { + return String.valueOf(object.getActive()); + } }); } Modified: trunk/src/fr/ifremer/wlo/imports/PresentationRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/PresentationRowModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/imports/PresentationRowModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,6 +25,7 @@ */ import fr.ifremer.wlo.models.referentials.Presentation; +import org.nuiton.csv.ValueGetterSetter; import org.nuiton.csv.ValueSetter; import org.nuiton.csv.ext.AbstractImportExportModel; @@ -32,23 +33,33 @@ * @author kmorin <kmorin@codelutin.com> * @since 0.1 */ -public class PresentationRowModel extends AbstractImportExportModel<Presentation> { +public class PresentationRowModel extends WloAbstractImportExportModel<Presentation> { public PresentationRowModel(char separator) { super(separator); - newMandatoryColumn("Presentation_cod", new ValueSetter<Presentation, String>() { + newColumnForImportExport("Presentation_cod", new ValueGetterSetter<Presentation, String>() { @Override public void set(Presentation object, String value) throws Exception { object.setCode(value); } + + @Override + public String get(Presentation object) throws Exception { + return object.getCode(); + } }); - newMandatoryColumn("Presentation_lib", new ValueSetter<Presentation, String>() { + newColumnForImportExport("Presentation_lib", new ValueGetterSetter<Presentation, String>() { @Override public void set(Presentation object, String value) throws Exception { object.setLabel(value); } + + @Override + public String get(Presentation object) throws Exception { + return object.getLabel(); + } }); } Modified: trunk/src/fr/ifremer/wlo/imports/ScientificSpeciesRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/ScientificSpeciesRowModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/imports/ScientificSpeciesRowModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,6 +25,7 @@ */ import fr.ifremer.wlo.models.referentials.ScientificSpecies; +import org.nuiton.csv.ValueGetterSetter; import org.nuiton.csv.ValueSetter; import org.nuiton.csv.ext.AbstractImportExportModel; @@ -32,29 +33,44 @@ * @author kmorin <kmorin@codelutin.com> * @since 0.1 */ -public class ScientificSpeciesRowModel extends AbstractImportExportModel<ScientificSpecies> { +public class ScientificSpeciesRowModel extends WloAbstractImportExportModel<ScientificSpecies> { public ScientificSpeciesRowModel(char separator) { super(separator); - newMandatoryColumn("C_Perm", new ValueSetter<ScientificSpecies, String>() { + newColumnForImportExport("C_Perm", new ValueGetterSetter<ScientificSpecies, String>() { @Override public void set(ScientificSpecies object, String value) throws Exception { object.setPermCode(value); } + + @Override + public String get(ScientificSpecies object) throws Exception { + return object.getPermCode(); + } }); - newMandatoryColumn("C_VALIDE", new ValueSetter<ScientificSpecies, String>() { + newColumnForImportExport("C_VALIDE", new ValueGetterSetter<ScientificSpecies, String>() { @Override public void set(ScientificSpecies object, String value) throws Exception { object.setCode(value); } + + @Override + public String get(ScientificSpecies object) throws Exception { + return object.getCode(); + } }); - newMandatoryColumn("L_VALIDE", new ValueSetter<ScientificSpecies, String>() { + newColumnForImportExport("L_VALIDE", new ValueGetterSetter<ScientificSpecies, String>() { @Override public void set(ScientificSpecies object, String value) throws Exception { object.setLabel(value); } + + @Override + public String get(ScientificSpecies object) throws Exception { + return object.getLabel(); + } }); } Modified: trunk/src/fr/ifremer/wlo/imports/StateRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/StateRowModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/imports/StateRowModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,6 +25,7 @@ */ import fr.ifremer.wlo.models.referentials.State; +import org.nuiton.csv.ValueGetterSetter; import org.nuiton.csv.ValueSetter; import org.nuiton.csv.ext.AbstractImportExportModel; @@ -32,23 +33,33 @@ * @author kmorin <kmorin@codelutin.com> * @since 0.1 */ -public class StateRowModel extends AbstractImportExportModel<State> { +public class StateRowModel extends WloAbstractImportExportModel<State> { public StateRowModel(char separator) { super(separator); - newMandatoryColumn("Etat_cod", new ValueSetter<State, String>() { + newColumnForImportExport("Etat_cod", new ValueGetterSetter<State, String>() { @Override public void set(State object, String value) throws Exception { object.setCode(value); } + + @Override + public String get(State object) throws Exception { + return object.getCode(); + } }); - newMandatoryColumn("Etat_lib", new ValueSetter<State, String>() { + newColumnForImportExport("Etat_lib", new ValueGetterSetter<State, String>() { @Override public void set(State object, String value) throws Exception { object.setLabel(value); } + + @Override + public String get(State object) throws Exception { + return object.getLabel(); + } }); } Modified: trunk/src/fr/ifremer/wlo/imports/VesselRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/VesselRowModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/imports/VesselRowModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -25,6 +25,7 @@ */ import fr.ifremer.wlo.models.referentials.Vessel; +import org.nuiton.csv.ValueGetterSetter; import org.nuiton.csv.ValueSetter; import org.nuiton.csv.ext.AbstractImportExportModel; @@ -32,29 +33,44 @@ * @author kmorin <kmorin@codelutin.com> * @since 0.1 */ -public class VesselRowModel extends AbstractImportExportModel<Vessel> { +public class VesselRowModel extends WloAbstractImportExportModel<Vessel> { public VesselRowModel(char separator) { super(separator); - newMandatoryColumn("NAVS_COD", new ValueSetter<Vessel, String>() { + newColumnForImportExport("NAVS_COD", new ValueGetterSetter<Vessel, String>() { @Override public void set(Vessel object, String value) throws Exception { object.setCode(value); } + + @Override + public String get(Vessel object) throws Exception { + return object.getCode(); + } }); - newMandatoryColumn("CARN_NOM", new ValueSetter<Vessel, String>() { + newColumnForImportExport("CARN_NOM", new ValueGetterSetter<Vessel, String>() { @Override public void set(Vessel object, String value) throws Exception { object.setName(value); } + + @Override + public String get(Vessel object) throws Exception { + return object.getName(); + } }); - newMandatoryColumn("QUARTIER_COD", new ValueSetter<Vessel, String>() { + newColumnForImportExport("QUARTIER_COD", new ValueGetterSetter<Vessel, String>() { @Override public void set(Vessel object, String value) throws Exception { object.setQuarterCode(value); } + + @Override + public String get(Vessel object) throws Exception { + return object.getQuarterCode(); + } }); } Added: trunk/src/fr/ifremer/wlo/imports/WloAbstractImportExportModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/WloAbstractImportExportModel.java (rev 0) +++ trunk/src/fr/ifremer/wlo/imports/WloAbstractImportExportModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -0,0 +1,32 @@ +package fr.ifremer.wlo.imports; + +import fr.ifremer.wlo.models.BaseModel; +import org.nuiton.csv.ValueGetter; +import org.nuiton.csv.ValueGetterSetter; +import org.nuiton.csv.ValueSetter; +import org.nuiton.csv.ext.AbstractImportExportModel; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public abstract class WloAbstractImportExportModel<M extends BaseModel> extends AbstractImportExportModel<M> { + + public WloAbstractImportExportModel(char separator) { + super(separator); + + newOptionalColumn("id", new ValueSetter<M, String>() { + @Override + public void set(M object, String value) throws Exception { + object.setId(value); + } + }); + + newColumnForExport("id", new ValueGetter<M, String>() { + @Override + public String get(M object) throws Exception { + return object.getId(); + } + }); + } +} Modified: trunk/src/fr/ifremer/wlo/models/BaseModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/BaseModel.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/models/BaseModel.java 2014-03-17 16:56:27 UTC (rev 76) @@ -34,6 +34,7 @@ import com.google.common.collect.Multimap; import com.google.common.collect.Sets; import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -86,7 +87,7 @@ } public boolean isNew() { - return id == null; + return StringUtils.isBlank(id); } public boolean isModified() { Modified: trunk/src/fr/ifremer/wlo/models/referentials/CalcifiedPartTaking.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/CalcifiedPartTaking.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/models/referentials/CalcifiedPartTaking.java 2014-03-17 16:56:27 UTC (rev 76) @@ -63,7 +63,7 @@ protected Integer startSize; protected Integer endSize; - protected int sizeStep; + protected Integer sizeStep; protected int step; protected Integer stop; @@ -104,11 +104,11 @@ this.endSize = endSize; } - public int getSizeStep() { + public Integer getSizeStep() { return sizeStep; } - public void setSizeStep(int sizeStep) { + public void setSizeStep(Integer sizeStep) { this.sizeStep = sizeStep; } Modified: trunk/src/fr/ifremer/wlo/preferences/CategoryCreationAcivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/CategoryCreationAcivity.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/preferences/CategoryCreationAcivity.java 2014-03-17 16:56:27 UTC (rev 76) @@ -34,7 +34,7 @@ import android.widget.TextView; import fr.ifremer.wlo.R; import fr.ifremer.wlo.WloBaseActivity; -import fr.ifremer.wlo.utils.ImportUtil; +import fr.ifremer.wlo.utils.ImportExportUtil; import fr.ifremer.wlo.models.categorization.CategoryModel; import fr.ifremer.wlo.models.categorization.QualitativeValueModel; import fr.ifremer.wlo.storage.DataCache; @@ -113,7 +113,7 @@ String path = data.getStringExtra(FileDialog.RESULT_PATH); try { Collection<QualitativeValueModel> qualitativeValueModels = - ImportUtil.importQualitativeValues(model, path); + ImportExportUtil.importQualitativeValues(model, path); model.setQualitativeValues(new ArrayList<>(qualitativeValueModels)); TextView textView = (TextView) findViewById(R.id.qualitative_values_nb_label); Modified: trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-03-17 16:56:27 UTC (rev 76) @@ -34,15 +34,11 @@ import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; -import android.preference.MultiSelectListPreference; import android.preference.Preference; import android.preference.PreferenceFragment; import android.util.Log; import android.widget.Toast; -import com.google.common.base.Functions; -import com.google.common.base.Preconditions; import com.google.common.collect.BiMap; -import com.google.common.collect.Collections2; import com.google.common.collect.HashBiMap; import com.google.common.collect.Maps; import fr.ifremer.wlo.MainActivity; @@ -54,7 +50,7 @@ import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.storage.StorageUtils; import fr.ifremer.wlo.storage.WloSqlOpenHelper; -import fr.ifremer.wlo.utils.ImportUtil; +import fr.ifremer.wlo.utils.ImportExportUtil; import fr.ifremer.wlo.utils.UIUtils; import fr.ifremer.wlo.utils.UpdateCheckTask; import fr.ifremer.wlo.utils.filechooser.FileDialog; @@ -65,10 +61,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; -import java.util.Collection; import java.util.Date; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -89,11 +83,21 @@ protected static final int REQUEST_IMPORT_STATES = 6; protected static final int REQUEST_IMPORT_VESSELS = 7; protected static final int REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS = 8; - protected static final int REQUEST_CREATE_CATEGORY = 9; - protected static final int REQUEST_FAVORITES_COMMERCIAL_SPECIES = 10; - protected static final int REQUEST_FAVORITES_SCIENTIFIC_SPECIES = 11; + protected static final int REQUEST_EXPORT_COMMERCIAL_SPECIES = 10; + protected static final int REQUEST_EXPORT_LOCATIONS = 11; + protected static final int REQUEST_EXPORT_MENSURATIONS = 12; + protected static final int REQUEST_EXPORT_METIERS = 13; + protected static final int REQUEST_EXPORT_PRESENTATIONS = 14; + protected static final int REQUEST_EXPORT_SCIENTIFIC_SPECIES = 15; + protected static final int REQUEST_EXPORT_STATES = 16; + protected static final int REQUEST_EXPORT_VESSELS = 17; + protected static final int REQUEST_EXPORT_CALCIFIED_PARTS_TAKINGS = 18; + protected static final int REQUEST_CREATE_CATEGORY = 20; + protected static final int REQUEST_FAVORITES_COMMERCIAL_SPECIES = 21; + protected static final int REQUEST_FAVORITES_SCIENTIFIC_SPECIES = 22; + @Override protected Integer getContentView() { return null; @@ -138,6 +142,16 @@ requestCodeByPrefKey.put("import_vessels", REQUEST_IMPORT_VESSELS); requestCodeByPrefKey.put("import_calcified_parts_takings", REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS); + requestCodeByPrefKey.put("export_commercial_species", REQUEST_EXPORT_COMMERCIAL_SPECIES); + requestCodeByPrefKey.put("export_locations", REQUEST_EXPORT_LOCATIONS); + requestCodeByPrefKey.put("export_mensurations", REQUEST_EXPORT_MENSURATIONS); + requestCodeByPrefKey.put("export_metiers", REQUEST_EXPORT_METIERS); + requestCodeByPrefKey.put("export_presentations", REQUEST_EXPORT_PRESENTATIONS); + requestCodeByPrefKey.put("export_scientific_species", REQUEST_EXPORT_SCIENTIFIC_SPECIES); + requestCodeByPrefKey.put("export_states", REQUEST_EXPORT_STATES); + requestCodeByPrefKey.put("export_vessels", REQUEST_EXPORT_VESSELS); + requestCodeByPrefKey.put("export_calcified_parts_takings", REQUEST_EXPORT_CALCIFIED_PARTS_TAKINGS); + Map<String, Integer> nbElementsByPrefKey = Maps.newHashMap(); Context context = getActivity(); nbElementsByPrefKey.put("import_commercial_species", DataCache.getAllCommercialSpecies(context).size()); @@ -149,6 +163,15 @@ nbElementsByPrefKey.put("import_states", DataCache.getAllStates(context).size()); nbElementsByPrefKey.put("import_vessels", DataCache.getAllVessels(context).size()); + nbElementsByPrefKey.put("export_commercial_species", DataCache.getAllCommercialSpecies(context).size()); + nbElementsByPrefKey.put("export_locations", DataCache.getAllLocations(context).size()); + nbElementsByPrefKey.put("export_mensurations", DataCache.getAllMensurations(context).size()); + nbElementsByPrefKey.put("export_metiers", DataCache.getAllMetiers(context).size()); + nbElementsByPrefKey.put("export_presentations", DataCache.getAllPresentations(context).size()); + nbElementsByPrefKey.put("export_scientific_species", DataCache.getAllScientificSpecies(context).size()); + nbElementsByPrefKey.put("export_states", DataCache.getAllStates(context).size()); + nbElementsByPrefKey.put("export_vessels", DataCache.getAllVessels(context).size()); + for (String key : requestCodeByPrefKey.keySet()) { final Integer requestCode = requestCodeByPrefKey.get(key); Preference filePicker = findPreference(key); @@ -161,7 +184,7 @@ public boolean onPreferenceClick(Preference preference) { Intent intent = new Intent(getActivity(), FileDialog.class); //Intent to start openIntents File Manager intent.putExtra(FileDialog.START_PATH, "/sdcard"); - intent.putExtra(FileDialog.CAN_SELECT_DIR, false); + intent.putExtra(FileDialog.CAN_SELECT_DIR, requestCode >= REQUEST_EXPORT_COMMERCIAL_SPECIES); intent.putExtra(FileDialog.SELECTION_MODE, SelectionMode.MODE_OPEN); //alternatively you can set file filter @@ -293,6 +316,11 @@ importData(requestCode, data, true); } + } else if (requestCode >= REQUEST_EXPORT_COMMERCIAL_SPECIES + && requestCode <= REQUEST_EXPORT_CALCIFIED_PARTS_TAKINGS) { + + exportData(requestCode, data); + } else if (requestCode == REQUEST_CREATE_CATEGORY) { int nb = DataCache.getAllCategories(context).size(); Preference categoryPref = findPreference("import_categories"); @@ -311,11 +339,15 @@ } protected void importData(final int requestCode, final Intent data, final boolean backupFile) { - final Context context = getActivity(); String path = data.getStringExtra(FileDialog.RESULT_PATH); new ImportAsyncTask().execute(path, requestCode, backupFile); } + protected void exportData(final int requestCode, final Intent data) { + String path = data.getStringExtra(FileDialog.RESULT_PATH); + new ExportAsyncTask().execute(path, requestCode); + } + protected <M extends BaseModel> void initMultiSelectListPreference(String prefKey, final Class clazz, final int requestCode) { @@ -361,6 +393,8 @@ protected ProgressDialog dialog; + protected String errorMessage; + @Override protected void onPreExecute() { super.onPreExecute(); @@ -388,42 +422,42 @@ try { switch (requestCode) { case REQUEST_IMPORT_COMMERCIAL_SPECIES: - result[0] = ImportUtil.importCommercialSpecies(context, path); + result[0] = ImportExportUtil.importCommercialSpecies(context, path); result[1] = DataCache.getAllCommercialSpecies(context).size(); break; case REQUEST_IMPORT_LOCATIONS: - result[0] = ImportUtil.importLocations(context, path); + result[0] = ImportExportUtil.importLocations(context, path); result[1] = DataCache.getAllLocations(context).size(); break; case REQUEST_IMPORT_MENSURATIONS: - result[0] = ImportUtil.importMensurations(context, path); + result[0] = ImportExportUtil.importMensurations(context, path); result[1] = DataCache.getAllMensurations(context).size(); break; case REQUEST_IMPORT_METIERS: - result[0] = ImportUtil.importMetiers(context, path); + result[0] = ImportExportUtil.importMetiers(context, path); result[1] = DataCache.getAllMetiers(context).size(); break; case REQUEST_IMPORT_PRESENTATIONS: - result[0] = ImportUtil.importPresentations(context, path); + result[0] = ImportExportUtil.importPresentations(context, path); result[1] = DataCache.getAllPresentations(context).size(); break; case REQUEST_IMPORT_SCIENTIFIC_SPECIES: - result[0] = ImportUtil.importScientificSpecies(context, path); + result[0] = ImportExportUtil.importScientificSpecies(context, path); result[1] = DataCache.getAllScientificSpecies(context).size(); break; case REQUEST_IMPORT_STATES: - result[0] = ImportUtil.importStates(context, path); + result[0] = ImportExportUtil.importStates(context, path); result[1] = DataCache.getAllStates(context).size(); break; case REQUEST_IMPORT_VESSELS: - result[0] = ImportUtil.importVessels(context, path); + result[0] = ImportExportUtil.importVessels(context, path); result[1] = DataCache.getAllVessels(context).size(); break; case REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS: WloSqlOpenHelper soh = new WloSqlOpenHelper(context); soh.clearCalcifiedPartTakings(); soh.close(); - result[0] = ImportUtil.importCalcifiedPartTakings(context, path); + result[0] = ImportExportUtil.importCalcifiedPartTakings(context, path); result[1] = null; break; default: @@ -446,7 +480,14 @@ } 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); + Log.e(TAG, "error during import " + requestCode, e); + + if (requestCode == REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS) { + errorMessage = e.getMessage(); + + } else { + errorMessage = getString(R.string.import_error); + } result = null; cancel(true); @@ -468,13 +509,6 @@ } Toast.makeText(getActivity(), getString(R.string.import_new_element_number, result[0]), Toast.LENGTH_LONG).show(); - -// } else { -// //error -// new AlertDialog.Builder(getActivity()) -// .setMessage(R.string.import_error) -// .setNeutralButton(android.R.string.ok, UIUtils.getCancelClickListener()) -// .create().show(); } } @@ -484,12 +518,111 @@ dialog.dismiss(); new AlertDialog.Builder(getActivity()) - .setMessage(R.string.import_error) + .setTitle(R.string.error) + .setMessage(errorMessage) .setNeutralButton(android.R.string.ok, UIUtils.getCancelClickListener()) .create().show(); } } + + protected class ExportAsyncTask extends AsyncTask<Object, Integer, Integer> { + + protected ProgressDialog dialog; + + @Override + protected void onPreExecute() { + super.onPreExecute(); + + dialog = new ProgressDialog(getActivity()); + dialog.setCancelable(false); + dialog.setMessage(getString(R.string.preferences_exporting_referential)); + dialog.show(); + } + + @Override + protected Integer doInBackground(Object... params) { + if (params.length != 2) { + return null; + } + Context context = getActivity(); + String path = (String) params[0]; + Integer requestCode = (Integer) params[1]; + Log.d(TAG, path); + + Integer result; + + try { + switch (requestCode) { + case REQUEST_EXPORT_COMMERCIAL_SPECIES: + result = ImportExportUtil.exportCommercialSpecies(context, path); + break; + case REQUEST_EXPORT_LOCATIONS: + result = ImportExportUtil.exportLocations(context, path); + break; + case REQUEST_EXPORT_MENSURATIONS: + result = ImportExportUtil.exportMensurations(context, path); + break; + case REQUEST_EXPORT_METIERS: + result = ImportExportUtil.exportMetiers(context, path); + break; + case REQUEST_EXPORT_PRESENTATIONS: + result = ImportExportUtil.exportPresentations(context, path); + break; + case REQUEST_EXPORT_SCIENTIFIC_SPECIES: + result = ImportExportUtil.exportScientificSpecies(context, path); + break; + case REQUEST_EXPORT_STATES: + result = ImportExportUtil.exportStates(context, path); + break; + case REQUEST_EXPORT_VESSELS: + result = ImportExportUtil.exportVessels(context, path); + break; + case REQUEST_EXPORT_CALCIFIED_PARTS_TAKINGS: + WloSqlOpenHelper soh = new WloSqlOpenHelper(context); + soh.clearCalcifiedPartTakings(); + soh.close(); + result = ImportExportUtil.exportCalcifiedPartTakings(context, path); + break; + default: + result = null; + } + + } 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); + result = null; + cancel(true); + + } + return result; + } + + @Override + protected void onPostExecute(Integer result) { + super.onPostExecute(result); + + dialog.dismiss(); + + if (result != null) { + Toast.makeText(getActivity(), getString(R.string.export_element_number, result), Toast.LENGTH_LONG).show(); + } + } + + @Override + protected void onCancelled(Integer integer) { + super.onCancelled(integer); + dialog.dismiss(); + + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.error) + .setMessage(R.string.export_error) + .setNeutralButton(android.R.string.ok, UIUtils.getCancelClickListener()) + .create().show(); + } + + } } } \ No newline at end of file Modified: trunk/src/fr/ifremer/wlo/storage/WloSqlOpenHelper.java =================================================================== --- trunk/src/fr/ifremer/wlo/storage/WloSqlOpenHelper.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/storage/WloSqlOpenHelper.java 2014-03-17 16:56:27 UTC (rev 76) @@ -28,6 +28,7 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; @@ -570,6 +571,13 @@ // CALCIFIED PART TAKING + public Cursor getAllCalcifiedPartTakings() { + SQLiteDatabase db = getReadableDatabase(); + Cursor cursor = db.query(CalcifiedPartTaking.TABLE_NAME, CalcifiedPartTaking.ALL_COLUMNS, + null, null, null, null, null); + return cursor; + } + public Cursor getAllCalcifiedPartTakings(String scientificSpeciesId) { SQLiteDatabase db = getReadableDatabase(); Cursor cursor = db.query(CalcifiedPartTaking.TABLE_NAME, CalcifiedPartTaking.ALL_COLUMNS, @@ -687,9 +695,11 @@ ContentValues values = model.convertIntoContentValues(); if (newSession) { + Log.d(TAG, "create " + values); db.insert(tableName, null, values); } else { + Log.d(TAG, "update " + values); db.update(tableName, values, BaseModel._ID + " = ?", new String[]{ model.getId() }); } model.setModified(false); Copied: trunk/src/fr/ifremer/wlo/utils/ImportExportUtil.java (from rev 69, trunk/src/fr/ifremer/wlo/utils/ImportUtil.java) =================================================================== --- trunk/src/fr/ifremer/wlo/utils/ImportExportUtil.java (rev 0) +++ trunk/src/fr/ifremer/wlo/utils/ImportExportUtil.java 2014-03-17 16:56:27 UTC (rev 76) @@ -0,0 +1,497 @@ +package fr.ifremer.wlo.utils; + +/* + * #%L + * WLO + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2013 - 2014 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import android.content.Context; +import android.database.Cursor; +import android.os.AsyncTask; +import android.util.Log; +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.HashMultimap; +import com.google.common.collect.Lists; +import com.google.common.collect.Multimap; +import fr.ifremer.wlo.R; +import fr.ifremer.wlo.imports.CalcifiedPartTakingRowModel; +import fr.ifremer.wlo.imports.CommercialSpeciesRowModel; +import fr.ifremer.wlo.imports.LocationRowModel; +import fr.ifremer.wlo.imports.MensurationRowModel; +import fr.ifremer.wlo.imports.MetierRowModel; +import fr.ifremer.wlo.imports.PresentationRowModel; +import fr.ifremer.wlo.imports.QualitativeValueRowModel; +import fr.ifremer.wlo.imports.ScientificSpeciesRowModel; +import fr.ifremer.wlo.imports.StateRowModel; +import fr.ifremer.wlo.imports.VesselRowModel; +import fr.ifremer.wlo.models.BaseModel; +import fr.ifremer.wlo.models.categorization.CategoryModel; +import fr.ifremer.wlo.models.categorization.QualitativeValueModel; +import fr.ifremer.wlo.models.referentials.CalcifiedPartTaking; +import fr.ifremer.wlo.models.referentials.HasCode; +import fr.ifremer.wlo.storage.DataCache; +import fr.ifremer.wlo.storage.WloSqlOpenHelper; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.ComparatorUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.Range; +import org.nuiton.csv.Export; +import org.nuiton.csv.ExportModel; +import org.nuiton.csv.Import; +import org.nuiton.csv.ImportModel; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * @author kmorin <kmorin@codelutin.com> + * @since 0.1 + */ +public class ImportExportUtil { + + private static final String TAG = "ImportExportUtil"; + + public static final char CSV_SEPARATOR = ';'; + + public static int importCommercialSpecies(Context context, String path) { + try { + return importCommercialSpecies(context, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return 0; + } + + public static int importCommercialSpecies(Context context, InputStream inputStream) { + CommercialSpeciesRowModel commercialSpeciesRowModel = new CommercialSpeciesRowModel(CSV_SEPARATOR); + int result = importData(context, commercialSpeciesRowModel, inputStream); + DataCache.invalidateCommercialSpecies(); + return result; + } + + public static int exportCommercialSpecies(Context context, String path) throws Exception { + Preconditions.checkNotNull(path); + CommercialSpeciesRowModel commercialSpeciesRowModel = new CommercialSpeciesRowModel(CSV_SEPARATOR); + int result = exportData(context, + commercialSpeciesRowModel, + DataCache.getAllCommercialSpecies(context), + path, + R.string.preferences_export_commercial_species_default_name); + return result; + } + + public static int importLocations(Context context, String path) { + try { + return importLocations(context, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return 0; + } + + public static int importLocations(Context context, InputStream inputStream) { + LocationRowModel locationRowModel = new LocationRowModel(CSV_SEPARATOR); + int result = importData(context, locationRowModel, inputStream); + DataCache.invalidateLocations(); + return result; + } + + public static int exportLocations(Context context, String path) throws Exception { + Preconditions.checkNotNull(path); + LocationRowModel locationRowModel = new LocationRowModel(CSV_SEPARATOR); + int result = exportData(context, + locationRowModel, + DataCache.getAllLocations(context), + path, + R.string.preferences_export_locations_default_name); + return result; + } + + public static int importMensurations(Context context, String path) { + try { + return importMensurations(context, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return 0; + } + + public static int importMensurations(Context context, InputStream inputStream) { + MensurationRowModel mensurationRowModel = new MensurationRowModel(CSV_SEPARATOR); + int result = importData(context, mensurationRowModel, inputStream); + DataCache.invalidateMensurations(); + return result; + } + + public static int exportMensurations(Context context, String path) throws Exception { + Preconditions.checkNotNull(path); + MensurationRowModel mensurationRowModel = new MensurationRowModel(CSV_SEPARATOR); + int result = exportData(context, + mensurationRowModel, + DataCache.getAllMensurations(context), + path, + R.string.preferences_export_mensurations_default_name); + return result; + } + + public static int importMetiers(Context context, String path) { + try { + return importMetiers(context, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return 0; + } + + public static int importMetiers(Context context, InputStream inputStream) { + MetierRowModel metierRowModel = new MetierRowModel(CSV_SEPARATOR); + int result = importData(context, metierRowModel, inputStream); + DataCache.invalidateMetiers(); + return result; + } + + + public static int exportMetiers(Context context, String path) throws Exception { + Preconditions.checkNotNull(path); + MetierRowModel metierRowModel = new MetierRowModel(CSV_SEPARATOR); + int result = exportData(context, + metierRowModel, + DataCache.getAllMetiers(context), + path, + R.string.preferences_export_metiers_default_name); + return result; + } + + public static int importPresentations(Context context, String path) { + try { + return importPresentations(context, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return 0; + } + + public static int importPresentations(Context context, InputStream inputStream) { + PresentationRowModel presentationRowModel = new PresentationRowModel(CSV_SEPARATOR); + int result = importData(context, presentationRowModel, inputStream); + DataCache.invalidatePresentations(); + return result; + } + + + public static int exportPresentations(Context context, String path) throws Exception { + Preconditions.checkNotNull(path); + PresentationRowModel presentationRowModel = new PresentationRowModel(CSV_SEPARATOR); + int result = exportData(context, + presentationRowModel, + DataCache.getAllPresentations(context), + path, + R.string.preferences_export_presentations_default_name); + return result; + } + + public static int importScientificSpecies(Context context, String path) { + try { + return importScientificSpecies(context, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return 0; + } + + public static int importScientificSpecies(Context context, InputStream inputStream) { + ScientificSpeciesRowModel scientificSpeciesRowModel = new ScientificSpeciesRowModel(CSV_SEPARATOR); + int result = importData(context, scientificSpeciesRowModel, inputStream); + DataCache.invalidateScientificSpecies(); + return result; + } + + public static int exportScientificSpecies(Context context, String path) throws Exception { + Preconditions.checkNotNull(path); + ScientificSpeciesRowModel scientificSpeciesRowModel = new ScientificSpeciesRowModel(CSV_SEPARATOR); + int result = exportData(context, + scientificSpeciesRowModel, + DataCache.getAllScientificSpecies(context), + path, + R.string.preferences_export_scientific_species_default_name); + return result; + } + + public static int importStates(Context context, String path) { + try { + return importStates(context, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return 0; + } + + public static int importStates(Context context, InputStream inputStream) { + StateRowModel stateRowModel = new StateRowModel(CSV_SEPARATOR); + int result = importData(context, stateRowModel, inputStream); + DataCache.invalidateStates(); + return result; + } + + public static int exportStates(Context context, String path) throws Exception { + Preconditions.checkNotNull(path); + StateRowModel stateRowModel = new StateRowModel(CSV_SEPARATOR); + int result = exportData(context, + stateRowModel, + DataCache.getAllStates(context), + path, + R.string.preferences_export_states_default_name); + return result; + } + + public static int importVessels(Context context, String path) { + try { + return importVessels(context, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return 0; + } + + public static int importVessels(Context context, InputStream inputStream) { + VesselRowModel vesselRowModel = new VesselRowModel(CSV_SEPARATOR); + int result = importData(context, vesselRowModel, inputStream); + DataCache.invalidateVessels(); + return result; + } + + public static int exportVessels(Context context, String path) throws Exception { + Preconditions.checkNotNull(path); + VesselRowModel vesselRowModel = new VesselRowModel(CSV_SEPARATOR); + int result = exportData(context, + vesselRowModel, + DataCache.getAllVessels(context), + path, + R.string.preferences_export_vessels_default_name); + return result; + } + + public static int importCalcifiedPartTakings(Context context, String path) throws Exception { + try { + return importCalcifiedPartTakings(context, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return 0; + } + + public static int importCalcifiedPartTakings(Context context, InputStream inputStream) { + CalcifiedPartTakingRowModel calcifiedPartTakingRowModel = new CalcifiedPartTakingRowModel(CSV_SEPARATOR, DataCache.getAllScientificSpecies(context)); + Collection<CalcifiedPartTaking> data = importData(calcifiedPartTakingRowModel, inputStream); + + Multimap<String, Range<Integer>> ranges = HashMultimap.create(); + Map<String, Integer> sizeSteps = new HashMap<>(); + + for (CalcifiedPartTaking taking : data) { + // check if the species exists + if (taking.getParent() == null) { + throw new NullPointerException(context.getString(R.string.preferences_import_calcified_parts_takings_null_species)); + } + + String parentId = taking.getParentId(); + + // check if the steps are the same for the species (at least one step must be defined for each species) + Integer speciesSizeStep = sizeSteps.get(parentId); + Integer sizeStep = taking.getSizeStep(); + if (speciesSizeStep != null && sizeStep != null && !speciesSizeStep.equals(sizeStep)) { + throw new RuntimeException(context.getString(R.string.preferences_import_calcified_parts_different_size_steps)); + } + if (sizeStep != null) { + sizeSteps.put(parentId, sizeStep); + } + + // check if the intervals are not overlapping + Integer startSize = taking.getStartSize(); + if (startSize == null) { + startSize = 0; + } + Integer endSize = taking.getEndSize(); + if (endSize == null) { + endSize = Integer.MAX_VALUE; + } + Range<Integer> currentRange = Range.between(startSize, endSize); + for (Range<Integer> range : ranges.get(parentId)) { + if (range.isOverlappedBy(currentRange)) { + throw new RuntimeException(context.getString(R.string.preferences_import_calcified_parts_takings_overlapping)); + } + } + + ranges.put(parentId, currentRange); + } + + // check if every species has a size step + if (sizeSteps.containsValue(null)) { + throw new RuntimeException(context.getString(R.string.preferences_import_calcified_parts_no_size_step)); + } + + // check if there is no interval out of the ranges + Set<Range<Integer>> rangesForSpecies = new TreeSet<>(new Comparator<Range<Integer>>() { + + @Override + public int compare(Range<Integer> lhs, Range<Integer> rhs) { + return ObjectUtils.compare(lhs.getMinimum(), rhs.getMinimum()); + } + }); + for (String speciesId : ranges.keySet()) { + int sizeStep = sizeSteps.get(speciesId); + rangesForSpecies.clear(); + rangesForSpecies.addAll(ranges.get(speciesId)); + int n = 0; + for (Range<Integer> range : rangesForSpecies) { + if (range.getMinimum() - sizeStep > n) { + throw new RuntimeException(context.getString(R.string.preferences_import_calcified_parts_takings_value_out_of_ranges)); + } + n = range.getMaximum(); + } + if (n < Integer.MAX_VALUE) { + throw new RuntimeException(context.getString(R.string.preferences_import_calcified_parts_takings_value_out_of_ranges)); + } + } + + WloSqlOpenHelper soh = new WloSqlOpenHelper(context); + soh.saveData(data); + soh.close(); + + int result = data.size(); + return result; + } + + public static int exportCalcifiedPartTakings(Context context, String path) throws Exception { + Preconditions.checkNotNull(path); + CalcifiedPartTakingRowModel calcifiedPartTakingRowModel = new CalcifiedPartTakingRowModel(CSV_SEPARATOR, DataCache.getAllScientificSpecies(context)); + WloSqlOpenHelper wloSqlOpenHelper = new WloSqlOpenHelper(context); + int result = exportData(context, + calcifiedPartTakingRowModel, + WloSqlOpenHelper.transformCursorIntoCollection(wloSqlOpenHelper.getAllCalcifiedPartTakings(), + new Function<Cursor, CalcifiedPartTaking>() { + @Override + public CalcifiedPartTaking apply(Cursor input) { + return new CalcifiedPartTaking(input); + } + }), + path, + R.string.preferences_export_calcified_parts_takings_default_name); + wloSqlOpenHelper.close(); + return result; + } + + public static Collection<QualitativeValueModel> importQualitativeValues(CategoryModel categoryModel, String path) { + try { + return importQualitativeValues(categoryModel, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return null; + } + + public static Collection<QualitativeValueModel> importQualitativeValues(CategoryModel categoryModel, InputStream inputStream) { + QualitativeValueRowModel qualitativeValueRowModel = new QualitativeValueRowModel(CSV_SEPARATOR, categoryModel); + Collection<QualitativeValueModel> result = importData(qualitativeValueRowModel, inputStream); + return result; + } + + protected static <M extends BaseModel> int importData(Context context, ImportModel<M> importModel, + InputStream inputStream) { + + Collection<M> models = importData(importModel, inputStream); + + if (CollectionUtils.isNotEmpty(models)) { + WloSqlOpenHelper soh = new WloSqlOpenHelper(context); + soh.saveData(models); + soh.close(); + } + + return models.size(); + } + + protected static <M extends BaseModel> Collection<M> importData(ImportModel<M> importModel, InputStream inputStream) { + Import<M> importer = null; + Collection<M> models = null; + + 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(); + } + } + return models; + } + + protected static <M extends BaseModel> int exportData(Context context, + ExportModel<M> exportModel, + Collection<M> data, + String path, + int defaultFileName) throws Exception { + + int result = 0; + + File targetFile = new File(path); + if (targetFile.isDirectory()) { + String fileName = context.getString(defaultFileName) + + String.format("-%1$tY%1$tm%1$td-%1$tH%1$tM%1$tS", new Date()) + ".csv"; + targetFile = new File(targetFile, fileName); + } + + try { + Export<M> exporter = Export.newExport(exportModel, data); + exporter.write(targetFile); + result = data.size(); + + } 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); + throw e; + } + return result; + } +} Deleted: trunk/src/fr/ifremer/wlo/utils/ImportUtil.java =================================================================== --- trunk/src/fr/ifremer/wlo/utils/ImportUtil.java 2014-03-11 17:30:27 UTC (rev 75) +++ trunk/src/fr/ifremer/wlo/utils/ImportUtil.java 2014-03-17 16:56:27 UTC (rev 76) @@ -1,295 +0,0 @@ -package fr.ifremer.wlo.utils; - -/* - * #%L - * WLO - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2013 - 2014 Ifremer - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -import android.content.Context; -import android.os.AsyncTask; -import android.util.Log; -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.imports.CalcifiedPartTakingRowModel; -import fr.ifremer.wlo.imports.CommercialSpeciesRowModel; -import fr.ifremer.wlo.imports.LocationRowModel; -import fr.ifremer.wlo.imports.MensurationRowModel; -import fr.ifremer.wlo.imports.MetierRowModel; -import fr.ifremer.wlo.imports.PresentationRowModel; -import fr.ifremer.wlo.imports.QualitativeValueRowModel; -import fr.ifremer.wlo.imports.ScientificSpeciesRowModel; -import fr.ifremer.wlo.imports.StateRowModel; -import fr.ifremer.wlo.imports.VesselRowModel; -import fr.ifremer.wlo.models.BaseModel; -import fr.ifremer.wlo.models.categorization.CategoryModel; -import fr.ifremer.wlo.models.categorization.QualitativeValueModel; -import fr.ifremer.wlo.models.referentials.CalcifiedPartTaking; -import fr.ifremer.wlo.models.referentials.HasCode; -import fr.ifremer.wlo.storage.DataCache; -import fr.ifremer.wlo.storage.WloSqlOpenHelper; -import org.apache.commons.collections.CollectionUtils; -import org.nuiton.csv.Import; -import org.nuiton.csv.ImportModel; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; - -/** - * @author kmorin <kmorin@codelutin.com> - * @since 0.1 - */ -public class ImportUtil { - - private static final String TAG = "ImportUtil"; - - public static final char CSV_SEPARATOR = ';'; - - public static int importCommercialSpecies(Context context, String path) { - try { - return importCommercialSpecies(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importCommercialSpecies(Context context, InputStream inputStream) { - CommercialSpeciesRowModel commercialSpeciesRowModel = new CommercialSpeciesRowModel(CSV_SEPARATOR); - int result = importData(context, commercialSpeciesRowModel, inputStream, DataCache.getAllCommercialSpecies(context)); - DataCache.invalidateCommercialSpecies(); - return result; - } - - public static int importLocations(Context context, String path) { - try { - return importLocations(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importLocations(Context context, InputStream inputStream) { - LocationRowModel locationRowModel = new LocationRowModel(CSV_SEPARATOR); - int result = importData(context, locationRowModel, inputStream, DataCache.getAllLocations(context)); - DataCache.invalidateLocations(); - return result; - } - - public static int importMensurations(Context context, String path) { - try { - return importMensurations(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importMensurations(Context context, InputStream inputStream) { - MensurationRowModel mensurationRowModel = new MensurationRowModel(CSV_SEPARATOR); - int result = importData(context, mensurationRowModel, inputStream, DataCache.getAllMensurations(context)); - DataCache.invalidateMensurations(); - return result; - } - - public static int importMetiers(Context context, String path) { - try { - return importMetiers(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importMetiers(Context context, InputStream inputStream) { - MetierRowModel metierRowModel = new MetierRowModel(CSV_SEPARATOR); - int result = importData(context, metierRowModel, inputStream, DataCache.getAllMetiers(context)); - DataCache.invalidateMetiers(); - return result; - } - - public static int importPresentations(Context context, String path) { - try { - return importPresentations(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importPresentations(Context context, InputStream inputStream) { - PresentationRowModel presentationRowModel = new PresentationRowModel(CSV_SEPARATOR); - int result = importData(context, presentationRowModel, inputStream, DataCache.getAllPresentations(context)); - DataCache.invalidatePresentations(); - return result; - } - - public static int importScientificSpecies(Context context, String path) { - try { - return importScientificSpecies(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importScientificSpecies(Context context, InputStream inputStream) { - ScientificSpeciesRowModel scientificSpeciesRowModel = new ScientificSpeciesRowModel(CSV_SEPARATOR); - int result = importData(context, scientificSpeciesRowModel, inputStream, DataCache.getAllScientificSpecies(context)); - DataCache.invalidateScientificSpecies(); - return result; - } - - public static int importStates(Context context, String path) { - try { - return importStates(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importStates(Context context, InputStream inputStream) { - StateRowModel stateRowModel = new StateRowModel(CSV_SEPARATOR); - int result = importData(context, stateRowModel, inputStream, DataCache.getAllStates(context)); - DataCache.invalidateStates(); - return result; - } - - public static int importVessels(Context context, String path) { - try { - return importVessels(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importVessels(Context context, InputStream inputStream) { - VesselRowModel vesselRowModel = new VesselRowModel(CSV_SEPARATOR); - int result = importData(context, vesselRowModel, inputStream, DataCache.getAllVessels(context)); - DataCache.invalidateVessels(); - return result; - } - - public static int importCalcifiedPartTakings(Context context, String path) { - try { - return importCalcifiedPartTakings(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importCalcifiedPartTakings(Context context, InputStream inputStream) { - CalcifiedPartTakingRowModel calcifiedPartTakingRowModel = new CalcifiedPartTakingRowModel(CSV_SEPARATOR, DataCache.getAllScientificSpecies(context)); - int result = importData(context, calcifiedPartTakingRowModel, inputStream, new ArrayList<CalcifiedPartTaking>()); - return result; - } - - public static Collection<QualitativeValueModel> importQualitativeValues(CategoryModel categoryModel, String path) { - try { - return importQualitativeValues(categoryModel, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return null; - } - - public static Collection<QualitativeValueModel> importQualitativeValues(CategoryModel categoryModel, InputStream inputStream) { - QualitativeValueRowModel qualitativeValueRowModel = new QualitativeValueRowModel(CSV_SEPARATOR, categoryModel); - Collection<QualitativeValueModel> result = importData(qualitativeValueRowModel, inputStream, new ArrayList<QualitativeValueModel>()); - return result; - } - - protected static <M extends BaseModel> int importData(Context context, ImportModel<M> importModel, - InputStream inputStream, final Collection<M> actualReferential) { - - Collection<M> models = importData(importModel, inputStream, actualReferential); - - if (CollectionUtils.isNotEmpty(models)) { - Predicate<M> filter; - //if the models have a code attribute, check - M first = models.iterator().next(); - if (HasCode.class.isAssignableFrom(first.getClass())) { - Collection<HasCode> hasCodes = Collections2.transform(actualReferential, new Function<M, HasCode>() { - @Override - public HasCode apply(M m) { - return (HasCode) m; - } - }); - final Collection<String> codes = Collections2.transform(hasCodes, HasCode.GET_CODE_FUNCTION); - filter = new Predicate<M>() { - @Override - public boolean apply(M model) { - HasCode hasCode = (HasCode) model; - return !codes.contains(hasCode.getCode()); - } - }; - } else { - filter = new Predicate<M>() { - @Override - public boolean apply(M m) { - return !actualReferential.contains(m); - } - }; - } - - models = Collections2.filter(models, filter); - WloSqlOpenHelper soh = new WloSqlOpenHelper(context); - soh.saveData(models); - soh.close(); - } - - return models.size(); - } - - protected static <M extends BaseModel> Collection<M> importData(ImportModel<M> importModel, InputStream inputStream, - Collection<M> actualReferential) { - Preconditions.checkNotNull(actualReferential); - Import<M> importer = null; - Collection<M> models = null; - - 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(); - } - } - return models; - } -}