r53 - in trunk: . res/layout src/fr/ifremer/wlo src/fr/ifremer/wlo/storage
Author: kmorin Date: 2014-02-23 18:05:13 +0100 (Sun, 23 Feb 2014) New Revision: 53 Url: http://forge.codelutin.com/projects/wlo/repository/revisions/53 Log: refs #4193 [EXPORT] ?\195?\137cran Export des donn?\195?\169es Added: trunk/src/fr/ifremer/wlo/storage/Exporter.java Modified: trunk/pom.xml trunk/res/layout/main.xml trunk/src/fr/ifremer/wlo/MainActivity.java Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2014-02-19 14:00:00 UTC (rev 52) +++ trunk/pom.xml 2014-02-23 17:05:13 UTC (rev 53) @@ -166,13 +166,6 @@ </dependency> <dependency> - <groupId>com.google.code.gson</groupId> - <artifactId>gson</artifactId> - <version>${gsonVersion}</version> - <scope>compile</scope> - </dependency> - - <dependency> <groupId>org.achartengine</groupId> <artifactId>achartengine</artifactId> <version>1.1.0</version> Modified: trunk/res/layout/main.xml =================================================================== --- trunk/res/layout/main.xml 2014-02-19 14:00:00 UTC (rev 52) +++ trunk/res/layout/main.xml 2014-02-23 17:05:13 UTC (rev 53) @@ -30,7 +30,8 @@ android:onClick="openContexts"/> <Button style="@style/MainButton" - android:text="@string/main_export_data"/> + android:text="@string/main_export_data" + android:onClick="export"/> <Button style="@style/MainButton" android:id="@+id/main_connect_ichtyometer_button" Modified: trunk/src/fr/ifremer/wlo/MainActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/MainActivity.java 2014-02-19 14:00:00 UTC (rev 52) +++ trunk/src/fr/ifremer/wlo/MainActivity.java 2014-02-23 17:05:13 UTC (rev 53) @@ -29,7 +29,9 @@ import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.content.res.Configuration; +import android.database.Cursor; import android.os.Bundle; +import android.os.Environment; import android.os.Handler; import android.os.Message; import android.os.Messenger; @@ -40,13 +42,22 @@ import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; +import fr.ifremer.wlo.models.ContextModel; import fr.ifremer.wlo.models.referentials.imports.ImportUtil; 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.BigFinFeedReaderRecord; +import fr.ifremer.wlo.utils.filechooser.FileDialog; +import fr.ifremer.wlo.utils.filechooser.SelectionMode; +import org.apache.commons.io.FileUtils; +import org.json.JSONException; +import org.json.JSONObject; +import java.io.File; import java.io.IOException; +import java.util.Date; /** * @author kmorin <kmorin@codelutin.com> @@ -58,6 +69,7 @@ protected static final int REQUEST_ENABLE_BT = 1; protected static final int REQUEST_CONNECT_ICHTYOMETER = 2; + protected static final int REQUEST_SELECT_EXPORT_FOLDER = 3; // Local Bluetooth adapter protected BluetoothAdapter mBluetoothAdapter = null; @@ -215,6 +227,47 @@ setOrientation(newConfig.orientation); } + public void openContexts(View source) { + startActivity(new Intent(this, ContextsActivity.class)); + } + + public void connectIchtyometer(View source) { + // If BT is not on, request that it be enabled. + if (!mBluetoothAdapter.isEnabled()) { + Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); + startActivityForResult(enableIntent, REQUEST_ENABLE_BT); + + } else { + selectDevice(); + } + } + + public void disconnectIchtyometer(View source) { + Message message = Message.obtain(null, BigFinCommunicationService.MESSAGE_DISCONNECT_DEVICE); + try { + mServiceMessenger.send(message); + + } catch (RemoteException e) { + Log.e(TAG, "Error while sending data to the service"); + } + } + + public void openSettings(View source) { + startActivity(new Intent(this, SettingsActivity.class)); + } + + public void export(View source) { + Intent intent = new Intent(this, FileDialog.class); //Intent to start openIntents File Manager + intent.putExtra(FileDialog.START_PATH, "/sdcard"); + intent.putExtra(FileDialog.CAN_SELECT_DIR, true); + intent.putExtra(FileDialog.SELECTION_MODE, SelectionMode.MODE_OPEN); + + //alternatively you can set file filter + intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "json" }); + + startActivityForResult(intent, REQUEST_SELECT_EXPORT_FOLDER); + } + @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -239,6 +292,24 @@ } break; + case REQUEST_SELECT_EXPORT_FOLDER: + if (resultCode == Activity.RESULT_OK) { + try { + String jsonData = Exporter.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); + + } catch (JSONException | IOException e) { + Log.e(TAG, "error while exporting to JSON", e); + } + } + break; } } @@ -248,20 +319,20 @@ if (orientation == Configuration.ORIENTATION_LANDSCAPE) { logoParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, - LinearLayout.LayoutParams.MATCH_PARENT); + LinearLayout.LayoutParams.MATCH_PARENT); logoParams.weight = 1.0f; buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, - LinearLayout.LayoutParams.MATCH_PARENT); + LinearLayout.LayoutParams.MATCH_PARENT); buttonParams.weight = 1.0f; } else { logoParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT); + LinearLayout.LayoutParams.WRAP_CONTENT); logoParams.weight = 1.0f; buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT); + LinearLayout.LayoutParams.WRAP_CONTENT); buttonParams.weight = 0.0f; } logoPanel.setLayoutParams(logoParams); @@ -269,35 +340,6 @@ mainPanel.setOrientation(orientation); } - public void openContexts(View source) { - startActivity(new Intent(this, ContextsActivity.class)); - } - - public void connectIchtyometer(View source) { - // If BT is not on, request that it be enabled. - if (!mBluetoothAdapter.isEnabled()) { - Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); - startActivityForResult(enableIntent, REQUEST_ENABLE_BT); - - } else { - selectDevice(); - } - } - - public void disconnectIchtyometer(View source) { - Message message = Message.obtain(null, BigFinCommunicationService.MESSAGE_DISCONNECT_DEVICE); - try { - mServiceMessenger.send(message); - - } catch (RemoteException e) { - Log.e(TAG, "Error while sending data to the service"); - } - } - - public void openSettings(View source) { - startActivity(new Intent(this, SettingsActivity.class)); - } - protected void selectDevice() { // Launch the DeviceListActivity to see devices and do scan Intent serverIntent = new Intent(this, DeviceListActivity.class); Added: trunk/src/fr/ifremer/wlo/storage/Exporter.java =================================================================== --- trunk/src/fr/ifremer/wlo/storage/Exporter.java (rev 0) +++ trunk/src/fr/ifremer/wlo/storage/Exporter.java 2014-02-23 17:05:13 UTC (rev 53) @@ -0,0 +1,392 @@ +package fr.ifremer.wlo.storage; + +import android.content.Context; +import android.database.Cursor; +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import fr.ifremer.wlo.models.*; +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.List; + +/** + * @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"); + + public static String exportData(Context context) throws JSONException { + WloSqlOpenHelper soh = new WloSqlOpenHelper(context); + JSONArray root = new JSONArray(); + try { + + 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) { + 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); + } + }); + JSONArray jsonMeasurements = new JSONArray(); + for (MeasurementModel measurement : measurements) { + JSONObject jsonMeasurement = createJSONMeasurementModel(measurement, context); + 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, context); + jsonCategoryWeightModels.put(jsonCategoryWeightModel); + } + jsonScientificSpeciesModel.put("poids-par-categorie", jsonCategoryWeightModels); + cursor.close(); + + return jsonScientificSpeciesModel; + } + + public static JSONObject createJSONMeasurementModel(MeasurementModel model, + final Context context) throws JSONException { + Preconditions.checkNotNull(model); + + 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()); + } + 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())); + + return jsonMeasurementModel; + } + + public static JSONObject createJSONCategoryWeightModel(CategoryWeightModel model, + final Context context) throws JSONException { + Preconditions.checkNotNull(model); + + 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()); + } + Age age = DataCache.getAgeById(context, model.getCategory3()); + if (age != null) { + jsonCategoryWeightModel.put("âge", age.getLabel()); + } + 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; + } + + 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
participants (1)
-
kmorin@users.forge.codelutin.com