Author: kmorin Date: 2014-03-01 17:46:23 +0100 (Sat, 01 Mar 2014) New Revision: 57 Url: http://codelutin.com/projects/wlo/repository/revisions/57 Log: refs #4176 Import du param?\195?\169trage de l'algorithme de pr?\195?\169l?\195?\168vement des pi?\195?\168ces calcifi?\195?\169es fixes #4122 Gestion des pr?\195?\169l?\195?\168vements de pi?\195?\168ces calcifi?\195?\169es fixes #4093 Ajouter les cat?\195?\169gorisations au formulaire de cr?\195?\169ation de lieu fixes #4173 Ajouter les cat?\195?\169gorisations au formulaire de cr?\195?\169ation de lieu fixes #4187 Afficher les cat?\195?\169gorisations s?\195?\169lectionn?\195?\169es dans le lieu Added: trunk/res/layout/category_creation_form.xml trunk/src/fr/ifremer/wlo/imports/ trunk/src/fr/ifremer/wlo/imports/CalcifiedPartTakingRowModel.java trunk/src/fr/ifremer/wlo/imports/QualitativeValueRowModel.java trunk/src/fr/ifremer/wlo/models/categorization/ trunk/src/fr/ifremer/wlo/models/categorization/CategoryModel.java trunk/src/fr/ifremer/wlo/models/categorization/QualitativeValueModel.java trunk/src/fr/ifremer/wlo/models/referentials/CalcifiedPartTaking.java trunk/src/fr/ifremer/wlo/preferences/CategoryCreationAcivity.java Removed: trunk/src/fr/ifremer/wlo/imports/AgeRowModel.java trunk/src/fr/ifremer/wlo/imports/GenderRowModel.java trunk/src/fr/ifremer/wlo/imports/MaturityRowModel.java trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTaking.java trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTakingRowModel.java trunk/src/fr/ifremer/wlo/models/referentials/Age.java trunk/src/fr/ifremer/wlo/models/referentials/Gender.java trunk/src/fr/ifremer/wlo/models/referentials/Maturity.java trunk/src/fr/ifremer/wlo/models/referentials/imports/ Modified: trunk/AndroidManifest.xml trunk/assets/ref_import_ages.csv trunk/assets/ref_import_genders.csv trunk/assets/ref_import_maturities.csv trunk/pom.xml trunk/res/layout/commercial_species_form.xml trunk/res/layout/measurement.xml trunk/res/values-fr/arrays.xml trunk/res/values-fr/strings.xml trunk/res/values/strings.xml trunk/res/xml/preferences.xml trunk/src/fr/ifremer/wlo/CommercialSpeciesFormActivity.java trunk/src/fr/ifremer/wlo/MainActivity.java trunk/src/fr/ifremer/wlo/WeightsActivity.java trunk/src/fr/ifremer/wlo/imports/CommercialSpeciesRowModel.java trunk/src/fr/ifremer/wlo/imports/ImportUtil.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/measurement/LogsFragment.java trunk/src/fr/ifremer/wlo/measurement/MeasurementActivity.java trunk/src/fr/ifremer/wlo/models/BaseModel.java trunk/src/fr/ifremer/wlo/models/CommercialSpeciesModel.java trunk/src/fr/ifremer/wlo/models/MeasurementModel.java trunk/src/fr/ifremer/wlo/models/MeasurementsModel.java trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java trunk/src/fr/ifremer/wlo/storage/DataCache.java trunk/src/fr/ifremer/wlo/storage/Exporter.java trunk/src/fr/ifremer/wlo/storage/WloSqlOpenHelper.java Modified: trunk/AndroidManifest.xml =================================================================== --- trunk/AndroidManifest.xml 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/AndroidManifest.xml 2014-03-01 16:46:23 UTC (rev 57) @@ -117,6 +117,10 @@ android:label="@string/main_settings" android:configChanges="orientation|screenSize|keyboardHidden"/> + <activity android:name=".preferences.CategoryCreationAcivity" + android:label="@string/category_creation_title" + android:configChanges="orientation|screenSize|keyboardHidden"/> + <activity android:name=".utils.filechooser.FileDialog" android:configChanges="orientation|screenSize|keyboardHidden"/> Modified: trunk/assets/ref_import_ages.csv =================================================================== --- trunk/assets/ref_import_ages.csv 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/assets/ref_import_ages.csv 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -Age +Code 1 2 3 Modified: trunk/assets/ref_import_genders.csv =================================================================== --- trunk/assets/ref_import_genders.csv 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/assets/ref_import_genders.csv 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -Sexe_cod;Sexe_lib +Code;Libelle UNK;Indetermine M;Male F;Femelle Modified: trunk/assets/ref_import_maturities.csv =================================================================== --- trunk/assets/ref_import_maturities.csv 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/assets/ref_import_maturities.csv 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -Maturite +Code 1 2 3 Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/pom.xml 2014-03-01 16:46:23 UTC (rev 57) @@ -162,7 +162,6 @@ <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> - <version>${guavaVersion}</version> </dependency> <dependency> @@ -174,7 +173,7 @@ <dependency> <groupId>org.nuiton</groupId> <artifactId>nuiton-csv</artifactId> - <version>3.0-alpha-1</version> + <version>3.0-alpha-3</version> </dependency> <dependency> @@ -229,14 +228,6 @@ <checksumPolicy>fail</checksumPolicy> </releases> </pluginRepository> - <pluginRepository> - <id>oss.sonatype.org-jayway-snapshots</id> - <name>Jayway OpenSource SNAPSHOTs on Sonatype.org</name> - <url>http://oss.sonatype.org/content/repositories/jayway-snapshots/</url> - <snapshots> - <enabled>true</enabled> - </snapshots> - </pluginRepository> </pluginRepositories> <build> @@ -253,7 +244,7 @@ <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> - <version>3.8.3-SNAPSHOT</version> + <version>3.8.2</version> <executions> <execution> <id>alignApk</id> Added: trunk/res/layout/category_creation_form.xml =================================================================== --- trunk/res/layout/category_creation_form.xml (rev 0) +++ trunk/res/layout/category_creation_form.xml 2014-03-01 16:46:23 UTC (rev 57) @@ -0,0 +1,59 @@ +<?xml version="1.0" encoding="utf-8"?> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <ScrollView android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="1" + android:fillViewport="true"> + + <LinearLayout android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="wrap_content"> + + <TextView android:text="@string/category_label" + android:layout_width="match_parent" + android:layout_height="wrap_content"/> + + <EditText android:id="@+id/category_label" + android:layout_width="match_parent" + android:layout_height="wrap_content"/> + + <Button android:id="@+id/import_qualitative_values_button" + android:text="@string/import_qualitative_values" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:onClick="importQualitativeValues"/> + + <TextView android:id="@+id/qualitative_values_nb_label" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:visibility="gone"/> + + </LinearLayout> + + </ScrollView> + + <LinearLayout android:orientation="horizontal" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:gravity="bottom"> + + <Button android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="@android:string/cancel" + android:onClick="cancel" /> + + <Button android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="@android:string/ok" + android:onClick="validate" /> + + </LinearLayout> + +</LinearLayout> \ No newline at end of file Modified: trunk/res/layout/commercial_species_form.xml =================================================================== --- trunk/res/layout/commercial_species_form.xml 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/res/layout/commercial_species_form.xml 2014-03-01 16:46:23 UTC (rev 57) @@ -41,7 +41,6 @@ <Spinner android:id="@+id/commercial_species_form_precision" android:layout_width="match_parent" android:layout_height="wrap_content" - android:singleLine="true" android:hint="@string/undefined"/> <CheckBox android:id="@+id/commercial_species_form_species_mix" @@ -82,6 +81,33 @@ android:singleLine="true" android:hint="@string/undefined"/> + <TextView android:text="@string/commercial_species_form_category1" + android:layout_width="match_parent" + android:layout_height="wrap_content"/> + + <Spinner android:id="@+id/category1_spinner" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="@string/undefined"/> + + <TextView android:text="@string/commercial_species_form_category2" + android:layout_width="match_parent" + android:layout_height="wrap_content"/> + + <Spinner android:id="@+id/category2_spinner" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="@string/undefined"/> + + <TextView android:text="@string/commercial_species_form_category3" + android:layout_width="match_parent" + android:layout_height="wrap_content"/> + + <Spinner android:id="@+id/category3_spinner" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="@string/undefined"/> + <TextView android:text="@string/form_comment" android:layout_width="match_parent" android:layout_height="wrap_content"/> Modified: trunk/res/layout/measurement.xml =================================================================== --- trunk/res/layout/measurement.xml 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/res/layout/measurement.xml 2014-03-01 16:46:23 UTC (rev 57) @@ -17,42 +17,78 @@ android:layout_height="match_parent" android:orientation="vertical"> - <TextView android:text="@string/gender" - android:layout_width="wrap_content" + <TextView android:id="@+id/form_category1_label" + android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingLeft="10dp" android:paddingRight="5dp"/> - <Spinner android:id="@+id/gender_spinner" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_weight="1"/> + <LinearLayout android:id="@+id/category1_panel" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_weight="1"> - <TextView android:text="@string/maturity" - android:layout_width="wrap_content" + <Spinner android:id="@+id/category1_spinner" + android:layout_width="match_parent" + android:layout_height="match_parent"/> + + <EditText android:id="@+id/category1_text" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:singleLine="true" + android:inputType="number"/> + + </LinearLayout> + + <TextView android:id="@+id/form_category2_label" + android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingLeft="10dp" android:paddingRight="5dp"/> - <Spinner android:id="@+id/maturity_spinner" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_weight="1"/> + <LinearLayout android:id="@+id/category2_panel" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_weight="1"> - <TextView android:text="@string/age" - android:layout_width="wrap_content" + <Spinner android:id="@+id/category2_spinner" + android:layout_width="match_parent" + android:layout_height="match_parent"/> + + <EditText android:id="@+id/category2_text" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:singleLine="true" + android:inputType="number"/> + + </LinearLayout> + + <TextView android:id="@+id/form_category3_label" + android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingLeft="10dp" android:paddingRight="5dp"/> - <Spinner android:id="@+id/age_spinner" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_weight="1"/> + <LinearLayout android:id="@+id/category3_panel" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_weight="1"> + <Spinner android:id="@+id/category3_spinner" + android:layout_width="match_parent" + android:layout_height="match_parent"/> + + <EditText android:id="@+id/category3_text" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:singleLine="true" + android:inputType="number"/> + + </LinearLayout> + <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> @@ -72,14 +108,15 @@ android:layout_height="wrap_content" android:onClick="incSize"/> + <Button android:id="@+id/measurement_add_button" + android:text="@android:string/ok" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:onClick="addMeasurement" + android:layout_weight="1"/> + </LinearLayout> - <Button android:id="@+id/measurement_add_button" - android:text="@android:string/ok" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:onClick="addMeasurement"/> - </LinearLayout> <View android:layout_width="1px" Modified: trunk/res/values/strings.xml =================================================================== --- trunk/res/values/strings.xml 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/res/values/strings.xml 2014-03-01 16:46:23 UTC (rev 57) @@ -103,7 +103,8 @@ <string name="preferences_import_scientific_species">Import scientific species</string> <string name="preferences_import_states">Import states</string> <string name="preferences_import_vessels">Import vessels</string> - <string name="preferences_import_calcified_parts_takings">Import calcified parts taking algorithm settings</string> + <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_importing_referential">Importing referential data</string> @@ -114,6 +115,15 @@ <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="category_creation_title">New category</string> + <string name="category_label">Label</string> + <string name="import_qualitative_values">Import qualitative values</string> + <string name="qualitative_values_nb">%s qualitative values</string> + <string name="category_created">The category has been successfully created.</string> + <!-- Contexts --> <string name="add_context">Create a new context</string> <string name="contexts_title">Contexts</string> @@ -158,6 +168,9 @@ <string name="commercial_species_form_state">State</string> <string name="commercial_species_form_presentation">Presentation</string> <string name="commercial_species_form_sortCategory">Sort category</string> + <string name="commercial_species_form_category1">Category 1</string> + <string name="commercial_species_form_category2">Category 2</string> + <string name="commercial_species_form_category3">Category 3</string> <string name="commercial_species_title">Commercial species</string> <string name="commercial_species_subtitle">Metier %s</string> <string name="add_commercial_species">Create a new commercial species</string> Modified: trunk/res/values-fr/arrays.xml =================================================================== --- trunk/res/values-fr/arrays.xml 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/res/values-fr/arrays.xml 2014-03-01 16:46:23 UTC (rev 57) @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <resources> - <string-array name="preferences_weightunit_entries"> + <string-array name="preferences_weight_unit_entries"> <item>grammes (g)</item> <item>kilogrammes (kg)</item> </string-array> Modified: trunk/res/values-fr/strings.xml =================================================================== --- trunk/res/values-fr/strings.xml 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/res/values-fr/strings.xml 2014-03-01 16:46:23 UTC (rev 57) @@ -96,8 +96,8 @@ <string name="preferences_import_scientific_species">Importer des espèces scientifiques</string> <string name="preferences_import_states">Importer des états</string> <string name="preferences_import_vessels">Importer des navires</string> - <string name="preferences_import_calcified_parts_takings">Import des paramètres des prélèvements</string> - <string name="preferences_import_calcified_parts_takings_summary">des pièces calcifiées</string> + <string name="preferences_import_categories">Import des catégorisations</string> + <string name="preferences_import_no_external_storage_title">Stockage externe non trouvé</string> <string name="preferences_import_no_external_storage_message">Aucun stockage externe n\'est disponible.\nVoulez vous continuer sans sauvegarder le fichier d\'origine ?</string> <string name="preferences_importing_referential">Import des données du référentiel en cours</string> @@ -108,6 +108,15 @@ <string name="preferences_favorites_commercial_species">Espèces commerciales</string> <string name="preferences_favorites_scientific_species">Espèces scientifiques</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="category_creation_title">Nouvelle catégorie</string> + <string name="category_label">Libellé</string> + <string name="import_qualitative_values">Importer les valeurs possibles</string> + <string name="qualitative_values_nb">%s valeurs pour cette catégorie</string> + <string name="category_created">La catégorie a bien été créée.</string> + <!-- Contexts --> <string name="add_context">Créer un nouveau contexte</string> <string name="contexts_title">Contextes</string> @@ -152,6 +161,9 @@ <string name="commercial_species_form_state">État</string> <string name="commercial_species_form_presentation">Présentation</string> <string name="commercial_species_form_sortCategory">Catégorie de tri</string> + <string name="commercial_species_form_category1">Catégorie 1</string> + <string name="commercial_species_form_category2">Catégorie 2</string> + <string name="commercial_species_form_category3">Catégorie 3</string> <string name="commercial_species_title">Espèces commerciales</string> <string name="commercial_species_subtitle">Métier %s</string> <string name="add_commercial_species">Créer une nouvelle espèce commerciale</string> @@ -169,7 +181,7 @@ <!-- Weights --> <string name="weights_title">Poids</string> <string name="commercial_species_total_unloaded_weight">Poids total débarqué</string> - <string name="scientific_species_sample_weight">Poids trié</string> + <string name="scientific_species_sorted_weight">Poids trié</string> <string name="scientific_species_sample_weight">Poids d\'échantillon</string> <string name="scientific_species_categories">Poids par catégorie (Sexe / maturité / age)</string> Modified: trunk/res/xml/preferences.xml =================================================================== --- trunk/res/xml/preferences.xml 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/res/xml/preferences.xml 2014-03-01 16:46:23 UTC (rev 57) @@ -57,15 +57,8 @@ android:key="import_states"/> <Preference android:title="@string/preferences_import_mensurations" android:key="import_mensurations"/> - <Preference android:title="@string/preferences_import_ages" - android:key="import_ages"/> - <Preference android:title="@string/preferences_import_maturities" - android:key="import_maturities"/> - <Preference android:title="@string/preferences_import_genders" - android:key="import_genders"/> - <Preference android:title="@string/preferences_import_calcified_parts_takings" - android:summary="@string/preferences_import_calcified_parts_takings_summary" - android:key="import_calcified_parts_takings"/> + <Preference android:title="@string/preferences_import_categories" + android:key="import_categories"/> </PreferenceScreen> @@ -85,4 +78,13 @@ </PreferenceCategory> + <PreferenceCategory + android:title="@string/preferences_calcified_parts_takings" + android:key="preferences_calcified_parts_takings"> + + <Preference android:title="@string/preferences_import_calcified_parts_takings" + android:key="import_calcified_parts_takings"/> + + </PreferenceCategory> + </PreferenceScreen> \ No newline at end of file Modified: trunk/src/fr/ifremer/wlo/CommercialSpeciesFormActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/CommercialSpeciesFormActivity.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/CommercialSpeciesFormActivity.java 2014-03-01 16:46:23 UTC (rev 57) @@ -36,11 +36,14 @@ import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Spinner; +import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import fr.ifremer.wlo.models.BaseModel; import fr.ifremer.wlo.models.CommercialSpeciesModel; +import fr.ifremer.wlo.models.categorization.CategoryModel; import fr.ifremer.wlo.models.referentials.CommercialSpecies; import fr.ifremer.wlo.models.referentials.Mensuration; import fr.ifremer.wlo.models.referentials.Presentation; @@ -50,6 +53,7 @@ import fr.ifremer.wlo.storage.WloSqlOpenHelper; import fr.ifremer.wlo.utils.WloAutoCompleteTextViewWithFavorites; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; @@ -164,8 +168,65 @@ initEditText(R.id.form_comment, CommercialSpeciesModel.COLUMN_COMMENT); + initCategorySpinner((Spinner) findViewById(R.id.category1_spinner), + model.getCategory1(), + new Function<CategoryModel, Void>() { + @Override + public Void apply(CategoryModel input) { + model.setCategory1(input); + return null; + } + }); + initCategorySpinner((Spinner) findViewById(R.id.category2_spinner), + model.getCategory2(), + new Function<CategoryModel, Void>() { + @Override + public Void apply(CategoryModel input) { + model.setCategory2(input); + return null; + } + }); + initCategorySpinner((Spinner) findViewById(R.id.category3_spinner), + model.getCategory3(), + new Function<CategoryModel, Void>() { + @Override + public Void apply(CategoryModel input) { + model.setCategory3(input); + return null; + } + }); + } + protected void initCategorySpinner(Spinner spinner, + CategoryModel category, + final Function<CategoryModel, Void> setCategoryFunction) { + List<Object> categories = new ArrayList<Object>(DataCache.getAllCategories(this)); + categories.add(0, ""); + Log.d(TAG, categories.toString()); + ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, categories); + spinner.setAdapter(adapter); + spinner.setSelection(adapter.getPosition(category)); + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { + CategoryModel category; + if (position == 0) { + category = null; + } else { + category = (CategoryModel) parent.getItemAtPosition(position); + category = DataCache.getCategoryById(CommercialSpeciesFormActivity.this, category.getId()); + } + setCategoryFunction.apply(category); + } + + @Override + public void onNothingSelected(AdapterView<?> parent) { + setCategoryFunction.apply(null); + } + }); + } + @Override protected void saveModel() { super.saveModel(); Modified: trunk/src/fr/ifremer/wlo/MainActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/MainActivity.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/MainActivity.java 2014-03-01 16:46:23 UTC (rev 57) @@ -39,10 +39,12 @@ import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; -import fr.ifremer.wlo.models.referentials.imports.ImportUtil; +import fr.ifremer.wlo.imports.ImportUtil; +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.filechooser.FileDialog; import fr.ifremer.wlo.utils.filechooser.SelectionMode; import org.apache.commons.io.FileUtils; @@ -129,69 +131,83 @@ dialog.setIndeterminate(false); dialog.setCancelable(false); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - dialog.setMax(11); + dialog.setMax(9); dialog.setMessage(getString(R.string.main_loading_referential)); dialog.show(); new Thread(new Runnable() { @Override public void run() { - try { - if (DataCache.getAllAges(MainActivity.this).isEmpty()) { - ImportUtil.importAges(MainActivity.this, getAssets().open("ref_import_ages.csv")); - } - dialog.setProgress(1); - if (DataCache.getAllCommercialSpecies(MainActivity.this).isEmpty()) { - ImportUtil.importCommercialSpecies(MainActivity.this, getAssets().open("ref_import_commercial_species.csv")); - } - dialog.setProgress(2); - if (DataCache.getAllGenders(MainActivity.this).isEmpty()) { - ImportUtil.importGenders(MainActivity.this, getAssets().open("ref_import_genders.csv")); - } - dialog.setProgress(3); - if (DataCache.getAllLocations(MainActivity.this).isEmpty()) { - ImportUtil.importLocations(MainActivity.this, getAssets().open("ref_import_locations.csv")); - } - dialog.setProgress(4); - if (DataCache.getAllMaturities(MainActivity.this).isEmpty()) { - ImportUtil.importMaturities(MainActivity.this, getAssets().open("ref_import_maturities.csv")); - } - dialog.setProgress(5); - if (DataCache.getAllMensurations(MainActivity.this).isEmpty()) { - ImportUtil.importMensurations(MainActivity.this, getAssets().open("ref_import_mensurations.csv")); - } - dialog.setProgress(6); - if (DataCache.getAllMetiers(MainActivity.this).isEmpty()) { - ImportUtil.importMetiers(MainActivity.this, getAssets().open("ref_import_metiers.csv")); - } - dialog.setProgress(7); - if (DataCache.getAllPresentations(MainActivity.this).isEmpty()) { - ImportUtil.importPresentations(MainActivity.this, getAssets().open("ref_import_presentations.csv")); - } - dialog.setProgress(8); - if (DataCache.getAllScientificSpecies(MainActivity.this).isEmpty()) { - ImportUtil.importScientificSpecies(MainActivity.this, getAssets().open("ref_import_scientific_species.csv")); - } - dialog.setProgress(9); - if (DataCache.getAllStates(MainActivity.this).isEmpty()) { - ImportUtil.importStates(MainActivity.this, getAssets().open("ref_import_states.csv")); - } - dialog.setProgress(10); - if (DataCache.getAllVessels(MainActivity.this).isEmpty()) { - ImportUtil.importVessels(MainActivity.this, getAssets().open("ref_import_vessels.csv")); - } - dialog.setProgress(11); + initData(); + } + }).start(); - } catch (IOException | RuntimeException e) { - //TODO kmorin 20140130 show error to the user - Log.e(TAG, "error on initial import", e); + } - } finally { - dialog.dismiss(); - } + protected void initData() { + try { + if (DataCache.getAllCommercialSpecies(this).isEmpty()) { + ImportUtil.importCommercialSpecies(this, getAssets().open("ref_import_commercial_species.csv")); } - }).start(); + dialog.setProgress(1); + if (DataCache.getAllLocations(this).isEmpty()) { + ImportUtil.importLocations(this, getAssets().open("ref_import_locations.csv")); + } + dialog.setProgress(2); + if (DataCache.getAllMensurations(this).isEmpty()) { + ImportUtil.importMensurations(this, getAssets().open("ref_import_mensurations.csv")); + } + dialog.setProgress(3); + if (DataCache.getAllMetiers(this).isEmpty()) { + ImportUtil.importMetiers(this, getAssets().open("ref_import_metiers.csv")); + } + dialog.setProgress(4); + if (DataCache.getAllPresentations(this).isEmpty()) { + ImportUtil.importPresentations(this, getAssets().open("ref_import_presentations.csv")); + } + dialog.setProgress(5); + if (DataCache.getAllScientificSpecies(this).isEmpty()) { + ImportUtil.importScientificSpecies(this, getAssets().open("ref_import_scientific_species.csv")); + } + dialog.setProgress(6); + if (DataCache.getAllStates(this).isEmpty()) { + ImportUtil.importStates(this, getAssets().open("ref_import_states.csv")); + } + dialog.setProgress(7); + if (DataCache.getAllVessels(this).isEmpty()) { + ImportUtil.importVessels(this, getAssets().open("ref_import_vessels.csv")); + } + dialog.setProgress(8); + if (DataCache.getAllCategories(this).isEmpty()) { + WloSqlOpenHelper soh = new WloSqlOpenHelper(this); + CategoryModel ageCategory = new CategoryModel(); + ageCategory.setLabel(getString(R.string.age)); + soh.saveData(ageCategory); + + CategoryModel genderCategory = new CategoryModel(); + genderCategory.setLabel(getString(R.string.gender)); + soh.saveData(genderCategory); + + CategoryModel maturityCategory = new CategoryModel(); + 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.close(); + } + dialog.setProgress(9); + + } catch (IOException | RuntimeException e) { + //TODO kmorin 20140130 show error to the user + Log.e(TAG, "error on initial import", e); + + } finally { + dialog.dismiss(); + } } protected void bigfinConnected() { Modified: trunk/src/fr/ifremer/wlo/WeightsActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/WeightsActivity.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/WeightsActivity.java 2014-03-01 16:46:23 UTC (rev 57) @@ -38,9 +38,13 @@ import android.widget.LinearLayout; import android.widget.TextView; import com.google.common.base.Function; +import com.google.common.collect.Maps; +import fr.ifremer.wlo.models.BaseModel; import fr.ifremer.wlo.models.CategoryWeightModel; import fr.ifremer.wlo.models.CommercialSpeciesModel; import fr.ifremer.wlo.models.ScientificSpeciesModel; +import fr.ifremer.wlo.models.categorization.CategoryModel; +import fr.ifremer.wlo.models.categorization.QualitativeValueModel; import fr.ifremer.wlo.preferences.ListItemPreference; import fr.ifremer.wlo.preferences.StringPreference; import fr.ifremer.wlo.storage.DataCache; @@ -49,8 +53,10 @@ import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; /** * @author Kevin Morin (Code Lutin) @@ -70,6 +76,8 @@ protected List<CategoryWeightModel> categoryWeightModels = new ArrayList<>(); + protected Map<String, QualitativeValueModel> valuesById = new HashMap<>(); + protected int weightUnit; protected String weightUnitLabel; @@ -93,6 +101,20 @@ } commercialSpeciesModel = (CommercialSpeciesModel) getIntent().getSerializableExtra(INTENT_COMMERCIAL_SPECIES); + + CategoryModel category1 = commercialSpeciesModel.getCategory1(); + if (category1 != null) { + valuesById.putAll(Maps.uniqueIndex(category1.getQualitativeValues(), BaseModel.GET_ID_FUNCTION)); + } + CategoryModel category2 = commercialSpeciesModel.getCategory2(); + if (category2 != null) { + valuesById.putAll(Maps.uniqueIndex(category2.getQualitativeValues(), BaseModel.GET_ID_FUNCTION)); + } + CategoryModel category3 = commercialSpeciesModel.getCategory3(); + if (category3 != null) { + valuesById.putAll(Maps.uniqueIndex(category3.getQualitativeValues(), BaseModel.GET_ID_FUNCTION)); + } + getSupportActionBar().setSubtitle(commercialSpeciesModel.toString(this)); // create total unloaded weight label @@ -176,16 +198,21 @@ } }); + for (final CategoryWeightModel categoryWeightModel : categoryWeightModels) { - // create the weight field - String category1Id = categoryWeightModel.getCategory1(); - String category2Id = categoryWeightModel.getCategory2(); - String category3Id = categoryWeightModel.getCategory3(); - String label = (category1Id != null ? DataCache.getGenderById(this, category1Id).getLabel() : "NA") + ", " + - (category2Id != null ? DataCache.getMaturityById(this, category2Id).getLabel() : "NA") + ", " + - (category3Id != null ? DataCache.getAgeById(this, category3Id).getLabel() : "NA"); + QualitativeValueModel category1Value = valuesById.get(categoryWeightModel.getCategory1()); + QualitativeValueModel category2Value = valuesById.get(categoryWeightModel.getCategory2()); + QualitativeValueModel category3Value = valuesById.get(categoryWeightModel.getCategory3()); + String category1ValueLabel = category1Value != null ? category1Value.toString() : categoryWeightModel.getCategory1(); + String category2ValueLabel = category2Value != null ? category2Value.toString() : categoryWeightModel.getCategory2(); + String category3ValueLabel = category3Value != null ? category3Value.toString() : categoryWeightModel.getCategory3(); + + String label = (category1ValueLabel != null ? category1ValueLabel : "NA") + ", " + + (category2ValueLabel != null ? category2ValueLabel : "NA") + ", " + + (category3ValueLabel != null ? category3ValueLabel : "NA"); + // add label createWeightLabel(label); Deleted: trunk/src/fr/ifremer/wlo/imports/AgeRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/AgeRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/AgeRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,52 +0,0 @@ -package fr.ifremer.wlo.models.referentials.imports; - -/* - * #%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 fr.ifremer.wlo.models.referentials.Age; -import org.nuiton.csv.ValueSetter; -import org.nuiton.csv.ext.AbstractImportExportModel; - -/** - * @author kmorin <kmorin@codelutin.com> - * @since 0.1 - */ -public class AgeRowModel extends AbstractImportExportModel<Age> { - - public AgeRowModel(char separator) { - super(separator); - - newMandatoryColumn("Age", new ValueSetter<Age, String>() { - @Override - public void set(Age object, String value) throws Exception { - object.setLabel(value); - } - }); - } - - @Override - public Age newEmptyInstance() { - return new Age(); - } -} Copied: trunk/src/fr/ifremer/wlo/imports/CalcifiedPartTakingRowModel.java (from rev 56, trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTakingRowModel.java) =================================================================== --- trunk/src/fr/ifremer/wlo/imports/CalcifiedPartTakingRowModel.java (rev 0) +++ trunk/src/fr/ifremer/wlo/imports/CalcifiedPartTakingRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -0,0 +1,133 @@ +package fr.ifremer.wlo.imports; + +/* + * #%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.util.Log; +import com.google.common.base.Function; +import com.google.common.collect.Maps; +import fr.ifremer.wlo.models.referentials.CalcifiedPartTaking; +import fr.ifremer.wlo.models.referentials.ScientificSpecies; +import org.nuiton.csv.ValueSetter; +import org.nuiton.csv.ext.AbstractImportExportModel; + +import java.util.Collection; +import java.util.Map; + +/** + * @author kmorin <kmorin@codelutin.com> + * @since 0.1 + */ +public class CalcifiedPartTakingRowModel extends AbstractImportExportModel<CalcifiedPartTaking> { + + private static final String TAG = "CalcifiedPartTakingRowModel"; + + public CalcifiedPartTakingRowModel(char separator, Collection<ScientificSpecies> scientificSpecies) { + + super(separator); + + final Map<String, ScientificSpecies> scientificSpeciesByCode = Maps.uniqueIndex(scientificSpecies, new Function<ScientificSpecies, String>() { + @Override + public String apply(ScientificSpecies input) { + return input.getCode(); + } + }); + + newMandatoryColumn("Espece_scientifique", new ValueSetter<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"); + } + ScientificSpecies species = scientificSpeciesByCode.get(value); + object.setParent(species); + } + }); + + newMandatoryColumn("Classe_debut", new ValueSetter<CalcifiedPartTaking, String>() { + @Override + public void set(CalcifiedPartTaking object, String value) throws Exception { + Integer size; + try { + size = Integer.parseInt(value); + } catch (NumberFormatException e) { + size = null; + } + Log.d(TAG, object + "setStartSize " + size); + object.setStartSize(size); + } + }); + + newMandatoryColumn("Classe_fin", new ValueSetter<CalcifiedPartTaking, String>() { + @Override + public void set(CalcifiedPartTaking object, String value) throws Exception { + Integer size; + try { + size = Integer.parseInt(value); + } catch (NumberFormatException e) { + size = null; + } + Log.d(TAG, object + "setEndSize " + size); + object.setEndSize(size); + } + }); + + newMandatoryColumn("Pas_de_classe", new ValueSetter<CalcifiedPartTaking, String>() { + @Override + public void set(CalcifiedPartTaking object, String value) throws Exception { + Integer size = Integer.parseInt(value); + Log.d(TAG, object + "setSizeStep " + size); + object.setSizeStep(size); + } + }); + + newMandatoryColumn("Pas", new ValueSetter<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); + } + }); + + newMandatoryColumn("Arret", new ValueSetter<CalcifiedPartTaking, String>() { + @Override + public void set(CalcifiedPartTaking object, String value) throws Exception { + Integer size; + try { + size = Integer.parseInt(value); + } catch (NumberFormatException e) { + size = null; + } + Log.d(TAG, object + "setStop " + size); + object.setStop(size); + } + }); + } + + @Override + public CalcifiedPartTaking newEmptyInstance() { + return new CalcifiedPartTaking(); + } +} Modified: trunk/src/fr/ifremer/wlo/imports/CommercialSpeciesRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/CommercialSpeciesRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/CommercialSpeciesRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -package fr.ifremer.wlo.models.referentials.imports; +package fr.ifremer.wlo.imports; /* * #%L Deleted: trunk/src/fr/ifremer/wlo/imports/GenderRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/GenderRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/GenderRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,59 +0,0 @@ -package fr.ifremer.wlo.models.referentials.imports; - -/* - * #%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 fr.ifremer.wlo.models.referentials.Gender; -import org.nuiton.csv.ValueSetter; -import org.nuiton.csv.ext.AbstractImportExportModel; - -/** - * @author kmorin <kmorin@codelutin.com> - * @since 0.1 - */ -public class GenderRowModel extends AbstractImportExportModel<Gender> { - - - public GenderRowModel(char separator) { - super(separator); - - newMandatoryColumn("Sexe_cod", new ValueSetter<Gender, String>() { - @Override - public void set(Gender object, String value) throws Exception { - object.setCode(value); - } - }); - newMandatoryColumn("Sexe_lib", new ValueSetter<Gender, String>() { - @Override - public void set(Gender object, String value) throws Exception { - object.setLabel(value); - } - }); - } - - @Override - public Gender newEmptyInstance() { - return new Gender(); - } -} Modified: trunk/src/fr/ifremer/wlo/imports/ImportUtil.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/ImportUtil.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/ImportUtil.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -package fr.ifremer.wlo.models.referentials.imports; +package fr.ifremer.wlo.imports; /* * #%L @@ -24,33 +24,27 @@ * #L% */ -import android.app.AlertDialog; import android.content.Context; import android.util.Log; -import com.csvreader.CsvReader; 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.calcifiedPartTaking.CalcifiedPartTaking; -import fr.ifremer.wlo.models.calcifiedPartTaking.CalcifiedPartTakingRowModel; +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.io.IOUtils; import org.nuiton.csv.Import; import org.nuiton.csv.ImportModel; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.IOException; import java.io.InputStream; -import java.io.StringWriter; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; @@ -64,22 +58,6 @@ public static final char CSV_SEPARATOR = ';'; - public static int importAges(Context context, String path) { - try { - return importAges(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importAges(Context context, InputStream inputStream) { - AgeRowModel ageRowModel = new AgeRowModel(CSV_SEPARATOR); - int result = importData(context, ageRowModel, inputStream, DataCache.getAllAges(context)); - DataCache.invalidateAges(); - return result; - } - public static int importCommercialSpecies(Context context, String path) { try { return importCommercialSpecies(context, new FileInputStream(path)); @@ -96,22 +74,6 @@ return result; } - public static int importGenders(Context context, String path) { - try { - return importGenders(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importGenders(Context context, InputStream inputStream) { - GenderRowModel genderRowModel = new GenderRowModel(CSV_SEPARATOR); - int result = importData(context, genderRowModel, inputStream, DataCache.getAllGenders(context)); - DataCache.invalidateGenders(); - return result; - } - public static int importLocations(Context context, String path) { try { return importLocations(context, new FileInputStream(path)); @@ -128,22 +90,6 @@ return result; } - public static int importMaturities(Context context, String path) { - try { - return importMaturities(context, new FileInputStream(path)); - } catch (FileNotFoundException e) { - Log.e(TAG, "File " + path + " not found", e); - } - return 0; - } - - public static int importMaturities(Context context, InputStream inputStream) { - MaturityRowModel maturityRowModel = new MaturityRowModel(CSV_SEPARATOR); - int result = importData(context, maturityRowModel, inputStream, DataCache.getAllMaturities(context)); - DataCache.invalidateMaturities(); - return result; - } - public static int importMensurations(Context context, String path) { try { return importMensurations(context, new FileInputStream(path)); @@ -255,28 +201,26 @@ return result; } - protected static <M extends BaseModel> int importData(Context context, ImportModel<M> importModel, - InputStream inputStream, final Collection<M> actualReferential) { - Preconditions.checkNotNull(actualReferential); - Import<M> importer = null; - Collection<M> models = null; - + public static Collection<QualitativeValueModel> importQualitativeValues(CategoryModel categoryModel, String path) { try { - importer = Import.newImport(importModel, inputStream); - models = Lists.newArrayList(importer.iterator()); + return importQualitativeValues(categoryModel, new FileInputStream(path)); + } catch (FileNotFoundException e) { + Log.e(TAG, "File " + path + " not found", e); + } + return null; + } - } 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; + 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; + } - } finally { - if (importer != null) { - importer.close(); - } - } + 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 @@ -313,4 +257,28 @@ 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; + } } Modified: trunk/src/fr/ifremer/wlo/imports/LocationRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/LocationRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/LocationRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -package fr.ifremer.wlo.models.referentials.imports; +package fr.ifremer.wlo.imports; /* * #%L Deleted: trunk/src/fr/ifremer/wlo/imports/MaturityRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/MaturityRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/MaturityRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,53 +0,0 @@ -package fr.ifremer.wlo.models.referentials.imports; - -/* - * #%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 fr.ifremer.wlo.models.referentials.Maturity; -import org.nuiton.csv.ValueSetter; -import org.nuiton.csv.ext.AbstractImportExportModel; - -/** - * @author kmorin <kmorin@codelutin.com> - * @since 0.1 - */ -public class MaturityRowModel extends AbstractImportExportModel<Maturity> { - - - public MaturityRowModel(char separator) { - super(separator); - - newMandatoryColumn("Maturite", new ValueSetter<Maturity, String>() { - @Override - public void set(Maturity object, String value) throws Exception { - object.setLabel(value); - } - }); - } - - @Override - public Maturity newEmptyInstance() { - return new Maturity(); - } -} Modified: trunk/src/fr/ifremer/wlo/imports/MensurationRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/MensurationRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/MensurationRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -package fr.ifremer.wlo.models.referentials.imports; +package fr.ifremer.wlo.imports; /* * #%L Modified: trunk/src/fr/ifremer/wlo/imports/MetierRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/MetierRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/MetierRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -package fr.ifremer.wlo.models.referentials.imports; +package fr.ifremer.wlo.imports; /* * #%L Modified: trunk/src/fr/ifremer/wlo/imports/PresentationRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/PresentationRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/PresentationRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -package fr.ifremer.wlo.models.referentials.imports; +package fr.ifremer.wlo.imports; /* * #%L Added: trunk/src/fr/ifremer/wlo/imports/QualitativeValueRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/imports/QualitativeValueRowModel.java (rev 0) +++ trunk/src/fr/ifremer/wlo/imports/QualitativeValueRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -0,0 +1,41 @@ +package fr.ifremer.wlo.imports; + +import fr.ifremer.wlo.models.categorization.CategoryModel; +import fr.ifremer.wlo.models.categorization.QualitativeValueModel; +import org.nuiton.csv.ValueSetter; +import org.nuiton.csv.ext.AbstractImportExportModel; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class QualitativeValueRowModel extends AbstractImportExportModel<QualitativeValueModel> { + + protected CategoryModel categoryModel; + + public QualitativeValueRowModel(char separator, CategoryModel category) { + super(separator); + + categoryModel = category; + + newMandatoryColumn("Code", new ValueSetter<QualitativeValueModel, String>() { + @Override + public void set(QualitativeValueModel object, String value) throws Exception { + object.setValue(value); + } + }); + newOptionalColumn("Libelle", new ValueSetter<QualitativeValueModel, String>() { + @Override + public void set(QualitativeValueModel object, String value) throws Exception { + object.setLabel(value); + } + }); + } + + @Override + public QualitativeValueModel newEmptyInstance() { + QualitativeValueModel qualitativeValueModel = new QualitativeValueModel(); + qualitativeValueModel.setCategory(categoryModel); + return qualitativeValueModel; + } +} Modified: trunk/src/fr/ifremer/wlo/imports/ScientificSpeciesRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/ScientificSpeciesRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/ScientificSpeciesRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -package fr.ifremer.wlo.models.referentials.imports; +package fr.ifremer.wlo.imports; /* * #%L Modified: trunk/src/fr/ifremer/wlo/imports/StateRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/StateRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/StateRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -package fr.ifremer.wlo.models.referentials.imports; +package fr.ifremer.wlo.imports; /* * #%L Modified: trunk/src/fr/ifremer/wlo/imports/VesselRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/imports/VesselRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/imports/VesselRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,4 +1,4 @@ -package fr.ifremer.wlo.models.referentials.imports; +package fr.ifremer.wlo.imports; /* * #%L Modified: trunk/src/fr/ifremer/wlo/measurement/LogsFragment.java =================================================================== --- trunk/src/fr/ifremer/wlo/measurement/LogsFragment.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/measurement/LogsFragment.java 2014-03-01 16:46:23 UTC (rev 57) @@ -80,19 +80,33 @@ public String apply(MeasurementModel measurement) { Context context = getActivity(); String result = measurement.toString(context,measurements.getPrecision()) + "\n"; + List<Object> cats = Lists.newArrayList(); - List<Object> cats = Lists.newArrayList(); - Object cat1 = DataCache.getGenderById(context, measurement.getCategory1()); - if (cat1 != null) { - cats.add(cat1); + String category1 = measurement.getCategory1(); + if (category1 != null) { + Object category1Value = measurements.getCategoryValuesById(category1); + if (category1Value == null) { + category1Value = category1; + } + cats.add(category1Value); } - Object cat2 = DataCache.getMaturityById(context, measurement.getCategory2()); - if (cat2 != null) { - cats.add(cat2); + + String category2 = measurement.getCategory2(); + if (category2 != null) { + Object category2Value = measurements.getCategoryValuesById(category2); + if (category2Value == null) { + category2Value = category2; + } + cats.add(category2Value); } - Object cat3 = DataCache.getAgeById(context, measurement.getCategory3()); - if (cat3 != null) { - cats.add(cat3); + + String category3 = measurement.getCategory3(); + if (category3 != null) { + Object category3Value = measurements.getCategoryValuesById(category3); + if (category3Value == null) { + category3Value = category3; + } + cats.add(category3Value); } result += StringUtils.join(cats, " - "); return result; Modified: trunk/src/fr/ifremer/wlo/measurement/MeasurementActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/measurement/MeasurementActivity.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/measurement/MeasurementActivity.java 2014-03-01 16:46:23 UTC (rev 57) @@ -26,7 +26,6 @@ import android.app.ActionBar; import android.app.AlertDialog; -import android.content.DialogInterface; import android.content.Intent; import android.content.res.Configuration; import android.database.Cursor; @@ -50,6 +49,7 @@ import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.ListView; import android.widget.Spinner; import android.widget.TabHost; @@ -67,9 +67,10 @@ import fr.ifremer.wlo.WloBaseActivity; import fr.ifremer.wlo.WloModelEditionActivity; import fr.ifremer.wlo.models.CategoryWeightModel; -import fr.ifremer.wlo.models.calcifiedPartTaking.CalcifiedPartTaking; +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.Mensuration; -import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.models.BaseModel; import fr.ifremer.wlo.models.CommercialSpeciesModel; import fr.ifremer.wlo.models.MeasurementModel; @@ -81,6 +82,7 @@ import fr.ifremer.wlo.utils.BaseTextWatcher; import fr.ifremer.wlo.utils.BigFinFeedReaderRecord; import fr.ifremer.wlo.utils.UIUtils; +import org.apache.commons.collections4.CollectionUtils; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -235,27 +237,8 @@ observedNumberText = (TextView) findViewById(R.id.observed_number); observedNumberText.setText(String.valueOf(measurements.getMeasurements().size())); - Spinner genderSpinner = (Spinner) findViewById(R.id.gender_spinner); - genderSpinner.setOnItemSelectedListener(this); - List genders = Lists.newArrayList(DataCache.getAllGenders(this)); - genders.add(0, ""); - ArrayAdapter gendersAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, genders); - genderSpinner.setAdapter(gendersAdapter); + initCategories(); - Spinner maturitySpinner = (Spinner) findViewById(R.id.maturity_spinner); - maturitySpinner.setOnItemSelectedListener(this); - List maturities = Lists.newArrayList(DataCache.getAllMaturities(this)); - maturities.add(0, ""); - ArrayAdapter maturitiesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, maturities); - maturitySpinner.setAdapter(maturitiesAdapter); - - Spinner ageSpinner = (Spinner) findViewById(R.id.age_spinner); - ageSpinner.setOnItemSelectedListener(this); - List ages = Lists.newArrayList(DataCache.getAllAges(this)); - ages.add(0, ""); - ArrayAdapter agesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, ages); - ageSpinner.setAdapter(agesAdapter); - initFishMeasurement(10 * measurements.getPrecision().getUnitDivider(), null, null, null); DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); @@ -296,7 +279,7 @@ if (record != null && record.getLength() != null) { - int size = Math.round(record.getLength()); + int size = (int) Math.floor(record.getLength()); measurement.setSize(size); MeasurementModel savedMeasurement = addMeasurement(); @@ -324,6 +307,72 @@ startActivityForResult(checkIntent, CHECK_TTS_REQUEST_CODE); } + protected void initCategories() { + CategoryModel category1 = commercialSpecies.getCategory1(); + TextView category1Label = (TextView) findViewById(R.id.form_category1_label); + LinearLayout category1Editors = (LinearLayout) findViewById(R.id.category1_panel); + Spinner category1Spinner = (Spinner) findViewById(R.id.category1_spinner); + EditText category1Text = (EditText) findViewById(R.id.category1_text); + initCategoryInput(category1, category1Label, category1Editors, category1Spinner, category1Text); + + CategoryModel category2 = commercialSpecies.getCategory2(); + TextView category2Label = (TextView) findViewById(R.id.form_category2_label); + LinearLayout category2Editors = (LinearLayout) findViewById(R.id.category2_panel); + Spinner category2Spinner = (Spinner) findViewById(R.id.category2_spinner); + EditText category2Text = (EditText) findViewById(R.id.category2_text); + initCategoryInput(category2, category2Label, category2Editors, category2Spinner, category2Text); + + CategoryModel category3 = commercialSpecies.getCategory3(); + TextView category3Label = (TextView) findViewById(R.id.form_category3_label); + LinearLayout category3Editors = (LinearLayout) findViewById(R.id.category3_panel); + Spinner category3Spinner = (Spinner) findViewById(R.id.category3_spinner); + EditText category3Text = (EditText) findViewById(R.id.category3_text); + initCategoryInput(category3, category3Label, category3Editors, category3Spinner, category3Text); + } + + protected void initCategoryInput(CategoryModel category, TextView categoryLabel, LinearLayout categoryEditorPanel, + Spinner categorySpinner, final EditText categoryText) { + if (category == null) { + categoryLabel.setVisibility(View.INVISIBLE); + categoryEditorPanel.setVisibility(View.INVISIBLE); + + } else { + categoryLabel.setText(category.getLabel()); + List values = Lists.newArrayList(category.getQualitativeValues()); + if (values.isEmpty()) { + categorySpinner.setVisibility(View.GONE); + categoryText.addTextChangedListener(new BaseTextWatcher() { + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + categoryText.setSelection(start + count); + } + + @Override + public void afterTextChanged(Editable s) { + switch(categoryText.getId()) { + case R.id.category1_text: + measurement.setCategory1(String.valueOf(s)); + break; + case R.id.category2_text: + measurement.setCategory2(String.valueOf(s)); + break; + case R.id.category3_text: + measurement.setCategory3(String.valueOf(s)); + break; + } + } + }); + + } else { + categoryText.setVisibility(View.GONE); + categorySpinner.setOnItemSelectedListener(this); + measurements.addCategoryValues(values); + ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, values); + categorySpinner.setAdapter(adapter); + } + } + } + @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); @@ -353,6 +402,7 @@ case 3: commercialSpecies = (CommercialSpeciesModel) data.getSerializableExtra(WloModelEditionActivity.INTENT_EXTRA_MODEL); measurements.setPrecision(commercialSpecies.getPrecision()); + initCategories(); break; case 4: scientificSpecies = (ScientificSpeciesModel) data.getSerializableExtra(WloModelEditionActivity.INTENT_EXTRA_MODEL); @@ -390,21 +440,16 @@ @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { - String id; - if (i == 0) { - id = null; - } else { - BaseModel selected = (BaseModel) adapterView.getItemAtPosition(i); - id = selected != null ? selected.getId() : null; - } + BaseModel selected = (BaseModel) adapterView.getItemAtPosition(i); + String id = selected != null ? selected.getId() : null; switch(adapterView.getId()) { - case R.id.gender_spinner: + case R.id.category1_spinner: measurement.setCategory1(id); break; - case R.id.maturity_spinner: + case R.id.category2_spinner: measurement.setCategory2(id); break; - case R.id.age_spinner: + case R.id.category3_spinner: measurement.setCategory3(id); break; } @@ -413,13 +458,13 @@ @Override public void onNothingSelected(AdapterView<?> adapterView) { switch(adapterView.getId()) { - case R.id.gender_spinner: + case R.id.category1_spinner: measurement.setCategory1(null); break; - case R.id.maturity_spinner: + case R.id.category2_spinner: measurement.setCategory2(null); break; - case R.id.age_spinner: + case R.id.category3_spinner: measurement.setCategory3(null); break; } @@ -516,29 +561,30 @@ break; case MeasurementModel.COLUMN_CATEGORY_1: - Spinner spinner = (Spinner) findViewById(R.id.gender_spinner); - ArrayAdapter adapter = (ArrayAdapter) spinner.getAdapter(); - Object o = DataCache.getGenderById(MeasurementActivity.this, (String) newValue); - int position = adapter.getPosition(o); - spinner.setSelection(position); + CategoryModel category = commercialSpecies.getCategory1(); + Spinner spinner = (Spinner) findViewById(R.id.category1_spinner); + setCategorySpinnerSelection(newValue, category, spinner); + + EditText editText = (EditText) findViewById(R.id.category1_text); + editText.setText(String.valueOf(newValue)); break; case MeasurementModel.COLUMN_CATEGORY_2: - newValue = event.getNewValue(); - spinner = (Spinner) findViewById(R.id.maturity_spinner); - adapter = (ArrayAdapter) spinner.getAdapter(); - o = DataCache.getMaturityById(MeasurementActivity.this, (String) newValue); - position = adapter.getPosition(o); - spinner.setSelection(position); + category = commercialSpecies.getCategory2(); + spinner = (Spinner) findViewById(R.id.category2_spinner); + setCategorySpinnerSelection(newValue, category, spinner); + + editText = (EditText) findViewById(R.id.category2_text); + editText.setText(String.valueOf(newValue)); break; case MeasurementModel.COLUMN_CATEGORY_3: - newValue = event.getNewValue(); - spinner = (Spinner) findViewById(R.id.age_spinner); - adapter = (ArrayAdapter) spinner.getAdapter(); - o = DataCache.getAgeById(MeasurementActivity.this, (String) newValue); - position = adapter.getPosition(o); - spinner.setSelection(position); + category = commercialSpecies.getCategory3(); + spinner = (Spinner) findViewById(R.id.category3_spinner); + setCategorySpinnerSelection(newValue, category, spinner); + + editText = (EditText) findViewById(R.id.category3_text); + editText.setText(String.valueOf(newValue)); break; } } @@ -550,6 +596,19 @@ measurement.setParent(scientificSpecies); } + protected void setCategorySpinnerSelection(Object newValue, CategoryModel category, Spinner spinner) { + List<QualitativeValueModel> qualitativeValues = category.getQualitativeValues(); + if (category != null && CollectionUtils.isNotEmpty(qualitativeValues)) { + ArrayAdapter adapter = (ArrayAdapter) spinner.getAdapter(); + QualitativeValueModel value = null; + if (newValue != null) { + value = measurements.getCategoryValuesById(newValue.toString()); + } + int position = adapter.getPosition(value); + spinner.setSelection(position); + } + } + protected void setupTab(int contentId, String tag, int label) { View tabview = LayoutInflater.from(this).inflate(R.layout.tabs_bg, null); TextView tv = (TextView) tabview.findViewById(R.id.tabsText); Modified: trunk/src/fr/ifremer/wlo/models/BaseModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/BaseModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/models/BaseModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -208,4 +208,24 @@ } } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + BaseModel baseModel = (BaseModel) o; + if (id != null) { + return id.equals(baseModel.id); + } + return super.equals(o); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : super.hashCode(); + } } Modified: trunk/src/fr/ifremer/wlo/models/CommercialSpeciesModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/CommercialSpeciesModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/models/CommercialSpeciesModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -28,6 +28,7 @@ import android.content.Context; import android.database.Cursor; import android.util.Log; +import fr.ifremer.wlo.models.categorization.CategoryModel; import fr.ifremer.wlo.models.referentials.CommercialSpecies; import fr.ifremer.wlo.models.referentials.Mensuration; import fr.ifremer.wlo.models.referentials.Presentation; @@ -53,6 +54,9 @@ public static final String COLUMN_SORT_CATEGORY = "sortCategory"; public static final String COLUMN_STATE = "state"; public static final String COLUMN_PRESENTATION = "presentation"; + public static final String COLUMN_CATEGORY1 = "category1"; + public static final String COLUMN_CATEGORY2 = "category2"; + public static final String COLUMN_CATEGORY3 = "category3"; public static final String COLUMN_COMMENT = "comment"; public static final String COLUMN_TOTAL_UNLOADED_WEIGHT = "totalUnloadedWeight"; public static final String COLUMN_METIER_ID = "metierId"; @@ -65,6 +69,9 @@ COLUMN_SORT_CATEGORY, COLUMN_STATE, COLUMN_PRESENTATION, + COLUMN_CATEGORY1, + COLUMN_CATEGORY2, + COLUMN_CATEGORY3, COLUMN_COMMENT, COLUMN_TOTAL_UNLOADED_WEIGHT, COLUMN_METIER_ID @@ -77,6 +84,9 @@ protected String sortCategory; protected State state; protected Presentation presentation; + protected CategoryModel category1; + protected CategoryModel category2; + protected CategoryModel category3; protected String comment; protected Integer totalUnloadedWeight; @@ -98,9 +108,15 @@ state = DataCache.getStateById(context, stateId); String presentationId = cursor.getString(7); presentation = DataCache.getPresentationById(context, presentationId); - comment = cursor.getString(8); - if (!cursor.isNull(9)) { - totalUnloadedWeight = cursor.getInt(9); + String category1Id = cursor.getString(8); + category1 = DataCache.getCategoryById(context, category1Id); + String category2Id = cursor.getString(9); + category2 = DataCache.getCategoryById(context, category2Id); + String category3Id = cursor.getString(10); + category3 = DataCache.getCategoryById(context, category3Id); + comment = cursor.getString(11); + if (!cursor.isNull(12)) { + totalUnloadedWeight = cursor.getInt(12); } } @@ -175,6 +191,30 @@ firePropertyChange(_ID, oldValue, id); } + public CategoryModel getCategory1() { + return category1; + } + + public void setCategory1(CategoryModel category1) { + this.category1 = category1; + } + + public CategoryModel getCategory2() { + return category2; + } + + public void setCategory2(CategoryModel category2) { + this.category2 = category2; + } + + public CategoryModel getCategory3() { + return category3; + } + + public void setCategory3(CategoryModel category3) { + this.category3 = category3; + } + public String getComment() { return comment; } @@ -215,6 +255,9 @@ putValue(value, COLUMN_SORT_CATEGORY, sortCategory); putValue(value, COLUMN_STATE, state != null ? state.getId() : null); putValue(value, COLUMN_PRESENTATION, presentation != null ? presentation.getId() : null); + putValue(value, COLUMN_CATEGORY1, category1 != null ? category1.getId() : null); + putValue(value, COLUMN_CATEGORY2, category2 != null ? category2.getId() : null); + putValue(value, COLUMN_CATEGORY3, category3 != null ? category3.getId() : null); putValue(value, COLUMN_COMMENT, comment); putValue(value, COLUMN_TOTAL_UNLOADED_WEIGHT, totalUnloadedWeight); putValue(value, COLUMN_METIER_ID, getParentId()); Modified: trunk/src/fr/ifremer/wlo/models/MeasurementModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/MeasurementModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/models/MeasurementModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -27,15 +27,9 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; -import android.util.Log; import fr.ifremer.wlo.R; -import fr.ifremer.wlo.models.referentials.Age; -import fr.ifremer.wlo.models.referentials.Gender; -import fr.ifremer.wlo.models.referentials.Maturity; import fr.ifremer.wlo.models.referentials.Mensuration; -import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.utils.UIUtils; -import org.apache.commons.lang3.ObjectUtils; import java.util.Calendar; Modified: trunk/src/fr/ifremer/wlo/models/MeasurementsModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/MeasurementsModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/models/MeasurementsModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -24,16 +24,21 @@ * #L% */ -import android.util.Log; import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.google.common.collect.Multimap; -import fr.ifremer.wlo.models.calcifiedPartTaking.CalcifiedPartTaking; +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.Mensuration; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author kmorin <kmorin@codelutin.com> @@ -51,6 +56,8 @@ protected List<CalcifiedPartTaking> calcifiedPartTakings; + protected Map<String, QualitativeValueModel> categoryValuesById = new HashMap<>(); + transient protected List<MeasurementsListener> listeners = new ArrayList<MeasurementsListener>(); public ScientificSpeciesModel getScientificSpecies() { @@ -87,31 +94,24 @@ fireMeasurementAdded(fishMeasurement); boolean result = false; - Log.d(TAG, "calcifiedPartTakings : " + calcifiedPartTakings); if (calcifiedPartTakings != null) { CalcifiedPartTaking calcifiedPartTaking = null; for (CalcifiedPartTaking cpt : calcifiedPartTakings) { - Log.d(TAG, "size : " + size); Integer startSize = cpt.getStartSize(); - Log.d(TAG, "startSize : " + startSize); Integer endSize = cpt.getEndSize(); - Log.d(TAG, "endSize : " + endSize); if ((startSize == null || startSize <= size) && (endSize == null || endSize >= size)) { calcifiedPartTaking = cpt; break; } } - Log.d(TAG, "calcifiedPartTaking : " + calcifiedPartTaking); if (calcifiedPartTaking != null) { int nb = measurements.get(size).size(); - Log.d(TAG, "nb : " + nb); int step = calcifiedPartTaking.getStep(); - Log.d(TAG, "step : " + step); Integer stop = calcifiedPartTaking.getStop(); - Log.d(TAG, "stop : " + stop); - Log.d(TAG, "nb % step : " + (nb % step)); - result = nb == 1 || (nb % step == 0 && (stop == null || nb <= stop)); + boolean stepOk = (nb - 1) % step == 0; + boolean stopOk = stop == null || (nb - 1) / step < stop; + result = nb == 1 || stepOk && stopOk; } } @@ -132,6 +132,16 @@ this.calcifiedPartTakings = calcifiedPartTakings; } + public QualitativeValueModel getCategoryValuesById(String id) { + return categoryValuesById.get(id); + } + + public void addCategoryValues(Collection<QualitativeValueModel> categoryValues) { + if (categoryValues != null) { + categoryValuesById.putAll(Maps.uniqueIndex(categoryValues, BaseModel.GET_ID_FUNCTION)); + } + } + public void addMeasurementsListener(MeasurementsListener listener) { ensureListeners(); listeners.add(listener); Deleted: trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTaking.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTaking.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTaking.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,142 +0,0 @@ -package fr.ifremer.wlo.models.calcifiedPartTaking; - -/* - * #%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.ContentValues; -import android.database.Cursor; -import android.util.Log; -import fr.ifremer.wlo.models.BaseModel; -import fr.ifremer.wlo.models.HierarchicalModel; -import fr.ifremer.wlo.models.referentials.ScientificSpecies; - -/** - * @author Kevin Morin (Code Lutin) - * @since x.x - */ -public class CalcifiedPartTaking extends HierarchicalModel<ScientificSpecies> { - - private static final String TAG = "CalcifiedPartTaking"; - - public static final String TABLE_NAME = "calcifiedPartTaking"; - public static final String COLUMN_START_SIZE = "startSize"; - public static final String COLUMN_END_SIZE = "endSize"; - public static final String COLUMN_SIZE_STEP = "sizeStep"; - public static final String COLUMN_STEP = "step"; - public static final String COLUMN_STOP = "stop"; - public static final String COLUMN_SCIENTIFIC_SPECIES_ID = "scientificSpeciesId"; - public static final String[] ALL_COLUMNS = new String[] { - _ID, - COLUMN_START_SIZE, - COLUMN_END_SIZE, - COLUMN_SIZE_STEP, - COLUMN_STEP, - COLUMN_STOP, - COLUMN_SCIENTIFIC_SPECIES_ID - }; - - @Override - public String getTableName() { - return TABLE_NAME; - } - - protected Integer startSize; - protected Integer endSize; - protected int sizeStep; - protected int step; - protected Integer stop; - - public CalcifiedPartTaking() { - } - - public CalcifiedPartTaking(Cursor cursor) { - super(cursor); - if (!cursor.isNull(1)) { - startSize = cursor.getInt(1); - } - if (!cursor.isNull(2)) { - endSize = cursor.getInt(2); - } - if (!cursor.isNull(3)) { - sizeStep = cursor.getInt(3); - } - step = cursor.getInt(4); - if (!cursor.isNull(5)) { - stop = cursor.getInt(5); - } - Log.d(TAG, startSize + " " + endSize + " " + sizeStep + " " + step + " " + stop); - } - - public Integer getStartSize() { - return startSize; - } - - public void setStartSize(Integer startSize) { - this.startSize = startSize; - } - - public Integer getEndSize() { - return endSize; - } - - public void setEndSize(Integer endSize) { - this.endSize = endSize; - } - - public int getSizeStep() { - return sizeStep; - } - - public void setSizeStep(int sizeStep) { - this.sizeStep = sizeStep; - } - - public int getStep() { - return step; - } - - public void setStep(int step) { - this.step = step; - } - - public Integer getStop() { - return stop; - } - - public void setStop(Integer stop) { - this.stop = stop; - } - - @Override - public ContentValues convertIntoContentValues() { - ContentValues value = super.convertIntoContentValues(); - putValue(value, COLUMN_START_SIZE, startSize); - putValue(value, COLUMN_END_SIZE, endSize); - putValue(value, COLUMN_SIZE_STEP, sizeStep); - putValue(value, COLUMN_STEP, step); - putValue(value, COLUMN_STOP, stop); - putValue(value, COLUMN_SCIENTIFIC_SPECIES_ID, getParentId()); - return value; - } -} \ No newline at end of file Deleted: trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTakingRowModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTakingRowModel.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTakingRowModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,132 +0,0 @@ -package fr.ifremer.wlo.models.calcifiedPartTaking; - -/* - * #%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.util.Log; -import com.google.common.base.Function; -import com.google.common.collect.Maps; -import fr.ifremer.wlo.models.referentials.ScientificSpecies; -import org.nuiton.csv.ValueSetter; -import org.nuiton.csv.ext.AbstractImportExportModel; - -import java.util.Collection; -import java.util.Map; - -/** - * @author kmorin <kmorin@codelutin.com> - * @since 0.1 - */ -public class CalcifiedPartTakingRowModel extends AbstractImportExportModel<CalcifiedPartTaking> { - - private static final String TAG = "CalcifiedPartTakingRowModel"; - - public CalcifiedPartTakingRowModel(char separator, Collection<ScientificSpecies> scientificSpecies) { - - super(separator); - - final Map<String, ScientificSpecies> scientificSpeciesByCode = Maps.uniqueIndex(scientificSpecies, new Function<ScientificSpecies, String>() { - @Override - public String apply(ScientificSpecies input) { - return input.getCode(); - } - }); - - newMandatoryColumn("Espece_scientifique", new ValueSetter<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"); - } - ScientificSpecies species = scientificSpeciesByCode.get(value); - object.setParent(species); - } - }); - - newMandatoryColumn("Classe_debut", new ValueSetter<CalcifiedPartTaking, String>() { - @Override - public void set(CalcifiedPartTaking object, String value) throws Exception { - Integer size; - try { - size = Integer.parseInt(value); - } catch (NumberFormatException e) { - size = null; - } - Log.d(TAG, object + "setStartSize " + size); - object.setStartSize(size); - } - }); - - newMandatoryColumn("Classe_fin", new ValueSetter<CalcifiedPartTaking, String>() { - @Override - public void set(CalcifiedPartTaking object, String value) throws Exception { - Integer size; - try { - size = Integer.parseInt(value); - } catch (NumberFormatException e) { - size = null; - } - Log.d(TAG, object + "setEndSize " + size); - object.setEndSize(size); - } - }); - - newMandatoryColumn("Pas_de_classe", new ValueSetter<CalcifiedPartTaking, String>() { - @Override - public void set(CalcifiedPartTaking object, String value) throws Exception { - Integer size = Integer.parseInt(value); - Log.d(TAG, object + "setSizeStep " + size); - object.setSizeStep(size); - } - }); - - newMandatoryColumn("Pas", new ValueSetter<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); - } - }); - - newMandatoryColumn("Arret", new ValueSetter<CalcifiedPartTaking, String>() { - @Override - public void set(CalcifiedPartTaking object, String value) throws Exception { - Integer size; - try { - size = Integer.parseInt(value); - } catch (NumberFormatException e) { - size = null; - } - Log.d(TAG, object + "setStop " + size); - object.setStop(size); - } - }); - } - - @Override - public CalcifiedPartTaking newEmptyInstance() { - return new CalcifiedPartTaking(); - } -} Added: trunk/src/fr/ifremer/wlo/models/categorization/CategoryModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/categorization/CategoryModel.java (rev 0) +++ trunk/src/fr/ifremer/wlo/models/categorization/CategoryModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -0,0 +1,68 @@ +package fr.ifremer.wlo.models.categorization; + +import android.content.ContentValues; +import android.database.Cursor; +import android.util.Log; +import fr.ifremer.wlo.models.BaseModel; + +import java.util.List; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class CategoryModel extends BaseModel { + + private static final String TAG = "CategoryModel"; + + public static final String TABLE_NAME = "categories"; + public static final String COLUMN_LABEL = "label"; + public static final String[] ALL_COLUMNS = new String[] { + _ID, + COLUMN_LABEL + }; + + @Override + public String getTableName() { + return TABLE_NAME; + } + + protected String label; + protected List<QualitativeValueModel> qualitativeValues; + + public CategoryModel() { + } + + public CategoryModel(Cursor cursor) { + super(cursor); + label = cursor.getString(1); + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public List<QualitativeValueModel> getQualitativeValues() { + return qualitativeValues; + } + + public void setQualitativeValues(List<QualitativeValueModel> qualitativeValues) { + this.qualitativeValues = qualitativeValues; + } + + @Override + public String toString() { + return label; + } + + @Override + public ContentValues convertIntoContentValues() { + ContentValues value = super.convertIntoContentValues(); + putValue(value, COLUMN_LABEL, label); + return value; + } +} \ No newline at end of file Added: trunk/src/fr/ifremer/wlo/models/categorization/QualitativeValueModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/categorization/QualitativeValueModel.java (rev 0) +++ trunk/src/fr/ifremer/wlo/models/categorization/QualitativeValueModel.java 2014-03-01 16:46:23 UTC (rev 57) @@ -0,0 +1,84 @@ +package fr.ifremer.wlo.models.categorization; + +import android.content.ContentValues; +import android.database.Cursor; +import android.util.Log; +import fr.ifremer.wlo.models.BaseModel; +import org.apache.commons.lang3.StringUtils; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class QualitativeValueModel extends BaseModel { + + private static final String TAG = "QualitativeValueModel"; + + public static final String TABLE_NAME = "qualitativeValues"; + public static final String COLUMN_VALUE = "value"; + public static final String COLUMN_LABEL = "label"; + public static final String COLUMN_CATEGORY_ID = "categoryId"; + public static final String[] ALL_COLUMNS = new String[] { + _ID, + COLUMN_VALUE, + COLUMN_LABEL, + COLUMN_CATEGORY_ID + }; + + @Override + public String getTableName() { + return TABLE_NAME; + } + + protected String value; + protected String label; + protected CategoryModel category; + + public QualitativeValueModel() { + } + + public QualitativeValueModel(Cursor cursor) { + super(cursor); + value = cursor.getString(1); + label = cursor.getString(2); + Log.d(TAG, cursor.getString(3) + " " + value + " " + label); + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public CategoryModel getCategory() { + return category; + } + + public void setCategory(CategoryModel category) { + this.category = category; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + @Override + public String toString() { + return StringUtils.isNotEmpty(label) ? label : value; + } + + @Override + public ContentValues convertIntoContentValues() { + ContentValues value = super.convertIntoContentValues(); + putValue(value, COLUMN_VALUE, this.value); + putValue(value, COLUMN_LABEL, label); + putValue(value, COLUMN_CATEGORY_ID, category != null ? category.getId() : null); + return value; + } +} \ No newline at end of file Deleted: trunk/src/fr/ifremer/wlo/models/referentials/Age.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/Age.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/models/referentials/Age.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,86 +0,0 @@ -package fr.ifremer.wlo.models.referentials; - -/* - * #%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.ContentValues; -import android.database.Cursor; -import fr.ifremer.wlo.models.BaseModel; -import fr.ifremer.wlo.utils.UIUtils; - -/** - * @author kmorin <kmorin@codelutin.com> - * @since 0.1 - */ -public class Age extends BaseModel { - - private static final String TAG = "Age"; - - public static final String TABLE_NAME = "ref_ages"; - public static final String COLUMN_LABEL = "label"; - public static final String[] ALL_COLUMNS = new String[] { - _ID, - COLUMN_LABEL - }; - - @Override - public String getTableName() { - return TABLE_NAME; - } - - protected String label; - - public Age() { - } - - public Age(Cursor cursor) { - super(cursor); - label = cursor.getString(1); - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - @Override - public String toString() { - return label; - } - - @Override - public String toString(android.content.Context context) { - return UIUtils.getStringOrUndefined(label, context); - } - - @Override - public ContentValues convertIntoContentValues() { - ContentValues value = super.convertIntoContentValues(); - putValue(value, COLUMN_LABEL, label); - return value; - } -} Copied: trunk/src/fr/ifremer/wlo/models/referentials/CalcifiedPartTaking.java (from rev 56, trunk/src/fr/ifremer/wlo/models/calcifiedPartTaking/CalcifiedPartTaking.java) =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/CalcifiedPartTaking.java (rev 0) +++ trunk/src/fr/ifremer/wlo/models/referentials/CalcifiedPartTaking.java 2014-03-01 16:46:23 UTC (rev 57) @@ -0,0 +1,142 @@ +package fr.ifremer.wlo.models.referentials; + +/* + * #%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.ContentValues; +import android.database.Cursor; +import android.util.Log; +import fr.ifremer.wlo.models.BaseModel; +import fr.ifremer.wlo.models.HierarchicalModel; +import fr.ifremer.wlo.models.referentials.ScientificSpecies; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class CalcifiedPartTaking extends HierarchicalModel<ScientificSpecies> { + + private static final String TAG = "CalcifiedPartTaking"; + + public static final String TABLE_NAME = "calcifiedPartTaking"; + public static final String COLUMN_START_SIZE = "startSize"; + public static final String COLUMN_END_SIZE = "endSize"; + public static final String COLUMN_SIZE_STEP = "sizeStep"; + public static final String COLUMN_STEP = "step"; + public static final String COLUMN_STOP = "stop"; + public static final String COLUMN_SCIENTIFIC_SPECIES_ID = "scientificSpeciesId"; + public static final String[] ALL_COLUMNS = new String[] { + _ID, + COLUMN_START_SIZE, + COLUMN_END_SIZE, + COLUMN_SIZE_STEP, + COLUMN_STEP, + COLUMN_STOP, + COLUMN_SCIENTIFIC_SPECIES_ID + }; + + @Override + public String getTableName() { + return TABLE_NAME; + } + + protected Integer startSize; + protected Integer endSize; + protected int sizeStep; + protected int step; + protected Integer stop; + + public CalcifiedPartTaking() { + } + + public CalcifiedPartTaking(Cursor cursor) { + super(cursor); + if (!cursor.isNull(1)) { + startSize = cursor.getInt(1); + } + if (!cursor.isNull(2)) { + endSize = cursor.getInt(2); + } + if (!cursor.isNull(3)) { + sizeStep = cursor.getInt(3); + } + step = cursor.getInt(4); + if (!cursor.isNull(5)) { + stop = cursor.getInt(5); + } + Log.d(TAG, startSize + " " + endSize + " " + sizeStep + " " + step + " " + stop); + } + + public Integer getStartSize() { + return startSize; + } + + public void setStartSize(Integer startSize) { + this.startSize = startSize; + } + + public Integer getEndSize() { + return endSize; + } + + public void setEndSize(Integer endSize) { + this.endSize = endSize; + } + + public int getSizeStep() { + return sizeStep; + } + + public void setSizeStep(int sizeStep) { + this.sizeStep = sizeStep; + } + + public int getStep() { + return step; + } + + public void setStep(int step) { + this.step = step; + } + + public Integer getStop() { + return stop; + } + + public void setStop(Integer stop) { + this.stop = stop; + } + + @Override + public ContentValues convertIntoContentValues() { + ContentValues value = super.convertIntoContentValues(); + putValue(value, COLUMN_START_SIZE, startSize); + putValue(value, COLUMN_END_SIZE, endSize); + putValue(value, COLUMN_SIZE_STEP, sizeStep); + putValue(value, COLUMN_STEP, step); + putValue(value, COLUMN_STOP, stop); + putValue(value, COLUMN_SCIENTIFIC_SPECIES_ID, getParentId()); + return value; + } +} \ No newline at end of file Deleted: trunk/src/fr/ifremer/wlo/models/referentials/Gender.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/Gender.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/models/referentials/Gender.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,99 +0,0 @@ -package fr.ifremer.wlo.models.referentials; - -/* - * #%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.ContentValues; -import android.database.Cursor; -import fr.ifremer.wlo.models.BaseModel; -import fr.ifremer.wlo.utils.UIUtils; - -/** - * @author kmorin <kmorin@codelutin.com> - * @since 0.1 - */ -public class Gender extends BaseModel implements HasCode { - - private static final String TAG = "Gender"; - - public static final String TABLE_NAME = "ref_genders"; - public static final String COLUMN_CODE = "code"; - public static final String COLUMN_LABEL = "label"; - public static final String[] ALL_COLUMNS = new String[] { - _ID, - COLUMN_CODE, - COLUMN_LABEL - }; - - @Override - public String getTableName() { - return TABLE_NAME; - } - - protected String code; - protected String label; - - public Gender() { - } - - public Gender(Cursor cursor) { - super(cursor); - code = cursor.getString(1); - label = cursor.getString(2); - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - @Override - public String toString() { - return label; - } - - @Override - public String toString(android.content.Context context) { - return UIUtils.getStringOrUndefined(label, context); - } - - @Override - public ContentValues convertIntoContentValues() { - ContentValues value = super.convertIntoContentValues(); - putValue(value, COLUMN_CODE, code); - putValue(value, COLUMN_LABEL, label); - return value; - } -} Deleted: trunk/src/fr/ifremer/wlo/models/referentials/Maturity.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/Maturity.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/models/referentials/Maturity.java 2014-03-01 16:46:23 UTC (rev 57) @@ -1,86 +0,0 @@ -package fr.ifremer.wlo.models.referentials; - -/* - * #%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.ContentValues; -import android.database.Cursor; -import fr.ifremer.wlo.models.BaseModel; -import fr.ifremer.wlo.utils.UIUtils; - -/** - * @author kmorin <kmorin@codelutin.com> - * @since 0.1 - */ -public class Maturity extends BaseModel { - - private static final String TAG = "Maturity"; - - public static final String TABLE_NAME = "ref_maturities"; - public static final String COLUMN_LABEL = "label"; - public static final String[] ALL_COLUMNS = new String[] { - _ID, - COLUMN_LABEL - }; - - @Override - public String getTableName() { - return TABLE_NAME; - } - - protected String label; - - public Maturity() { - } - - public Maturity(Cursor cursor) { - super(cursor); - label = cursor.getString(1); - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - @Override - public String toString() { - return label; - } - - @Override - public String toString(android.content.Context context) { - return UIUtils.getStringOrUndefined(label, context); - } - - @Override - public ContentValues convertIntoContentValues() { - ContentValues value = super.convertIntoContentValues(); - putValue(value, COLUMN_LABEL, label); - return value; - } -} Added: trunk/src/fr/ifremer/wlo/preferences/CategoryCreationAcivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/CategoryCreationAcivity.java (rev 0) +++ trunk/src/fr/ifremer/wlo/preferences/CategoryCreationAcivity.java 2014-03-01 16:46:23 UTC (rev 57) @@ -0,0 +1,119 @@ +package fr.ifremer.wlo.preferences; + +import android.app.AlertDialog; +import android.content.Intent; +import android.os.Bundle; +import android.text.Editable; +import android.util.Log; +import android.view.View; +import android.widget.EditText; +import android.widget.TextView; +import fr.ifremer.wlo.R; +import fr.ifremer.wlo.WloBaseActivity; +import fr.ifremer.wlo.imports.ImportUtil; +import fr.ifremer.wlo.models.categorization.CategoryModel; +import fr.ifremer.wlo.models.categorization.QualitativeValueModel; +import fr.ifremer.wlo.storage.DataCache; +import fr.ifremer.wlo.storage.WloSqlOpenHelper; +import fr.ifremer.wlo.utils.BaseTextWatcher; +import fr.ifremer.wlo.utils.UIUtils; +import fr.ifremer.wlo.utils.filechooser.FileDialog; +import fr.ifremer.wlo.utils.filechooser.SelectionMode; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class CategoryCreationAcivity extends WloBaseActivity { + + private static final String TAG = "CategoryCreationAcivity"; + + protected static final int REQUEST_IMPORT_QUALITATIVE_VALUES = 0; + + protected CategoryModel model = new CategoryModel(); + + @Override + protected Integer getContentView() { + return R.layout.category_creation_form; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + EditText labelEditor = (EditText) findViewById(R.id.category_label); + labelEditor.addTextChangedListener(new BaseTextWatcher() { + @Override + public void afterTextChanged(Editable s) { + model.setLabel(String.valueOf(s)); + } + }); + } + + @Override + protected Class<? extends WloBaseActivity> getUpActivity() { + return null; + } + + public void validate(View source) { + WloSqlOpenHelper soh = new WloSqlOpenHelper(this); + soh.saveData(model); + if (model.getQualitativeValues() != null) { + soh.saveData(model.getQualitativeValues()); + } + + DataCache.addCategory(model); + + setResult(RESULT_OK); + finish(); + } + + public void importQualitativeValues(View source) { + Intent intent = new Intent(this, FileDialog.class); + intent.putExtra(FileDialog.START_PATH, "/sdcard"); + intent.putExtra(FileDialog.CAN_SELECT_DIR, false); + intent.putExtra(FileDialog.SELECTION_MODE, SelectionMode.MODE_OPEN); + + //alternatively you can set file filter + intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "csv" }); + + startActivityForResult(intent, REQUEST_IMPORT_QUALITATIVE_VALUES); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_IMPORT_QUALITATIVE_VALUES && resultCode == RESULT_OK) { + String path = data.getStringExtra(FileDialog.RESULT_PATH); + try { + Collection<QualitativeValueModel> qualitativeValueModels = + ImportUtil.importQualitativeValues(model, path); + model.setQualitativeValues(new ArrayList<>(qualitativeValueModels)); + + TextView textView = (TextView) findViewById(R.id.qualitative_values_nb_label); + textView.setText(getString(R.string.qualitative_values_nb, qualitativeValueModels.size())); + textView.setVisibility(View.VISIBLE); + + } 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); + runOnUiThread(new Runnable() { + @Override + public void run() { + new AlertDialog.Builder(CategoryCreationAcivity.this) + .setMessage(R.string.import_error) + .setNeutralButton(android.R.string.ok, UIUtils.getCancelClickListener()) + .create().show(); + } + }); + + } + + } else { + super.onActivityResult(requestCode, resultCode, data); + } + } +} Modified: trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-03-01 16:46:23 UTC (rev 57) @@ -24,6 +24,7 @@ * #L% */ +import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; @@ -35,25 +36,25 @@ import android.preference.MultiSelectListPreference; import android.preference.Preference; import android.preference.PreferenceFragment; -import android.text.format.DateFormat; import android.util.Log; +import android.view.View; import android.widget.Toast; import com.google.common.base.Functions; 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 com.google.common.collect.Sets; import fr.ifremer.wlo.MainActivity; import fr.ifremer.wlo.R; import fr.ifremer.wlo.WloBaseActivity; import fr.ifremer.wlo.models.BaseModel; +import fr.ifremer.wlo.models.categorization.CategoryModel; import fr.ifremer.wlo.models.referentials.CommercialSpecies; import fr.ifremer.wlo.models.referentials.ScientificSpecies; import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.storage.StorageUtils; import fr.ifremer.wlo.storage.WloSqlOpenHelper; -import fr.ifremer.wlo.models.referentials.imports.ImportUtil; +import fr.ifremer.wlo.imports.ImportUtil; import fr.ifremer.wlo.utils.UIUtils; import fr.ifremer.wlo.utils.filechooser.FileDialog; import fr.ifremer.wlo.utils.filechooser.SelectionMode; @@ -63,7 +64,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.util.Collection; -import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.Map; @@ -77,19 +77,18 @@ private static final String TAG = "SettingsActivity"; - protected static final int REQUEST_IMPORT_AGES = 0; - protected static final int REQUEST_IMPORT_COMMERCIAL_SPECIES = 1; - protected static final int REQUEST_IMPORT_GENDERS = 2; - protected static final int REQUEST_IMPORT_LOCATIONS = 3; - protected static final int REQUEST_IMPORT_MATURITIES = 4; - protected static final int REQUEST_IMPORT_MENSURATIONS = 5; - protected static final int REQUEST_IMPORT_METIERS = 6; - protected static final int REQUEST_IMPORT_PRESENTATIONS = 7; - protected static final int REQUEST_IMPORT_SCIENTIFIC_SPECIES = 8; - protected static final int REQUEST_IMPORT_STATES = 9; - protected static final int REQUEST_IMPORT_VESSELS = 10; - protected static final int REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS = 11; + protected static final int REQUEST_IMPORT_COMMERCIAL_SPECIES = 0; + protected static final int REQUEST_IMPORT_LOCATIONS = 1; + protected static final int REQUEST_IMPORT_MENSURATIONS = 2; + protected static final int REQUEST_IMPORT_METIERS = 3; + protected static final int REQUEST_IMPORT_PRESENTATIONS = 4; + protected static final int REQUEST_IMPORT_SCIENTIFIC_SPECIES = 5; + 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; + @Override protected Integer getContentView() { return null; @@ -124,11 +123,8 @@ addPreferencesFromResource(R.xml.preferences); // create map pref_key / request code - requestCodeByPrefKey.put("import_ages", REQUEST_IMPORT_AGES); requestCodeByPrefKey.put("import_commercial_species", REQUEST_IMPORT_COMMERCIAL_SPECIES); - requestCodeByPrefKey.put("import_genders", REQUEST_IMPORT_GENDERS); requestCodeByPrefKey.put("import_locations", REQUEST_IMPORT_LOCATIONS); - requestCodeByPrefKey.put("import_maturities", REQUEST_IMPORT_MATURITIES); requestCodeByPrefKey.put("import_mensurations", REQUEST_IMPORT_MENSURATIONS); requestCodeByPrefKey.put("import_metiers", REQUEST_IMPORT_METIERS); requestCodeByPrefKey.put("import_presentations", REQUEST_IMPORT_PRESENTATIONS); @@ -139,11 +135,8 @@ Map<String, Integer> nbElementsByPrefKey = Maps.newHashMap(); Context context = getActivity(); - nbElementsByPrefKey.put("import_ages", DataCache.getAllAges(context).size()); nbElementsByPrefKey.put("import_commercial_species", DataCache.getAllCommercialSpecies(context).size()); - nbElementsByPrefKey.put("import_genders", DataCache.getAllGenders(context).size()); nbElementsByPrefKey.put("import_locations", DataCache.getAllLocations(context).size()); - nbElementsByPrefKey.put("import_maturities", DataCache.getAllMaturities(context).size()); nbElementsByPrefKey.put("import_mensurations", DataCache.getAllMensurations(context).size()); nbElementsByPrefKey.put("import_metiers", DataCache.getAllMetiers(context).size()); nbElementsByPrefKey.put("import_presentations", DataCache.getAllPresentations(context).size()); @@ -175,6 +168,19 @@ }); } + // categories + Preference categoryPref = findPreference("import_categories"); + int nb = DataCache.getAllCategories(context).size(); + categoryPref.setSummary(getString(R.string.preferences_import_summary, nb)); + categoryPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + Intent intent = new Intent(getActivity(), CategoryCreationAcivity.class); + startActivityForResult(intent, REQUEST_CREATE_CATEGORY); + return true; + } + }); + } @Override @@ -239,10 +245,10 @@ @Override public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { + Context context = getActivity(); if (resultCode == RESULT_OK - && requestCode >= REQUEST_IMPORT_AGES && requestCode <= REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS) { + && requestCode >= REQUEST_IMPORT_COMMERCIAL_SPECIES && requestCode <= REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS) { - Context context = getActivity(); if (!StorageUtils.isExternalStorageWritable()) { AlertDialog dialog = new AlertDialog.Builder(context) .setTitle(R.string.preferences_import_no_external_storage_title) @@ -261,6 +267,12 @@ importData(requestCode, data, true); } + } else if (resultCode == RESULT_OK && requestCode == REQUEST_CREATE_CATEGORY) { + Toast.makeText(context, R.string.category_created, Toast.LENGTH_LONG).show(); + int nb = DataCache.getAllCategories(context).size(); + Preference categoryPref = findPreference("import_categories"); + categoryPref.setSummary(getString(R.string.preferences_import_summary, nb)); + } else { super.onActivityResult(requestCode, resultCode, data); } @@ -284,26 +296,14 @@ 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(); @@ -407,4 +407,5 @@ } } + } \ No newline at end of file Modified: trunk/src/fr/ifremer/wlo/storage/DataCache.java =================================================================== --- trunk/src/fr/ifremer/wlo/storage/DataCache.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/storage/DataCache.java 2014-03-01 16:46:23 UTC (rev 57) @@ -27,29 +27,26 @@ import android.content.Context; import android.database.Cursor; import com.google.common.base.Function; -import com.google.common.collect.HashBasedTable; +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import com.google.common.collect.Table; import fr.ifremer.wlo.models.BaseModel; -import fr.ifremer.wlo.models.referentials.Age; +import fr.ifremer.wlo.models.categorization.CategoryModel; +import fr.ifremer.wlo.models.categorization.QualitativeValueModel; import fr.ifremer.wlo.models.referentials.CommercialSpecies; -import fr.ifremer.wlo.models.referentials.Gender; import fr.ifremer.wlo.models.referentials.HasCode; import fr.ifremer.wlo.models.referentials.Location; -import fr.ifremer.wlo.models.referentials.Maturity; import fr.ifremer.wlo.models.referentials.Mensuration; import fr.ifremer.wlo.models.referentials.Metier; import fr.ifremer.wlo.models.referentials.Presentation; import fr.ifremer.wlo.models.referentials.ScientificSpecies; import fr.ifremer.wlo.models.referentials.State; import fr.ifremer.wlo.models.referentials.Vessel; -import org.apache.commons.lang3.ObjectUtils; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -62,11 +59,8 @@ public static final String DEFAULT_MENSURATION_CODE = "LT"; - protected static Map<String, Age> ages; protected static Map<String, CommercialSpecies> commercialSpecies; - protected static Map<String, Gender> genders; protected static Map<String, Location> locations; - protected static Map<String, Maturity> maturities; protected static Map<String, Mensuration> mensurations; protected static Mensuration defaultMensuration; protected static Map<String, Metier> metiers; @@ -74,22 +68,9 @@ protected static Map<String, ScientificSpecies> scientificSpecies; protected static Map<String, State> states; protected static Map<String, Vessel> vessels; + protected static Map<String, CategoryModel> categories; protected static Set<String> sortCategories; - public static Collection<Age> getAllAges(Context context) { - initAges(context); - return ages.values(); - } - - public static Age getAgeById(Context context, String id) { - initAges(context); - return ages.get(id); - } - - public static void invalidateAges() { - ages = null; - } - public static Collection<CommercialSpecies> getAllCommercialSpecies(Context context) { initCommercialSpecies(context); List<CommercialSpecies> result = Lists.newArrayList(commercialSpecies.values()); @@ -106,20 +87,6 @@ commercialSpecies = null; } - public static Collection<Gender> getAllGenders(Context context) { - initGenders(context); - return genders.values(); - } - - public static Gender getGenderById(Context context, String id) { - initGenders(context); - return genders.get(id); - } - - public static void invalidateGenders() { - genders = null; - } - public static Collection<Location> getAllLocations(Context context) { initLocations(context); List<Location> result = Lists.newArrayList(locations.values()); @@ -136,20 +103,6 @@ locations = null; } - public static Collection<Maturity> getAllMaturities(Context context) { - initMaturities(context); - return maturities.values(); - } - - public static Maturity getMaturityById(Context context, String id) { - initMaturities(context); - return maturities.get(id); - } - - public static void invalidateMaturities() { - maturities = null; - } - public static Collection<Mensuration> getAllMensurations(Context context) { initMensurations(context); List<Mensuration> result = Lists.newArrayList(mensurations.values()); @@ -264,22 +217,36 @@ vessels = null; } - protected static void initAges(Context context) { - if (ages == null) { + public static Collection<CategoryModel> getAllCategories(Context context) { + initCategories(context); + List<CategoryModel> result = Lists.newArrayList(categories.values()); + return result; + } + + public static CategoryModel getCategoryById(Context context, String id) { + initCategories(context); + CategoryModel result = categories.get(id); + if (result != null && result.getQualitativeValues() == null) { WloSqlOpenHelper soh = new WloSqlOpenHelper(context); - Cursor cursor = soh.getAllRefAges(); - List<Age> data = WloSqlOpenHelper.transformCursorIntoCollection(cursor, - new Function<Cursor, Age>() { - @Override - public Age apply(Cursor input) { - return new Age(input); - } - }); - ages = Maps.uniqueIndex(data, BaseModel.GET_ID_FUNCTION); + Cursor cursor = soh.getAllQualitativeValues(id); + List<QualitativeValueModel> qualitativeValues = + WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, QualitativeValueModel>() { + @Override + public QualitativeValueModel apply(Cursor input) { + return new QualitativeValueModel(input); + } + }); + result.setQualitativeValues(qualitativeValues); soh.close(); } + return result; } + public static void addCategory(CategoryModel categoryModel) { + Preconditions.checkNotNull(categoryModel); + categories.put(categoryModel.getId(), categoryModel); + } + protected static void initCommercialSpecies(Context context) { if (commercialSpecies == null) { WloSqlOpenHelper soh = new WloSqlOpenHelper(context); @@ -297,23 +264,6 @@ } } - protected static void initGenders(Context context) { - if (genders == null) { - WloSqlOpenHelper soh = new WloSqlOpenHelper(context); - Cursor cursor = soh.getAllRefGenders(); - List<Gender> data = WloSqlOpenHelper.transformCursorIntoCollection(cursor, - new Function<Cursor, Gender>() { - @Override - public Gender apply(Cursor input) { - return new Gender(input); - } - }); - - genders = Maps.uniqueIndex(data, BaseModel.GET_ID_FUNCTION); - soh.close(); - } - } - protected static void initLocations(Context context) { if (locations == null) { WloSqlOpenHelper soh = new WloSqlOpenHelper(context); @@ -330,22 +280,6 @@ } } - protected static void initMaturities(Context context) { - if (maturities == null) { - WloSqlOpenHelper soh = new WloSqlOpenHelper(context); - Cursor cursor = soh.getAllRefMaturities(); - List<Maturity> data = WloSqlOpenHelper.transformCursorIntoCollection(cursor, - new Function<Cursor, Maturity>() { - @Override - public Maturity apply(Cursor input) { - return new Maturity(input); - } - }); - maturities = Maps.uniqueIndex(data, BaseModel.GET_ID_FUNCTION); - soh.close(); - } - } - protected static void initMensurations(Context context) { if (mensurations == null) { WloSqlOpenHelper soh = new WloSqlOpenHelper(context); @@ -448,17 +382,34 @@ } } + protected static void initCategories(Context context) { + if (categories == null) { + WloSqlOpenHelper soh = new WloSqlOpenHelper(context); + Cursor cursor = soh.getAllCategories(); + List<CategoryModel> data = WloSqlOpenHelper.transformCursorIntoCollection(cursor, + new Function<Cursor, CategoryModel>() { + @Override + public CategoryModel apply(Cursor input) { + return new CategoryModel(input); + } + }); + + categories = new HashMap<>(Maps.uniqueIndex(data, BaseModel.GET_ID_FUNCTION)); + soh.close(); + } + } + protected static void initSortCategories(Context context) { if (sortCategories == null) { WloSqlOpenHelper soh = new WloSqlOpenHelper(context); Cursor cursor = soh.getAllSortCategories(); List<String> sc = WloSqlOpenHelper.transformCursorIntoCollection(cursor, - new Function<Cursor, String>() { - @Override - public String apply(Cursor cursor) { - return cursor.getString(0); - } - }); + new Function<Cursor, String>() { + @Override + public String apply(Cursor cursor) { + return cursor.getString(0); + } + }); sortCategories = Sets.newHashSet(sc); soh.close(); } Modified: trunk/src/fr/ifremer/wlo/storage/Exporter.java =================================================================== --- trunk/src/fr/ifremer/wlo/storage/Exporter.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/storage/Exporter.java 2014-03-01 16:46:23 UTC (rev 57) @@ -26,16 +26,22 @@ import android.content.Context; import android.database.Cursor; +import android.util.Pair; import com.google.common.base.Function; import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; import fr.ifremer.wlo.models.*; +import fr.ifremer.wlo.models.categorization.CategoryModel; +import fr.ifremer.wlo.models.categorization.QualitativeValueModel; import fr.ifremer.wlo.models.referentials.*; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * @author Kevin Morin (Code Lutin) @@ -45,11 +51,23 @@ public static final SimpleDateFormat UTC_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ"); + protected static Map<String, QualitativeValueModel> qualitativeValuesById; + public static String exportData(Context context) throws JSONException { WloSqlOpenHelper soh = new WloSqlOpenHelper(context); JSONArray root = new JSONArray(); try { + Cursor qualitativeValuesCursor = soh.getAllQualitativeValues(); + List<QualitativeValueModel> qualitativeValues = WloSqlOpenHelper.transformCursorIntoCollection(qualitativeValuesCursor, + new Function<Cursor, QualitativeValueModel>() { + @Override + public QualitativeValueModel apply(Cursor input) { + return new QualitativeValueModel(input); + } + }); + qualitativeValuesById = Maps.uniqueIndex(qualitativeValues, BaseModel.GET_ID_FUNCTION); + Cursor cursor = soh.getAllContexts(); List<ContextModel> contexts = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, ContextModel>() { @Override @@ -224,6 +242,7 @@ }); JSONArray jsonScientificSpeciesArray = new JSONArray(); for (ScientificSpeciesModel scientificSpeciesModel : scientificSpecies) { + scientificSpeciesModel.setParent(model); JSONObject jsonScientificSpecies = createJSONScientificSpeciesModel(scientificSpeciesModel, context, soh); jsonScientificSpeciesArray.put(jsonScientificSpecies); } @@ -253,9 +272,19 @@ return new MeasurementModel(input); } }); + + List<String> categories = new ArrayList<>(); + CommercialSpeciesModel parent = model.getParent(); + CategoryModel cat1 = parent.getCategory1(); + categories.add(cat1 != null ? cat1.getLabel() : null); + CategoryModel cat2 = parent.getCategory2(); + categories.add(cat2 != null ? cat2.getLabel() : null); + CategoryModel cat3 = parent.getCategory3(); + categories.add(cat3 != null ? cat3.getLabel() : null); + JSONArray jsonMeasurements = new JSONArray(); for (MeasurementModel measurement : measurements) { - JSONObject jsonMeasurement = createJSONMeasurementModel(measurement, context); + JSONObject jsonMeasurement = createJSONMeasurementModel(measurement, categories); jsonMeasurements.put(jsonMeasurement); } jsonScientificSpeciesModel.put("observations", jsonMeasurements); @@ -270,7 +299,7 @@ }); JSONArray jsonCategoryWeightModels = new JSONArray(); for (CategoryWeightModel categoryWeight : categoryWeights) { - JSONObject jsonCategoryWeightModel = createJSONCategoryWeightModel(categoryWeight, context); + JSONObject jsonCategoryWeightModel = createJSONCategoryWeightModel(categoryWeight, categories); jsonCategoryWeightModels.put(jsonCategoryWeightModel); } jsonScientificSpeciesModel.put("poids-par-categorie", jsonCategoryWeightModels); @@ -280,19 +309,38 @@ } public static JSONObject createJSONMeasurementModel(MeasurementModel model, - final Context context) throws JSONException { + List<String> categories) throws JSONException { Preconditions.checkNotNull(model); + JSONObject jsonMeasurementModel = new JSONObject(); + for (int i = 0 ; i < categories.size() ; i++) { + String categoryLabel = categories.get(i); + if (categoryLabel != null) { + String measurementCategory = null; + switch (i) { + case 0: + measurementCategory = model.getCategory1(); + break; + case 1: + measurementCategory = model.getCategory2(); + break; + case 2: + measurementCategory = model.getCategory3(); + break; + } + if (measurementCategory != null) { + QualitativeValueModel value = qualitativeValuesById.get(measurementCategory); + if (value == null) { + jsonMeasurementModel.put(categoryLabel, measurementCategory); - JSONObject jsonMeasurementModel = new JSONObject(); - jsonMeasurementModel.put("sexe", createJSONGender(DataCache.getGenderById(context, model.getCategory1()))); - Maturity maturity = DataCache.getMaturityById(context, model.getCategory2()); - if (maturity != null) { - jsonMeasurementModel.put("maturité", maturity.getLabel()); + } else { + JSONObject jsonCategory = new JSONObject(); + jsonCategory.put("code", value.getValue()); + jsonCategory.put("libellé", value.getLabel()); + jsonMeasurementModel.put(categoryLabel, jsonCategory); + } + } + } } - Age age = DataCache.getAgeById(context, model.getCategory3()); - if (age != null) { - jsonMeasurementModel.put("âge", age.getLabel()); - } jsonMeasurementModel.put("taille", model.getSize()); jsonMeasurementModel.put("date", UTC_DATE_FORMAT.format(model.getDate().getTime())); @@ -300,19 +348,38 @@ } public static JSONObject createJSONCategoryWeightModel(CategoryWeightModel model, - final Context context) throws JSONException { + List<String> categories) throws JSONException { Preconditions.checkNotNull(model); + JSONObject jsonCategoryWeightModel = new JSONObject(); + for (int i = 0 ; i < categories.size() ; i++) { + String categoryLabel = categories.get(i); + if (categoryLabel != null) { + String measurementCategory = null; + switch (i) { + case 0: + measurementCategory = model.getCategory1(); + break; + case 1: + measurementCategory = model.getCategory2(); + break; + case 2: + measurementCategory = model.getCategory3(); + break; + } + if (measurementCategory != null) { + QualitativeValueModel value = qualitativeValuesById.get(measurementCategory); + if (value == null) { + jsonCategoryWeightModel.put(categoryLabel, measurementCategory); - JSONObject jsonCategoryWeightModel = new JSONObject(); - jsonCategoryWeightModel.put("sexe", createJSONGender(DataCache.getGenderById(context, model.getCategory1()))); - Maturity maturity = DataCache.getMaturityById(context, model.getCategory2()); - if (maturity != null) { - jsonCategoryWeightModel.put("maturité", maturity.getLabel()); + } else { + JSONObject jsonCategory = new JSONObject(); + jsonCategory.put("code", value.getValue()); + jsonCategory.put("libellé", value.getLabel()); + jsonCategoryWeightModel.put(categoryLabel, jsonCategory); + } + } + } } - Age age = DataCache.getAgeById(context, model.getCategory3()); - if (age != null) { - jsonCategoryWeightModel.put("âge", age.getLabel()); - } jsonCategoryWeightModel.put("poids", model.getWeight()); return jsonCategoryWeightModel; @@ -403,14 +470,4 @@ return jsonScientificSpecies; } - public static JSONObject createJSONGender(Gender gender) throws JSONException { - if (gender == null) { - return null; - } - JSONObject jsonGender = new JSONObject(); - jsonGender.put("code", gender.getCode()); - jsonGender.put("libellé", gender.getLabel()); - return jsonGender; - } - } \ No newline at end of file Modified: trunk/src/fr/ifremer/wlo/storage/WloSqlOpenHelper.java =================================================================== --- trunk/src/fr/ifremer/wlo/storage/WloSqlOpenHelper.java 2014-02-28 13:25:58 UTC (rev 56) +++ trunk/src/fr/ifremer/wlo/storage/WloSqlOpenHelper.java 2014-03-01 16:46:23 UTC (rev 57) @@ -28,7 +28,6 @@ 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; @@ -41,12 +40,11 @@ import fr.ifremer.wlo.models.MetierModel; import fr.ifremer.wlo.models.ScientificSpeciesModel; import fr.ifremer.wlo.models.VesselModel; -import fr.ifremer.wlo.models.calcifiedPartTaking.CalcifiedPartTaking; -import fr.ifremer.wlo.models.referentials.Age; +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.CommercialSpecies; -import fr.ifremer.wlo.models.referentials.Gender; import fr.ifremer.wlo.models.referentials.Location; -import fr.ifremer.wlo.models.referentials.Maturity; import fr.ifremer.wlo.models.referentials.Mensuration; import fr.ifremer.wlo.models.referentials.Metier; import fr.ifremer.wlo.models.referentials.Presentation; @@ -67,7 +65,7 @@ private static final String TAG = "WloOpenHelper"; public static final String DATABASE_NAME = "wlo.db"; - public static final int DATABASE_VERSION = 18; + public static final int DATABASE_VERSION = 19; public static final String TEXT_TYPE = " TEXT"; public static final String INTEGER_TYPE = " INTEGER"; @@ -155,6 +153,9 @@ CommercialSpeciesModel.COLUMN_SORT_CATEGORY + TEXT_TYPE + COMMA_SEP + CommercialSpeciesModel.COLUMN_STATE + TEXT_TYPE + COMMA_SEP + CommercialSpeciesModel.COLUMN_PRESENTATION + TEXT_TYPE + COMMA_SEP + + CommercialSpeciesModel.COLUMN_CATEGORY1 + TEXT_TYPE + COMMA_SEP + + CommercialSpeciesModel.COLUMN_CATEGORY2 + TEXT_TYPE + COMMA_SEP + + CommercialSpeciesModel.COLUMN_CATEGORY3 + TEXT_TYPE + COMMA_SEP + CommercialSpeciesModel.COLUMN_COMMENT + TEXT_TYPE + COMMA_SEP + CommercialSpeciesModel.COLUMN_TOTAL_UNLOADED_WEIGHT + INTEGER_TYPE + COMMA_SEP + CommercialSpeciesModel.COLUMN_METIER_ID + TEXT_TYPE + NOT_NULL + COMMA_SEP + @@ -167,7 +168,13 @@ "FOREIGN KEY(" + CommercialSpeciesModel.COLUMN_STATE + ") REFERENCES " + State.TABLE_NAME + "(" + State._ID + ")" + COMMA_SEP + "FOREIGN KEY(" + CommercialSpeciesModel.COLUMN_PRESENTATION + ") REFERENCES " + - Presentation.TABLE_NAME + "(" + Presentation._ID + ")" + + Presentation.TABLE_NAME + "(" + Presentation._ID + ")" + COMMA_SEP + + "FOREIGN KEY(" + CommercialSpeciesModel.COLUMN_CATEGORY1 + ") REFERENCES " + + CategoryModel.TABLE_NAME + "(" + CategoryModel._ID + ")" + COMMA_SEP + + "FOREIGN KEY(" + CommercialSpeciesModel.COLUMN_CATEGORY2 + ") REFERENCES " + + CategoryModel.TABLE_NAME + "(" + CategoryModel._ID + ")" + COMMA_SEP + + "FOREIGN KEY(" + CommercialSpeciesModel.COLUMN_CATEGORY3 + ") REFERENCES " + + CategoryModel.TABLE_NAME + "(" + CategoryModel._ID + ")" + " )"; protected static final String SQL_DELETE_COMMERCIAL_SPECIES = @@ -242,18 +249,31 @@ protected static final String SQL_DELETE_CALCIFIED_PART_TAKINGS = "DROP TABLE IF EXISTS " + CalcifiedPartTaking.TABLE_NAME; - // Referentials + // CATEGORIES + protected static final String SQL_CREATE_CATEGORIES = + "CREATE TABLE " + CategoryModel.TABLE_NAME + " (" + + CategoryModel._ID + TEXT_TYPE + " PRIMARY KEY" + COMMA_SEP + + CategoryModel.COLUMN_LABEL + TEXT_TYPE + NOT_NULL + + " )"; - // Ages - protected static final String SQL_CREATE_REF_AGES = - "CREATE TABLE " + Age.TABLE_NAME + " (" + - Age._ID + TEXT_TYPE + " PRIMARY KEY," + - Age.COLUMN_LABEL + TEXT_TYPE + NOT_NULL + - " )"; + protected static final String SQL_DELETE_CATEGORIES = + "DROP TABLE IF EXISTS " + CategoryModel.TABLE_NAME; - protected static final String SQL_DELETE_REF_AGES = - "DROP TABLE IF EXISTS " + Age.TABLE_NAME; + protected static final String SQL_CREATE_QUALITATIVE_VALUES = + "CREATE TABLE " + QualitativeValueModel.TABLE_NAME + " (" + + QualitativeValueModel._ID + TEXT_TYPE + " PRIMARY KEY" + COMMA_SEP + + QualitativeValueModel.COLUMN_VALUE + TEXT_TYPE + NOT_NULL + COMMA_SEP + + QualitativeValueModel.COLUMN_LABEL + TEXT_TYPE + COMMA_SEP + + QualitativeValueModel.COLUMN_CATEGORY_ID + TEXT_TYPE + NOT_NULL + COMMA_SEP + + "FOREIGN KEY(" + QualitativeValueModel.COLUMN_CATEGORY_ID + ") REFERENCES " + + CategoryModel.TABLE_NAME + "(" + CategoryModel._ID + ") ON DELETE CASCADE" + + " )"; + protected static final String SQL_DELETE_QUALITATIVE_VALUES = + "DROP TABLE IF EXISTS " + QualitativeValueModel.TABLE_NAME; + + // Referentials + // Commercial Species protected static final String SQL_CREATE_REF_COMMERCIAL_SPECIES = "CREATE TABLE " + CommercialSpecies.TABLE_NAME + " (" + @@ -271,17 +291,6 @@ protected static final String SQL_DELETE_REF_COMMERCIAL_SPECIES = "DROP TABLE IF EXISTS " + CommercialSpecies.TABLE_NAME; - // Genders - protected static final String SQL_CREATE_REF_GENDERS = - "CREATE TABLE " + Gender.TABLE_NAME + " (" + - Gender._ID + TEXT_TYPE + " PRIMARY KEY" + COMMA_SEP + - Gender.COLUMN_CODE + TEXT_TYPE + NOT_NULL + COMMA_SEP + - Gender.COLUMN_LABEL + TEXT_TYPE + - " )"; - - protected static final String SQL_DELETE_REF_GENDERS = - "DROP TABLE IF EXISTS " + Gender.TABLE_NAME; - // Locations protected static final String SQL_CREATE_REF_LOCATIONS = "CREATE TABLE " + Location.TABLE_NAME + " (" + @@ -294,16 +303,6 @@ protected static final String SQL_DELETE_REF_LOCATIONS = "DROP TABLE IF EXISTS " + Location.TABLE_NAME; - // Maturities - protected static final String SQL_CREATE_REF_MATURITIES = - "CREATE TABLE " + Maturity.TABLE_NAME + " (" + - Maturity._ID + TEXT_TYPE + " PRIMARY KEY" + COMMA_SEP + - Maturity.COLUMN_LABEL + TEXT_TYPE + NOT_NULL + - " )"; - - protected static final String SQL_DELETE_REF_MATURITIES = - "DROP TABLE IF EXISTS " + Maturity.TABLE_NAME; - // Mensurations protected static final String SQL_CREATE_REF_MENSURATIONS = "CREATE TABLE " + Mensuration.TABLE_NAME + " (" + @@ -387,11 +386,8 @@ @Override public void onCreate(SQLiteDatabase db) { // referentials - db.execSQL(SQL_CREATE_REF_AGES); db.execSQL(SQL_CREATE_REF_COMMERCIAL_SPECIES); - db.execSQL(SQL_CREATE_REF_GENDERS); db.execSQL(SQL_CREATE_REF_LOCATIONS); - db.execSQL(SQL_CREATE_REF_MATURITIES); db.execSQL(SQL_CREATE_REF_MENSURATIONS); db.execSQL(SQL_CREATE_REF_METIERS); db.execSQL(SQL_CREATE_REF_PRESENTATIONS); @@ -399,6 +395,9 @@ db.execSQL(SQL_CREATE_REF_STATES); db.execSQL(SQL_CREATE_REF_VESSELS); + db.execSQL(SQL_CREATE_CATEGORIES); + db.execSQL(SQL_CREATE_QUALITATIVE_VALUES); + // models db.execSQL(SQL_CREATE_CONTEXTS); db.execSQL(SQL_CREATE_LOCATIONS); @@ -425,12 +424,12 @@ db.execSQL(SQL_DELETE_LOCATIONS); db.execSQL(SQL_DELETE_CONTEXTS); + db.execSQL(SQL_DELETE_CATEGORIES); + db.execSQL(SQL_DELETE_QUALITATIVE_VALUES); + // referentials - db.execSQL(SQL_DELETE_REF_AGES); db.execSQL(SQL_DELETE_REF_COMMERCIAL_SPECIES); - db.execSQL(SQL_DELETE_REF_GENDERS); db.execSQL(SQL_DELETE_REF_LOCATIONS); - db.execSQL(SQL_DELETE_REF_MATURITIES); db.execSQL(SQL_DELETE_REF_MENSURATIONS); db.execSQL(SQL_DELETE_REF_METIERS); db.execSQL(SQL_DELETE_REF_PRESENTATIONS); @@ -584,25 +583,35 @@ db.delete(CalcifiedPartTaking.TABLE_NAME, null, null); } - // Referentials + // CATEGORIES - public Cursor getAllRefAges() { + public Cursor getAllCategories() { SQLiteDatabase db = getReadableDatabase(); - Cursor cursor = db.query(Age.TABLE_NAME, Age.ALL_COLUMNS, + Cursor cursor = db.query(CategoryModel.TABLE_NAME, CategoryModel.ALL_COLUMNS, null, null, null, null, null); return cursor; } - public Cursor getAllRefCommercialSpecies() { + public Cursor getAllQualitativeValues() { SQLiteDatabase db = getReadableDatabase(); - Cursor cursor = db.query(CommercialSpecies.TABLE_NAME, CommercialSpecies.ALL_COLUMNS, + Cursor cursor = db.query(QualitativeValueModel.TABLE_NAME, QualitativeValueModel.ALL_COLUMNS, null, null, null, null, null); return cursor; } - public Cursor getAllRefGenders() { + public Cursor getAllQualitativeValues(String categoryId) { SQLiteDatabase db = getReadableDatabase(); - Cursor cursor = db.query(Gender.TABLE_NAME, Gender.ALL_COLUMNS, + Cursor cursor = db.query(QualitativeValueModel.TABLE_NAME, QualitativeValueModel.ALL_COLUMNS, + QualitativeValueModel.COLUMN_CATEGORY_ID + " = ?", new String[]{ categoryId }, + null, null, null, null); + return cursor; + } + + // Referentials + + public Cursor getAllRefCommercialSpecies() { + SQLiteDatabase db = getReadableDatabase(); + Cursor cursor = db.query(CommercialSpecies.TABLE_NAME, CommercialSpecies.ALL_COLUMNS, null, null, null, null, null); return cursor; } @@ -614,13 +623,6 @@ return cursor; } - public Cursor getAllRefMaturities() { - SQLiteDatabase db = getReadableDatabase(); - Cursor cursor = db.query(Maturity.TABLE_NAME, Maturity.ALL_COLUMNS, - null, null, null, null, null); - return cursor; - } - public Cursor getAllRefMensurations() { SQLiteDatabase db = getReadableDatabase(); Cursor cursor = db.query(Mensuration.TABLE_NAME, Mensuration.ALL_COLUMNS,