Android Open Source - dissertation-project Resilience Db Manager






From Project

Back to project page dissertation-project.

License

The source code is released under:

MIT License

If you think the Android project dissertation-project 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.fyp.resilience.database;
/*w  w  w  .  ja  va  2s  .co m*/
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Callable;

import android.content.Context;
import android.util.Log;

import com.fyp.resilience.Flags;
import com.fyp.resilience.ResilienceApplication;
import com.fyp.resilience.database.model.DataPiece;
import com.fyp.resilience.database.model.DataWhole;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;

public class ResilienceDbManager {

    /**
     * A helper method used by functions within this class.
     * 
     * @param {@link Context}
     * @return {@link ResilienceDbHelper}
     */
    private static ResilienceDbHelper getDbHelper(final Context context) {
        return OpenHelperManager.getHelper(context, ResilienceDbHelper.class);
    }

    /**
     * Returns all the DataWholes currently within the application's database.
     * 
     * @param {@link Context}
     * @return a {@link List} of the {@link DataWhole}s within the database.
     */
    public static List<DataWhole> getDataWholes(final Context context) {
        final ResilienceDbHelper dBHelper = getDbHelper(context);
        List<DataWhole> wholes = null;
        List<DataPiece> pieces = null;

        try {
            final Dao<DataWhole, String> wholeDao = dBHelper.getDataWholeDao();
            final Dao<DataPiece, String> pieceDao = dBHelper.getDataPieceDao();

            wholes = wholeDao.queryForAll();

            for (final DataWhole dataWhole : wholes) {
                pieces = pieceDao.queryForEq(DataPiece.COL_WHOLE_ID, dataWhole);

                if (pieces.size() > 0) {
                    dataWhole.setPieces(pieces);
                }
            }

        } catch (SQLException e) {
            if (Flags.DEBUG) {
                Log.e(ResilienceDbManager.class.getSimpleName(), e.getMessage());
            }
        } finally {
            OpenHelperManager.releaseHelper();
        }
        return wholes;
    }

    /**
     * Returns all the DataPieces currently within the application's database.
     * 
     * @param {@link Context}
     * @return a {@link List} of the {@link DataPiece}s within the database.
     */
    public static List<DataPiece> getDataPieces(final Context context) {
        final ResilienceDbHelper dBHelper = getDbHelper(context);
        List<DataPiece> pieces = null;

        try {
            final Dao<DataPiece, String> dao = dBHelper.getDataPieceDao();
            pieces = dao.queryForAll();
        } catch (SQLException e) {
            if (Flags.DEBUG) {
                Log.e(ResilienceDbManager.class.getSimpleName(), e.getMessage());
            }
        } finally {
            OpenHelperManager.releaseHelper();
        }
        return pieces;
    }

