Android Open Source - AquaBase Table Helper






From Project

Back to project page AquaBase.

License

The source code is released under:

GNU General Public License

If you think the Android project AquaBase listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 *  This file is part of AquaBase.// w w  w .jav  a2 s  .co  m
 *
 *  AquaBase 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.
 *
 *  AquaBase 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 AquaBase.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright (c) 2014 Cdric Bosdonnat <cedric@bosdonnat.fr>
 */
package org.aquabase.data;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.net.http.AndroidHttpClient;
import android.util.Log;

public abstract class TableHelper {

    protected static void loadCsvHelper(Context context, String csvUrl,
                                        String tableName, CsvMapping mapping) {
        AndroidHttpClient httpClient = AndroidHttpClient.newInstance("AquaBase"); //$NON-NLS-1$
        try {
            // Grab the csv file and parse it
            HttpResponse response = httpClient.execute(new HttpGet(csvUrl));
            parseCsv(context, response.getEntity().getContent(), tableName, mapping);
        } catch (Exception e) {
            Log.e("Trace", "Failed to load CSV file", e); //$NON-NLS-1$ //$NON-NLS-2$
        }
        httpClient.close();
    }

    protected static void parseCsv(Context context, InputStream stream,
                                   String tableName, CsvMapping mapping) throws IOException {
        CSVParser parser = new CSVParser(stream);
        CSVParser.Result result = parser.parse();

        // First delete the content of the table
        Uri uri = Uri.parse(AquabaseContentProvider.BASE_URI + tableName);
        context.getContentResolver().delete(uri, null, null);

        // The push all entries into a brand new database file
        for (String[] row : result.getRows()) {
            ContentValues values = getContentValues(result.getColumns(), row, mapping);
            context.getContentResolver().insert(uri, values);
        }
    }

    protected static ContentValues getContentValues(String[] columns, String[] row, CsvMapping csvMapping) {

        ContentValues values = new ContentValues();
        for (int i = 0; i < columns.length; i++) {
            String csvName = columns[i];
            String value = row[i];
            String key = csvMapping.getColumnName(csvName);
            if (csvMapping.getType(csvName).equals(String.class)) {
                values.put(key, value);
            } else if (csvMapping.getType(csvName).equals(int.class)) {
                values.put(key, Integer.parseInt(value));
            } else if (csvMapping.getType(csvName).equals(double.class)) {
                values.put(key, Double.parseDouble(value));
            }
        }

        return values;
    }
}




Java Source Code List

org.aquabase.DetailsActivity.java
org.aquabase.DetailsFragment.java
org.aquabase.MainActivity.java
org.aquabase.NavigationDrawerFragment.java
org.aquabase.SpeciesListFragment.java
org.aquabase.data.AquabaseContentProvider.java
org.aquabase.data.CSVParser.java
org.aquabase.data.CrustaceanTableHelper.java
org.aquabase.data.CsvMapping.java
org.aquabase.data.DatabaseHelper.java
org.aquabase.data.FishTableHelper.java
org.aquabase.data.TableHelper.java