Android Open Source - UK-Weather-repo City Table






From Project

Back to project page UK-Weather-repo.

License

The source code is released under:

Apache License

If you think the Android project UK-Weather-repo 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

package com.haringeymobile.ukweather.database;
//from  ww  w .ja  v a  2 s . c  om
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;

import com.haringeymobile.ukweather.data.InitialCity;
import com.haringeymobile.ukweather.utils.MiscMethods;

/** The database table to hold city current weather and weather forecast records. */
public final class CityTable implements BaseColumns {

  public static final long CITY_NEVER_QUERIED = -1;
  public static final int CITY_ID_DOES_NOT_EXIST = -1;

  public static final String TABLE_CITIES = "Cities";
  public static final String COLUMN_CITY_ID = "Id";
  public static final String COLUMN_NAME = "Name";

  public static final String COLUMN_LAST_QUERY_TIME_FOR_CURRENT_WEATHER = "TimeCurrent";
  public static final String COLUMN_CACHED_JSON_CURRENT = "JsonCurrent";
  public static final String COLUMN_LAST_QUERY_TIME_FOR_DAILY_WEATHER_FORECAST = "TimeDailyForecast";
  public static final String COLUMN_CACHED_JSON_DAILY_FORECAST = "JsonDailyForecast";
  public static final String COLUMN_LAST_QUERY_TIME_FOR_THREE_HOURLY_WEATHER_FORECAST = "TimeThreeHourlyForecast";
  public static final String COLUMN_CACHED_JSON_THREE_HOURLY_FORECAST = "JsonThreeHourlyForecast";
  public static final String COLUMN_LAST_OVERALL_QUERY_TIME = "LastQueryTime";

  private static final String TABLE_TEMP = "tempTable";
  private static final String COLUMN_LAST_QUERY_TIME_FOR_CURRENT_WEATHER_VERSION_1 = "Date";
  private static final String COLUMN_CACHED_JSON_CURRENT_VERSION_1 = "Current";
  // Not used in version 1:
  // private static final String COLUMN_LAST_QUERY_TIME_FOR_WEATHER_FORECAST_VERSION_1 = "DateForecast";
  // private static final String COLUMN_CACHED_JSON_FORECAST_VERSION_1 = "Forecast";

  private static final String TABLE_CREATE = "CREATE TABLE "
      + TABLE_CITIES 
      + "(" 
      + _ID + " integer primary key autoincrement, "
      + COLUMN_CITY_ID + " integer, " 
      + COLUMN_NAME + " text not null, "
      + COLUMN_LAST_QUERY_TIME_FOR_CURRENT_WEATHER + " integer, "
      + COLUMN_CACHED_JSON_CURRENT + " text, "
      + COLUMN_LAST_QUERY_TIME_FOR_DAILY_WEATHER_FORECAST + " integer, "
      + COLUMN_CACHED_JSON_DAILY_FORECAST + " text, "
      + COLUMN_LAST_QUERY_TIME_FOR_THREE_HOURLY_WEATHER_FORECAST + " integer, "
      + COLUMN_CACHED_JSON_THREE_HOURLY_FORECAST + " text, "
      + COLUMN_LAST_OVERALL_QUERY_TIME + " integer"
      + ");";

  public static void onCreate(SQLiteDatabase database) {
    database.execSQL(TABLE_CREATE);
    insertInitialData(database);
  }

  /**
   * Inserts initial cities with their default values into the database.
   * 
   * @see InitialCity
   */
  private static void insertInitialData(SQLiteDatabase database) {
    for (InitialCity city : InitialCity.values()) {
      ContentValues newValues = new ContentValues();
      newValues.put(COLUMN_CITY_ID, city.getOpenWeatherMapId());
      newValues.put(COLUMN_NAME, city.getDisplayName());
      newValues.put(COLUMN_LAST_QUERY_TIME_FOR_CURRENT_WEATHER,
          CITY_NEVER_QUERIED);
      newValues.putNull(COLUMN_CACHED_JSON_CURRENT);
      putInitialDataForVersion2(newValues);
      database.insert(TABLE_CITIES, null, newValues);
    }
  }

  /**
   * Puts default values for the new columns in databse version 2 into the
   * provided {@link ContentValues}.
   * 
   * @param newValues
   *            values for the new record to be inserted into the database
   */
  private static void putInitialDataForVersion2(ContentValues newValues) {
    newValues.put(COLUMN_LAST_QUERY_TIME_FOR_DAILY_WEATHER_FORECAST,
        CITY_NEVER_QUERIED);
    newValues.putNull(COLUMN_CACHED_JSON_DAILY_FORECAST);
    newValues.put(COLUMN_LAST_QUERY_TIME_FOR_THREE_HOURLY_WEATHER_FORECAST,
        CITY_NEVER_QUERIED);
    newValues.putNull(COLUMN_CACHED_JSON_THREE_HOURLY_FORECAST);
    newValues.put(COLUMN_LAST_OVERALL_QUERY_TIME, CITY_NEVER_QUERIED);
  }

  public static void onUpgrade(SQLiteDatabase database, int oldVersion,
      int newVersion) {
    MiscMethods.log("Upgrading database from version " + oldVersion
        + " to version " + newVersion);
    if (oldVersion == 1 && newVersion > 1) {
      alterDatabaseVersion_1(database);
    } else {
      database.execSQL("DROP TABLE IF EXISTS " + TABLE_CITIES);
      onCreate(database);
    }
  }

