r84 - in trunk: res/values-fr src/fr/ifremer/wlo src/fr/ifremer/wlo/models src/fr/ifremer/wlo/storage src/fr/ifremer/wlo/utils
Author: kmorin Date: 2014-03-28 13:49:26 +0100 (Fri, 28 Mar 2014) New Revision: 84 Url: http://forge.codelutin.com/projects/wlo/repository/revisions/84 Log: refs-100 #4710 [EXPORT] Exporter les donn?\195?\169es dans un seul tableau fixes #4846 [POIDS] Lorsqu'il y a plusieurs esp?\195?\168ces scientifiques, seuls les poids de la denri?\195?\168re sont sauvegard?\195?\169s Added: trunk/src/fr/ifremer/wlo/storage/CsvExporter.java trunk/src/fr/ifremer/wlo/storage/JsonExporter.java Removed: trunk/src/fr/ifremer/wlo/storage/Exporter.java Modified: trunk/res/values-fr/strings.xml trunk/src/fr/ifremer/wlo/MainActivity.java trunk/src/fr/ifremer/wlo/WeightsActivity.java trunk/src/fr/ifremer/wlo/models/HierarchicalModel.java trunk/src/fr/ifremer/wlo/models/ScientificSpeciesModel.java trunk/src/fr/ifremer/wlo/utils/ImportExportUtil.java trunk/src/fr/ifremer/wlo/utils/UIUtils.java trunk/src/fr/ifremer/wlo/utils/WloItemListViewBinder.java Modified: trunk/res/values-fr/strings.xml =================================================================== --- trunk/res/values-fr/strings.xml 2014-03-27 10:20:09 UTC (rev 83) +++ trunk/res/values-fr/strings.xml 2014-03-28 12:49:26 UTC (rev 84) @@ -70,7 +70,7 @@ <!-- Home screen --> <string name="main_open_contexts">Voir / saisir des données</string> - <string name="main_export_data">Export des données</string> + <string name="main_export_data">Exporter les données</string> <string name="main_connect_ichtyometer">Connecter un ichtyomètre</string> <string name="main_disconnect_ichtyometer">Déconnecter l\'ichtyomètre</string> <string name="main_settings">Configuration</string> Modified: trunk/src/fr/ifremer/wlo/MainActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/MainActivity.java 2014-03-27 10:20:09 UTC (rev 83) +++ trunk/src/fr/ifremer/wlo/MainActivity.java 2014-03-28 12:49:26 UTC (rev 84) @@ -30,6 +30,7 @@ import android.content.Context; import android.content.Intent; import android.content.res.Configuration; +import android.database.Cursor; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; @@ -41,21 +42,38 @@ import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; +import com.google.common.base.Function; +import fr.ifremer.wlo.models.CategoryWeightModel; +import fr.ifremer.wlo.models.CommercialSpeciesModel; +import fr.ifremer.wlo.models.ContextModel; +import fr.ifremer.wlo.models.LocationModel; +import fr.ifremer.wlo.models.MeasurementModel; +import fr.ifremer.wlo.models.MetierModel; +import fr.ifremer.wlo.models.ScientificSpeciesModel; +import fr.ifremer.wlo.models.VesselModel; +import fr.ifremer.wlo.models.referentials.CommercialSpecies; +import fr.ifremer.wlo.storage.CsvExporter; +import fr.ifremer.wlo.storage.JsonExporter; import fr.ifremer.wlo.utils.ImportExportUtil; import fr.ifremer.wlo.models.categorization.CategoryModel; import fr.ifremer.wlo.preferences.SettingsActivity; import fr.ifremer.wlo.storage.DataCache; -import fr.ifremer.wlo.storage.Exporter; import fr.ifremer.wlo.storage.WloSqlOpenHelper; import fr.ifremer.wlo.utils.UpdateCheckTask; import fr.ifremer.wlo.utils.filechooser.FileDialog; import fr.ifremer.wlo.utils.filechooser.SelectionMode; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.tuple.Triple; import org.json.JSONException; +import org.nuiton.csv.Export; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @author kmorin <kmorin@codelutin.com> @@ -195,7 +213,7 @@ intent.putExtra(FileDialog.SELECTION_MODE, SelectionMode.MODE_OPEN); //alternatively you can set file filter - intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "json" }); + intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "csv" }); startActivityForResult(intent, REQUEST_SELECT_EXPORT_FOLDER); } @@ -229,20 +247,24 @@ case REQUEST_SELECT_EXPORT_FOLDER: if (resultCode == Activity.RESULT_OK) { try { - String jsonData = Exporter.exportData(this); +// String jsonData = JsonExporter.exportData(this); File selectedFile = new File(data.getStringExtra(FileDialog.RESULT_PATH)); if (selectedFile.isFile()) { selectedFile = selectedFile.getParentFile(); } String date = String.format("-%1$tY%1$tm%1$td-%1$tH%1$tM%1$tS", new Date()); - File file = new File(selectedFile, "export" + date + ".json"); - FileUtils.write(file, jsonData); + File file = new File(selectedFile, "export" + date + ".csv"); +// FileUtils.write(file, jsonData); + CsvExporter exporter = new CsvExporter(';', this); + List<MeasurementModel> measurementModelList = getMeasurementModels(); + Export.exportToFile(exporter, measurementModelList, file); + Toast.makeText(this, "Export effectué dans le fichier " + file.getAbsolutePath(), Toast.LENGTH_LONG).show(); - } catch (JSONException | IOException e) { - Log.e(TAG, "error while exporting to JSON", e); + } catch (Exception e) { + Log.e(TAG, "error while exporting to CSV", e); Toast.makeText(this, "Erreur lors de l'export", Toast.LENGTH_SHORT).show(); } } @@ -251,6 +273,120 @@ } + protected List<MeasurementModel> getMeasurementModels() { + List<MeasurementModel> measurementModelList = new ArrayList<>(); + + WloSqlOpenHelper soh = new WloSqlOpenHelper(this); + Cursor cursor = soh.getAllContexts(); + List<ContextModel> contexts = + WloSqlOpenHelper.transformCursorIntoCollection(cursor, + new Function<Cursor, ContextModel>() { + @Override + public ContextModel apply(Cursor input) { + return new ContextModel(input); + } + }); + for (final ContextModel contextModel : contexts) { + cursor = soh.getAllLocations(contextModel.getId()); + List<LocationModel> locationModels = + WloSqlOpenHelper.transformCursorIntoCollection(cursor, + new Function<Cursor, LocationModel>() { + @Override + public LocationModel apply(Cursor input) { + LocationModel locationModel = new LocationModel(MainActivity.this, input); + locationModel.setParent(contextModel); + return locationModel; + } + }); + for (final LocationModel locationModel : locationModels) { + cursor = soh.getAllVessels(locationModel.getId()); + List<VesselModel> vesselModels = + WloSqlOpenHelper.transformCursorIntoCollection(cursor, + new Function<Cursor, VesselModel>() { + @Override + public VesselModel apply(Cursor input) { + VesselModel vesselModel = new VesselModel(MainActivity.this, input); + vesselModel.setParent(locationModel); + return vesselModel; + } + }); + for (final VesselModel vesselModel : vesselModels) { + cursor = soh.getAllMetiers(vesselModel.getId()); + List<MetierModel> metierModels = + WloSqlOpenHelper.transformCursorIntoCollection(cursor, + new Function<Cursor, MetierModel>() { + @Override + public MetierModel apply(Cursor input) { + MetierModel metierModel = new MetierModel(MainActivity.this, input); + metierModel.setParent(vesselModel); + return metierModel; + } + }); + for (final MetierModel metierModel : metierModels) { + cursor = soh.getAllCommercialSpecies(metierModel.getId()); + List<CommercialSpeciesModel> commercialSpeciesModels = + WloSqlOpenHelper.transformCursorIntoCollection(cursor, + new Function<Cursor, CommercialSpeciesModel>() { + @Override + public CommercialSpeciesModel apply(Cursor input) { + CommercialSpeciesModel commercialSpeciesModel = new CommercialSpeciesModel(MainActivity.this, input); + commercialSpeciesModel.setParent(metierModel); + return commercialSpeciesModel; + } + }); + for (final CommercialSpeciesModel commercialSpeciesModel : commercialSpeciesModels) { + cursor = soh.getAllScientificSpecies(commercialSpeciesModel.getId()); + List<ScientificSpeciesModel> scientificSpeciesModels = + WloSqlOpenHelper.transformCursorIntoCollection(cursor, + new Function<Cursor, ScientificSpeciesModel>() { + @Override + public ScientificSpeciesModel apply(Cursor input) { + ScientificSpeciesModel scientificSpeciesModel = new ScientificSpeciesModel(MainActivity.this, input); + scientificSpeciesModel.setParent(commercialSpeciesModel); + return scientificSpeciesModel; + } + }); + + for (final ScientificSpeciesModel scientificSpeciesModel : scientificSpeciesModels) { + Map<Triple<String, String, String>, Integer> categoryWeights = new HashMap<>(); + cursor = soh.getAllCategoryWeigths(scientificSpeciesModel.getId()); + boolean cont = cursor.moveToFirst(); + while (cont) { + String category1 = cursor.getString(1); + String category2 = cursor.getString(2); + String category3 = cursor.getString(3); + Integer weight = null; + if (!cursor.isNull(4)) { + weight = cursor.getInt(4); + } + Triple<String, String, String> key = Triple.of(category1, category2, category3); + categoryWeights.put(key, weight); + + cont = cursor.moveToNext(); + } + scientificSpeciesModel.setCategoryWeights(categoryWeights); + + cursor = soh.getAllMeasurements(scientificSpeciesModel.getId()); + List<MeasurementModel> measurementModels = + WloSqlOpenHelper.transformCursorIntoCollection(cursor, + new Function<Cursor, MeasurementModel>() { + @Override + public MeasurementModel apply(Cursor input) { + MeasurementModel measurementModel = new MeasurementModel(input); + measurementModel.setParent(scientificSpeciesModel); + return measurementModel; + } + }); + measurementModelList.addAll(measurementModels); + } + } + } + } + } + } + return measurementModelList; + } + protected void setOrientation(int orientation) { LinearLayout.LayoutParams logoParams, buttonParams; Modified: trunk/src/fr/ifremer/wlo/WeightsActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/WeightsActivity.java 2014-03-27 10:20:09 UTC (rev 83) +++ trunk/src/fr/ifremer/wlo/WeightsActivity.java 2014-03-28 12:49:26 UTC (rev 84) @@ -195,7 +195,7 @@ // get all the category weights Cursor categoryWeighs = woh.getAllCategoryWeigths(scientificSpecies.getId()); - categoryWeightModels = WloSqlOpenHelper.transformCursorIntoCollection(categoryWeighs, + List<CategoryWeightModel> categoryWeightModels = WloSqlOpenHelper.transformCursorIntoCollection(categoryWeighs, new Function<Cursor, CategoryWeightModel>() { @Override public CategoryWeightModel apply(Cursor input) { @@ -204,6 +204,7 @@ return result; } }); + this.categoryWeightModels.addAll(categoryWeightModels); for (final CategoryWeightModel categoryWeightModel : categoryWeightModels) { Modified: trunk/src/fr/ifremer/wlo/models/HierarchicalModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/HierarchicalModel.java 2014-03-27 10:20:09 UTC (rev 83) +++ trunk/src/fr/ifremer/wlo/models/HierarchicalModel.java 2014-03-28 12:49:26 UTC (rev 84) @@ -44,13 +44,12 @@ return parent; } - public BaseModel getParent(int level) { + public <T extends BaseModel> T getParent(Class<T> parentClass) { BaseModel result = this; - //TODO kmorin 20131219 handle NPE - for (int i = 0 ; i < level ; i++) { + while(result != null && !result.getClass().isAssignableFrom(parentClass)) { result = ((HierarchicalModel) result).getParent(); } - return result; + return (T) result; } public void setParent(P parent) { Modified: trunk/src/fr/ifremer/wlo/models/ScientificSpeciesModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/ScientificSpeciesModel.java 2014-03-27 10:20:09 UTC (rev 83) +++ trunk/src/fr/ifremer/wlo/models/ScientificSpeciesModel.java 2014-03-28 12:49:26 UTC (rev 84) @@ -31,7 +31,10 @@ import fr.ifremer.wlo.models.referentials.ScientificSpecies; import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.utils.UIUtils; +import org.apache.commons.lang3.tuple.Triple; +import java.util.Map; + /** * @author kmorin <kmorin@codelutin.com> * @since 0.1 @@ -63,6 +66,9 @@ protected Integer sortedWeight; protected Integer sampleWeight; + // used for the export + protected Map<Triple<String, String, String>, Integer> categoryWeights; + public ScientificSpeciesModel() { } @@ -130,6 +136,14 @@ firePropertyChange(COLUMN_SAMPLE_WEIGHT, oldValue, sampleWeight); } + public Map<Triple<String, String, String>, Integer> getCategoryWeights() { + return categoryWeights; + } + + public void setCategoryWeights(Map<Triple<String, String, String>, Integer> categoryWeights) { + this.categoryWeights = categoryWeights; + } + @Override public String getTableName() { return TABLE_NAME; Added: trunk/src/fr/ifremer/wlo/storage/CsvExporter.java =================================================================== --- trunk/src/fr/ifremer/wlo/storage/CsvExporter.java (rev 0) +++ trunk/src/fr/ifremer/wlo/storage/CsvExporter.java 2014-03-28 12:49:26 UTC (rev 84) @@ -0,0 +1,531 @@ +package fr.ifremer.wlo.storage; + +import android.content.Context; +import android.database.Cursor; +import android.util.Log; +import com.google.common.base.Function; +import com.google.common.collect.Maps; +import fr.ifremer.wlo.models.BaseModel; +import fr.ifremer.wlo.models.CommercialSpeciesModel; +import fr.ifremer.wlo.models.ContextModel; +import fr.ifremer.wlo.models.LocationModel; +import fr.ifremer.wlo.models.MeasurementModel; +import fr.ifremer.wlo.models.MetierModel; +import fr.ifremer.wlo.models.ScientificSpeciesModel; +import fr.ifremer.wlo.models.VesselModel; +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.Location; +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.utils.UIUtils; +import org.apache.commons.lang3.tuple.Triple; +import org.nuiton.csv.ValueGetter; +import org.nuiton.csv.ext.AbstractExportModel; + +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Map; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class CsvExporter extends AbstractExportModel<MeasurementModel> { + + private static final String TAG = "CsvExporter"; + + protected Map<String, QualitativeValueModel> qualitativeValuesById; + + public CsvExporter(char separator, Context context) { + super(separator); + + WloSqlOpenHelper soh = new WloSqlOpenHelper(context); + 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); + + // context + newColumnForExport("contexte_nom", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + ContextModel contextModel = object.getParent(ContextModel.class); + return contextModel.getName(); + } + }); + + newColumnForExport("contexte_commentaire", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + ContextModel contextModel = object.getParent(ContextModel.class); + return contextModel.getComment(); + } + }); + + // location + newColumnForExport("lieu_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + LocationModel locationModel = object.getParent(LocationModel.class); + return locationModel.getLocation().getCode(); + } + }); + + newColumnForExport("lieu_libelle", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + LocationModel locationModel = object.getParent(LocationModel.class); + return locationModel.getLocation().getLabel(); + } + }); + + newColumnForExport("lieu_type", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + LocationModel locationModel = object.getParent(LocationModel.class); + return locationModel.getLocation().getTypeLabel(); + } + }); + + newColumnForExport("lieu_date_debut", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + LocationModel locationModel = object.getParent(LocationModel.class); + Calendar startDate = locationModel.getStartDate(); + return formatDate(startDate); + } + }); + + newColumnForExport("lieu_date_fin", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + LocationModel locationModel = object.getParent(LocationModel.class); + Calendar endDate = locationModel.getEndDate(); + return formatDate(endDate); + } + }); + + newColumnForExport("lieu_observateur", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + LocationModel locationModel = object.getParent(LocationModel.class); + return locationModel.getOperator(); + } + }); + + newColumnForExport("lieu_commentaire", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + LocationModel locationModel = object.getParent(LocationModel.class); + return locationModel.getComment(); + } + }); + + // vessel + + newColumnForExport("navire_immatriculation", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + VesselModel vesselModel = object.getParent(VesselModel.class); + return vesselModel.getRegistrationNumber(); + } + }); + + newColumnForExport("navire_nom", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + VesselModel vesselModel = object.getParent(VesselModel.class); + return vesselModel.getName(); + } + }); + + newColumnForExport("navire_date_debarquement", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + VesselModel vesselModel = object.getParent(VesselModel.class); + Calendar landingDate = vesselModel.getLandingDate(); + return formatDate(landingDate); + } + }); + + newColumnForExport("navire_lieu_debarquement_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + VesselModel vesselModel = object.getParent(VesselModel.class); + Location landingLocation = vesselModel.getLandingLocation(); + return landingLocation != null ? landingLocation.getCode() : null; + } + }); + + newColumnForExport("navire_lieu_debarquement_libelle", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + VesselModel vesselModel = object.getParent(VesselModel.class); + Location landingLocation = vesselModel.getLandingLocation(); + return landingLocation != null ? landingLocation.getLabel() : null; + } + }); + + newColumnForExport("navire_lieu_debarquement_type", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + VesselModel vesselModel = object.getParent(VesselModel.class); + Location landingLocation = vesselModel.getLandingLocation(); + return landingLocation != null ? landingLocation.getTypeLabel() : null; + } + }); + + newColumnForExport("navire_commentaire", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + VesselModel vesselModel = object.getParent(VesselModel.class); + return vesselModel.getComment(); + } + }); + + // metier + + newColumnForExport("metier_engin_id", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + MetierModel metierModel = object.getParent(MetierModel.class); + Metier gearSpecies = metierModel.getGearSpecies(); + return gearSpecies != null ? gearSpecies.getMetierId() : null; + } + }); + + newColumnForExport("metier_engin_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + MetierModel metierModel = object.getParent(MetierModel.class); + Metier gearSpecies = metierModel.getGearSpecies(); + return gearSpecies != null ? gearSpecies.getCode() : null; + } + }); + + newColumnForExport("metier_engin_libelle", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + MetierModel metierModel = object.getParent(MetierModel.class); + Metier gearSpecies = metierModel.getGearSpecies(); + return gearSpecies != null ? gearSpecies.getLabel() : null; + } + }); + + newColumnForExport("metier_secteur_peche", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + MetierModel metierModel = object.getParent(MetierModel.class); + return metierModel.getZone(); + } + }); + + newColumnForExport("metier_plan", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + MetierModel metierModel = object.getParent(MetierModel.class); + return metierModel.getSampleRowCode(); + } + }); + + newColumnForExport("metier_commentaire", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + MetierModel metierModel = object.getParent(MetierModel.class); + return metierModel.getComment(); + } + }); + + // commercial species + + newColumnForExport("espece_commerciale_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + CommercialSpecies species = commercialSpeciesModel.getFaoCode(); + return species != null ? species.getCode() : null; + } + }); + + newColumnForExport("espece_commerciale_label", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + CommercialSpecies species = commercialSpeciesModel.getFaoCode(); + return species != null ? species.getFrenchLabel() : null; + } + }); + + newColumnForExport("espece_commerciale_methode_mensuration_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + Mensuration measurementMethod = commercialSpeciesModel.getMeasurementMethod(); + return measurementMethod != null ? measurementMethod.getCode() : null; + } + }); + + newColumnForExport("espece_commerciale_methode_mensuration_libelle", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + Mensuration measurementMethod = commercialSpeciesModel.getMeasurementMethod(); + return measurementMethod != null ? measurementMethod.getLabel() : null; + } + }); + + newColumnForExport("espece_commerciale_precision", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + return commercialSpeciesModel.getPrecision().getLabel(); + } + }); + + newColumnForExport("espece_commerciale_melange_especes", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + return String.valueOf(commercialSpeciesModel.isSpeciesMix()); + } + }); + + newColumnForExport("espece_commerciale_categorie_tri", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + return commercialSpeciesModel.getSortCategory(); + } + }); + + newColumnForExport("espece_commerciale_etat_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + State state = commercialSpeciesModel.getState(); + return state != null ? state.getCode() : null; + } + }); + + newColumnForExport("espece_commerciale_etat_libelle", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + State state = commercialSpeciesModel.getState(); + return state != null ? state.getLabel() : null; + } + }); + + newColumnForExport("espece_commerciale_presentation_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + Presentation presentation = commercialSpeciesModel.getPresentation(); + return presentation != null ? presentation.getCode() : null; + } + }); + + newColumnForExport("espece_commerciale_presentation_libelle", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + Presentation presentation = commercialSpeciesModel.getPresentation(); + return presentation != null ? presentation.getLabel() : null; + } + }); + + newColumnForExport("espece_commerciale_categorie1", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + CategoryModel category = commercialSpeciesModel.getCategory1(); + return category != null ? category.getLabel() : null; + } + }); + + newColumnForExport("espece_commerciale_categorie2", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + CategoryModel category = commercialSpeciesModel.getCategory2(); + return category != null ? category.getLabel() : null; + } + }); + + newColumnForExport("espece_commerciale_categorie3", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + CategoryModel category = commercialSpeciesModel.getCategory3(); + return category != null ? category.getLabel() : null; + } + }); + + newColumnForExport("espece_commerciale_poids_total", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + Integer totalUnloadedWeight = commercialSpeciesModel.getTotalUnloadedWeight(); + return totalUnloadedWeight != null ? totalUnloadedWeight.toString() : null; + } + }); + + newColumnForExport("espece_commerciale_commentaire", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + CommercialSpeciesModel commercialSpeciesModel = object.getParent(CommercialSpeciesModel.class); + return commercialSpeciesModel.getComment(); + } + }); + + // scientific species + + newColumnForExport("espece_scientifique_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + ScientificSpeciesModel speciesModel = object.getParent(); + ScientificSpecies species = speciesModel.getName(); + return species != null ? species.getCode() : null; + } + }); + + newColumnForExport("espece_scientifique_name", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + ScientificSpeciesModel speciesModel = object.getParent(); + ScientificSpecies species = speciesModel.getName(); + return species != null ? species.getLabel() : null; + } + }); + + newColumnForExport("espece_scientifique_comment", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + ScientificSpeciesModel speciesModel = object.getParent(); + return speciesModel.getComment(); + } + }); + + newColumnForExport("espece_scientifique_sorted_weight", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + ScientificSpeciesModel speciesModel = object.getParent(); + Integer sortedWeight = speciesModel.getSortedWeight(); + return sortedWeight != null ? sortedWeight.toString() : null; + } + }); + + newColumnForExport("espece_scientifique_sample_weight", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + ScientificSpeciesModel speciesModel = object.getParent(); + Integer sampleWeight = speciesModel.getSampleWeight(); + return sampleWeight != null ? sampleWeight.toString() : null; + } + }); + + // observations + + newColumnForExport("observation_categorie1_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + return getCategoryValue(object.getCategory1()); + } + }); + + newColumnForExport("observation_categorie1_label", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + return getCategoryLabel(object.getCategory1()); + } + }); + + newColumnForExport("observation_categorie2_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + return getCategoryValue(object.getCategory2()); + } + }); + + newColumnForExport("observation_categorie2_label", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + return getCategoryLabel(object.getCategory2()); + } + }); + + newColumnForExport("observation_categorie3_code", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + return getCategoryValue(object.getCategory3()); + } + }); + + newColumnForExport("observation_categorie3_label", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + return getCategoryLabel(object.getCategory3()); + } + }); + + newColumnForExport("observation_categories_poids", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + ScientificSpeciesModel speciesModel = object.getParent(); + Map<Triple<String, String, String>, Integer> categoryWeights = speciesModel.getCategoryWeights(); + Triple<String, String, String> key = Triple.of(object.getCategory1(), object.getCategory2(), object.getCategory3()); + Integer weight = categoryWeights.get(key); + return weight != null ? weight.toString() : null; + } + }); + + newColumnForExport("observation_date", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + return formatDate(object.getDate()); + } + }); + + newColumnForExport("observation_taille", new ValueGetter<MeasurementModel, String>() { + @Override + public String get(MeasurementModel object) throws Exception { + return object.getSize().toString(); + } + }); + } + + protected String getCategoryValue(String measurementCategory) { + QualitativeValueModel value = qualitativeValuesById.get(measurementCategory); + if (value == null) { + return measurementCategory; + + } + return value.getValue(); + } + + protected String getCategoryLabel(String measurementCategory) { + QualitativeValueModel value = qualitativeValuesById.get(measurementCategory); + if (value == null) { + return measurementCategory; + + } + return value.getLabel(); + } + + protected String formatDate(Calendar calendar) { + return calendar != null ? UIUtils.UTC_DATE_FORMAT.format(calendar.getTime()) : null; + } +} Deleted: trunk/src/fr/ifremer/wlo/storage/Exporter.java =================================================================== --- trunk/src/fr/ifremer/wlo/storage/Exporter.java 2014-03-27 10:20:09 UTC (rev 83) +++ trunk/src/fr/ifremer/wlo/storage/Exporter.java 2014-03-28 12:49:26 UTC (rev 84) @@ -1,473 +0,0 @@ -package fr.ifremer.wlo.storage; - -/* - * #%L - * WLO - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2013 - 2014 Ifremer - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -import android.content.Context; -import android.database.Cursor; -import android.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) - * @since x.x - */ -public class Exporter { - - 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 - public ContextModel apply(Cursor input) { - return new ContextModel(input); - } - }); - for (ContextModel contextModel : contexts) { - JSONObject jsonContext = createJSONContextModel(contextModel, context, soh); - root.put(jsonContext); - } - - } finally { - soh.close(); - } - return root.toString(2); - } - - public static JSONObject createJSONContextModel(ContextModel model, - final Context context, - WloSqlOpenHelper soh) throws JSONException { - Preconditions.checkNotNull(model); - Preconditions.checkNotNull(soh); - - JSONObject jsonContext = new JSONObject(); - jsonContext.put("contexte", model.getName()); - jsonContext.put("commentaire", model.getComment()); - - Cursor cursor = soh.getAllLocations(model.getId()); - List<LocationModel> locations = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, LocationModel>() { - @Override - public LocationModel apply(Cursor input) { - return new LocationModel(context, input); - } - }); - JSONArray jsonLocations = new JSONArray(); - for (LocationModel location : locations) { - JSONObject jsonLocation = createJSONLocationModel(location, context, soh); - jsonLocations.put(jsonLocation); - } - jsonContext.put("lieux", jsonLocations); - cursor.close(); - - return jsonContext; - } - - public static JSONObject createJSONLocationModel(LocationModel model, - final Context context, - WloSqlOpenHelper soh) throws JSONException { - Preconditions.checkNotNull(model); - Preconditions.checkNotNull(soh); - - JSONObject jsonLocation = new JSONObject(); - - jsonLocation.put("lieu", createJSONLocation(model.getLocation())); - if (model.getStartDate() != null) { - jsonLocation.put("date-début", UTC_DATE_FORMAT.format(model.getStartDate().getTime())); - } - if (model.getEndDate() != null) { - jsonLocation.put("date-fin", UTC_DATE_FORMAT.format(model.getEndDate().getTime())); - } - jsonLocation.put("observateur", model.getOperator()); - jsonLocation.put("commentaire", model.getComment()); - - Cursor cursor = soh.getAllVessels(model.getId()); - List<VesselModel> vessels = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, VesselModel>() { - @Override - public VesselModel apply(Cursor input) { - return new VesselModel(context, input); - } - }); - JSONArray jsonVessels = new JSONArray(); - for (VesselModel vessel : vessels) { - JSONObject jsonVessel = createJSONVesselModel(vessel, context, soh); - jsonVessels.put(jsonVessel); - } - jsonLocation.put("navires", jsonVessels); - cursor.close(); - - return jsonLocation; - } - - public static JSONObject createJSONVesselModel(VesselModel model, - final Context context, - WloSqlOpenHelper soh) throws JSONException { - Preconditions.checkNotNull(model); - Preconditions.checkNotNull(soh); - - JSONObject jsonVessel = new JSONObject(); - - jsonVessel.put("immatriculation", model.getRegistrationNumber()); - jsonVessel.put("nom", model.getRegistrationNumber()); - if (model.getLandingDate() != null) { - jsonVessel.put("date-débarquement", UTC_DATE_FORMAT.format(model.getLandingDate().getTime())); - } - jsonVessel.put("lieu-débarquement", createJSONLocation(model.getLandingLocation())); - jsonVessel.put("commentaire", model.getComment()); - - Cursor cursor = soh.getAllMetiers(model.getId()); - List<MetierModel> metiers = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, MetierModel>() { - @Override - public MetierModel apply(Cursor input) { - return new MetierModel(context, input); - } - }); - JSONArray jsonMetiers = new JSONArray(); - for (MetierModel metier : metiers) { - JSONObject jsonMetier = createJSONMetierModel(metier, context, soh); - jsonMetiers.put(jsonMetier); - } - jsonVessel.put("métiers", jsonMetiers); - cursor.close(); - - return jsonVessel; - } - - public static JSONObject createJSONMetierModel(MetierModel model, - final Context context, - WloSqlOpenHelper soh) throws JSONException { - Preconditions.checkNotNull(model); - Preconditions.checkNotNull(soh); - - JSONObject jsonMetier = new JSONObject(); - - jsonMetier.put("engin-espèce", createJSONMetier(model.getGearSpecies())); - jsonMetier.put("secteur", model.getZone()); - jsonMetier.put("référence-plan", model.getSampleRowCode()); - jsonMetier.put("commentaire", model.getComment()); - - Cursor cursor = soh.getAllCommercialSpecies(model.getId()); - List<CommercialSpeciesModel> commercialSpecies = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, CommercialSpeciesModel>() { - @Override - public CommercialSpeciesModel apply(Cursor input) { - return new CommercialSpeciesModel(context, input); - } - }); - JSONArray jsonCommercialSpeciesArray = new JSONArray(); - for (CommercialSpeciesModel commercialSpeciesModel : commercialSpecies) { - JSONObject jsonCommercialSpecies = createJSONComercialSpeciesModel(commercialSpeciesModel, context, soh); - jsonCommercialSpeciesArray.put(jsonCommercialSpecies); - } - jsonMetier.put("espèces-fao", jsonCommercialSpeciesArray); - cursor.close(); - - return jsonMetier; - } - - public static JSONObject createJSONComercialSpeciesModel(CommercialSpeciesModel model, - final Context context, - WloSqlOpenHelper soh) throws JSONException { - Preconditions.checkNotNull(model); - Preconditions.checkNotNull(soh); - - JSONObject jsonCommercialSpecies = new JSONObject(); - - jsonCommercialSpecies.put("espèce-fao", createJSONCommercialSpecies(model.getFaoCode())); - jsonCommercialSpecies.put("méthode-mensuration", createJSONMensuration(model.getMeasurementMethod())); - jsonCommercialSpecies.put("état", createJSONState(model.getState())); - jsonCommercialSpecies.put("présentation", createJSONPresentation(model.getPresentation())); - jsonCommercialSpecies.put("précision", model.getPrecision().getValue()); - jsonCommercialSpecies.put("mélange-espèces", model.isSpeciesMix()); - jsonCommercialSpecies.put("catégorie-tri", model.getSortCategory()); - jsonCommercialSpecies.put("poids-total-déchargé", model.getTotalUnloadedWeight()); - jsonCommercialSpecies.put("commentaire", model.getComment()); - - Cursor cursor = soh.getAllScientificSpecies(model.getId()); - List<ScientificSpeciesModel> scientificSpecies = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, ScientificSpeciesModel>() { - @Override - public ScientificSpeciesModel apply(Cursor input) { - return new ScientificSpeciesModel(context, input); - } - }); - JSONArray jsonScientificSpeciesArray = new JSONArray(); - for (ScientificSpeciesModel scientificSpeciesModel : scientificSpecies) { - scientificSpeciesModel.setParent(model); - JSONObject jsonScientificSpecies = createJSONScientificSpeciesModel(scientificSpeciesModel, context, soh); - jsonScientificSpeciesArray.put(jsonScientificSpecies); - } - jsonCommercialSpecies.put("espèces-scientifiques", jsonScientificSpeciesArray); - cursor.close(); - - return jsonCommercialSpecies; - } - - public static JSONObject createJSONScientificSpeciesModel(ScientificSpeciesModel model, - final Context context, - WloSqlOpenHelper soh) throws JSONException { - Preconditions.checkNotNull(model); - Preconditions.checkNotNull(soh); - - JSONObject jsonScientificSpeciesModel = new JSONObject(); - jsonScientificSpeciesModel.put("espèce-scientifique", createJSONScientificSpecies(model.getName())); - jsonScientificSpeciesModel.put("prélèvement-pièces-calcifiées", model.isTakingActivation()); - jsonScientificSpeciesModel.put("commentaire", model.getComment()); - jsonScientificSpeciesModel.put("poids-trié", model.getSortedWeight()); - jsonScientificSpeciesModel.put("poids-échantillon", model.getSampleWeight()); - - Cursor cursor = soh.getAllMeasurements(model.getId()); - List<MeasurementModel> measurements = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, MeasurementModel>() { - @Override - public MeasurementModel apply(Cursor input) { - 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, categories); - jsonMeasurements.put(jsonMeasurement); - } - jsonScientificSpeciesModel.put("observations", jsonMeasurements); - cursor.close(); - - cursor = soh.getAllCategoryWeigths(model.getId()); - List<CategoryWeightModel> categoryWeights = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, CategoryWeightModel>() { - @Override - public CategoryWeightModel apply(Cursor input) { - return new CategoryWeightModel(input); - } - }); - JSONArray jsonCategoryWeightModels = new JSONArray(); - for (CategoryWeightModel categoryWeight : categoryWeights) { - JSONObject jsonCategoryWeightModel = createJSONCategoryWeightModel(categoryWeight, categories); - jsonCategoryWeightModels.put(jsonCategoryWeightModel); - } - jsonScientificSpeciesModel.put("poids-par-categorie", jsonCategoryWeightModels); - cursor.close(); - - return jsonScientificSpeciesModel; - } - - public static JSONObject createJSONMeasurementModel(MeasurementModel model, - 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); - - } else { - JSONObject jsonCategory = new JSONObject(); - jsonCategory.put("code", value.getValue()); - jsonCategory.put("libellé", value.getLabel()); - jsonMeasurementModel.put(categoryLabel, jsonCategory); - } - } - } - } - jsonMeasurementModel.put("taille", model.getSize()); - jsonMeasurementModel.put("date", UTC_DATE_FORMAT.format(model.getDate().getTime())); - - return jsonMeasurementModel; - } - - public static JSONObject createJSONCategoryWeightModel(CategoryWeightModel model, - 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); - - } else { - JSONObject jsonCategory = new JSONObject(); - jsonCategory.put("code", value.getValue()); - jsonCategory.put("libellé", value.getLabel()); - jsonCategoryWeightModel.put(categoryLabel, jsonCategory); - } - } - } - } - jsonCategoryWeightModel.put("poids", model.getWeight()); - - return jsonCategoryWeightModel; - } - - public static JSONObject createJSONLocation(Location location) throws JSONException { - if (location == null) { - return null; - } - JSONObject jsonLocation = new JSONObject(); - jsonLocation.put("type", location.getTypeLabel()); - jsonLocation.put("code", location.getCode()); - jsonLocation.put("libellé", location.getLabel()); - return jsonLocation; - } - - public static JSONObject createJSONMetier(Metier metier) throws JSONException { - if (metier == null) { - return null; - } - JSONObject jsonMetier = new JSONObject(); - jsonMetier.put("id", metier.getId()); - jsonMetier.put("code", metier.getCode()); - jsonMetier.put("libellé", metier.getLabel()); - jsonMetier.put("engin-code", metier.getGearCode()); - jsonMetier.put("engin-libellé", metier.getGearLabel()); - jsonMetier.put("espece-code", metier.getSpeciesCode()); - jsonMetier.put("espece-libellé", metier.getSpeciesLabel()); - jsonMetier.put("pêche", metier.getFishing()); - jsonMetier.put("actif", metier.getActive()); - return jsonMetier; - } - - public static JSONObject createJSONCommercialSpecies(CommercialSpecies commercialSpecies) throws JSONException { - if (commercialSpecies == null) { - return null; - } - JSONObject jsonCommercialSpecies = new JSONObject(); - jsonCommercialSpecies.put("code", commercialSpecies.getCode()); - jsonCommercialSpecies.put("isscap", commercialSpecies.getIsscap()); - jsonCommercialSpecies.put("code-taxon", commercialSpecies.getTaxonCode()); - jsonCommercialSpecies.put("libellé-scientifique", commercialSpecies.getScientificLabel()); - jsonCommercialSpecies.put("libellé-français", commercialSpecies.getFrenchLabel()); - jsonCommercialSpecies.put("famille", commercialSpecies.getFamily()); - jsonCommercialSpecies.put("ordre", commercialSpecies.getSpeciesOrder()); - jsonCommercialSpecies.put("actif", commercialSpecies.getActive()); - return jsonCommercialSpecies; - } - - public static JSONObject createJSONMensuration(Mensuration mensuration) throws JSONException { - if (mensuration == null) { - return null; - } - JSONObject jsonMensuration = new JSONObject(); - jsonMensuration.put("code", mensuration.getCode()); - jsonMensuration.put("libellé", mensuration.getLabel()); - return jsonMensuration; - } - - public static JSONObject createJSONState(State state) throws JSONException { - if (state == null) { - return null; - } - JSONObject jsonState = new JSONObject(); - jsonState.put("code", state.getCode()); - jsonState.put("libellé", state.getLabel()); - return jsonState; - } - - public static JSONObject createJSONPresentation(Presentation presentation) throws JSONException { - if (presentation == null) { - return null; - } - JSONObject jsonPresentation = new JSONObject(); - jsonPresentation.put("code", presentation.getCode()); - jsonPresentation.put("libellé", presentation.getLabel()); - return jsonPresentation; - } - - public static JSONObject createJSONScientificSpecies(ScientificSpecies scientificSpecies) throws JSONException { - if (scientificSpecies == null) { - return null; - } - JSONObject jsonScientificSpecies = new JSONObject(); - jsonScientificSpecies.put("code", scientificSpecies.getCode()); - jsonScientificSpecies.put("libellé", scientificSpecies.getLabel()); - jsonScientificSpecies.put("perm-code", scientificSpecies.getPermCode()); - return jsonScientificSpecies; - } - -} \ No newline at end of file Copied: trunk/src/fr/ifremer/wlo/storage/JsonExporter.java (from rev 81, trunk/src/fr/ifremer/wlo/storage/Exporter.java) =================================================================== --- trunk/src/fr/ifremer/wlo/storage/JsonExporter.java (rev 0) +++ trunk/src/fr/ifremer/wlo/storage/JsonExporter.java 2014-03-28 12:49:26 UTC (rev 84) @@ -0,0 +1,472 @@ +package fr.ifremer.wlo.storage; + +/* + * #%L + * WLO + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2013 - 2014 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import android.content.Context; +import android.database.Cursor; +import android.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 fr.ifremer.wlo.utils.UIUtils; +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) + * @since x.x + */ +public class JsonExporter { + + 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 + public ContextModel apply(Cursor input) { + return new ContextModel(input); + } + }); + for (ContextModel contextModel : contexts) { + JSONObject jsonContext = createJSONContextModel(contextModel, context, soh); + root.put(jsonContext); + } + + } finally { + soh.close(); + } + return root.toString(2); + } + + public static JSONObject createJSONContextModel(ContextModel model, + final Context context, + WloSqlOpenHelper soh) throws JSONException { + Preconditions.checkNotNull(model); + Preconditions.checkNotNull(soh); + + JSONObject jsonContext = new JSONObject(); + jsonContext.put("contexte", model.getName()); + jsonContext.put("commentaire", model.getComment()); + + Cursor cursor = soh.getAllLocations(model.getId()); + List<LocationModel> locations = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, LocationModel>() { + @Override + public LocationModel apply(Cursor input) { + return new LocationModel(context, input); + } + }); + JSONArray jsonLocations = new JSONArray(); + for (LocationModel location : locations) { + JSONObject jsonLocation = createJSONLocationModel(location, context, soh); + jsonLocations.put(jsonLocation); + } + jsonContext.put("lieux", jsonLocations); + cursor.close(); + + return jsonContext; + } + + public static JSONObject createJSONLocationModel(LocationModel model, + final Context context, + WloSqlOpenHelper soh) throws JSONException { + Preconditions.checkNotNull(model); + Preconditions.checkNotNull(soh); + + JSONObject jsonLocation = new JSONObject(); + + jsonLocation.put("lieu", createJSONLocation(model.getLocation())); + if (model.getStartDate() != null) { + jsonLocation.put("date-début", UIUtils.UTC_DATE_FORMAT.format(model.getStartDate().getTime())); + } + if (model.getEndDate() != null) { + jsonLocation.put("date-fin", UIUtils.UTC_DATE_FORMAT.format(model.getEndDate().getTime())); + } + jsonLocation.put("observateur", model.getOperator()); + jsonLocation.put("commentaire", model.getComment()); + + Cursor cursor = soh.getAllVessels(model.getId()); + List<VesselModel> vessels = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, VesselModel>() { + @Override + public VesselModel apply(Cursor input) { + return new VesselModel(context, input); + } + }); + JSONArray jsonVessels = new JSONArray(); + for (VesselModel vessel : vessels) { + JSONObject jsonVessel = createJSONVesselModel(vessel, context, soh); + jsonVessels.put(jsonVessel); + } + jsonLocation.put("navires", jsonVessels); + cursor.close(); + + return jsonLocation; + } + + public static JSONObject createJSONVesselModel(VesselModel model, + final Context context, + WloSqlOpenHelper soh) throws JSONException { + Preconditions.checkNotNull(model); + Preconditions.checkNotNull(soh); + + JSONObject jsonVessel = new JSONObject(); + + jsonVessel.put("immatriculation", model.getRegistrationNumber()); + jsonVessel.put("nom", model.getRegistrationNumber()); + if (model.getLandingDate() != null) { + jsonVessel.put("date-débarquement", UIUtils.UTC_DATE_FORMAT.format(model.getLandingDate().getTime())); + } + jsonVessel.put("lieu-débarquement", createJSONLocation(model.getLandingLocation())); + jsonVessel.put("commentaire", model.getComment()); + + Cursor cursor = soh.getAllMetiers(model.getId()); + List<MetierModel> metiers = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, MetierModel>() { + @Override + public MetierModel apply(Cursor input) { + return new MetierModel(context, input); + } + }); + JSONArray jsonMetiers = new JSONArray(); + for (MetierModel metier : metiers) { + JSONObject jsonMetier = createJSONMetierModel(metier, context, soh); + jsonMetiers.put(jsonMetier); + } + jsonVessel.put("métiers", jsonMetiers); + cursor.close(); + + return jsonVessel; + } + + public static JSONObject createJSONMetierModel(MetierModel model, + final Context context, + WloSqlOpenHelper soh) throws JSONException { + Preconditions.checkNotNull(model); + Preconditions.checkNotNull(soh); + + JSONObject jsonMetier = new JSONObject(); + + jsonMetier.put("engin-espèce", createJSONMetier(model.getGearSpecies())); + jsonMetier.put("secteur", model.getZone()); + jsonMetier.put("référence-plan", model.getSampleRowCode()); + jsonMetier.put("commentaire", model.getComment()); + + Cursor cursor = soh.getAllCommercialSpecies(model.getId()); + List<CommercialSpeciesModel> commercialSpecies = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, CommercialSpeciesModel>() { + @Override + public CommercialSpeciesModel apply(Cursor input) { + return new CommercialSpeciesModel(context, input); + } + }); + JSONArray jsonCommercialSpeciesArray = new JSONArray(); + for (CommercialSpeciesModel commercialSpeciesModel : commercialSpecies) { + JSONObject jsonCommercialSpecies = createJSONComercialSpeciesModel(commercialSpeciesModel, context, soh); + jsonCommercialSpeciesArray.put(jsonCommercialSpecies); + } + jsonMetier.put("espèces-fao", jsonCommercialSpeciesArray); + cursor.close(); + + return jsonMetier; + } + + public static JSONObject createJSONComercialSpeciesModel(CommercialSpeciesModel model, + final Context context, + WloSqlOpenHelper soh) throws JSONException { + Preconditions.checkNotNull(model); + Preconditions.checkNotNull(soh); + + JSONObject jsonCommercialSpecies = new JSONObject(); + + jsonCommercialSpecies.put("espèce-fao", createJSONCommercialSpecies(model.getFaoCode())); + jsonCommercialSpecies.put("méthode-mensuration", createJSONMensuration(model.getMeasurementMethod())); + jsonCommercialSpecies.put("état", createJSONState(model.getState())); + jsonCommercialSpecies.put("présentation", createJSONPresentation(model.getPresentation())); + jsonCommercialSpecies.put("précision", model.getPrecision().getValue()); + jsonCommercialSpecies.put("mélange-espèces", model.isSpeciesMix()); + jsonCommercialSpecies.put("catégorie-tri", model.getSortCategory()); + jsonCommercialSpecies.put("poids-total-déchargé", model.getTotalUnloadedWeight()); + jsonCommercialSpecies.put("commentaire", model.getComment()); + + Cursor cursor = soh.getAllScientificSpecies(model.getId()); + List<ScientificSpeciesModel> scientificSpecies = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, ScientificSpeciesModel>() { + @Override + public ScientificSpeciesModel apply(Cursor input) { + return new ScientificSpeciesModel(context, input); + } + }); + JSONArray jsonScientificSpeciesArray = new JSONArray(); + for (ScientificSpeciesModel scientificSpeciesModel : scientificSpecies) { + scientificSpeciesModel.setParent(model); + JSONObject jsonScientificSpecies = createJSONScientificSpeciesModel(scientificSpeciesModel, context, soh); + jsonScientificSpeciesArray.put(jsonScientificSpecies); + } + jsonCommercialSpecies.put("espèces-scientifiques", jsonScientificSpeciesArray); + cursor.close(); + + return jsonCommercialSpecies; + } + + public static JSONObject createJSONScientificSpeciesModel(ScientificSpeciesModel model, + final Context context, + WloSqlOpenHelper soh) throws JSONException { + Preconditions.checkNotNull(model); + Preconditions.checkNotNull(soh); + + JSONObject jsonScientificSpeciesModel = new JSONObject(); + jsonScientificSpeciesModel.put("espèce-scientifique", createJSONScientificSpecies(model.getName())); + jsonScientificSpeciesModel.put("prélèvement-pièces-calcifiées", model.isTakingActivation()); + jsonScientificSpeciesModel.put("commentaire", model.getComment()); + jsonScientificSpeciesModel.put("poids-trié", model.getSortedWeight()); + jsonScientificSpeciesModel.put("poids-échantillon", model.getSampleWeight()); + + Cursor cursor = soh.getAllMeasurements(model.getId()); + List<MeasurementModel> measurements = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, MeasurementModel>() { + @Override + public MeasurementModel apply(Cursor input) { + 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, categories); + jsonMeasurements.put(jsonMeasurement); + } + jsonScientificSpeciesModel.put("observations", jsonMeasurements); + cursor.close(); + + cursor = soh.getAllCategoryWeigths(model.getId()); + List<CategoryWeightModel> categoryWeights = WloSqlOpenHelper.transformCursorIntoCollection(cursor, new Function<Cursor, CategoryWeightModel>() { + @Override + public CategoryWeightModel apply(Cursor input) { + return new CategoryWeightModel(input); + } + }); + JSONArray jsonCategoryWeightModels = new JSONArray(); + for (CategoryWeightModel categoryWeight : categoryWeights) { + JSONObject jsonCategoryWeightModel = createJSONCategoryWeightModel(categoryWeight, categories); + jsonCategoryWeightModels.put(jsonCategoryWeightModel); + } + jsonScientificSpeciesModel.put("poids-par-categorie", jsonCategoryWeightModels); + cursor.close(); + + return jsonScientificSpeciesModel; + } + + public static JSONObject createJSONMeasurementModel(MeasurementModel model, + 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); + + } else { + JSONObject jsonCategory = new JSONObject(); + jsonCategory.put("code", value.getValue()); + jsonCategory.put("libellé", value.getLabel()); + jsonMeasurementModel.put(categoryLabel, jsonCategory); + } + } + } + } + jsonMeasurementModel.put("taille", model.getSize()); + jsonMeasurementModel.put("date", UIUtils.UTC_DATE_FORMAT.format(model.getDate().getTime())); + + return jsonMeasurementModel; + } + + public static JSONObject createJSONCategoryWeightModel(CategoryWeightModel model, + 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); + + } else { + JSONObject jsonCategory = new JSONObject(); + jsonCategory.put("code", value.getValue()); + jsonCategory.put("libellé", value.getLabel()); + jsonCategoryWeightModel.put(categoryLabel, jsonCategory); + } + } + } + } + jsonCategoryWeightModel.put("poids", model.getWeight()); + + return jsonCategoryWeightModel; + } + + public static JSONObject createJSONLocation(Location location) throws JSONException { + if (location == null) { + return null; + } + JSONObject jsonLocation = new JSONObject(); + jsonLocation.put("type", location.getTypeLabel()); + jsonLocation.put("code", location.getCode()); + jsonLocation.put("libellé", location.getLabel()); + return jsonLocation; + } + + public static JSONObject createJSONMetier(Metier metier) throws JSONException { + if (metier == null) { + return null; + } + JSONObject jsonMetier = new JSONObject(); + jsonMetier.put("id", metier.getId()); + jsonMetier.put("code", metier.getCode()); + jsonMetier.put("libellé", metier.getLabel()); + jsonMetier.put("engin-code", metier.getGearCode()); + jsonMetier.put("engin-libellé", metier.getGearLabel()); + jsonMetier.put("espece-code", metier.getSpeciesCode()); + jsonMetier.put("espece-libellé", metier.getSpeciesLabel()); + jsonMetier.put("pêche", metier.getFishing()); + jsonMetier.put("actif", metier.getActive()); + return jsonMetier; + } + + public static JSONObject createJSONCommercialSpecies(CommercialSpecies commercialSpecies) throws JSONException { + if (commercialSpecies == null) { + return null; + } + JSONObject jsonCommercialSpecies = new JSONObject(); + jsonCommercialSpecies.put("code", commercialSpecies.getCode()); + jsonCommercialSpecies.put("isscap", commercialSpecies.getIsscap()); + jsonCommercialSpecies.put("code-taxon", commercialSpecies.getTaxonCode()); + jsonCommercialSpecies.put("libellé-scientifique", commercialSpecies.getScientificLabel()); + jsonCommercialSpecies.put("libellé-français", commercialSpecies.getFrenchLabel()); + jsonCommercialSpecies.put("famille", commercialSpecies.getFamily()); + jsonCommercialSpecies.put("ordre", commercialSpecies.getSpeciesOrder()); + jsonCommercialSpecies.put("actif", commercialSpecies.getActive()); + return jsonCommercialSpecies; + } + + public static JSONObject createJSONMensuration(Mensuration mensuration) throws JSONException { + if (mensuration == null) { + return null; + } + JSONObject jsonMensuration = new JSONObject(); + jsonMensuration.put("code", mensuration.getCode()); + jsonMensuration.put("libellé", mensuration.getLabel()); + return jsonMensuration; + } + + public static JSONObject createJSONState(State state) throws JSONException { + if (state == null) { + return null; + } + JSONObject jsonState = new JSONObject(); + jsonState.put("code", state.getCode()); + jsonState.put("libellé", state.getLabel()); + return jsonState; + } + + public static JSONObject createJSONPresentation(Presentation presentation) throws JSONException { + if (presentation == null) { + return null; + } + JSONObject jsonPresentation = new JSONObject(); + jsonPresentation.put("code", presentation.getCode()); + jsonPresentation.put("libellé", presentation.getLabel()); + return jsonPresentation; + } + + public static JSONObject createJSONScientificSpecies(ScientificSpecies scientificSpecies) throws JSONException { + if (scientificSpecies == null) { + return null; + } + JSONObject jsonScientificSpecies = new JSONObject(); + jsonScientificSpecies.put("code", scientificSpecies.getCode()); + jsonScientificSpecies.put("libellé", scientificSpecies.getLabel()); + jsonScientificSpecies.put("perm-code", scientificSpecies.getPermCode()); + return jsonScientificSpecies; + } + +} \ No newline at end of file Modified: trunk/src/fr/ifremer/wlo/utils/ImportExportUtil.java =================================================================== --- trunk/src/fr/ifremer/wlo/utils/ImportExportUtil.java 2014-03-27 10:20:09 UTC (rev 83) +++ trunk/src/fr/ifremer/wlo/utils/ImportExportUtil.java 2014-03-28 12:49:26 UTC (rev 84) @@ -482,8 +482,7 @@ } try { - Export<M> exporter = Export.newExport(exportModel, data); - exporter.write(targetFile); + Export.exportToFile(exportModel, data, targetFile); result = data.size(); } catch (Exception e) { Modified: trunk/src/fr/ifremer/wlo/utils/UIUtils.java =================================================================== --- trunk/src/fr/ifremer/wlo/utils/UIUtils.java 2014-03-27 10:20:09 UTC (rev 83) +++ trunk/src/fr/ifremer/wlo/utils/UIUtils.java 2014-03-28 12:49:26 UTC (rev 84) @@ -36,6 +36,7 @@ import fr.ifremer.wlo.models.referentials.Mensuration; import fr.ifremer.wlo.preferences.ListItemPreference; +import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; @@ -47,6 +48,8 @@ private static final String TAG = "UIUtils"; + public static final SimpleDateFormat UTC_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ"); + /** * @return A dialog click listener which cancel the dialog */ Modified: trunk/src/fr/ifremer/wlo/utils/WloItemListViewBinder.java =================================================================== --- trunk/src/fr/ifremer/wlo/utils/WloItemListViewBinder.java 2014-03-27 10:20:09 UTC (rev 83) +++ trunk/src/fr/ifremer/wlo/utils/WloItemListViewBinder.java 2014-03-28 12:49:26 UTC (rev 84) @@ -120,7 +120,8 @@ textView.setText(ref != null ? ref.toString(context): ""); return true; } - if (DataType.SCIENTIFIC_SPECIES.equals(dataType)) { + if (DataType.SCIENTIFIC_SPECIES.equals(dataType) + || DataType.METIER.equals(dataType)) { textView.setText( context.getString(R.string.undefined)); return true;
participants (1)
-
kmorin@users.forge.codelutin.com