org.android.activityminer.persistence
Class DBManager

Package class diagram package DBManager
java.lang.Object
  extended by org.android.activityminer.persistence.DBManager

public final class DBManager
extends Object

The database manager implemented as global singleton.


Field Summary
private  Context context
          The context.
private  DBAdapter dbAdapter
          The database adapter.
private static DBManager instance
          The singleton database manager instance
 
Constructor Summary
private DBManager(Context context)
          Constructor
 
Method Summary
 boolean clearAllExtractedTrainingSampleData()
          Does set the processing state for all recoding to new ( not processed ) and does delete all stored extracted feature vectors in the database.
 void deleteActivityLabel(ActivityLabel activityLabel)
          Does delete the activity label in the database.
 void deleteAll()
          Does delete all stored data.
 boolean deleteAllRecordings()
          Does delete all stored recording data .
 boolean deleteRecording(long recordingId)
          Does delete the recording with the given identifier from DB.
 boolean deleteRecordings(List<Long> recordingIds)
          Does delete the recordings with the given identifiers and all related features.
 int deleteRecordingsForActivityLabel(ActivityLabel activityLabel)
          Does delete the recording with the given activity label from DB.
 void deleteSamplesForActivityLabels(List<Long> activityLabelIds)
          Does delete all existing samples for the given set of labels.
 void deleteSamplesForRecording(long idRecording)
          Does delete the extracted samples for the given recording identifier
 void deleteSamplesForSequenceId(long sequenceId)
          Does delete the extracted samples for the given sequence identifier
 int deleteSequence(long sid)
          Does delete all recordings with the given sequence identifier
 boolean doDBUpgrade()
          Simple db call on activity start to trigger db upgrades
 String getActivityLabelForId(Long id)
          Getter for an activity label name for a given identifier.
 List<Long> getActivityLabelIdsWithSamplesIgnoreDependecies()
          Getter for all activity labels with own extracted training samples.
 ActivityLabelMap getActivityLabels()
          Does request all known activity labels from the database.
 List<Long> getAllRecordingIdentifiers()
          Does request all available recording identifiers.
 List<Long> getAllSequenceIdentifiers()
          Does request all available sequence identifiers.
 long getAvailableTrainingSampleCount()
          Getter for the count of available training samples.
 List<Long> getAvailableTrainingSampleIds()
          Does request all available training sample identifiers.
 List<Long> getAvailableTrainingSampleIdsForClassId(long selectedClassId)
          Does request all available training sample identifiers for a given activity label.
 Configuration getConfigurationFromDB()
          Does get the stored configuration from an sqlite database
static int getDatabaseVersion()
          Getter for the database version
 DBStatistic getDBRecordingStatisticsForAllActivities()
          Getter for a recording count statistics for all activities.
private  DBStatistic getDBStatistics(boolean activitiesWithSamplesOnly, boolean skipSampleCounts)
          Getter for sample statistics.
 DBStatistic getDBStatisticsForAllActivities()
          Getter for a complete database statistics (recording and sample counts for all activities).
 DBStatistic getDBStatisticsForAllActivitiesWithSamples()
          Getter for a recording count statistics for all activities with extracted training samples.
static DBManager getInstance(Context context)
          Getter for the instance
 LabeledSample getLabeledSample(Long id)
          Does request a labeled training sample for a given identifier.
 long getNewRecordingCount()
          Does request the count of available new recordings.
 List<Long> getNewRecordingIdentifiers()
          Does request all available new recording identifiers.
 List<Long> getNewSequenceIdentifiers()
          Does request all available new continuous recording sequence identifiers.
 long getNextRecordingSequenceId()
          Does determine the next unique sequence identifier for continuous recordings
<T extends Enum<T> & Dimensions>
LabeledRecording<T>
getRecording(Long recordingId)
          Does request a recording by its unique identifier.
 long getRecordingCount()
          Does request the count of available recordings.
 List<Long> getRecordingIdsForSequenceId(long sid)
          Does look up the recording identifiers of a given sequence identifier
 DBRecordingStatistic getRecordingStatistics()
          Does request statistic data for all available recordings.
 long getSampleRate()
          Does determine the sample rate
static long getSampleRateFromDB(SQLiteDatabase db)
          Does get the sample rate from an sqlite database
 List<LabeledSample> getSamplesForRecording(long idRecording)
          Does request all available labeled training samples for a given recording identifier.
 List<LabeledSample> getSamplesForSequence(long sId)
          Does request all available labeled training samples for a given sequence identifier.
 List<DBSampleStatistic> getSampleStatistic(long[] activityLabelIds)
          Method to get sample and recording statistics for the given activity labels
 List<ActivityLabel> getSelectedActivities(Configuration config)
          Method to determine the classes of the identifier selection in configuration