  /** Performs transactions required to upgrade the database from version 1. */
  private static void alterDatabaseVersion_1(SQLiteDatabase database) {
    database.beginTransaction();
    try {
      alterCityTable(database);
      database.setTransactionSuccessful();
    } catch (Exception e) {
      throw new RuntimeException(
          "Error upgrading database from version 1");
    } finally {
      database.endTransaction();
    }
  }

  /**
   * Prepares the "Cities" table for alteration, and inserts the new columns;
   * this is required to upgrade the database from version 1.
   */
  private static void alterCityTable(SQLiteDatabase database) {
    String RENAME_ORIGINAL_TABLE = "ALTER TABLE " + TABLE_CITIES
        + " RENAME TO " + TABLE_TEMP;
    String COPY_OLD_TABLE_TO_NEW_TABLE = "INSERT INTO " + TABLE_CITIES
        + "(" + COLUMN_CITY_ID + ", " + COLUMN_NAME + ", "
        + COLUMN_LAST_QUERY_TIME_FOR_CURRENT_WEATHER + ", "
        + COLUMN_CACHED_JSON_CURRENT + ") SELECT " + COLUMN_CITY_ID
        + ", " + COLUMN_NAME + ", "
        + COLUMN_LAST_QUERY_TIME_FOR_CURRENT_WEATHER_VERSION_1 + ", "
        + COLUMN_CACHED_JSON_CURRENT_VERSION_1 + " FROM " + TABLE_TEMP
        + ";";

    database.execSQL(RENAME_ORIGINAL_TABLE);
    database.execSQL(TABLE_CREATE);
    database.execSQL(COPY_OLD_TABLE_TO_NEW_TABLE);
    database.execSQL("DROP TABLE " + TABLE_TEMP);

    insertInitialWeatherForecastValues(database);
  }

  /** Inserts new columns into the "Cities" table. */
  private static void insertInitialWeatherForecastValues(
      SQLiteDatabase database) {
    ContentValues initialForecastValues = new ContentValues();
    putInitialDataForVersion2(initialForecastValues);
    database.insert(TABLE_CITIES, null, initialForecastValues);
  }

}




Java Source Code List

com.astuetz.PagerSlidingTabStrip.java
com.haringeymobile.ukweather.AboutActivity.java
com.haringeymobile.ukweather.BaseCityCursorAdapter.java
com.haringeymobile.ukweather.BaseCityListFragmentWithButtons.java
com.haringeymobile.ukweather.CityListFragmentWithUtilityButtons.java
com.haringeymobile.ukweather.CityListFragmentWithWeatherButtons.java
com.haringeymobile.ukweather.CityManagementActivity.java
com.haringeymobile.ukweather.CitySearchResultsDialog.java
com.haringeymobile.ukweather.CityUtilitiesCursorAdapter.java
com.haringeymobile.ukweather.CityWeatherCursorAdapter.java
com.haringeymobile.ukweather.DeleteCityDialog.java
com.haringeymobile.ukweather.GetAvailableCitiesTask.java
com.haringeymobile.ukweather.MainActivity.java
com.haringeymobile.ukweather.SettingsActivityPreHoneycomb.java
com.haringeymobile.ukweather.SettingsActivity.java
com.haringeymobile.ukweather.SettingsFragment.java
com.haringeymobile.ukweather.WeatherCurrentInfoFragment.java
com.haringeymobile.ukweather.WeatherDailyWeatherForecastChildFragment.java
com.haringeymobile.ukweather.WeatherForecastParentFragment.java
com.haringeymobile.ukweather.WeatherInfoActivity.java
com.haringeymobile.ukweather.WeatherInfoFragment.java
com.haringeymobile.ukweather.WeatherInfoType.java
com.haringeymobile.ukweather.WeatherThreeHourlyForecastChildFragment.java
com.haringeymobile.ukweather.WorkerFragmentToRetrieveJsonString.java
com.haringeymobile.ukweather.data.InitialCity.java
com.haringeymobile.ukweather.data.JsonFetcher.java
com.haringeymobile.ukweather.data.OpenWeatherMapUrl.java
com.haringeymobile.ukweather.data.objects.CityCurrentWeather.java
com.haringeymobile.ukweather.data.objects.CityDailyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.CityInfo.java
com.haringeymobile.ukweather.data.objects.CityThreeHourlyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.Clouds.java
com.haringeymobile.ukweather.data.objects.Coordinates.java
com.haringeymobile.ukweather.data.objects.NumericParameters.java
com.haringeymobile.ukweather.data.objects.Rain.java
com.haringeymobile.ukweather.data.objects.SearchResponseForDailyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForFindQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForThreeHourlyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SystemParameters.java
com.haringeymobile.ukweather.data.objects.TemperatureScale.java
com.haringeymobile.ukweather.data.objects.Temperature.java
com.haringeymobile.ukweather.data.objects.WeatherInformation.java
com.haringeymobile.ukweather.data.objects.Weather.java
com.haringeymobile.ukweather.data.objects.WindSpeedMeasurementUnit.java
com.haringeymobile.ukweather.data.objects.Wind.java
com.haringeymobile.ukweather.database.CityTable.java
com.haringeymobile.ukweather.database.DatabaseHelper.java
com.haringeymobile.ukweather.database.GeneralDatabaseService.java
com.haringeymobile.ukweather.database.SqlOperation.java
com.haringeymobile.ukweather.database.WeatherContentProvider.java
com.haringeymobile.ukweather.utils.AsyncTaskWithProgressBar.java
com.haringeymobile.ukweather.utils.GlobalConstants.java
com.haringeymobile.ukweather.utils.MiscMethods.java
com.haringeymobile.ukweather.utils.SharedPrefsHelper.java