    /**
     * Persists a DataWhole, along with its attached DataPieces, to the
     * database. Uses a batch creation upon DataPieces.
     * 
     * @param {@link Context}
     * @param The {@link DataWhole} to be persisted
     */
    public static void persistDataWhole(final Context context, final DataWhole dataWhole) {
        final ResilienceApplication resilApp = (ResilienceApplication) context.getApplicationContext();

        resilApp.getDatabaseThreadExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                final ResilienceDbHelper dbHelper = getDbHelper(context);
                try {
                    final Dao<DataWhole, String> wholeDao = dbHelper.getDataWholeDao();
                    wholeDao.createOrUpdate(dataWhole);

                    final Dao<DataPiece, String> pieceDao = dbHelper.getDataPieceDao();
                    final List<DataPiece> pieceList = dataWhole.getPieces();

                    if (pieceList != null) {

                        pieceDao.callBatchTasks(new Callable<Object>() {
                            @Override
                            public Object call() throws Exception {
                                try {
                                    for (final DataPiece dataPiece : pieceList) {
                                        pieceDao.createOrUpdate(dataPiece);
                                        if (Flags.DEBUG) {
                                            Log.i(ResilienceDbManager.class.getSimpleName(),
                                                    dataPiece.getKey() + " has been persisted!");
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                return true;
                            }
                        });

                    }

                    if (Flags.DEBUG) {
                        Log.d(ResilienceDbManager.class.getSimpleName(), dataWhole.getKey()
                                + " has been persisted!");
                    }
                } catch (SQLException e) {
                    if (Flags.DEBUG) {
                        Log.e(ResilienceDbManager.class.getSimpleName(), e.getMessage());
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    OpenHelperManager.releaseHelper();
                }
            }
        });

    }

    /**
     * @param context
     * @param dataPiece
     */
    public static void persistDataPiece(final Context context, final DataPiece dataPiece) {
        final ResilienceApplication resilApp = (ResilienceApplication) context.getApplicationContext();
        resilApp.getDatabaseThreadExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                final ResilienceDbHelper dbHelper = getDbHelper(context);
                try {
                    final Dao<DataPiece, String> pieceDao = dbHelper.getDataPieceDao();
                    pieceDao.createOrUpdate(dataPiece);
                } catch (Exception e) {

                } finally {
                    OpenHelperManager.releaseHelper();
                }
            }
        });
    }

    public static void removeDataPieces(final Context context, final List<DataPiece> pieces) {
        final ResilienceApplication resilApp = (ResilienceApplication) context.getApplicationContext();

        resilApp.getDatabaseThreadExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                final ResilienceDbHelper dbHelper = getDbHelper(context);
                try {
                    final Dao<DataPiece, String> pieceDao = dbHelper.getDataPieceDao();
                    pieceDao.delete(pieces);

                } catch (SQLException e) {
                    if (Flags.DEBUG) {
                        Log.e(ResilienceDbManager.class.getSimpleName(), e.getMessage());
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    OpenHelperManager.releaseHelper();
                }
            }
        });
    }

    /**
     * Removes the specified DataWhole, along with its associated DataPieces,
     * from the database.
     * 
     * @param {@link Context}
     * @param The {@link DataWhole} to be removed
     */
    public static void removeDataWhole(final Context context, final DataWhole dataWhole) {
        final ResilienceApplication resilApp = (ResilienceApplication) context.getApplicationContext();

        resilApp.getDatabaseThreadExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                final ResilienceDbHelper dbHelper = getDbHelper(context);
                try {
                    /* Delete the DataPieces */
                    final Dao<DataPiece, String> pieceDao = dbHelper.getDataPieceDao();
                    pieceDao.delete(dataWhole.getPieces());

                    /* Delete the DataWholes */
                    final Dao<DataWhole, String> wholeDao = dbHelper.getDataWholeDao();
                    wholeDao.delete(dataWhole);

                    if (Flags.DEBUG) {
                        Log.d(ResilienceDbManager.class.getSimpleName(), dataWhole.getKey()
                                + " has been deleted!");
                    }

                } catch (SQLException e) {
                    if (Flags.DEBUG) {
                        Log.e(ResilienceDbManager.class.getSimpleName(), e.getMessage());
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    OpenHelperManager.releaseHelper();
                }
            }
        });
    }

}




Java Source Code List

