<commit_before>/*******************************************************************************
 * Copyright 2009 OmniDroid - http://code.google.com/p/omnidroid
 *
 * 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 edu.nyu.cs.omnidroid.app.model;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import edu.nyu.cs.omnidroid.app.controller.actions.CallPhoneAction;
import edu.nyu.cs.omnidroid.app.controller.actions.OmniAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SendGmailAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SendSmsAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SetPhoneLoudAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SetPhoneSilentAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SetPhoneVibrateAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SetScreenBrightnessAction;
import edu.nyu.cs.omnidroid.app.controller.actions.ShowAlertAction;
import edu.nyu.cs.omnidroid.app.controller.actions.ShowNotificationAction;
import edu.nyu.cs.omnidroid.app.controller.actions.ShowWebsiteAction;
import edu.nyu.cs.omnidroid.app.controller.actions.UpdateTwitterStatusAction;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniArea;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniDate;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniDayOfWeek;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniPasswordInput;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniPhoneNumber;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniText;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniTimePeriod;
import edu.nyu.cs.omnidroid.app.controller.events.LocationChangedEvent;
import edu.nyu.cs.omnidroid.app.controller.events.PhoneRingingEvent;
import edu.nyu.cs.omnidroid.app.controller.events.CallEndedEvent;
import edu.nyu.cs.omnidroid.app.controller.events.SMSReceivedEvent;
import edu.nyu.cs.omnidroid.app.controller.events.SystemEvent;
import edu.nyu.cs.omnidroid.app.controller.events.TimeTickEvent;
import edu.nyu.cs.omnidroid.app.model.db.DataFilterDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.DataTypeDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.DbHelper;
import edu.nyu.cs.omnidroid.app.model.db.ExternalAttributeDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredActionDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredActionParameterDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredAppDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredEventAttributeDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredEventDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RuleActionDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RuleActionParameterDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RuleDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RuleFilterDbAdapter;

/**
 * Class used for migrating a database for Omnidroid from one version to another.
 */
public class DbMigration {
  private static final String TAG = DbMigration.class.getSimpleName();

  /**
   * This class does not need to be instantiated.
   */
  private DbMigration() {
  }

  /**
   * Migrate the database to its latest version
   * 
   * @param db
   *          the database to migrate
   * @param currentDbVersionNumber
   *          the version number of the current database before migrating
   */
  public static void migrateToLatest(SQLiteDatabase db, int currentDbVersionNumber) {
    Log.i(TAG, "Migrating database from version " + currentDbVersionNumber);
    switch (currentDbVersionNumber) {
    case 1:
    case 2:
    case 3:
    case 4:
      initialVersion(db);
    case 5:
      addCallEndEvent(db);

      // insert new versions before this line
      break;
    default:
      Log.w(TAG, "Attempting to migrate from an unknown version!");
      break;
    }
  }

