danger.app
Class CategoryDB

java.lang.Object
  extended by danger.app.Listener
      extended by danger.app.SettingsDB
          extended by danger.app.CategoryDB


public class CategoryDB
extends SettingsDB

Application interface to the CategoryDB class This class is used for managing categories, or folders. The category data is a key value pair. The key is the category name, and the value is an unique id assigned for that category. CategoryDB extends SettingsDB and uses SettingsDB's base implementation to manipulate the data.


Field Summary
static boolean SORT_CATEGORIES
           
 
Fields inherited from class danger.app.SettingsDB
DATA_TYPE_BYTES, DATA_TYPE_INTEGER, DATA_TYPE_STRING, DEBUG_CONFLICT_RESOLVE, DEBUG_CONFLICT_RESOLVE_VERBOSE, DEBUG_SORT, DEFAULT_SETTING, IS_DEBUG, mDataStore
 
Constructor Summary
CategoryDB(String dbName)
          Create or get a CategoryDB.
CategoryDB(String dbName, boolean autoSync)
          Create or get a CategoryDB.
 
Method Summary
 int addCategory(String inCategoryName)
          Add the category name to the CategoryDB.
 void dumpStats()
          Dump the CategoryDB data.
 boolean editCategory(int i, String inName)
          Edit the category name for index i Edit the category name for index i
 int find(String categoryName)
          Find and return the index for a category in the database.
protected  int find2(String categoryName)
          Find and return the index for a category in the database.
 String getCategory(int i)
          Gets the category by index.
 String getCategoryByID(int id)
          Gets the categoryDB name for a given category id.
 int getCategoryFlags(int i)
          Gets the category flag by index.
 int getCategoryID(int i)
          Gets the category unique ID for a given index.
 int getCategoryIndexByID(int id)
          Gets the categoryDB index for a given category id.
 boolean getSortCategories()
          Checks whether or not the categories are sorted.
 int numCategories()
          Returns the number of categories.
 boolean receiveEvent(Event e)
          Receive a CategoryDB related event.
 boolean removeCategory(int i)
          Remove the category for index i Remove the category for index i
protected  void resolveAllRecords()
          Resolves each record with every other record in the CategoryDB database.
protected  void resolveAutoSyncConflicts()
          Resolves potential conflicts caused by auto-syncing the data from Danger service.
 void setAutoSyncNotifyee(Listener inNotifyee)
          Sets the notifyee when autosync conflict resolution is done.
 void setCategoryFlags(int i, int flag)
          Sets the category flag by index.
 void setSortCategories(boolean flag)
          Sets the option to sort the categories or not.
protected  void sort()
          Sort by category names Sort by category names only if getSortCategories() return true.
 
Methods inherited from class danger.app.SettingsDB
clearAll, dump, findDB, findDB, getBytes, getBytes, getDataType, getIntValue, getIntValue, getKey, getNumEntries, getObject, getStringValue, getStringValue, registerAutoSyncBundleListener, remove, renameKey, renameKey, setBytes, setBytes, setIntValue, setIntValue, setObject, setObject, setStringValue, setStringValue
 
Methods inherited from class danger.app.Listener
cancelEvents, childEventPump, childEventPumpDone, eventProblem, isDispatchingThread, receiveEventInternal, sendEvent, sendEvent, sendEvent, sendEvent, sendEvent, sendLowPriorityEvent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SORT_CATEGORIES

public static final boolean SORT_CATEGORIES
See Also:
Constant Field Values
Constructor Detail

CategoryDB

public CategoryDB(String dbName,
                  boolean autoSync)
Create or get a CategoryDB. Used to create or retrieve a CategoryDB database. If a database with the name dbName is found, it is returned. If not, a new database is constructed. Use the findDB method to determine whether a database with the specified name exists before assuming you can use it. The autoSync parameter is true if the CategoryDB should be automatically backed up to the service.

Parameters:
dbName - A String specifying the name of the CategoryDB database.
autoSync - A boolean that is true if the database is to be backed up to the service.

CategoryDB

public CategoryDB(String dbName)
Create or get a CategoryDB. Used to create or retrieve a CategoryDB database. If a database with the name dbName is found, it is returned. If not, a new database is constructed. Use the findDB method to determine whether a database with the specified name exists before assuming you can use it.

Parameters:
dbName - A String specifying the name of the CategoryDB database.
Method Detail

receiveEvent

public boolean receiveEvent(Event e)
Receive a CategoryDB related event. CategoryDB is a listener, and can receive events. It currently handles EventType.EVENT_DATASTORE_RESTORED event.

Overrides:
receiveEvent in class SettingsDB
Parameters:
e - an Event object containing the event type and data.
Returns:
true if the method handled the event.

setSortCategories

