Search in sources :

Example 1 with DaoSession

use of com.hippo.ehviewer.dao.DaoSession in project EhViewer by seven332.

the class EhDB method importDB.

/**
 * @param file The db file
 * @return error string, null for no error
 */
public static synchronized String importDB(Context context, File file) {
    try {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        int newVersion = DaoMaster.SCHEMA_VERSION;
        int oldVersion = db.getVersion();
        if (oldVersion < newVersion) {
            upgradeDB(db, oldVersion);
            db.setVersion(newVersion);
        } else if (oldVersion > newVersion) {
            return context.getString(R.string.cant_read_the_file);
        }
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession session = daoMaster.newSession();
        // Downloads
        DownloadManager manager = EhApplication.getDownloadManager(context);
        List<DownloadInfo> downloadInfoList = session.getDownloadsDao().queryBuilder().list();
        manager.addDownload(downloadInfoList);
        // Download label
        List<DownloadLabel> downloadLabelList = session.getDownloadLabelDao().queryBuilder().list();
        manager.addDownloadLabel(downloadLabelList);
        // Download dirname
        List<DownloadDirname> downloadDirnameList = session.getDownloadDirnameDao().queryBuilder().list();
        for (DownloadDirname dirname : downloadDirnameList) {
            putDownloadDirname(dirname.getGid(), dirname.getDirname());
        }
        // History
        List<HistoryInfo> historyInfoList = session.getHistoryDao().queryBuilder().list();
        putHistoryInfo(historyInfoList);
        // QuickSearch
        List<QuickSearch> quickSearchList = session.getQuickSearchDao().queryBuilder().list();
        List<QuickSearch> currentQuickSearchList = sDaoSession.getQuickSearchDao().queryBuilder().list();
        for (QuickSearch quickSearch : quickSearchList) {
            String name = quickSearch.name;
            for (QuickSearch q : currentQuickSearchList) {
                if (ObjectUtils.equal(q.name, name)) {
                    // The same name
                    name = null;
                    break;
                }
            }
            if (null == name) {
                continue;
            }
            insertQuickSearch(quickSearch);
        }
        // LocalFavorites
        List<LocalFavoriteInfo> localFavoriteInfoList = session.getLocalFavoritesDao().queryBuilder().list();
        for (LocalFavoriteInfo info : localFavoriteInfoList) {
            putLocalFavorites(info);
        }
        // Bookmarks
        // TODO
        // Filter
        List<Filter> filterList = session.getFilterDao().queryBuilder().list();
        List<Filter> currentFilterList = sDaoSession.getFilterDao().queryBuilder().list();
        for (Filter filter : filterList) {
            if (!currentFilterList.contains(filter)) {
                addFilter(filter);
            }
        }
        return null;
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        // Ignore
        return context.getString(R.string.cant_read_the_file);
    }
}
Also used : LocalFavoriteInfo(com.hippo.ehviewer.dao.LocalFavoriteInfo) DownloadDirname(com.hippo.ehviewer.dao.DownloadDirname) DownloadManager(com.hippo.ehviewer.download.DownloadManager) DaoMaster(com.hippo.ehviewer.dao.DaoMaster) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Filter(com.hippo.ehviewer.dao.Filter) DownloadInfo(com.hippo.ehviewer.dao.DownloadInfo) QuickSearch(com.hippo.ehviewer.dao.QuickSearch) DownloadLabel(com.hippo.ehviewer.dao.DownloadLabel) HistoryInfo(com.hippo.ehviewer.dao.HistoryInfo) DaoSession(com.hippo.ehviewer.dao.DaoSession)

Example 2 with DaoSession

use of com.hippo.ehviewer.dao.DaoSession in project EhViewer by seven332.

the class EhDB method exportDB.

public static synchronized boolean exportDB(Context context, File file) {
    final String ehExportName = "eh.export.db";
    // Delete old export db
    context.deleteDatabase(ehExportName);
    DBOpenHelper helper = new DBOpenHelper(context.getApplicationContext(), ehExportName, null);
    try {
        // Copy data to a export db
        try (SQLiteDatabase db = helper.getWritableDatabase()) {
            DaoMaster daoMaster = new DaoMaster(db);
            DaoSession exportSession = daoMaster.newSession();
            if (!copyDao(sDaoSession.getDownloadsDao(), exportSession.getDownloadsDao()))
                return false;
            if (!copyDao(sDaoSession.getDownloadLabelDao(), exportSession.getDownloadLabelDao()))
                return false;
            if (!copyDao(sDaoSession.getDownloadDirnameDao(), exportSession.getDownloadDirnameDao()))
                return false;
            if (!copyDao(sDaoSession.getHistoryDao(), exportSession.getHistoryDao()))
                return false;
            if (!copyDao(sDaoSession.getQuickSearchDao(), exportSession.getQuickSearchDao()))
                return false;
            if (!copyDao(sDaoSession.getLocalFavoritesDao(), exportSession.getLocalFavoritesDao()))
                return false;
            if (!copyDao(sDaoSession.getBookmarksBao(), exportSession.getBookmarksBao()))
                return false;
            if (!copyDao(sDaoSession.getFilterDao(), exportSession.getFilterDao()))
                return false;
        }
        // Copy export db to data dir
        File dbFile = context.getDatabasePath(ehExportName);
        if (dbFile == null || !dbFile.isFile()) {
            return false;
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(dbFile);
            os = new FileOutputStream(file);
            IOUtils.copy(is, os);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
        // Delete failed file
        file.delete();
        return false;
    } finally {
        context.deleteDatabase(ehExportName);
    }
}
Also used : DaoMaster(com.hippo.ehviewer.dao.DaoMaster) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) DaoSession(com.hippo.ehviewer.dao.DaoSession)

Aggregations

SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 DaoMaster (com.hippo.ehviewer.dao.DaoMaster)2 DaoSession (com.hippo.ehviewer.dao.DaoSession)2 DownloadDirname (com.hippo.ehviewer.dao.DownloadDirname)1 DownloadInfo (com.hippo.ehviewer.dao.DownloadInfo)1 DownloadLabel (com.hippo.ehviewer.dao.DownloadLabel)1 Filter (com.hippo.ehviewer.dao.Filter)1 HistoryInfo (com.hippo.ehviewer.dao.HistoryInfo)1 LocalFavoriteInfo (com.hippo.ehviewer.dao.LocalFavoriteInfo)1 QuickSearch (com.hippo.ehviewer.dao.QuickSearch)1 DownloadManager (com.hippo.ehviewer.download.DownloadManager)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1