Android Open Source - dejalist Dejalist Database






From Project

Back to project page dejalist.

License

The source code is released under:

Apache License

If you think the Android project dejalist 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.luboganev.dejalist.data;
/*  w  w w. ja  v  a  2s .co m*/
import static nl.qbusict.cupboard.CupboardFactory.cupboard;

import java.io.File;

import nl.qbusict.cupboard.EntityCompartment;

import com.luboganev.dejalist.R;
import com.luboganev.dejalist.data.DejalistContract.Categories;
import com.luboganev.dejalist.data.DejalistContract.Products;
import com.luboganev.dejalist.data.entities.Category;
import com.luboganev.dejalist.data.entities.Product;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;

public class DejalistDatabase extends SQLiteOpenHelper {

  private static final String DATABASE_NAME = "dejalist_db";
  private static final int DATABASE_VERSION = 3;
  
  interface Tables {
    String PRODUCTS = "products";
    String CATEGORIES = "categories";
  }
  
  static {
    cupboard().register(Product.class);
    cupboard().register(Category.class);
  }
  
  private String[] mSampleCategoriesNames;
  
  private Context mContext;
  
    public DejalistDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mSampleCategoriesNames = context.getResources().getStringArray(R.array.sample_categories);
        mContext = context;
    }
    
  public DejalistDatabase(Context context, String name, CursorFactory factory, int version) {
    super(context, name, factory, version);
    mSampleCategoriesNames = context.getResources().getStringArray(R.array.sample_categories);
    mContext = context;
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
     db.execSQL("CREATE TABLE " + Tables.PRODUCTS + " (" +
           BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
          Products.PRODUCT_NAME + " TEXT, " +
          Products.PRODUCT_URI + " TEXT, " +
          Products.PRODUCT_INLIST + " INTEGER, " +
          Products.PRODUCT_CHECKED + " INTEGER, " +
          Products.PRODUCT_CATEGORY_ID + " INTEGER, " +
          Products.PRODUCT_USED_COUNT + " INTEGER, " +
          Products.PRODUCT_LAST_USED + " INTEGER, " +
          Products.PRODUCT_DELETED + " INTEGER " +
        ");");
     
     db.execSQL("CREATE TABLE " + Tables.CATEGORIES + " (" +
           BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
          Categories.CATEGORY_NAME + " TEXT, " +
          Categories.CATEGORY_COLOR + " INTEGER " +
        ");");
     
     insertSampleCategories(db);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    switch(oldVersion) {
      case 1:
        // add new columns
                db.execSQL("ALTER TABLE " + Tables.PRODUCTS + " ADD COLUMN "
                    + Products.PRODUCT_CATEGORY_ID + " INTEGER DEFAULT " + Products.PRODUCT_CATEGORY_NONE_ID);
                
                db.execSQL("ALTER TABLE " + Tables.PRODUCTS + " ADD COLUMN "
                    + Products.PRODUCT_USED_COUNT + " INTEGER DEFAULT 0");
                
                db.execSQL("ALTER TABLE " + Tables.PRODUCTS + " ADD COLUMN "
                    + Products.PRODUCT_LAST_USED + " INTEGER DEFAULT 0");
                
                db.execSQL("ALTER TABLE " + Tables.PRODUCTS + " ADD COLUMN "
                    + Products.PRODUCT_DELETED + " INTEGER DEFAULT 0");
                
                // move the pictures from the external storage
                movePicturesAndUpdateDb(db);
        
                // add the new table for categories
        db.execSQL("CREATE TABLE " + Tables.CATEGORIES + " (" +
             BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            Categories.CATEGORY_NAME + " TEXT, " +
            Categories.CATEGORY_COLOR + " INTEGER " +
          ");");
        
        // add the sample categories
        insertSampleCategories(db);
      case 2:
        /**
         * This is change moves product images to a new internal folder because the old
         * used to be created in a bad way. This is necessary only in case of upgrading from
         * version 2 to version 3. Otherwise, the correct directory is being already used for
         * moving files when upgrading from version 1 to version 2
         */
        if(oldVersion == 2) {
          // move the pictures from the wrong internal folder to the new correct internal folder
                  movePicturesAndUpdateDb(db);
          // Finally get rid of all the old pictures and the wrong folder
          ProductImageFileHelper.deleteWrongImagesFolder(mContext);
        }
      default:
        break;
    }
  }
  
  private void movePicturesAndUpdateDb(SQLiteDatabase db) {
        Cursor c = db.query(
            Tables.PRODUCTS, 
        new String[]{Products._ID, Products.PRODUCT_URI}, 
        null,
        null,
        null, 
        null, 
        null);
    
        if(c.getCount() > 0) {
          c.moveToFirst();
          long[] productIds = new long[c.getCount()];
          String[] productUris = new String[c.getCount()];
          
          int i=0;
          do {
            productIds[i] = c.getLong(0);
            productUris[i] = c.getString(1);
            i++;
      } while (c.moveToNext());
          
          for (i = 0; i < productIds.length; i++) {
            // if no picture just move to the next product
            if(productUris[i] == null) continue;
            // copy the product picture
            Uri productFileUri = ProductImageFileHelper.copyToANewProductImageFile(mContext, Uri.parse(productUris[i]));
            // update product uri
            ContentValues values = new ContentValues();
            values.put(Products.PRODUCT_URI, productFileUri.toString());
            db.update(Tables.PRODUCTS, values, Products._ID + " = ?", new String[] {String.valueOf(productIds[i])});
            // delete old picture
            ProductImageFileHelper.deleteProductImageFile(Uri.parse(productUris[i]));
      }
        }
        c.close();
  }
  
  private void insertSampleCategories(SQLiteDatabase db) {
    EntityCompartment<Category> ec = cupboard().withEntity(Category.class);
    
    Category category = new Category();
    category.color = 0xFFE5E539;
    category.name = mSampleCategoriesNames[0];
    db.insert(Tables.CATEGORIES, null, ec.toContentValues(category));
    
    category = new Category();
    category.color = 0xFF5ADD45;
    category.name = mSampleCategoriesNames[1];
    db.insert(Tables.CATEGORIES, null, ec.toContentValues(category));
    
    category = new Category();
    category.color = 0xFFE55CE5;
    category.name = mSampleCategoriesNames[2];
    db.insert(Tables.CATEGORIES, null, ec.toContentValues(category));
    
    category = new Category();
    category.color = 0xFF4040FF;
    category.name = mSampleCategoriesNames[3];
    db.insert(Tables.CATEGORIES, null, ec.toContentValues(category));
  }
  
    public static void deleteDatabase(Context context) {
        context.deleteDatabase(DATABASE_NAME);
    }
}




