Android Open Source - AStory db Book






From Project

Back to project page AStory.

License

The source code is released under:

Apache License

If you think the Android project AStory 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.Litterfeldt.AStory.dbConnector;
/* ww  w.  j  av a  2s .com*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.Litterfeldt.AStory.models.Book;
import com.Litterfeldt.AStory.models.Chapter;
import java.util.ArrayList;

public class dbBook {

    public static void purge(Context c){
        (new dbBook(c)).purgeAllBooks();
    }
    public static ArrayList<Book> getBooks(Context c){
        return (new dbBook(c)).getAllBooks();
    }
    public static void addBook(Context c, Book b){
        (new dbBook(c)).addBook(b);
    }
    public static Book bookById(Context c, int id){
        return (new dbBook(c)).getBookById(id);
    }

    public static int bookIdByName(Context c, String name){
        return (new dbBook(c)).getBookId(name);
    }


    private static final String ALL_BOOKS = "SELECT * FROM "+ dbConnector.TABLE_NAME_BOOK_LIST + " ;";
    private static final String ALL_BOOKS_BY_ID = "SELECT * FROM " + dbConnector.TABLE_NAME_BOOK_LIST + " WHERE "+dbConnector.COLUMN_BOOK_LIST_ID+" = ? ;";
    private static final String ALL_CHAPTERS_BY_BOOK = "SELECT * FROM " + dbConnector.TABLE_NAME_CHAPTER_LIST + " WHERE BOOK_ID = ? ;";
    private static final String IMAGE = "Select IMG_BLOB from " + dbConnector.TABLE_NAME_BOOK_IMG +  " where BOOK_ID = ?;";
    private static final String PURGE = "DELETE FROM ";

    private dbConnector connector;

    public dbBook(Context c) {
        connector = dbConnector.getInstance(c);
    }
    public void addImg (int bookId, byte[] img){
        ContentValues initialValues = new ContentValues();
        initialValues.put(dbConnector.COLUMN_BOOK_IMG_BOOK_IMG,img);
        initialValues.put(dbConnector.COLUMN_BOOK_IMG_BOOK_ID,bookId);
        connector.write().insert(dbConnector.TABLE_NAME_BOOK_IMG, null, initialValues);
    }
    public void addChapter(Chapter chapter, int bookID){
        ContentValues initialValues = new ContentValues();
        initialValues.put(dbConnector.COLUMN_CHAPTER_LIST_CHAPTER_PATH,chapter.Path());
        initialValues.put(dbConnector.COLUMN_CHAPTER_LIST_CHAPTER_NR,chapter.Nr());
        initialValues.put(dbConnector.COLUMN_CHAPTER_LIST_CHAPTER_DURATION,chapter.Duration());
        initialValues.put(dbConnector.COLUMN_CHAPTER_LIST_BOOK_ID,bookID);
        connector.write().insert(dbConnector.TABLE_NAME_CHAPTER_LIST, null, initialValues);

    }
    public void addBook(Book book) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(dbConnector.COLUMN_BOOK_LIST_NAME,book.name());
        initialValues.put(dbConnector.COLUMN_BOOK_LIST_AUTHOR,book.author());
        connector.write().insert(dbConnector.TABLE_NAME_BOOK_LIST, null, initialValues);
        int bookID = getBookId(book.name());
        for (Chapter chapter : book.getChapters()){
            addChapter(chapter, bookID);
        }
        addImg(bookID,book.image());
    }
    public int getBookId(String bookName){
        Cursor c = connector.read().query(dbConnector.TABLE_NAME_BOOK_LIST,
                new String[]{dbConnector.COLUMN_BOOK_LIST_ID},
                dbConnector.COLUMN_BOOK_LIST_NAME+"="+"\""+bookName+"\"",
                null,null,null,null);
        c.moveToFirst();
        return c.getInt(0);
    }
    private Book getBookById(int id){
        Cursor bookCursor = connector.read().rawQuery(ALL_BOOKS_BY_ID.replace("?",Integer.toString(id)), null);
        boolean notEmpty = bookCursor.moveToFirst();
        if (notEmpty) {
            String name = bookCursor.getString(1);
            String author = bookCursor.getString(2);
            ArrayList<Chapter> chapters = getAllChapters(id);
            byte[] img = getImageById(id);
            Book book = new Book(id,name, author, chapters, img);
            return book;
        } else {
            return null;
        }


    }
    public void purgeAllBooks(){
        connector.write().execSQL(PURGE + dbConnector.TABLE_NAME_BOOK_LIST +";" );
        connector.write().execSQL(PURGE + dbConnector.TABLE_NAME_CHAPTER_LIST+";" );
        connector.write().execSQL(PURGE + dbConnector.TABLE_NAME_BOOK_IMG+";" );
    }
    public ArrayList<Book> getAllBooks() {
        Cursor bookCursor = connector.read().rawQuery(ALL_BOOKS, null);
        boolean notEmpty = bookCursor.moveToFirst();
        if (notEmpty) {
            ArrayList<Book> books = new ArrayList<Book>();
            while(!bookCursor.isAfterLast()){
                int id = bookCursor.getInt(0);
                String name = bookCursor.getString(1);
                String author = bookCursor.getString(2);
                ArrayList<Chapter> chapters = getAllChapters(id);
                byte[] img = getImageById(id);
                Book book = new Book(id,name, author, chapters, img);
                books.add(book);
                bookCursor.moveToNext();
            }
            return books;
        } else {
            return new ArrayList<Book>();
        }
    }
    public byte[] getImageById(int bookId) {
        Cursor imageCursor = connector.read().rawQuery(IMAGE.replace("?", Integer.toString(bookId)), null);
        boolean notEmpty = imageCursor.moveToFirst();
        if (notEmpty) {
            return imageCursor.getBlob(0);
        } else {
            return null;
        }
    }
    public ArrayList<Chapter> getAllChapters(int bookId) {
        Cursor chapterCursor = connector.read()
                .rawQuery(ALL_CHAPTERS_BY_BOOK.replace("?", Integer.toString(bookId)), null);
        boolean notEmpty = chapterCursor.moveToFirst();
        if (notEmpty) {
            ArrayList<Chapter> chapters = new ArrayList<Chapter>();
            while(!chapterCursor.isAfterLast()){
                int chapterID = chapterCursor.getInt(0);
                int chapterNr = chapterCursor.getInt(2);
                String chapterPath = chapterCursor.getString(3);
                int chapterDuration = chapterCursor.getInt(4);
                Chapter chapter = new Chapter(chapterID, chapterPath, chapterNr, chapterDuration);
                chapters.add(chapter);
                chapterCursor.moveToNext();
            }

            return chapters;
        } else {
            return null;
        }
    }
}




Java Source Code List

com.Litterfeldt.AStory.adapters.LibraryAdapter.java
com.Litterfeldt.AStory.customClasses.CoreApplication.java
com.Litterfeldt.AStory.customClasses.CustomMediaPlayer.java
com.Litterfeldt.AStory.dbConnector.dbBook.java
com.Litterfeldt.AStory.dbConnector.dbConnector.java
com.Litterfeldt.AStory.dbConnector.dbSave.java
com.Litterfeldt.AStory.fragments.LibraryFragment.java
com.Litterfeldt.AStory.fragments.PlayerFragment.java
com.Litterfeldt.AStory.models.Book.java
com.Litterfeldt.AStory.models.Chapter.java
com.Litterfeldt.AStory.models.FileSystem.java
com.Litterfeldt.AStory.models.SaveState.java
com.Litterfeldt.AStory.services.AudioplayerService.java
com.Litterfeldt.AStory.pagerView.java
com.handmark.pulltorefresh.library.ILoadingLayout.java
com.handmark.pulltorefresh.library.IPullToRefresh.java
com.handmark.pulltorefresh.library.LoadingLayoutProxy.java
com.handmark.pulltorefresh.library.OverscrollHelper.java
com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.java
com.handmark.pulltorefresh.library.PullToRefreshBase.java
com.handmark.pulltorefresh.library.PullToRefreshExpandableListView.java
com.handmark.pulltorefresh.library.PullToRefreshGridView.java
com.handmark.pulltorefresh.library.PullToRefreshHorizontalScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshListView.java
com.handmark.pulltorefresh.library.PullToRefreshScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshWebView.java
com.handmark.pulltorefresh.library.extras.PullToRefreshWebView2.java
com.handmark.pulltorefresh.library.extras.SoundPullEventListener.java
com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor.java
com.handmark.pulltorefresh.library.internal.FlipLoadingLayout.java
com.handmark.pulltorefresh.library.internal.IndicatorLayout.java
com.handmark.pulltorefresh.library.internal.LoadingLayout.java
com.handmark.pulltorefresh.library.internal.RotateLoadingLayout.java
com.handmark.pulltorefresh.library.internal.Utils.java
com.handmark.pulltorefresh.library.internal.ViewCompat.java