ca.spaz.cron.datasource.sql
Class SQLDatasource

java.lang.Object
  extended by ca.spaz.cron.datasource.AbstractFoodDataSource
      extended by ca.spaz.cron.datasource.AbstractMutableFoodDatasource
          extended by ca.spaz.cron.datasource.sql.SQLDatasource
All Implemented Interfaces:
IFoodDatasource, ILocalFoodDatasource

public class SQLDatasource
extends AbstractMutableFoodDatasource

A Food datasource that is based on a SQL database.

Author:
Chris Rose

Field Summary
protected  java.sql.Connection conn
           
static java.lang.String FOOD_DB_ID
           
 
Constructor Summary
protected SQLDatasource(java.lang.String name, java.lang.String id, java.sql.Connection conn, boolean allowLocal)
           
 
Method Summary
 void close()
          Close this connection.
 boolean containsFood(Food food)
          Determine if this datasource contains the specified food.
static ILocalFoodDatasource createPersonalFoodSource()
           
static ILocalFoodDatasource createPersonalFoodSource(java.lang.String connectionId)
           
static IFoodDatasource createReadonlyFoodSource(java.lang.String connectionID)
           
protected  Food doAddFood(Food food)
          Add a Food to this datasource.
protected  void doAddFoodGroup(FoodGroup foodGroup)
          Add a new FoodGroup to the data source.
protected  boolean doAddMeasure(Food food, Measure measure)
          Each food in the Datasource has available a list of Measures that describe portions or servings.
protected  boolean doChangeMeasure(Food food, java.util.List measures)
          Update all changes made to a set of measures for the food.
protected  boolean doChangeServingAmount(Serving serving)
          Changes the amount of a food consumed to the amount in the serving provided.
protected  Food doConsumeFood(Serving serving)
          This method consumes the food in a serving.
protected  Food doCreateNewFood()
          Create a new Food object associated with this datasource.
protected  java.util.List doFindAllFoods()
          Retrieve a list of all foods in this datasource.
protected  java.util.List doFindFoods(java.lang.String[] parts)
          Retrieve a List of all foods in this particular Datasource.
protected  java.util.List doFindFoods(java.lang.String[] keys, java.lang.String foodGroup, java.lang.String source)
          Retrieve a List of all foods in this particular Datasource having the given food group and source.
protected  java.util.List doGetMeasuresFor(Food food)
          Return a List of Measure objects for the provided food.
protected  void doGetNutrientsFor(Food food, NutrientTable nutrients)
          Fill the provided NutrientTable with the nutrients associated with the Food.
protected  int doGetTimesConsumed(Food food)
          Retrieve the total number of times that this food has been consumed.
protected  int doGetTimesConsumed(Food food, java.util.Date startDate, java.util.Date endDate)
          Retrieve the number of times the specified Food was consumed between the provided dates.
protected  boolean doRemoveFood(Food food)
          Remove a Food from the datasource.
protected  boolean doRemoveMeasure(Food food, Measure m)
          Remove a measure from the database.
protected  boolean doSaveFood(Food food)
          Alter the information of some Food object in the backing representation of the datasource.
protected  boolean doSaveMeasure(Measure measure)
           
protected  boolean doUnConsumeFood(Serving serving)
          Remove the Food in a particular serving from the user's consumed list.
protected  Food findFoodBySourceUID(java.lang.String sourceUID)
          Find a local food by its sourceUID field value.
 java.util.List getConsumedOn(java.util.Date date)
          Retrieve a list of all Servings consumed on a particular date.
 java.util.List getFoodGroups()
          Get a list of all food groups in this datasource.
 java.lang.String getId()
          Retrieve the id from the SQLDatasource.
 java.util.List getSources()
          Get a list of all sources in this datasource.
 void initialize()
          Initialize the datasource to a working state.
 boolean isAvailable()
          Determines if the datasource is operable and available.
 boolean isListable()
          Implementors that can provide listing services should return true here, false otherwise.
 boolean isSearchable()
          Implementors that can provide search services should return true here, false otherwise.
 void setId(java.lang.String id)
          Set the id in the SQLDatasource.
 
Methods inherited from class ca.spaz.cron.datasource.AbstractMutableFoodDatasource
addFood, addFoodDatasourceListener, addFoodGroup, addMeasure, addServing, changeMeasure, changeServingAmount, createNewFood, getNotify, getTimesConsumed, getTimesConsumed, notifyObservers, notifyObservers, removeFood, removeFoodDatasourceListener, removeMeasure, removeServing, saveFood, setNotify
 
Methods inherited from class ca.spaz.cron.datasource.AbstractFoodDataSource
diffSource, findAllFoods, findFoods, findFoods, getLastError, getMeasuresFor, getName, getNutrientsFor, notNull, registerError, sameSource, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ca.spaz.cron.datasource.IFoodDatasource
findAllFoods, findFoods, findFoods, getLastError, getMeasuresFor, getName, getNutrientsFor
 

Field Detail

conn

protected java.sql.Connection conn

FOOD_DB_ID

public static final java.lang.String FOOD_DB_ID
See Also:
Constant Field Values
Constructor Detail

SQLDatasource

protected SQLDatasource(java.lang.String name,
                        java.lang.String id,
                        java.sql.Connection conn,
                        boolean allowLocal)
Method Detail

createReadonlyFoodSource

public static IFoodDatasource createReadonlyFoodSource(java.lang.String connectionID)

createPersonalFoodSource

public static ILocalFoodDatasource createPersonalFoodSource()

createPersonalFoodSource

public static ILocalFoodDatasource createPersonalFoodSource(java.lang.String connectionId)

doConsumeFood

protected Food doConsumeFood(Serving serving)
Description copied from class: AbstractMutableFoodDatasource
This method consumes the food in a serving. This an exception to the requirement that a Food object have this as its datasource, since in this case if the Food object has a different datasource, it will either A) be added to this one, and then consumed, or B) if it is already present in this datasource it will simply be added from this datasource.

Specified by:
doConsumeFood in class AbstractMutableFoodDatasource
Parameters:
serving - a Serving to add to the user's consumed list.
Returns:
A new Food object that represents the Food that is associated with this datasource. If the food was already associated with this datasource, the same object will be returned. null will be returned if some error occurred.

doChangeServingAmount

protected boolean doChangeServingAmount(Serving serving)
Description copied from class: AbstractMutableFoodDatasource
Changes the amount of a food consumed to the amount in the serving provided. This will alter the amount of the Food retrieved by calling getFood() on newServing. If the food is not already in the database as being consumed, it will be added. The food must already be in the user datasource, however.

Specified by:
doChangeServingAmount in class AbstractMutableFoodDatasource
Parameters:
serving - a Serving object whose Food's serving quantity on the proper date will be altered.
Returns:
true if the serving was altered, false if some error occurred.

doUnConsumeFood

protected boolean doUnConsumeFood(Serving serving)
Description copied from class: AbstractMutableFoodDatasource
Remove the Food in a particular serving from the user's consumed list. Operates regardless of quantity.

Specified by:
doUnConsumeFood in class AbstractMutableFoodDatasource
Parameters:
serving - the serving to un-consume.
Returns:
true if the action succeeded, false if there was some error.

doChangeMeasure

protected boolean doChangeMeasure(Food food,
                                  java.util.List measures)
Update all changes made to a set of measures for the food. First removes any old Measures no longer referenced in the new list and then saves or adds all the measures in the list.

Specified by:
doChangeMeasure in class AbstractMutableFoodDatasource
Returns:
true if successful, false if some error occurred.

doRemoveMeasure

protected boolean doRemoveMeasure(Food food,
                                  Measure m)
Remove a measure from the database. Safely modifies all Servings that reference this measure, converting them to canonical GRAMS Measures.

Specified by:
doRemoveMeasure in class AbstractMutableFoodDatasource
Parameters:
m - the Measure to delete
Returns:
true if successful

doAddMeasure

protected boolean doAddMeasure(Food food,
                               Measure measure)
Description copied from class: AbstractMutableFoodDatasource
Each food in the Datasource has available a list of Measures that describe portions or servings. This method will add a valid Measure to a Food object stored in this datasource.

Specified by:
doAddMeasure in class AbstractMutableFoodDatasource
Parameters:
food - The Food object to which the new measure applies.
measure - The new Measure.
Returns:
true if successful, false if some error occurred.

doSaveMeasure

protected boolean doSaveMeasure(Measure measure)

doGetTimesConsumed

protected int doGetTimesConsumed(Food food)
Description copied from class: AbstractMutableFoodDatasource
Retrieve the total number of times that this food has been consumed.

Specified by:
doGetTimesConsumed in class AbstractMutableFoodDatasource
Parameters:
food - the Food to check.
Returns:
the number of times consumed, or -1 on error.

doGetTimesConsumed

protected int doGetTimesConsumed(Food food,
                                 java.util.Date startDate,
                                 java.util.Date endDate)
Description copied from class: AbstractMutableFoodDatasource
Retrieve the number of times the specified Food was consumed between the provided dates.

Specified by:
doGetTimesConsumed in class AbstractMutableFoodDatasource
Parameters:
food - the Food to check.
startDate - the starting date.
endDate - the ending date.
Returns:
the number of times consumed between the two dates, or -1 on error.

getConsumedOn

public java.util.List getConsumedOn(java.util.Date date)
Description copied from interface: ILocalFoodDatasource
Retrieve a list of all Servings consumed on a particular date.

Parameters:
date - the Date to retrieve the foods for.
Returns:
a List consisting only of Serving objects, or null if some error occurred. If no servings were consumed on the specified date, an empty list will be returned.

doAddFood

protected Food doAddFood(Food food)
Description copied from class: AbstractMutableFoodDatasource
Add a Food to this datasource. This method is an exception to the local datasource rule -- the Food provided to this method must not be attached to this datasource.

Specified by:
doAddFood in class AbstractMutableFoodDatasource
Parameters:
food - A Food object that comes from another datasource.
Returns:
a Food object whose information is the same as the parameter, but that exists only in this datasource, with all nutrient information still represtented. null will be returned if some error occurred.

doSaveFood

protected boolean doSaveFood(Food food)
Description copied from class: AbstractMutableFoodDatasource
Alter the information of some Food object in the backing representation of the datasource. This method has potential complications. If the Food implementation specific to the datasource has a concept of unique identifiers, a true alteration will occur. However, if the Food object does not have this feature, for example in a flat-file database or something like it, the implementor should check for name similarity, and if that does not match, simply add the new food to the database.

Specified by:
doSaveFood in class AbstractMutableFoodDatasource
Parameters:
food - a Food object to be changed.
Returns:
true if successful, false if some error occurred.

doRemoveFood

protected boolean doRemoveFood(Food food)
Description copied from class: AbstractMutableFoodDatasource
Remove a Food from the datasource.

Specified by:
doRemoveFood in class AbstractMutableFoodDatasource
Parameters:
food - the Food to remove.
Returns:
true if successful, false if some error occurred.

doFindFoods

protected java.util.List doFindFoods(java.lang.String[] parts)
Description copied from class: AbstractFoodDataSource
Retrieve a List of all foods in this particular Datasource.

Specified by:
doFindFoods in class AbstractFoodDataSource
Parameters:
parts - the keys to search on. This searches with an AND relation.
Returns:
a List of Food objects matching the criteria.

doFindFoods

protected java.util.List doFindFoods(java.lang.String[] keys,
                                     java.lang.String foodGroup,
                                     java.lang.String source)
Description copied from class: AbstractFoodDataSource
Retrieve a List of all foods in this particular Datasource having the given food group and source.

Specified by:
doFindFoods in class AbstractFoodDataSource
Parameters:
keys - the keys to search on. This searches with an AND relation.
foodGroup - the food group to search for. null to ignore.
source - the source of this food item. null to ignore.
Returns:
a List of Food objects matching the criteria.

doFindAllFoods

protected java.util.List doFindAllFoods()
Description copied from class: AbstractFoodDataSource
Retrieve a list of all foods in this datasource.

Specified by:
doFindAllFoods in class AbstractFoodDataSource
Returns:
a List of Food objects consisting of every food in the datasource.

doGetMeasuresFor

protected java.util.List doGetMeasuresFor(Food food)
Description copied from class: AbstractFoodDataSource
Return a List of Measure objects for the provided food.

Specified by:
doGetMeasuresFor in class AbstractFoodDataSource
Parameters:
food - a Food object to find the measures for
Returns:
a List that is guaranteed to contain only Measure objects.

doGetNutrientsFor

protected void doGetNutrientsFor(Food food,
                                 NutrientTable nutrients)
Description copied from class: AbstractFoodDataSource
Fill the provided NutrientTable with the nutrients associated with the Food.

Specified by:
doGetNutrientsFor in class AbstractFoodDataSource
Parameters:
food - The food item to load from
nutrients - the NutrientTable to fill.

getSources

public java.util.List getSources()
Description copied from interface: IFoodDatasource
Get a list of all sources in this datasource. What these are is subimplementation- dependent, and will most often be only one item.

Returns:
a List of String objects naming the sources for this Datasource.

getFoodGroups

public java.util.List getFoodGroups()
Description copied from interface: IFoodDatasource
Get a list of all food groups in this datasource.

Returns:
a List of FoodGroup objects naming the food groups for this Datasource.

isSearchable

public boolean isSearchable()
Description copied from interface: IFoodDatasource
Implementors that can provide search services should return true here, false otherwise.

Returns:
true if this Datasource supports search, false otherwise.

isAvailable

public boolean isAvailable()
Description copied from interface: IFoodDatasource
Determines if the datasource is operable and available. This includes connections to physical datasources, proper setup of tables, or any other conditions that may prevent this from being a functional datasource.

Returns:
true if this datasource is functioning, false otherwise

isListable

public boolean isListable()
Description copied from interface: IFoodDatasource
Implementors that can provide listing services should return true here, false otherwise.

Returns:
true if this Datasource supports listing, false otherwise.

doCreateNewFood

protected Food doCreateNewFood()
Description copied from class: AbstractMutableFoodDatasource
Create a new Food object associated with this datasource.

Specified by:
doCreateNewFood in class AbstractMutableFoodDatasource
Returns:
a new Food instance.

close

public void close()
Description copied from interface: IFoodDatasource
Close this connection. May or may not be required, but this method should ensure that there is nothing remaining of the connection.


initialize

public void initialize()
Description copied from interface: IFoodDatasource
Initialize the datasource to a working state. At the time that this method completes normally (without throwing an exception), isAvailable must return true for this data source. If this source is already available, this method will do nothing.


doAddFoodGroup

protected void doAddFoodGroup(FoodGroup foodGroup)
Description copied from class: AbstractMutableFoodDatasource
Add a new FoodGroup to the data source. The food group may already exist, in which case an implementing method should silently succeed.

Specified by:
doAddFoodGroup in class AbstractMutableFoodDatasource
Parameters:
foodGroup - the new food group to add to the data source.

setId

public void setId(java.lang.String id)
Set the id in the SQLDatasource.

Parameters:
id - The id to set.

getId

public java.lang.String getId()
Retrieve the id from the SQLDatasource. This will be unique between all SQL data source instances.

Returns:
Returns the id.

findFoodBySourceUID

protected Food findFoodBySourceUID(java.lang.String sourceUID)
Description copied from class: AbstractMutableFoodDatasource
Find a local food by its sourceUID field value.

Specified by:
findFoodBySourceUID in class AbstractMutableFoodDatasource
Parameters:
sourceUID - The key to search on
Returns:
The Food found, or null if no food matched.

containsFood

public boolean containsFood(Food food)
Description copied from interface: IFoodDatasource
Determine if this datasource contains the specified food. This method will return true if for any Food in this datasource, the Food's SourceUID is equal to that of the food on the command line.

Parameters:
food - The food to test for
Returns:
true if there is a Food in this datasource matching the provided one.


Copyright © 2005 Spaz. All Rights Reserved.