package net.sf.openrocket.android.db;\r
\r
import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;\r
+import net.sf.openrocket.android.util.AndroidLogWrapper;\r
+import net.sf.openrocket.android.util.AndroidLogWrapper.LogHelper;\r
import net.sf.openrocket.motor.Manufacturer;\r
import net.sf.openrocket.motor.Motor;\r
import net.sf.openrocket.motor.ThrustCurveMotor;\r
import net.sf.openrocket.util.Coordinate;\r
import android.content.ContentValues;\r
import android.database.Cursor;\r
-import android.database.SQLException;\r
import android.database.sqlite.SQLiteDatabase;\r
-import android.util.Log;\r
\r
public class MotorDao {\r
-\r
- private static final String TAG = "MotorDao";\r
-\r
+ \r
private SQLiteDatabase mDb;\r
-\r
+ \r
private final static String DATABASE_TABLE = "motor";\r
private final static String DROP_TABLE = "DROP TABLE IF EXISTS " + DATABASE_TABLE;\r
private final static String CREATE_TABLE =\r
- "create table "+ DATABASE_TABLE + " ( " +\r
- "_id integer primary key, "+\r
- "unique_name text unique, "+\r
- "designation text, "+\r
- "delays blob, "+\r
- "diameter number, "+\r
- "tot_impulse_ns number, "+\r
- "avg_thrust_n number, "+\r
- "max_thrust_n number, "+\r
- "burn_time_s number, "+\r
+ "create table " + DATABASE_TABLE + " ( " +\r
+ "_id integer primary key, " +\r
+ "unique_name text unique, " +\r
+ "digest string, " +\r
+ "designation text, " +\r
+ "delays text, " +\r
+ "diameter number, " +\r
+ "tot_impulse_ns number, " +\r
+ "avg_thrust_n number, " +\r
+ "max_thrust_n number, " +\r
+ "burn_time_s number, " +\r
"length number," +\r
- "prop_mass_g number,"+\r
- "tot_mass_g number,"+\r
- "case_info text,"+\r
+ "prop_mass_g number," +\r
+ "tot_mass_g number," +\r
+ "case_info text," +\r
"manufacturer text," +\r
"type text," +\r
"impulse_class text," +\r
- "thrust_data blob,"+\r
+ "thrust_data blob," +\r
"time_data blob," +\r
- "cg_data blob"+\r
+ "cg_data blob" +\r
");";\r
-\r
- MotorDao( SQLiteDatabase mDb ) {\r
+ \r
+ MotorDao(SQLiteDatabase mDb) {\r
this.mDb = mDb;\r
}\r
-\r
- static String[] create() { return new String[] {CREATE_TABLE}; }\r
-\r
- static String[] update( int oldVer, int newVer ) {\r
+ \r
+ static String[] create() {\r
+ return new String[] { CREATE_TABLE };\r
+ }\r
+ \r
+ static String[] update(int oldVer, int newVer) {\r
return new String[] { DROP_TABLE, CREATE_TABLE };\r
}\r
-\r
+ \r
public final static String ID = "_id";\r
public final static String UNIQUE_NAME = "unique_name";\r
+ public final static String DIGEST = "digest";\r
public final static String DESIGNATION = "designation";\r
public final static String DELAYS = "delays";\r
public final static String DIAMETER = "diameter";\r
- public final static String TOTAL_IMPULSE = "tot_impulse_ns"; \r
+ public final static String TOTAL_IMPULSE = "tot_impulse_ns";\r
public final static String AVG_THRUST = "avg_thrust_n";\r
public final static String MAX_THRUST = "max_thrust_n";\r
public final static String BURN_TIME = "burn_time_s";\r
public final static String THRUST_DATA = "thrust_data";\r
public final static String TIME_DATA = "time_data";\r
public final static String CG_DATA = "cg_data";\r
-\r
+ \r
private final static String[] ALL_COLS = new String[] {\r
- ID,\r
- DESIGNATION ,\r
- DELAYS ,\r
- DIAMETER ,\r
- TOTAL_IMPULSE ,\r
- AVG_THRUST ,\r
- MAX_THRUST ,\r
- BURN_TIME ,\r
- LENGTH,\r
- CASE_INFO,\r
- TYPE,\r
- IMPULSE_CLASS,\r
- MANUFACTURER,\r
- THRUST_DATA,\r
- TIME_DATA,\r
- CG_DATA\r
+ ID,\r
+ DIGEST,\r
+ DESIGNATION,\r
+ DELAYS,\r
+ DIAMETER,\r
+ TOTAL_IMPULSE,\r
+ AVG_THRUST,\r
+ MAX_THRUST,\r
+ BURN_TIME,\r
+ LENGTH,\r
+ CASE_INFO,\r
+ TYPE,\r
+ IMPULSE_CLASS,\r
+ MANUFACTURER,\r
+ THRUST_DATA,\r
+ TIME_DATA,\r
+ CG_DATA\r
};\r
-\r
+ \r
public long insertOrUpdateMotor(ExtendedThrustCurveMotor mi) throws Exception {\r
ContentValues initialValues = new ContentValues();\r
- final ThrustCurveMotor tcm = mi.getThrustCurveMotor();\r
initialValues.put(ID, mi.getId());\r
- initialValues.put(UNIQUE_NAME, tcm.getManufacturer()+tcm.getDesignation());\r
- initialValues.put(DESIGNATION, tcm.getDesignation());\r
- initialValues.put(DELAYS, ConversionUtils.serializeArrayOfDouble(tcm.getStandardDelays()));\r
- initialValues.put(DIAMETER,tcm.getDiameter());\r
- initialValues.put(TOTAL_IMPULSE,tcm.getTotalImpulseEstimate());\r
- initialValues.put(AVG_THRUST,tcm.getAverageThrustEstimate());\r
- initialValues.put(MAX_THRUST,tcm.getMaxThrustEstimate());\r
- initialValues.put(BURN_TIME,tcm.getBurnTimeEstimate());\r
- initialValues.put(LENGTH, tcm.getLength());\r
+ initialValues.put(UNIQUE_NAME, mi.getManufacturer() + mi.getDesignation());\r
+ initialValues.put(DIGEST, mi.getDigest());\r
+ initialValues.put(DESIGNATION, mi.getDesignation());\r
+ initialValues.put(DELAYS, ConversionUtils.delaysToString(mi.getStandardDelays()));\r
+ initialValues.put(DIAMETER, mi.getDiameter());\r
+ initialValues.put(TOTAL_IMPULSE, mi.getTotalImpulseEstimate());\r
+ initialValues.put(AVG_THRUST, mi.getAverageThrustEstimate());\r
+ initialValues.put(MAX_THRUST, mi.getMaxThrustEstimate());\r
+ initialValues.put(BURN_TIME, mi.getBurnTimeEstimate());\r
+ initialValues.put(LENGTH, mi.getLength());\r
initialValues.put(CASE_INFO, mi.getCaseInfo());\r
- initialValues.put(TYPE, tcm.getMotorType().getName());\r
- initialValues.put(IMPULSE_CLASS,mi.getImpulseClass());\r
- initialValues.put(MANUFACTURER,tcm.getManufacturer().getSimpleName());\r
- initialValues.put(THRUST_DATA, ConversionUtils.serializeArrayOfDouble(tcm.getThrustPoints()));\r
- initialValues.put(TIME_DATA,ConversionUtils.serializeArrayOfDouble(tcm.getTimePoints()));\r
- initialValues.put(CG_DATA,ConversionUtils.serializeArrayOfCoordinate(tcm.getCGPoints()));\r
-\r
- Log.d(TAG,"insertOrUpdate Motor");\r
- long rv = mDb.insertWithOnConflict(DATABASE_TABLE, null, initialValues,SQLiteDatabase.CONFLICT_REPLACE);\r
+ initialValues.put(TYPE, mi.getMotorType().name());\r
+ initialValues.put(IMPULSE_CLASS, mi.getImpulseClass());\r
+ initialValues.put(MANUFACTURER, mi.getManufacturer().getSimpleName());\r
+ initialValues.put(THRUST_DATA, ConversionUtils.serializeArrayOfDouble(mi.getThrustPoints()));\r
+ initialValues.put(TIME_DATA, ConversionUtils.serializeArrayOfDouble(mi.getTimePoints()));\r
+ initialValues.put(CG_DATA, ConversionUtils.serializeArrayOfCoordinate(mi.getCGPoints()));\r
+ \r
+ AndroidLogWrapper.d(MotorDao.class, "insertOrUpdate Motor");\r
+ long rv = mDb.insertWithOnConflict(DATABASE_TABLE, null, initialValues, SQLiteDatabase.CONFLICT_REPLACE);\r
return rv;\r
}\r
-\r
+ \r
/**\r
* Delete the motor and burn data with the given rowId\r
* \r
* @return true if deleted, false otherwise\r
*/\r
public boolean deleteMotor(Long id) {\r
-\r
- boolean rv = mDb.delete(DATABASE_TABLE, ID + "=" + id, null) > 0;\r
+ \r
+ boolean rv = mDb.delete(DATABASE_TABLE, ID + "=" + id, null) > 0;\r
return rv;\r
}\r
-\r
+ \r
/**\r
* \r
* @param groupCol\r
* @param groupVal\r
* @return\r
*/\r
- public Cursor fetchAllInGroups( String groupCol, String groupVal ) {\r
- return mDb.query(DATABASE_TABLE, \r
- /* columns */ ALL_COLS,\r
+ public Cursor fetchAllInGroups(String groupCol, String groupVal) {\r
+ return mDb.query(DATABASE_TABLE,\r
+ /* columns */ALL_COLS,\r
/* selection */groupCol + "=?",\r
- /* selection args*/new String[] {groupVal},\r
+ /* selection args*/new String[] { groupVal },\r
/* groupby */null,\r
/* having*/null,\r
- /* orderby*/ DESIGNATION );\r
-\r
+ /* orderby*/TOTAL_IMPULSE);\r
+ \r
}\r
-\r
+ \r
/**\r
* Fetch the groups based on groupCol\r
* @param groupCol\r
* @return\r
*/\r
- public Cursor fetchGroups( String groupCol ) {\r
- return mDb.query(true, DATABASE_TABLE, \r
+ public Cursor fetchGroups(String groupCol) {\r
+ return mDb.query(true, DATABASE_TABLE,\r
/* columns */new String[] {\r
groupCol\r
- },\r
- /* selection */null,\r
- /* selection args*/null,\r
- /* groupby */null,\r
- /* having*/null,\r
- /* orderby*/null,\r
- /* limit*/ null);\r
-\r
+ },\r
+ /* selection */null,\r
+ /* selection args*/null,\r
+ /* groupby */null,\r
+ /* having*/null,\r
+ /* orderby*/groupCol,\r
+ /* limit*/null);\r
+ \r
}\r
-\r
+ \r
/**\r
* Return a Cursor over the list of all motors\r
* \r
* @return Cursor over all notes\r
*/\r
public Cursor fetchAllMotors() {\r
-\r
+ \r
return mDb.query(DATABASE_TABLE,\r
- /* columns */ ALL_COLS,\r
+ /* columns */ALL_COLS,\r
/* selection */null,\r
/* selection args*/null,\r
/* groupby */null,\r
/* having*/null,\r
/* orderby*/null);\r
}\r
-\r
- private ExtendedThrustCurveMotor hydrateMotor( Cursor mCursor ) throws Exception {\r
- ExtendedThrustCurveMotor mi = new ExtendedThrustCurveMotor();\r
-\r
- mi.setId(mCursor.getLong(mCursor.getColumnIndex(ID)));\r
- mi.setCaseInfo(mCursor.getString(mCursor.getColumnIndex(CASE_INFO)));\r
- mi.setImpulseClass(mCursor.getString(mCursor.getColumnIndex(IMPULSE_CLASS)));\r
-\r
+ \r
+ private ExtendedThrustCurveMotor hydrateMotor(Cursor mCursor) throws Exception {\r
+ ExtendedThrustCurveMotor mi;\r
{\r
+ String digest = mCursor.getString(mCursor.getColumnIndex(DIGEST));\r
String designation = mCursor.getString(mCursor.getColumnIndex(DESIGNATION));\r
- double[] delays = ConversionUtils.deserializeArrayOfDouble( mCursor.getBlob(mCursor.getColumnIndex(DELAYS)));\r
+ String delayString = mCursor.getString(mCursor.getColumnIndex(DELAYS));\r
+ double[] delays = ConversionUtils.stringToDelays(delayString);\r
double diameter = mCursor.getDouble(mCursor.getColumnIndex(DIAMETER));\r
double totImpulse = mCursor.getDouble(mCursor.getColumnIndex(TOTAL_IMPULSE));\r
double avgImpulse = mCursor.getDouble(mCursor.getColumnIndex(AVG_THRUST));\r
double maxThrust = mCursor.getDouble(mCursor.getColumnIndex(MAX_THRUST));\r
double length = mCursor.getDouble(mCursor.getColumnIndex(LENGTH));\r
- Motor.Type type = Motor.Type.fromName( mCursor.getString(mCursor.getColumnIndex(TYPE)));\r
- Manufacturer manufacturer = Manufacturer.getManufacturer( mCursor.getString( mCursor.getColumnIndex(MANUFACTURER)));\r
- double[] thrustData = ConversionUtils.deserializeArrayOfDouble( mCursor.getBlob(mCursor.getColumnIndex(THRUST_DATA)));\r
- double[] timeData = ConversionUtils.deserializeArrayOfDouble( mCursor.getBlob(mCursor.getColumnIndex(TIME_DATA)));\r
- Coordinate[] cgData = ConversionUtils.deserializeArrayOfCoordinate( mCursor.getBlob(mCursor.getColumnIndex(CG_DATA)));\r
-\r
+ Motor.Type type;\r
+ try {\r
+ type = Enum.valueOf(Motor.Type.class, mCursor.getString(mCursor.getColumnIndex(TYPE)));\r
+ } catch (IllegalArgumentException e) {\r
+ type = Motor.Type.UNKNOWN;\r
+ }\r
+ Manufacturer manufacturer = Manufacturer.getManufacturer(mCursor.getString(mCursor.getColumnIndex(MANUFACTURER)));\r
+ double[] thrustData = ConversionUtils.deserializeArrayOfDouble(mCursor.getBlob(mCursor.getColumnIndex(THRUST_DATA)));\r
+ double[] timeData = ConversionUtils.deserializeArrayOfDouble(mCursor.getBlob(mCursor.getColumnIndex(TIME_DATA)));\r
+ Coordinate[] cgData = ConversionUtils.deserializeArrayOfCoordinate(mCursor.getBlob(mCursor.getColumnIndex(CG_DATA)));\r
+ \r
ThrustCurveMotor tcm = new ThrustCurveMotor(manufacturer,\r
designation,\r
"",\r
length,\r
timeData,\r
thrustData,\r
- cgData\r
+ cgData,\r
+ digest\r
);\r
- mi.setThrustCurveMotor(tcm);\r
+ mi = new ExtendedThrustCurveMotor(tcm);\r
+ \r
+ mi.setId(mCursor.getLong(mCursor.getColumnIndex(ID)));\r
+ mi.setCaseInfo(mCursor.getString(mCursor.getColumnIndex(CASE_INFO)));\r
+ mi.setImpulseClass(mCursor.getString(mCursor.getColumnIndex(IMPULSE_CLASS)));\r
+ \r
}\r
return mi;\r
-\r
+ \r
}\r
-\r
- public ExtendedThrustCurveMotor fetchMotor(Long id ) throws Exception {\r
- Cursor mCursor = mDb.query(DATABASE_TABLE, \r
- /* columns */ ALL_COLS,\r
- /* selection */ID + "="+id,\r
+ \r
+ public ExtendedThrustCurveMotor fetchMotor(Long id) throws Exception {\r
+ Cursor mCursor = mDb.query(DATABASE_TABLE,\r
+ /* columns */ALL_COLS,\r
+ /* selection */ID + "=" + id,\r
/* selection args*/null,\r
/* groupby */null,\r
/* having*/null,\r
/* orderby*/null);\r
- if ( mCursor == null ) {\r
+ if (mCursor == null) {\r
return null;\r
}\r
try {\r
}\r
mCursor.moveToFirst();\r
return hydrateMotor(mCursor);\r
- }\r
- finally {\r
+ } finally {\r
mCursor.close();\r
}\r
-\r
+ \r
}\r
-\r
- public ExtendedThrustCurveMotor fetchMotor(String manufacturerShortName, String designation ) throws Exception {\r
- Cursor mCursor = mDb.query(DATABASE_TABLE, \r
- /* columns */ ALL_COLS,\r
- /* selection */MANUFACTURER + "='"+manufacturerShortName + "' and "+DESIGNATION+"='"+designation+"'",\r
+ \r
+ public ExtendedThrustCurveMotor fetchMotor(String manufacturerShortName, String designation) throws Exception {\r
+ Cursor mCursor = mDb.query(DATABASE_TABLE,\r
+ /* columns */ALL_COLS,\r
+ /* selection */MANUFACTURER + "='" + manufacturerShortName + "' and " + DESIGNATION + "='" + designation + "'",\r
/* selection args*/null,\r
/* groupby */null,\r
/* having*/null,\r
/* orderby*/null);\r
- if ( mCursor == null ) {\r
+ if (mCursor == null) {\r
return null;\r
}\r
try {\r
}\r
mCursor.moveToFirst();\r
return hydrateMotor(mCursor);\r
- }\r
- finally {\r
+ } catch( Exception ex ) {\r
+ LogHelper.getInstance().debug("whoa!", ex);\r
+ throw ex;\r
+ } finally {\r
mCursor.close();\r
}\r
-\r
- }\r
- \r
- public static String extractPrettyDelayString( Cursor c ) {\r
- byte[] blob = c.getBlob(c.getColumnIndex(MotorDao.DELAYS));\r
- String s = "";\r
- try {\r
- double[] delayarry = ConversionUtils.deserializeArrayOfDouble(blob);\r
- boolean first = true;\r
- for( double d:delayarry ) {\r
- if (!first) {\r
- s += ",";\r
- } else {\r
- first = false;\r
- }\r
- if ( d == Motor.PLUGGED ) {\r
- s+= "P";\r
- } else {\r
- s += Math.round(d);\r
- }\r
- }\r
- } catch ( Exception ex ) {\r
- }\r
- return s;\r
+ \r
}\r
\r
}\r