Android Open Source - field-data-app Springs Db Helper






From Project

Back to project page field-data-app.

License

The source code is released under:

MIT License

If you think the Android project field-data-app 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 nz.cri.gns.springs.db;
//ww w  .  ja  v  a2 s  . c o  m
import java.sql.SQLException;

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

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * Initiates creation of and connections to the app's database.
 * @author duncanw
 */
public class SpringsDbHelper extends OrmLiteSqliteOpenHelper  {
  
    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 31;
    public static final String DATABASE_NAME = "1000-Springs-DB";
    
    private RuntimeExceptionDao<Feature, Long> featureDao = null;
    private RuntimeExceptionDao<Survey, Long> surveyDao = null;
    private RuntimeExceptionDao<SurveyImage, Long> surveyImageDao = null;
    private RuntimeExceptionDao<BiologicalSample, Long> biologicalSampleDao = null;
    private RuntimeExceptionDao<ChecklistItem, Long> checklistItemDao = null;
    private RuntimeExceptionDao<Configuration, String> configurationDao = null;
    
    public SpringsDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

  @Override
  public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    createTables(connectionSource);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,int oldVersion, int newVersion) {
     
    if (oldVersion < 27) {
      RuntimeExceptionDao<BiologicalSample, Long> sampleDao = getBiologicalSampleDao();
      sampleDao.executeRaw("ALTER TABLE BiologicalSample ADD COLUMN gasVolume DOUBLE");
    }
    
    if (oldVersion < 28) {
      db.execSQL("ALTER TABLE BiologicalSample ADD COLUMN soilCollected BOOLEAN");
      db.execSQL("ALTER TABLE BiologicalSample ADD COLUMN waterColumnCollected BOOLEAN");
    }
    
    if (oldVersion < 29) {
      db.execSQL("ALTER TABLE Feature ADD COLUMN accessType STRING");
    }
    
    if (oldVersion < 30) {
      db.execSQL("ALTER TABLE Feature ADD COLUMN district STRING");
      db.execSQL("ALTER TABLE Feature ADD COLUMN location STRING");
    }
    
    if (oldVersion < 31) {
      db.execSQL("ALTER TABLE BiologicalSample ADD COLUMN settledAt4C BOOLEAN");
      db.execSQL("ALTER TABLE BiologicalSample ADD COLUMN tds DOUBLE");
    }
  }
  
  private void createTables(ConnectionSource connectionSource) {
    try {
      TableUtils.createTable(connectionSource, Feature.class);
      TableUtils.createTable(connectionSource, Survey.class);
      TableUtils.createTable(connectionSource, SurveyImage.class);
      TableUtils.createTable(connectionSource, BiologicalSample.class);
      TableUtils.createTable(connectionSource, ChecklistItem.class);
      TableUtils.createTable(connectionSource, Configuration.class);
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }    
  }
  
  public RuntimeExceptionDao<Feature, Long> getFeatureDao() {
    if (featureDao == null) {
      Dao<Feature, Long> dao;
      try {
        dao = DaoManager.createDao(getConnectionSource(), Feature.class);
        featureDao = new RuntimeExceptionDao<Feature, Long>(dao) {
          private SpringsUpdater <Feature> springsUpdater = new SpringsUpdater<Feature>();
          @Override
          public int update(Feature feature) {
            springsUpdater.update(feature, this);
            return super.update(feature);
          }
        };
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    }
    
    return featureDao;
  }
  
  public RuntimeExceptionDao<Survey, Long> getSurveyDao() {
    if (surveyDao == null) {
      Dao<Survey, Long> dao;
      try {
        dao = DaoManager.createDao(getConnectionSource(), Survey.class);
        surveyDao = new RuntimeExceptionDao<Survey, Long>(dao) {
          private SpringsUpdater <Survey> springsUpdater = new SpringsUpdater<Survey>();
          @Override
          public int update(Survey survey) {
            springsUpdater.update(survey, this);
            return super.update(survey);
          }
        };
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    }
    
    return surveyDao;
  }
  
  public RuntimeExceptionDao<SurveyImage, Long> getSurveyImageDao() {
    if (surveyImageDao == null) {
      Dao<SurveyImage, Long> dao;
      try {
        dao = DaoManager.createDao(getConnectionSource(), SurveyImage.class);
        surveyImageDao = new RuntimeExceptionDao<SurveyImage, Long>(dao) {
          private SpringsUpdater <SurveyImage> springsUpdater = new SpringsUpdater<SurveyImage>();
          @Override
          public int update(SurveyImage surveyImage) {
            springsUpdater.update(surveyImage, this);
            return super.update(surveyImage);
          }
        };
      } catch (SQLException e) {
        throw new RuntimeException();
      }
    }
    
    return surveyImageDao;
  }
  
  public RuntimeExceptionDao<BiologicalSample, Long> getBiologicalSampleDao() {
    if (biologicalSampleDao == null) {
      Dao<BiologicalSample, Long> dao;
      try {
        dao = DaoManager.createDao(getConnectionSource(), BiologicalSample.class);
        biologicalSampleDao = new RuntimeExceptionDao<BiologicalSample, Long>(dao) {
          private SpringsUpdater <BiologicalSample> springsUpdater = new SpringsUpdater<BiologicalSample>();
          @Override
          public int update(BiologicalSample biologicalSample) {
            springsUpdater.update(biologicalSample, this);
            return super.update(biologicalSample);
          }
        };
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    }
    
    return biologicalSampleDao;
  }
  
  public RuntimeExceptionDao<ChecklistItem, Long> getChecklistItemDao() {
    if (checklistItemDao == null) {
      Dao<ChecklistItem, Long> dao;
      try {
        dao = DaoManager.createDao(getConnectionSource(), ChecklistItem.class);
        checklistItemDao = new RuntimeExceptionDao<ChecklistItem, Long>(dao);
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    }
    
    return checklistItemDao;
  }
  
  public RuntimeExceptionDao<Configuration, String> getConfigurationDao() {
    if (configurationDao == null) {
      Dao<Configuration, String> dao;
      try {
        dao = DaoManager.createDao(getConnectionSource(), Configuration.class);
        configurationDao = new RuntimeExceptionDao<Configuration, String>(dao);
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    }
    
    return configurationDao;
  }  
  
  public static class SpringsUpdater<T extends PersistentObject> {
    
    public void update(T object, RuntimeExceptionDao<T, Long> dao) {
      T current = dao.queryForSameId(object);
      if (PersistentObject.Status.EXPORTED == current.getStatus()) {
        object.setStatus(Feature.Status.UPDATED);
      }
      object.setUpdatedDate(System.currentTimeMillis());      
    }
  }

}




Java Source Code List

nz.cri.gns.springs.GpsLocation.java
nz.cri.gns.springs.SpringsApplication.java
nz.cri.gns.springs.activity.BioSampleActivity.java
nz.cri.gns.springs.activity.EditBiologicalSamplesActivity.java
nz.cri.gns.springs.activity.MainMenuActivity.java
nz.cri.gns.springs.activity.ManageBioSamplesActivity.java
nz.cri.gns.springs.activity.SettingsActivity.java
nz.cri.gns.springs.db.BiologicalSampleTest.java
nz.cri.gns.springs.db.BiologicalSample.java
nz.cri.gns.springs.db.ChecklistItem.java
nz.cri.gns.springs.db.Configuration.java
nz.cri.gns.springs.db.FeatureTest.java
nz.cri.gns.springs.db.Feature.java
nz.cri.gns.springs.db.PersistentObject.java
nz.cri.gns.springs.db.SpringsDbHelper.java
nz.cri.gns.springs.db.SurveyImage.java
nz.cri.gns.springs.db.SurveyTest.java
nz.cri.gns.springs.db.Survey.java
nz.cri.gns.springs.fragments.AppearanceFragment.java
nz.cri.gns.springs.fragments.BioSampleActivityFragment.java
nz.cri.gns.springs.fragments.BioSampleFragment.java
nz.cri.gns.springs.fragments.ChooseImageFragment.java
nz.cri.gns.springs.fragments.ExportSamplesFragment.java
nz.cri.gns.springs.fragments.FeatureIdFragment.java
nz.cri.gns.springs.fragments.ImageColourPickerFragment.java
nz.cri.gns.springs.fragments.ImageFragment.java
nz.cri.gns.springs.fragments.SpringsDialogFragment.java
nz.cri.gns.springs.fragments.SpringsFragment.java
nz.cri.gns.springs.util.CustomExceptionHandler.java
nz.cri.gns.springs.util.DataStatistics.java
nz.cri.gns.springs.util.DateTimePickerDialog.java
nz.cri.gns.springs.util.UiUtil.java
nz.cri.gns.springs.util.Util.java