Java Source Code List

com.larswerkman.colorpicker.ColorPicker.java
com.larswerkman.colorpicker.OpacityBar.java
com.larswerkman.colorpicker.SVBar.java
com.larswerkman.colorpicker.SaturationBar.java
com.larswerkman.colorpicker.ValueBar.java
com.luboganev.dejalist.Utils.java
com.luboganev.dejalist.crop.CropActivity.java
com.luboganev.dejalist.crop.CropDialogSave.java
com.luboganev.dejalist.crop.CropHighlightView.java
com.luboganev.dejalist.crop.CropUtils.java
com.luboganev.dejalist.crop.CropView.java
com.luboganev.dejalist.crop.ImageViewTouchBase.java
com.luboganev.dejalist.crop.RotateBitmap.java
com.luboganev.dejalist.data.BackupIntentService.java
com.luboganev.dejalist.data.CacheManager.java
com.luboganev.dejalist.data.DejalistContract.java
com.luboganev.dejalist.data.DejalistDatabase.java
com.luboganev.dejalist.data.DejalistProvider.java
com.luboganev.dejalist.data.ProductImageFileHelper.java
com.luboganev.dejalist.data.SelectionBuilder.java
com.luboganev.dejalist.data.entities.Category.java
com.luboganev.dejalist.data.entities.Product.java
com.luboganev.dejalist.ui.AboutActivity.java
com.luboganev.dejalist.ui.CategoriesListCursorAdapter$ViewHolder$$ViewInjector.java
com.luboganev.dejalist.ui.CategoriesListCursorAdapter.java
com.luboganev.dejalist.ui.CategoryDialogFragment$$ViewInjector.java
com.luboganev.dejalist.ui.CategoryDialogFragment.java
com.luboganev.dejalist.ui.CheckableRelativeLayout.java
com.luboganev.dejalist.ui.ChecklistActionTaker.java
com.luboganev.dejalist.ui.ChecklistController.java
com.luboganev.dejalist.ui.ChecklistCursorAdapter$ViewHolder$$ViewInjector.java
com.luboganev.dejalist.ui.ChecklistCursorAdapter.java
com.luboganev.dejalist.ui.ChecklistFragment$$ViewInjector.java
com.luboganev.dejalist.ui.ChecklistFragment.java
com.luboganev.dejalist.ui.ConfirmBackResDialogFragment.java
com.luboganev.dejalist.ui.MainActivity$$ViewInjector.java
com.luboganev.dejalist.ui.MainActivity.java
com.luboganev.dejalist.ui.NavigationCursorAdapter$ViewHolder$$ViewInjector.java
com.luboganev.dejalist.ui.NavigationCursorAdapter.java
com.luboganev.dejalist.ui.ProductActivity$$ViewInjector.java
com.luboganev.dejalist.ui.ProductActivity.java
com.luboganev.dejalist.ui.ProductsGalleryActionTaker.java
com.luboganev.dejalist.ui.ProductsGalleryController.java
com.luboganev.dejalist.ui.ProductsGalleryCursorAdapter$ViewHolder$$ViewInjector.java
com.luboganev.dejalist.ui.ProductsGalleryCursorAdapter.java
com.luboganev.dejalist.ui.ProductsGalleryFragment$$ViewInjector.java
com.luboganev.dejalist.ui.ProductsGalleryFragment.java
com.luboganev.dejalist.ui.SetProductsCategoryDialogFragment.java
com.luboganev.dejalist.ui.UndoBarController.java