Attention: It can happen, that old activity identifiers remains in the selection stored in preferences due to deletion of activities.
 List<String> getSelectedActivitiesWithoutSamples(Configuration config)
          Method to determine the count of configured activities without associated samples in the database.
 List<String> getSelectedActivitiesWithoutSamples(List<ActivityLabel> selectedActivities, List<Long> activityIdsWithOwnSamples)
          Method to determine the count of configured activities with samples in database.
 TrainingActivity getTrainingActivity(Long id)
          Does request a training activity representation for an existing training sample from database.
 String getUuid()
          Does get the stored UUID of the device the database was created on.
 boolean insertActivityLabel(String newActivityLabel)
          Does insert the activity label in the database without dependencies.
 boolean renameSequence(long sid, Long ActivityLabelId)
          Does update the activity assignment for all recordings with the given sequence identifier
static void resetDBVersion(Context context)
          Method to reset database version preference for database imports
 void saveFeatureVectorForRecording(long idRecording, FeatureVector featureVector)
          Does save a feature vector associated to a given recording identifier.
<T extends Enum<T> & Dimensions>
Long
saveRecording(LabeledRecording<T> recording)
          Does save a labeled recording in the database.
 boolean setRecordingProcessed(long recordingId)
          Does change the processing state for a recoding to processed.
 void storeConfiguration(Configuration config)
          Method to update the stored configuration
static boolean testForDBUgrade(Context context)
          Test method for an outstanding DB upgrade on next access
 void updateActivityLabel(ActivityLabel activityLabel)
          Does update the activity label in the database.
 boolean updateRecordingData(DBRecording recording)
          Method to update the recording data.
 void updateSampleRate(long sampleRate)
          Method to update the sample rate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dbAdapter

private final DBAdapter dbAdapter
The database adapter.


instance

private static DBManager instance
The singleton database manager instance


context

private final Context context
The context.

Constructor Detail

DBManager

private DBManager(Context context)
Constructor

Parameters:
context - the application context
Method Detail

getInstance

public static DBManager getInstance(Context context)
Getter for the instance

Parameters:
context - the application context
Returns:
the instance

resetDBVersion

public static final void resetDBVersion(Context context)
Method to reset database version preference for database imports

Parameters:
context - the context

testForDBUgrade

public static final boolean testForDBUgrade(Context context)
Test method for an outstanding DB upgrade on next access

Parameters:
context - the context
Returns:
true if an DB upgrade required

getDatabaseVersion

public static final int getDatabaseVersion()
Getter for the database version

Returns:
the database version

doDBUpgrade

public final boolean doDBUpgrade()
Simple db call on activity start to trigger db upgrades

Returns:
true if DB upgrade of raw time series is requested (for compatibility of older DB versions)

saveRecording

public final <T extends Enum<T> & Dimensions> Long saveRecording(LabeledRecording<T> recording)
                         throws Exception
Does save a labeled recording in the database.

Type Parameters:
T - the enumeration type for the dimensions
Parameters:
recording - the recording
Returns:
the record id if successful, -1 otherwise
Throws:
Exception

getActivityLabels

public final ActivityLabelMap getActivityLabels()
Does request all known activity labels from the database.

Returns:
a map with all activity labels from database.

deleteActivityLabel

public final void deleteActivityLabel(ActivityLabel activityLabel)
Does delete the activity label in the database.

Parameters:
activityLabel - the activity label to delete

insertActivityLabel

public final boolean insertActivityLabel(String newActivityLabel)
Does insert the activity label in the database without dependencies.

Parameters:
newActivityLabel - the activity label to insert
Returns:
true if successful, false if label already exists

updateActivityLabel

public final void updateActivityLabel(ActivityLabel activityLabel)
Does update the activity label in the database.

Parameters:
activityLabel - the activity label to update

getRecordingCount

public final long getRecordingCount()
Does request the count of available recordings.

Returns:
the count of available recordings

getRecordingStatistics

public final DBRecordingStatistic getRecordingStatistics()
Does request statistic data for all available recordings.

Returns:
a list with the recording statistics.

getNewRecordingCount

public final long getNewRecordingCount()
Does request the count of available new recordings.

Returns:
the count of available new recordings.

getAllRecordingIdentifiers

public final List<Long> getAllRecordingIdentifiers()
Does request all available recording identifiers.

Returns:
a list with the recordings identifiers.

getAllSequenceIdentifiers

public List<Long> getAllSequenceIdentifiers()
Does request all available sequence identifiers.

Returns:
a list with the sequence identifiers.

getNewRecordingIdentifiers

public final List<Long> getNewRecordingIdentifiers()
Does request all available new recording identifiers.

Returns:
a list with the new recordings identifiers.

getNewSequenceIdentifiers

public final List<Long> getNewSequenceIdentifiers()
Does request all available new continuous recording sequence identifiers.

Returns:
a list with the new sequence identifiers.

getRecording

public final <T extends Enum<T> & Dimensions> LabeledRecording<T> getRecording(Long recordingId)
Does request a recording by its unique identifier.

Type Parameters:
T - the enumeration type for the dimensions
Parameters:
recordingId - the recoding identifier
Returns:
the recording for the given identifier if exists, null otherwise

getRecordingIdsForSequenceId

public final List<Long> getRecordingIdsForSequenceId(long sid)
Does look up the recording identifiers of a given sequence identifier

Parameters:
sid - the sequence identifier
Returns:
a list with all recordings of the given sequence id

deleteRecording

public final boolean deleteRecording(long recordingId)
Does delete the recording with the given identifier from DB.

Parameters:
recordingId - the recoding identifier
Returns:
true if successful, false otherwise

deleteRecordings

public final boolean deleteRecordings(List<Long> recordingIds)
Does delete the recordings with the given identifiers and all related features.

Parameters:
recordingIds - s list with unique identifiers of the recordings to delete
Returns:
the count of deleted rows

deleteSequence

public final int deleteSequence(long sid)
Does delete all recordings with the given sequence identifier

Parameters:
sid - the unique identifier of the recording to delete
Returns:
the count of deleted rows

renameSequence

public final boolean renameSequence(long sid,
                                    Long ActivityLabelId)
Does update the activity assignment for all recordings with the given sequence identifier

Parameters:
sid - the unique identifier of the recording to delete
ActivityLabelId - the identifier of the new activity label
Returns:
true if successful, false otherwise

deleteRecordingsForActivityLabel

public final int deleteRecordingsForActivityLabel(ActivityLabel activityLabel)
Does delete the recording with the given activity label from DB.

Parameters:
activityLabel - the activity label
Returns:
the count of deleted recordings

setRecordingProcessed

public final boolean setRecordingProcessed(long recordingId)
Does change the processing state for a recoding to processed.

Parameters:
recordingId - the recoding identifier
Returns:
true if successful, false otherwise

updateRecordingData

public final boolean updateRecordingData(DBRecording recording)
Method to update the recording data.

Parameters:
recording - the recording with the updated raw sensor data
Returns:
true if successful, false otherwise

clearAllExtractedTrainingSampleData

public final boolean clearAllExtractedTrainingSampleData()
Does set the processing state for all recoding to new ( not processed ) and does delete all stored extracted feature vectors in the database.

Returns:
true if successful, false otherwise

saveFeatureVectorForRecording

public final void saveFeatureVectorForRecording(long idRecording,
                                                FeatureVector featureVector)
                                         throws Exception
Does save a feature vector associated to a given recording identifier.

Parameters:
idRecording - the associated recordings identifier
featureVector - the feature Vector to save
Throws:
Exception

getDBStatisticsForAllActivitiesWithSamples

public final DBStatistic getDBStatisticsForAllActivitiesWithSamples()
Getter for a recording count statistics for all activities with extracted training samples.

Returns:
a list with available sample statistics

getDBStatisticsForAllActivities

public final DBStatistic getDBStatisticsForAllActivities()
Getter for a complete database statistics (recording and sample counts for all activities).

Returns:
a list with available sample statistics

getDBRecordingStatisticsForAllActivities

public final DBStatistic getDBRecordingStatisticsForAllActivities()
Getter for a recording count statistics for all activities.

Returns:
a list of labels with available recording statistics

getDBStatistics

private final DBStatistic getDBStatistics(boolean activitiesWithSamplesOnly,
                                          boolean skipSampleCounts)
Getter for sample statistics.

Parameters:
activitiesWithSamplesOnly - if true, only statistics for activities with existing samples will be created
skipSampleCounts - flag if sample count calculation shall be skipped
Returns:
a list with available sample statistics

getSampleStatistic

public List<DBSampleStatistic> getSampleStatistic(long[] activityLabelIds)
Method to get sample and recording statistics for the given activity labels

Parameters:
activityLabelIds - the activity labels identifiers to get sample statistics for
Returns:
the recording and sample count statistics

getSamplesForRecording

public final List<LabeledSample> getSamplesForRecording(long idRecording)
                                                 throws Exception
Does request all available labeled training samples for a given recording identifier.

Parameters:
idRecording - the recordings identifier
Returns:
a list with the training samples for the given recording, created from the stored feature vectors of the training samples.
Throws:
Exception

getSamplesForSequence

public final List<LabeledSample> getSamplesForSequence(long sId)
                                                throws Exception
Does request all available labeled training samples for a given sequence identifier.

Parameters:
sId - the sequence identifier
Returns:
a list with the training samples for the given sequence, created from the stored feature vectors of the training samples.
Throws:
Exception

getActivityLabelIdsWithSamplesIgnoreDependecies

public final List<Long> getActivityLabelIdsWithSamplesIgnoreDependecies()
Getter for all activity labels with own extracted training samples.

Returns:
a list with all activity labels with own extracted training samples.

getActivityLabelForId

public final String getActivityLabelForId(Long id)
Getter for an activity label name for a given identifier.

Parameters:
id - the activity label identifier
Returns:
the name for the given activityLabel identifier

getAvailableTrainingSampleCount

public final long getAvailableTrainingSampleCount()
Getter for the count of available training samples.

Returns:
the count of available training samples

getTrainingActivity

public final TrainingActivity getTrainingActivity(Long id)
                                           throws Exception
Does request a training activity representation for an existing training sample from database.

Parameters:
id - the unique identifier of the database feature vector.
Returns:
training activity representation for the given identifier.
Throws:
Exception

getLabeledSample

public final LabeledSample getLabeledSample(Long id)
                                     throws Exception
Does request a labeled training sample for a given identifier.

Parameters:
id - the unique training sample identifier.
Returns:
the labeled sample for the identifier.
Throws:
Exception

getAvailableTrainingSampleIdsForClassId

public final List<Long> getAvailableTrainingSampleIdsForClassId(long selectedClassId)
                                                         throws Exception
Does request all available training sample identifiers for a given activity label. For labels with child activities, the successor label samples will be returned as well.

Parameters:
selectedClassId - the class activity label identifier to get sample identifier for
Returns:
a list with all available training sample identifiers.
Throws:
Exception

getAvailableTrainingSampleIds

public final List<Long> getAvailableTrainingSampleIds()
Does request all available training sample identifiers.

Returns:
a list with all available training sample identifiers.

deleteAll

public final void deleteAll()
Does delete all stored data.


deleteAllRecordings

public final boolean deleteAllRecordings()
Does delete all stored recording data .

Returns:
true if successful, false otherwise

updateSampleRate

public final void updateSampleRate(long sampleRate)
Method to update the sample rate

Parameters:
sampleRate - the sample rate to update

deleteSamplesForRecording

public void deleteSamplesForRecording(long idRecording)
Does delete the extracted samples for the given recording identifier

Parameters:
idRecording - the associated recordings identifier

deleteSamplesForSequenceId

public void deleteSamplesForSequenceId(long sequenceId)
Does delete the extracted samples for the given sequence identifier

Parameters:
sequenceId - the associated sequence identifier

deleteSamplesForActivityLabels

public void deleteSamplesForActivityLabels(List<Long> activityLabelIds)
Does delete all existing samples for the given set of labels.

Parameters:
activityLabelIds - the activity label identifiers to delete samples for

getSampleRateFromDB

public static final long getSampleRateFromDB(SQLiteDatabase db)
Does get the sample rate from an sqlite database

Parameters:
db - the database to query
Returns:
the sample rate

getSampleRate

public long getSampleRate()
Does determine the sample rate

Returns:
the sample rate

getConfigurationFromDB

public final Configuration getConfigurationFromDB()
Does get the stored configuration from an sqlite database

Returns:
the stored configuration from DB (can be null if none is stored)

getUuid

public final String getUuid()
Does get the stored UUID of the device the database was created on.

Returns:
the stored UUID from DB

storeConfiguration

public final void storeConfiguration(Configuration config)
Method to update the stored configuration

Parameters:
config - the configuration to update (can be null to clear stored configuration)

getNextRecordingSequenceId

public long getNextRecordingSequenceId()
Does determine the next unique sequence identifier for continuous recordings

Returns:
the next sequence identifier

getSelectedActivitiesWithoutSamples

public final List<String> getSelectedActivitiesWithoutSamples(Configuration config)
Method to determine the count of configured activities without associated samples in the database.

Parameters:
config - the current configuration
Returns:
the count of selected activities with samples

getSelectedActivities

public final List<ActivityLabel> getSelectedActivities(Configuration config)
Method to determine the classes of the identifier selection in configuration
Attention: It can happen, that old activity identifiers remains in the selection stored in preferences due to deletion of activities.

Parameters:
config - the current configuration
Returns:
the classes of the identifier selection in configuration

getSelectedActivitiesWithoutSamples

public final List<String> getSelectedActivitiesWithoutSamples(List<ActivityLabel> selectedActivities,
                                                              List<Long> activityIdsWithOwnSamples)
Method to determine the count of configured activities with samples in database.

Parameters:
selectedActivities - the selected activities
activityIdsWithOwnSamples - the known activity identifiers with own samples
Returns:
the count of selected classes with samples