Android Open Source - storm-gen Csv Table Reader






From Project

Back to project page storm-gen.

License

The source code is released under:

Apache License

If you think the Android project storm-gen 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

/*******************************************************************************
 * Copyright 2012 Google, Inc.//from w  w w  . ja va 2  s.  co m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.turbomanage.storm.csv;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.database.DatabaseUtils;
import android.database.DatabaseUtils.InsertHelper;
import android.database.sqlite.SQLiteDatabase;

import com.turbomanage.storm.TableHelper;

/**
 * Contains methods used to restore a table from a CSV file. Each
 * instance is associated with one table.
 *
 * @author David M. Chandler
 */
public class CsvTableReader {

  @SuppressWarnings("rawtypes")
  protected TableHelper th;
  private InsertHelper insertHelper;

  /**
   * Constructor requires a corresponding {@link TableHelper} class.
   *
   * @param tableHelper
   */
  @SuppressWarnings("rawtypes")
  public CsvTableReader(TableHelper tableHelper) {
    this.th = tableHelper;
  }

  protected String getCsvFilename(String dbPath, int dbVersion, String suffix) {
    String[] path = dbPath.split("/");
    String dbName = path[path.length-1];
    String tableName = th.getTableName();
    return String.format("%s.v%d.%s%s", dbName, dbVersion, tableName, suffix);
  }

  /**
   * Attempts to import a database table from a CSV file in the default
   * location.
   * 
   * @param ctx
   * @param db
   * @param suffix
   * 
   * @return count of rows imported or -1 if error
   */
  public int importFromCsv(Context ctx, SQLiteDatabase db, String suffix) {
    String filename = getCsvFilename(db.getPath(), db.getVersion(), suffix);
    FileInputStream fileInputStream;
    try {
      fileInputStream = ctx.openFileInput(filename);
      return importFromCsv(db, fileInputStream);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return -1;
    }
  }

  /**
   * Attempts to import a database table from an {@link InputStream}
   * formatted as a CSV file. Does all inserts in a single transaction
   * for efficiency and so that all inserts will succeed or fail
   * together.
   *
   * @param db
   * @param is
   * @return count of rows imported or -1 if error
   */
  public int importFromCsv(SQLiteDatabase db, InputStream is) {
    int numInserts = 0;
    db.beginTransaction();
    insertHelper = new DatabaseUtils.InsertHelper(db,
        th.getTableName());
    try {
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader reader = new BufferedReader(isr);
      String headerRow = reader.readLine();
      String csvRow = reader.readLine();
      while (csvRow != null) {
        long rowId = parseAndInsertRow(csvRow);
        if (rowId == -1L) {
          throw new RuntimeException("Error after row " + numInserts);
        }
        numInserts++;
        csvRow = reader.readLine();
      }
      db.setTransactionSuccessful();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      db.endTransaction();
    }
    return numInserts;
  }

  /**
   * Parse the values in a CSV row, map them to the table column
   * order, and insert. Uses {@link InsertHelper} so it may be
   * called repeatedly within a transaction for max performance.
   *
   * @param csvRow
   * @return row ID of the newly inserted row or -1
   */
  private long parseAndInsertRow(String csvRow) {
    List<String> textValues = CsvUtils.getValues(csvRow);
    insertHelper.prepareForInsert();
        String[] rowValues = textValues.toArray(new String[textValues.size()]);
    th.bindRowValues(insertHelper, rowValues);
    return insertHelper.execute();
  }

}




Java Source Code List

com.turbomanage.storm.DatabaseHelper.java
com.turbomanage.storm.ModelBase.java
com.turbomanage.storm.SQLiteDao.java
com.turbomanage.storm.TableHelper.java
com.turbomanage.storm.api.Converter.java
com.turbomanage.storm.api.DatabaseFactory.java
com.turbomanage.storm.api.Database.java
com.turbomanage.storm.api.Entity.java
com.turbomanage.storm.api.Id.java
com.turbomanage.storm.api.Syncable.java
com.turbomanage.storm.api.package-info.java
com.turbomanage.storm.apt.BaseDaoModel.java
com.turbomanage.storm.apt.ClassModel.java
com.turbomanage.storm.apt.ClassProcessor.java
com.turbomanage.storm.apt.ClassTemplate.java
com.turbomanage.storm.apt.MainProcessor.java
com.turbomanage.storm.apt.ProcessorLogger.java
com.turbomanage.storm.apt.SqlUtil.java
com.turbomanage.storm.apt.StormAnnotationException.java
com.turbomanage.storm.apt.StormEnvironment.java
com.turbomanage.storm.apt.converter.ConverterModel.java
com.turbomanage.storm.apt.converter.ConverterProcessor.java
com.turbomanage.storm.apt.converter.ConverterTypeAnnotationValueVisitor.java
com.turbomanage.storm.apt.converter.ConverterTypeAnnotationValuesVisitor.java
com.turbomanage.storm.apt.converter.package-info.java
com.turbomanage.storm.apt.database.DatabaseFactoryTemplate.java
com.turbomanage.storm.apt.database.DatabaseModel.java
com.turbomanage.storm.apt.database.DatabaseProcessor.java
com.turbomanage.storm.apt.database.package-info.java
com.turbomanage.storm.apt.entity.EntityDaoTemplate.java
com.turbomanage.storm.apt.entity.EntityModel.java
com.turbomanage.storm.apt.entity.EntityProcessor.java
com.turbomanage.storm.apt.entity.FieldModel.java
com.turbomanage.storm.apt.entity.TableHelperTemplate.java
com.turbomanage.storm.apt.entity.package-info.java
com.turbomanage.storm.csv.CsvTableReader.java
com.turbomanage.storm.csv.CsvTableWriter.java
com.turbomanage.storm.csv.CsvUtils.java
com.turbomanage.storm.csv.package-info.java
com.turbomanage.storm.exception.TooManyResultsException.java
com.turbomanage.storm.exception.TypeNotSupportedException.java
com.turbomanage.storm.exception.package-info.java
com.turbomanage.storm.query.Query.java
com.turbomanage.storm.query.package-info.java
com.turbomanage.storm.sample.activity.ContactActivity.java
com.turbomanage.storm.sample.activity.NewContactActivity.java
com.turbomanage.storm.sample.adapter.ContactAdapter.java
com.turbomanage.storm.sample.database.SampleDatabaseHelper.java
com.turbomanage.storm.sample.loader.AbstractAsyncTaskLoader.java
com.turbomanage.storm.sample.loader.ContactLoader.java
com.turbomanage.storm.sample.model.Contact.java
com.turbomanage.storm.types.BlobConverter.java
com.turbomanage.storm.types.BooleanConverter.java
com.turbomanage.storm.types.ByteConverter.java
com.turbomanage.storm.types.CharConverter.java
com.turbomanage.storm.types.DateConverter.java
com.turbomanage.storm.types.DoubleConverter.java
com.turbomanage.storm.types.EnumConverter.java
com.turbomanage.storm.types.FloatConverter.java
com.turbomanage.storm.types.IntegerConverter.java
com.turbomanage.storm.types.LongConverter.java
com.turbomanage.storm.types.PrimitiveConverter.java
com.turbomanage.storm.types.ShortConverter.java
com.turbomanage.storm.types.StringConverter.java
com.turbomanage.storm.types.TypeConverter.java
com.turbomanage.storm.types.package-info.java
com.turbomanage.storm.package-info.java