Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.05 KB | None | 0 0
  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2.  
  3.     private static DatabaseHelper helperInstance;
  4.  
  5.     private SQLiteDatabase myDataBase;
  6.     private final Context myContext;
  7.     private static final String DATABASE_NAME = "data.sqlite";
  8.     public static final int DATABASE_VERSION = 1;
  9.  
  10.     final private static int COLUMN_ID_COMPOSER_ID = 0;
  11.     final private static int COLUMN_ID_COMPOSER_LASTNAME = 1;
  12.     final private static int COLUMN_ID_COMPOSER_FIRSTNAME = 2;
  13.     final private static int COLUMN_ID_COMPOSER_BIRTHDATES = 3;
  14.     final private static int COLUMN_ID_COMPOSER_WIKIPEDIA_URL = 4;
  15.     final private static int COLUMN_ID_COMPOSER_IMSLP_URL = 5;
  16.     final private static int COLUMN_ID_COMPOSER_IMG_BIG = 6;
  17.     final private static int COLUMN_ID_COMPOSER_IMG_SMALL = 7;
  18.     final private static int COLUMN_ID_COMPOSER_IS_POPULAR = 8;
  19.  
  20.     /**
  21.      * Constructor
  22.      */
  23.     private DatabaseHelper(Context context) {
  24.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  25.         this.myContext = context;
  26.     }
  27.  
  28.     public static DatabaseHelper getInstance(Context context) {
  29.  
  30.         // Use the application context, which will ensure that you
  31.         // don't accidentally leak an Activity's context.
  32.         // See this article for more information: http://bit.ly/6LRzfx
  33.         if (helperInstance == null) {
  34.             helperInstance = new DatabaseHelper(context.getApplicationContext());
  35.         }
  36.         return helperInstance;
  37.     }
  38.  
  39.     // Create a empty database on the system
  40.     public void createDatabase() throws IOException {
  41.  
  42.         boolean dbExist = checkDataBase();
  43.  
  44.         if (dbExist) {
  45.             Logger.info("Database already exists.");
  46.             // By calling this method here onUpgrade will be called on a
  47.             // writeable database, but only if the version number has been
  48.             // bumped
  49.             // onUpgrade(myDataBase, DATABASE_VERSION_old, DATABASE_VERSION);
  50.         } else {
  51.             Logger.info("Database does not exist, copying from assets.");
  52.             this.getReadableDatabase();
  53.             try {
  54.                 this.close();
  55.                 copyDatabase();
  56.             } catch (IOException e) {
  57.                 throw new Error("Error copying database: " + e.getMessage());
  58.             }
  59.         }
  60.  
  61.     }
  62.  
  63.     // Check database already exist or not
  64.     private boolean checkDataBase() {
  65.         boolean checkDB = false;
  66.         try {
  67.             String myPath = myContext.getDatabasePath(DATABASE_NAME).getPath();
  68.             File databaseFile = new File(myPath);
  69.             checkDB = databaseFile.exists();
  70.         } catch (SQLiteException e) {
  71.             e.printStackTrace();
  72.         }
  73.         return checkDB;
  74.     }
  75.  
  76.     // Copies your database from your local assets-folder to the just created
  77.     // empty database in the system folder
  78.     private void copyDatabase() throws IOException {
  79.  
  80.         String outFileName = myContext.getDatabasePath(DATABASE_NAME).getPath();
  81.  
  82.         OutputStream myOutput = new FileOutputStream(outFileName);
  83.         InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
  84.  
  85.         byte[] buffer = new byte[1024];
  86.         int length;
  87.         while ((length = myInput.read(buffer)) > 0) {
  88.             myOutput.write(buffer, 0, length);
  89.         }
  90.         myInput.close();
  91.         myOutput.flush();
  92.         myOutput.close();
  93.     }
  94.  
  95.     // delete database
  96.     public void deleteDatabase() {
  97.         File databaseFile = new File(myContext.getDatabasePath(DATABASE_NAME).getPath());
  98.         if (databaseFile.exists()) {
  99.             databaseFile.delete();
  100.             Logger.info("Deleted database file.");
  101.         }
  102.     }
  103.  
  104.     // Open database
  105.     public void openDatabase() throws SQLException {
  106.         String myPath = myContext.getDatabasePath(DATABASE_NAME).getPath();
  107.         myDataBase = SQLiteDatabase.openDatabase(myPath, null,
  108.                 SQLiteDatabase.OPEN_READWRITE);
  109.     }
  110.  
  111.     public synchronized void closeDatabase() throws SQLException {
  112.         super.close();
  113.         if (myDataBase != null) {
  114.             myDataBase.close();
  115.         }
  116.     }
  117.  
  118.     public void onCreate(SQLiteDatabase db) {
  119.         // Empty implementation.
  120.     }
  121.  
  122.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  123.         if (newVersion > oldVersion) {
  124.             Log.w("Database", "Database version higher than old.");
  125.             deleteDatabase();
  126.         }
  127.     }
  128.  
  129.     public ArrayList<Composer> getPopularComposers() {
  130.         return getComposerList("SELECT * FROM AllComposers WHERE is_popular = 1");
  131.     }
  132.  
  133.     /**
  134.      * @param downloadedFile
  135.      * @deprecated
  136.      */
  137.     public void addDownloadedFile(DownloadedFile downloadedFile) {
  138.         SQLiteDatabase db = this.getWritableDatabase();
  139.  
  140.         ContentValues values = new ContentValues();
  141.         values.put("composer_id", 0);
  142.         values.put("path", downloadedFile.getPath());
  143.         values.put("piece_name", downloadedFile.getPieceName());
  144.         values.put("downloaded_at", downloadedFile.getTimestamp().toString());
  145.  
  146.         db.insert("Downloaded", null, values);
  147.         db.close();
  148.  
  149.     }
  150.  
  151.     public ArrayList<DownloadedFile> getDownloadedFiles() {
  152.  
  153.         ArrayList<DownloadedFile> resultList = new ArrayList<DownloadedFile>();
  154.         SQLiteDatabase db = this.getReadableDatabase();
  155.  
  156.         if (db == null) {
  157.             throw new NullPointerException();
  158.         }
  159.  
  160.         Cursor cursor = db.rawQuery("SELECT * FROM Downloaded", null);
  161.  
  162.         if (cursor == null) {
  163.             throw new NullPointerException();
  164.         }
  165.  
  166.         cursor.moveToFirst();
  167.  
  168.         while (!(cursor.isAfterLast())) {
  169.             final int id = cursor.getInt(0);
  170.             final int composerId = cursor.getInt(1);
  171.             final String path = cursor.getString(2);
  172.             final String pieceName = cursor.getString(3);
  173.             final String rawTimestamp = cursor.getString(4);
  174.             final Timestamp timestamp = Timestamp.valueOf(rawTimestamp);
  175.             DownloadedFile file = new DownloadedFile(id, composerId, pieceName, path, "-",
  176.                     timestamp);
  177.             resultList.add(file);
  178.             cursor.moveToNext();
  179.         }
  180.  
  181.         db.close();
  182.         cursor.close();
  183.         return resultList;
  184.     }
  185.  
  186.     public ArrayList<Composer> getAllComposers() {
  187.         return getComposerList("SELECT * FROM AllComposers");
  188.     }
  189.  
  190.     public ArrayList<Composer> getFavoriteComposers() {
  191.         String query = "SELECT * FROM AllComposers, FavoriteComposers WHERE composer_id = AllComposers.id";
  192.         return getComposerList(query);
  193.     }
  194.  
  195.     public ArrayList<Composer> getRecentComposers() {
  196.         return getComposerList("SELECT * FROM AllComposers, Recent WHERE composer_id = AllComposers.id");
  197.     }
  198.  
  199.  
  200.     private ArrayList<Composer> getComposerList(String query) {
  201.         ArrayList<Composer> resultList = new ArrayList<Composer>();
  202.         SQLiteDatabase db = this.getReadableDatabase();
  203.  
  204.         if (db == null) {
  205.             throw new NullPointerException();
  206.         }
  207.  
  208.         Cursor cursor = db.rawQuery(query, null);
  209.  
  210.         if (cursor == null) {
  211.             db.close();
  212.             throw new NullPointerException("Database cursor is null!");
  213.         }
  214.  
  215.         cursor.moveToFirst();
  216.  
  217.         while (!(cursor.isAfterLast())) {
  218.             resultList.add(cursorToComposer(cursor));
  219.             cursor.moveToNext();
  220.         }
  221.  
  222.         db.close();
  223.         cursor.close();
  224.         return resultList;
  225.     }
  226.  
  227.  
  228.     public Composer getComposerFromUrl(String url) {
  229.  
  230.         SQLiteDatabase db = this.getReadableDatabase();
  231.  
  232.         if (db == null) {
  233.             throw new NullPointerException();
  234.         }
  235.  
  236.         Cursor cursor = db.rawQuery("SELECT * FROM AllComposers WHERE imslp_url = \"" + url + "\"", null);
  237.  
  238.         if (cursor == null) {
  239.             throw new NullPointerException();
  240.         }
  241.  
  242.         cursor.moveToFirst();
  243.  
  244.         while (!(cursor.isAfterLast())) {
  245.             Composer composer = cursorToComposer(cursor);
  246.             db.close();
  247.             cursor.close();
  248.             boolean isFavorite = isFavoriteComposer(composer);
  249.             composer.setIsFavorite(isFavorite);
  250.             return composer;
  251.         }
  252.  
  253.         db.close();
  254.         cursor.close();
  255.         return null;
  256.     }
  257.  
  258.     private final static String IMSLP_BASE_URL = "http://imslp.org";
  259.  
  260.     private Composer cursorToComposer(Cursor cursor) {
  261.         final int id = cursor.getInt(COLUMN_ID_COMPOSER_ID);
  262.         final String lastName = cursor.getString(COLUMN_ID_COMPOSER_LASTNAME);
  263.         final String firstName = cursor.getString(COLUMN_ID_COMPOSER_FIRSTNAME);
  264.         final String birthDates = cursor.getString(COLUMN_ID_COMPOSER_BIRTHDATES);
  265.         final String rawImageBigUrl = cursor.getString(COLUMN_ID_COMPOSER_IMG_BIG);
  266.         final String imageBigUrl = rawImageBigUrl == null ? null : IMSLP_BASE_URL + rawImageBigUrl;
  267.         final String rawImageSmallUrl = cursor.getString(COLUMN_ID_COMPOSER_IMG_SMALL);
  268.         final String imageSmallUrl = rawImageSmallUrl == null ? null : IMSLP_BASE_URL + rawImageSmallUrl;
  269.         final String imslpUrl = cursor.getString(COLUMN_ID_COMPOSER_IMSLP_URL);
  270.         final String wikipediaUrl = cursor
  271.                 .getString(COLUMN_ID_COMPOSER_WIKIPEDIA_URL);
  272.         return new Composer(id, firstName, lastName, birthDates, imslpUrl, wikipediaUrl,
  273.                 imageBigUrl, imageSmallUrl);
  274.     }
  275.  
  276.  
  277.     public void removeFavoriteComposer(Composer composer) {
  278.         SQLiteDatabase db = this.getWritableDatabase();
  279.  
  280.         db.delete("FavoriteComposers", "composer_id = " + composer.getId(), null);
  281.         db.close();
  282.  
  283.     }
  284.  
  285.     public void addFavoriteComposer(Composer composer) {
  286.         SQLiteDatabase db = this.getWritableDatabase();
  287.  
  288.         ContentValues values = new ContentValues();
  289.         values.put("composer_id", composer.getId());
  290.  
  291.         db.insert("FavoriteComposers", null, values);
  292.         db.close();
  293.     }
  294.  
  295.     public boolean isFavoriteComposer(Composer composer) {
  296.         SQLiteDatabase db = this.getReadableDatabase();
  297.  
  298.         if (db == null) {
  299.             throw new NullPointerException();
  300.         }
  301.  
  302.         Cursor cursor = db.rawQuery("SELECT * FROM FavoriteComposers WHERE composer_id = " + composer.getId(), null);
  303.  
  304.         if (cursor == null) {
  305.             db.close();
  306.             return false;
  307.         }
  308.  
  309.         boolean isFavorite = cursor.getCount() > 0;
  310.         db.close();
  311.         cursor.close();
  312.  
  313.         return isFavorite;
  314.  
  315.     }
  316.  
  317.     public void addToRecentComposers(Composer composer) {
  318.         SQLiteDatabase db = this.getWritableDatabase();
  319.  
  320.         ContentValues values = new ContentValues();
  321.         values.put("composer_id", composer.getId());
  322.  
  323.         db.insert("Recent", null, values);
  324.         db.close();
  325.     }
  326.  
  327.  
  328.     @Deprecated
  329.     public void removeDownloadedFilesByUrl(String sourceUrl) {
  330.         SQLiteDatabase db = this.getWritableDatabase();
  331.         db.delete("Downloaded", "source_url = " + sourceUrl, null);
  332.         db.close();
  333.  
  334.     }
  335.  
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement