sk.baka.ambient.library
Class DBStrategy

java.lang.Object
  extended by sk.baka.ambient.library.DBStrategy

public final class DBStrategy
extends Object

The database backend for the library. Handles database operations.

Author:
Martin Vysny

Field Summary
static int DATABASE_VERSION
          The database version.
 
Constructor Summary
DBStrategy(Context ctx)
          Creates the backend instance and opens/creates the database.
 
Method Summary
(package private)  void clean(TrackOriginEnum storage)
          Removes all data from all tables.
(package private)  void close()
          Closes the database and releases all resources.
static EnumMap<CategoryEnum,List<String>> convertCriteria(Map<? extends CategoryEnum,? extends String> map)
          Converts a simple criteria to a complex criteria type.
 Cursor findAll()
          Finds all tracks.
 Cursor findByCriteria(EnumMap<CategoryEnum,List<String>> criteria, boolean like, boolean and, String orderBy)
          Searches tracks.
 Cursor findByCriteria(Map<CategoryEnum,String> criteria, boolean like, String orderBy)
          Searches tracks.
 Map<String,Long> getAllGenres()
          Loads all genres as a map.
 Cursor getCriteriaList(CategoryEnum[] crit, EnumMap<CategoryEnum,String> criteria)
          Returns matchable strings from existing tracks for given criteria values.
(package private)  SQLiteDatabase getDb()
          Returns the initialized database instance.
 int getGenreId(String genre)
          Searches for given genre and returns its id.
 Cursor rawQuery(String select, String[] selectionArgs)
          Invokes the SQLiteDatabase.rawQuery(String, String[]) method.
 Cursor rawTrackQuery(String where, String[] selectionArgs)
          Performs a raw query on the track table.
(package private)  long registerNewGenre(String name)
          Registers new genre and returns its id.
(package private)  long registerNewTrack(TrackMetadataBean track, Set<Long> genreIds)
          Registers a new track with given genres.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATABASE_VERSION

public static final int DATABASE_VERSION
The database version.

See Also:
Constant Field Values
Constructor Detail

DBStrategy

DBStrategy(Context ctx)
     throws FileNotFoundException
Creates the backend instance and opens/creates the database.

Parameters:
ctx - the context.
Throws:
FileNotFoundException - if the database fails to both open and create.
Method Detail

close

void close()
Closes the database and releases all resources.


getDb

SQLiteDatabase getDb()
Returns the initialized database instance.

Returns:
database instance, never null.
Throws:
IllegalStateException - if the database was closed.

clean

void clean(TrackOriginEnum storage)
Removes all data from all tables.

Parameters:
storage - the storage to clean. If null then all tracks and genres are removed.

getGenreId

public int getGenreId(String genre)
Searches for given genre and returns its id.

Parameters:
genre - the genre to search for, must not be null.
Returns:
id of the genre or -1 if given genre does not yet exist.

registerNewGenre

long registerNewGenre(String name)
Registers new genre and returns its id. The genre must be unique!

Parameters:
name - the genre name, must not be null.
Returns:
id.

registerNewTrack

long registerNewTrack(TrackMetadataBean track,
                      Set<Long> genreIds)
Registers a new track with given genres.

Parameters:
track - the track to register.
genreIds - list of genres for this track.
Returns:
track's new ID.

getCriteriaList

public Cursor getCriteriaList(CategoryEnum[] crit,
                              EnumMap<CategoryEnum,String> criteria)
Returns matchable strings from existing tracks for given criteria values. For example, CategoryEnum.Genre returns all genres. Returns ordered list.

Parameters:
crit - the criteria to search for
criteria - the criteria map. All criteria combines using the AND operator. Must not contain the crit parameter. May be null - this is equal to an empty map.
Returns:
list of all criteria, retrievable via Cursor.getString(0).

findByCriteria

public Cursor findByCriteria(Map<CategoryEnum,String> criteria,
                             boolean like,
                             String orderBy)
Searches tracks.

Parameters:
criteria - the criteria map. All criteria combines using the AND operator. May be null - this is equal to an empty map. When using the CategoryEnum.Origin criteria, use TrackOriginEnum.toDBString() as value.
orderBy - the order by clause, if null then ordering is undefined.
like - if true then given strings will be compared using the LIKE operator. Note that the values are automatically enclosed in %%.
Returns:
cursor with items convertible to TrackMetadataBean using LibraryUtils.cursorToTrackBean(Cursor). Use LibraryUtils.pollTracks(Cursor) to get all results.

findByCriteria

public Cursor findByCriteria(EnumMap<CategoryEnum,List<String>> criteria,
                             boolean like,
                             boolean and,
                             String orderBy)
Searches tracks.

Parameters:
criteria - the criteria map. All criteria are combined using the required operator (depending on the value of the and argument). May be null - this is equal to an empty map. Several values for a single criteria are combined depending on the value of the and parameter. When using the CategoryEnum.Origin criteria, use TrackOriginEnum.toDBString() as value.
like - if true then given strings will be compared using the LIKE operator. Note that the values are automatically enclosed in %%.
and - If true then the criteria is combined using the AND operator. If false then the criteria is combined using the OR operator.
orderBy - the order by clause, if null then ordering is undefined.
Returns:
cursor with items convertible to TrackMetadataBean using LibraryUtils.cursorToTrackBean(Cursor). Use LibraryUtils.pollTracks(Cursor) to get all results.

convertCriteria

public static EnumMap<CategoryEnum,List<String>> convertCriteria(Map<? extends CategoryEnum,? extends String> map)
Converts a simple criteria to a complex criteria type.

Parameters:
map - the simple criteria.
Returns:
the complex criteria type.

findAll

public Cursor findAll()
Finds all tracks.

Returns:
cursor with items convertible to TrackMetadataBean using LibraryUtils.cursorToTrackBean(Cursor). Use LibraryUtils.pollTracks(Cursor) to get all results.

rawTrackQuery

public Cursor rawTrackQuery(String where,
                            String[] selectionArgs)
Performs a raw query on the track table.

Parameters:
where - the where clausule
selectionArgs - optional selection argument values, may be null.
Returns:
track query cursor with items convertible to TrackMetadataBean using LibraryUtils.cursorToTrackBean(Cursor). Use LibraryUtils.pollTracks(Cursor) to get all results.

rawQuery

public Cursor rawQuery(String select,
                       String[] selectionArgs)
Invokes the SQLiteDatabase.rawQuery(String, String[]) method.

Parameters:
select - the SQL SELECT statement.
selectionArgs - optional arguments, may be null.
Returns:
opened cursor.

getAllGenres

public Map<String,Long> getAllGenres()
Loads all genres as a map.

Returns:
all genres.


Copyright © 2007-2008. All Rights Reserved.