public void setSortCategories(boolean flag)
Sets the option to sort the categories or not. Sets the option to sort the categories or not.

Parameters:
flag - a Boolean specifying whether to sort the categories.

getSortCategories

public boolean getSortCategories()
Checks whether or not the categories are sorted. Checks whether or not the categories are sorted.

Returns:
true if the categories are sorted.

numCategories

public int numCategories()
Returns the number of categories. Returns the number of categories in this CategoryDB.

Returns:
the number of categories.

find

public int find(String categoryName)
Find and return the index for a category in the database. Searches the database for a category with a name matching categoryName, then returns the associated index. Returns -1 if no matching name is found.

Overrides:
find in class SettingsDB
Parameters:
categoryName - the category name.
Returns:
the index of the category, or -1 if not found.

find2

protected int find2(String categoryName)
Find and return the index for a category in the database. Searches the database for a category with a name matching categoryName, then returns the associated index. If no matching name is found, it returns the index for which categoryName can be inserted after, in order to keep the categoryDB sorted. If the categoryDB is not sorted, it returns the index equals to numCategories().

Overrides:
find2 in class SettingsDB
Parameters:
categoryName - A String specifying the key for the record.
Returns:
An int containing -1 or the index of the key.

getCategory

public String getCategory(int i)
Gets the category by index. Gets the category by index.

Parameters:
i - the index of the category to be retrieved.
Returns:
the category name of index i.

getCategoryFlags

public int getCategoryFlags(int i)
Gets the category flag by index. Gets the category flag by index.

Returns:
the category flag of index i.

setCategoryFlags

public void setCategoryFlags(int i,
                             int flag)
Sets the category flag by index. Sets the category flag by index.

Parameters:
i - the index of the category flag to be set.
flag - the flag to be set.

getCategoryID

public int getCategoryID(int i)
Gets the category unique ID for a given index. Gets the category unique ID for a given index.

Parameters:
i - the index of the category ID to be retrieved.
Returns:
the category id of index i.

getCategoryIndexByID

public int getCategoryIndexByID(int id)
Gets the categoryDB index for a given category id. Gets the categoryDB index for a given category id.

Parameters:
id - the category id of the index to be retrieved.
Returns:
the index for the category id.

getCategoryByID

public String getCategoryByID(int id)
Gets the categoryDB name for a given category id. Gets the categoryDB name for a given category id.

Parameters:
id - the category id of the index to be retrieved.
Returns:
the category name for the category id.

addCategory

public int addCategory(String inCategoryName)
Add the category name to the CategoryDB. Add the category name to the CategoryDB. It returns a newly generated category id for the category added. If the category name already exists, it returns the category id for that category.

Parameters:
inCategoryName - the category to be added.
Returns:
the category id for the category being added.

editCategory

public boolean editCategory(int i,
                            String inName)
Edit the category name for index i Edit the category name for index i

Parameters:
i - the index of the category to be edited.
inName - the category to be added.
Returns:
true if the name is changed, and false if the index is invalid, or if the new name already exists.

removeCategory

public boolean removeCategory(int i)
Remove the category for index i Remove the category for index i

Parameters:
i - the index of the category to be removed.
Returns:
true if the category is removed, and false if the index is invalid.

sort

protected void sort()
Sort by category names Sort by category names only if getSortCategories() return true. The sort is done by alphebetical order.

Overrides:
sort in class SettingsDB

resolveAllRecords

protected void resolveAllRecords()
Resolves each record with every other record in the CategoryDB database. Resolves each record with every other record in the CategoryDB database. This is a more paranoid way of resolving records. It validates each record against every other record, even if all of them are autosync'd from the service. So, if the service screwed up, and say, created duplicate records, this method will go clean up the settings database and remove the duplicates.

Overrides:
resolveAllRecords in class SettingsDB

resolveAutoSyncConflicts

protected void resolveAutoSyncConflicts()
Resolves potential conflicts caused by auto-syncing the data from Danger service. If a CategoryDB is marked for auto-sync, then this method gets called after an auto-sync from Danger service takes place (this happens after a device hard reset). If there are conflicts in the CategoryDB database, this method attempts to resolve them. Conflicts may arise because the user or application added to the CategoryDB database before the autosync takes place. Currently this method calls ResolveAllRecords() to do the actual conflict resolution.

Overrides:
resolveAutoSyncConflicts in class SettingsDB

setAutoSyncNotifyee

public void setAutoSyncNotifyee(Listener inNotifyee)
Sets the notifyee when autosync conflict resolution is done. Sets the notifyee when autosync conflict resolution is done.

Overrides:
setAutoSyncNotifyee in class SettingsDB
Parameters:
inNotifyee - The listener to be notified of the autosync events.

dumpStats

public void dumpStats()
Dump the CategoryDB data. Dump the CategoryDB data.