com.fyp.resilience.Constants.java
com.fyp.resilience.Flags.java
com.fyp.resilience.GCMIntentService.java
com.fyp.resilience.PreferenceConstants.java
com.fyp.resilience.ResilienceApplication.java
com.fyp.resilience.ResilienceController.java
com.fyp.resilience.activity.LicenceActivity.java
com.fyp.resilience.activity.ResilienceActivity.java
com.fyp.resilience.activity.SettingsActivity.java
com.fyp.resilience.adapter.ClientListAdapter.java
com.fyp.resilience.adapter.ConnectionListAdapter.java
com.fyp.resilience.adapter.FileListAdapter.java
com.fyp.resilience.connection.Connectable.java
com.fyp.resilience.connection.ServerDownloadConnectable.java
com.fyp.resilience.connection.ServerUploadConnectable.java
com.fyp.resilience.connection.UploadConnectable.java
com.fyp.resilience.connection.WifiDownloadConnectable.java
com.fyp.resilience.connection.WifiUploadConnectable.java
com.fyp.resilience.database.ResilienceDbHelper.java
com.fyp.resilience.database.ResilienceDbManager.java
com.fyp.resilience.database.model.DataPiece.java
com.fyp.resilience.database.model.DataWhole.java
com.fyp.resilience.event.ClientListChanged.java
com.fyp.resilience.event.ClientModified.java
com.fyp.resilience.event.ConnectionProgressChange.java
com.fyp.resilience.event.ConnectionStateChange.java
com.fyp.resilience.event.ConnectionsModified.java
com.fyp.resilience.event.PieceStateChange.java
com.fyp.resilience.event.ServerRegistrationChanged.java
com.fyp.resilience.event.ServerUploadFinished.java
com.fyp.resilience.event.WholeModified.java
com.fyp.resilience.event.WifiDownloadFinished.java
com.fyp.resilience.event.WifiUploadFinished.java
com.fyp.resilience.fragment.ClientsFragment.java
com.fyp.resilience.fragment.ConnectionsFragment.java
com.fyp.resilience.fragment.FilesFragment.java
com.fyp.resilience.interfaces.Messagable.java
com.fyp.resilience.interfaces.Partialable.java
com.fyp.resilience.proto.ProtoBuffSpecification.java
com.fyp.resilience.receiver.AbstractConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.BootReceiver.java
com.fyp.resilience.receiver.ConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.WiFiDirectBroadcastReceiver.java
com.fyp.resilience.register.RegisterRequestInitializer.java
com.fyp.resilience.register.RegisterRequest.java
com.fyp.resilience.register.RegisterScopes.java
com.fyp.resilience.register.Register.java
com.fyp.resilience.register.model.DeviceInfo.java
com.fyp.resilience.service.PieceUploadService.java
com.fyp.resilience.stream.PiecedRandomAccessFile.java
com.fyp.resilience.swarm.helper.NsdHelper.java
com.fyp.resilience.swarm.helper.SwarmHelperInterface.java
com.fyp.resilience.swarm.helper.WifiDirectSdHelper.java
com.fyp.resilience.swarm.model.SwarmClient.java
com.fyp.resilience.thread.ResilienceRunnable.java
com.fyp.resilience.thread.ResilienceThreadFactory.java
com.fyp.resilience.util.ConnectionUtils.java
com.fyp.resilience.util.Utils.java
com.fyp.resilience.view.ClientView.java
com.fyp.resilience.view.ConnectionView.java
com.fyp.resilience.view.FileView.java
com.fyp.resilience.view.PieceProgressIndicator.java
com.fyp.resilience.widerst.WiderstRequestInitializer.java
com.fyp.resilience.widerst.WiderstRequest.java
com.fyp.resilience.widerst.WiderstScopes.java
com.fyp.resilience.widerst.Widerst.java
com.fyp.resilience.widerst.model.DataPiecePartial.java
com.fyp.resilience.widerst.model.DataWholePartial.java
com.fyp.resilience.widerst.model.PostResponse.java
com.fyp.widerst.Constants.java
com.fyp.widerst.WiderstObjectifyService.java
com.fyp.widerst.backend.FileJoinerBackend.java
com.fyp.widerst.cron.CronJobServlet.java
com.fyp.widerst.endpoint.DataPieceEndpoint.java
com.fyp.widerst.endpoint.DeviceInfoEndpoint.java
com.fyp.widerst.entity.DataPiece.java
com.fyp.widerst.entity.DataWhole.java
com.fyp.widerst.entity.DeviceInfo.java
com.fyp.widerst.handler.BlobstoreUploadHandler.java
com.fyp.widerst.partial.DataPiecePartial.java
com.fyp.widerst.partial.DataWholePartial.java
com.fyp.widerst.response.PostResponse.java
com.fyp.widerst.servlet.WholeFileServer.java
com.fyp.widerst.util.DbHelper.java
com.fyp.widerst.util.GcmHelper.java