  /**
   * Create the initial version of the Omnidroid database along with prepopulated data.
   * 
   * @param db
   *          SQLiteDatabase object to work with
   */
  private static void initialVersion(SQLiteDatabase db) {
    /**
     * Create tables
     */
    db.execSQL(RegisteredAppDbAdapter.getSqliteCreateStatement());
    db.execSQL(RegisteredEventDbAdapter.getSqliteCreateStatement());
    db.execSQL(RegisteredEventAttributeDbAdapter.getSqliteCreateStatement());
    db.execSQL(RegisteredActionDbAdapter.getSqliteCreateStatement());
    db.execSQL(RegisteredActionParameterDbAdapter.getSqliteCreateStatement());
    db.execSQL(DataFilterDbAdapter.getSqliteCreateStatement());
    db.execSQL(DataTypeDbAdapter.getSqliteCreateStatement());
    db.execSQL(ExternalAttributeDbAdapter.getSqliteCreateStatement());
    db.execSQL(RuleDbAdapter.getSqliteCreateStatement());
    db.execSQL(RuleFilterDbAdapter.getSqliteCreateStatement());
    db.execSQL(RuleActionDbAdapter.getSqliteCreateStatement());<commit_msg>Added comments for Issue 82.<commit_after>/*******************************************************************************
 * Copyright 2009 OmniDroid - http://code.google.com/p/omnidroid
 *
 * 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 edu.nyu.cs.omnidroid.app.model;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import edu.nyu.cs.omnidroid.app.controller.actions.CallPhoneAction;
import edu.nyu.cs.omnidroid.app.controller.actions.OmniAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SendGmailAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SendSmsAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SetPhoneLoudAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SetPhoneSilentAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SetPhoneVibrateAction;
import edu.nyu.cs.omnidroid.app.controller.actions.SetScreenBrightnessAction;
import edu.nyu.cs.omnidroid.app.controller.actions.ShowAlertAction;
import edu.nyu.cs.omnidroid.app.controller.actions.ShowNotificationAction;
import edu.nyu.cs.omnidroid.app.controller.actions.ShowWebsiteAction;
import edu.nyu.cs.omnidroid.app.controller.actions.UpdateTwitterStatusAction;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniArea;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniDate;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniDayOfWeek;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniPasswordInput;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniPhoneNumber;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniText;
import edu.nyu.cs.omnidroid.app.controller.datatypes.OmniTimePeriod;
import edu.nyu.cs.omnidroid.app.controller.events.LocationChangedEvent;
import edu.nyu.cs.omnidroid.app.controller.events.PhoneRingingEvent;
import edu.nyu.cs.omnidroid.app.controller.events.CallEndedEvent;
import edu.nyu.cs.omnidroid.app.controller.events.SMSReceivedEvent;
import edu.nyu.cs.omnidroid.app.controller.events.SystemEvent;
import edu.nyu.cs.omnidroid.app.controller.events.TimeTickEvent;
import edu.nyu.cs.omnidroid.app.model.db.DataFilterDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.DataTypeDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.DbHelper;
import edu.nyu.cs.omnidroid.app.model.db.ExternalAttributeDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredActionDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredActionParameterDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredAppDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredEventAttributeDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RegisteredEventDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RuleActionDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RuleActionParameterDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RuleDbAdapter;
import edu.nyu.cs.omnidroid.app.model.db.RuleFilterDbAdapter;

/**
 * Class used for migrating a database for Omnidroid from one version to another.
 */
public class DbMigration {
  private static final String TAG = DbMigration.class.getSimpleName();

  /**
   * This class does not need to be instantiated.
   */
  private DbMigration() {
  }

  /**
   * Migrate the database to its latest version
   * 
   * @param db
   *          the database to migrate
   * @param currentDbVersionNumber
   *          the version number of the current database before migrating
   */
  public static void migrateToLatest(SQLiteDatabase db, int currentDbVersionNumber) {
    Log.i(TAG, "Migrating database from version " + currentDbVersionNumber);
    switch (currentDbVersionNumber) {
    case 1:
    case 2:
    case 3:
    case 4:
      initialVersion(db);
    case 5:
      addCallEndEvent(db);

      /*
       * Insert new versions before this line and do not forget to update {@code
       * DbHelper.DATABASE_VERSION}. Otherwise, the constructor call on SQLiteOpenHelper will not
       * trigger the {@code onUpgrade} callback method.
       */
      break;
    default:
      Log.w(TAG, "Attempting to migrate from an unknown version!");
      break;
    }
  }

  /**
   * Create the initial version of the Omnidroid database along with prepopulated data.
   * 
   * @param db
   *          SQLiteDatabase object to work with
   */
  private static void initialVersion(SQLiteDatabase db) {
    /**
     * Create tables
     */
    db.execSQL(RegisteredAppDbAdapter.getSqliteCreateStatement());
    db.execSQL(RegisteredEventDbAdapter.getSqliteCreateStatement());
    db.execSQL(RegisteredEventAttributeDbAdapter.getSqliteCreateStatement());
    db.execSQL(RegisteredActionDbAdapter.getSqliteCreateStatement());
    db.execSQL(RegisteredActionParameterDbAdapter.getSqliteCreateStatement());
    db.execSQL(DataFilterDbAdapter.getSqliteCreateStatement());
    db.execSQL(DataTypeDbAdapter.getSqliteCreateStatement());
    db.execSQL(ExternalAttributeDbAdapter.getSqliteCreateStatement());
    db.execSQL(RuleDbAdapter.getSqliteCreateStatement());
    db.execSQL(RuleFilterDbAdapter.getSqliteCreateStatement());
    db.execSQL(RuleActionDbAdapter.getSqliteCreateStatement());
{'fraction_non_alphanumeric': 0.1262747184534894, 'fraction_numerical': 0.002482929857231533, 'mean_word_length': 4.816400206291903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5775657', 'n_tokens_mistral': 3664, 'n_tokens_neox': 3612, 'n_words': 633}