motor updates

This commit is contained in:
Sampo Niskanen 2012-01-23 19:07:36 +00:00
parent 7eca17a372
commit bd8e82c1d2

View File

@ -7,7 +7,6 @@ import net.sf.openrocket.motor.ThrustCurveMotor;
import net.sf.openrocket.util.Coordinate; import net.sf.openrocket.util.Coordinate;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.util.Log; import android.util.Log;
@ -20,36 +19,38 @@ public class MotorDao {
private final static String DATABASE_TABLE = "motor"; private final static String DATABASE_TABLE = "motor";
private final static String DROP_TABLE = "DROP TABLE IF EXISTS " + DATABASE_TABLE; private final static String DROP_TABLE = "DROP TABLE IF EXISTS " + DATABASE_TABLE;
private final static String CREATE_TABLE = private final static String CREATE_TABLE =
"create table "+ DATABASE_TABLE + " ( " + "create table " + DATABASE_TABLE + " ( " +
"_id integer primary key, "+ "_id integer primary key, " +
"unique_name text unique, "+ "unique_name text unique, " +
"digest string, " + "digest string, " +
"designation text, "+ "designation text, " +
"delays text, "+ "delays text, " +
"diameter number, "+ "diameter number, " +
"tot_impulse_ns number, "+ "tot_impulse_ns number, " +
"avg_thrust_n number, "+ "avg_thrust_n number, " +
"max_thrust_n number, "+ "max_thrust_n number, " +
"burn_time_s number, "+ "burn_time_s number, " +
"length number," + "length number," +
"prop_mass_g number,"+ "prop_mass_g number," +
"tot_mass_g number,"+ "tot_mass_g number," +
"case_info text,"+ "case_info text," +
"manufacturer text," + "manufacturer text," +
"type text," + "type text," +
"impulse_class text," + "impulse_class text," +
"thrust_data blob,"+ "thrust_data blob," +
"time_data blob," + "time_data blob," +
"cg_data blob"+ "cg_data blob" +
");"; ");";
MotorDao( SQLiteDatabase mDb ) { MotorDao(SQLiteDatabase mDb) {
this.mDb = mDb; this.mDb = mDb;
} }
static String[] create() { return new String[] {CREATE_TABLE}; } static String[] create() {
return new String[] { CREATE_TABLE };
}
static String[] update( int oldVer, int newVer ) { static String[] update(int oldVer, int newVer) {
return new String[] { DROP_TABLE, CREATE_TABLE }; return new String[] { DROP_TABLE, CREATE_TABLE };
} }
@ -73,49 +74,49 @@ public class MotorDao {
public final static String CG_DATA = "cg_data"; public final static String CG_DATA = "cg_data";
private final static String[] ALL_COLS = new String[] { private final static String[] ALL_COLS = new String[] {
ID, ID,
DIGEST, DIGEST,
DESIGNATION , DESIGNATION,
DELAYS , DELAYS,
DIAMETER , DIAMETER,
TOTAL_IMPULSE , TOTAL_IMPULSE,
AVG_THRUST , AVG_THRUST,
MAX_THRUST , MAX_THRUST,
BURN_TIME , BURN_TIME,
LENGTH, LENGTH,
CASE_INFO, CASE_INFO,
TYPE, TYPE,
IMPULSE_CLASS, IMPULSE_CLASS,
MANUFACTURER, MANUFACTURER,
THRUST_DATA, THRUST_DATA,
TIME_DATA, TIME_DATA,
CG_DATA CG_DATA
}; };
public long insertOrUpdateMotor(ExtendedThrustCurveMotor mi) throws Exception { public long insertOrUpdateMotor(ExtendedThrustCurveMotor mi) throws Exception {
ContentValues initialValues = new ContentValues(); ContentValues initialValues = new ContentValues();
final ThrustCurveMotor tcm = mi.getThrustCurveMotor(); final ThrustCurveMotor tcm = mi.getThrustCurveMotor();
initialValues.put(ID, mi.getId()); initialValues.put(ID, mi.getId());
initialValues.put(UNIQUE_NAME, tcm.getManufacturer()+tcm.getDesignation()); initialValues.put(UNIQUE_NAME, tcm.getManufacturer() + tcm.getDesignation());
initialValues.put(DIGEST, tcm.getDigest()); initialValues.put(DIGEST, tcm.getDigest());
initialValues.put(DESIGNATION, tcm.getDesignation()); initialValues.put(DESIGNATION, tcm.getDesignation());
initialValues.put(DELAYS, ConversionUtils.delaysToString(tcm.getStandardDelays())); initialValues.put(DELAYS, ConversionUtils.delaysToString(tcm.getStandardDelays()));
initialValues.put(DIAMETER,tcm.getDiameter()); initialValues.put(DIAMETER, tcm.getDiameter());
initialValues.put(TOTAL_IMPULSE,tcm.getTotalImpulseEstimate()); initialValues.put(TOTAL_IMPULSE, tcm.getTotalImpulseEstimate());
initialValues.put(AVG_THRUST,tcm.getAverageThrustEstimate()); initialValues.put(AVG_THRUST, tcm.getAverageThrustEstimate());
initialValues.put(MAX_THRUST,tcm.getMaxThrustEstimate()); initialValues.put(MAX_THRUST, tcm.getMaxThrustEstimate());
initialValues.put(BURN_TIME,tcm.getBurnTimeEstimate()); initialValues.put(BURN_TIME, tcm.getBurnTimeEstimate());
initialValues.put(LENGTH, tcm.getLength()); initialValues.put(LENGTH, tcm.getLength());
initialValues.put(CASE_INFO, mi.getCaseInfo()); initialValues.put(CASE_INFO, mi.getCaseInfo());
initialValues.put(TYPE, tcm.getMotorType().getName()); initialValues.put(TYPE, tcm.getMotorType().name());
initialValues.put(IMPULSE_CLASS,mi.getImpulseClass()); initialValues.put(IMPULSE_CLASS, mi.getImpulseClass());
initialValues.put(MANUFACTURER,tcm.getManufacturer().getSimpleName()); initialValues.put(MANUFACTURER, tcm.getManufacturer().getSimpleName());
initialValues.put(THRUST_DATA, ConversionUtils.serializeArrayOfDouble(tcm.getThrustPoints())); initialValues.put(THRUST_DATA, ConversionUtils.serializeArrayOfDouble(tcm.getThrustPoints()));
initialValues.put(TIME_DATA,ConversionUtils.serializeArrayOfDouble(tcm.getTimePoints())); initialValues.put(TIME_DATA, ConversionUtils.serializeArrayOfDouble(tcm.getTimePoints()));
initialValues.put(CG_DATA,ConversionUtils.serializeArrayOfCoordinate(tcm.getCGPoints())); initialValues.put(CG_DATA, ConversionUtils.serializeArrayOfCoordinate(tcm.getCGPoints()));
Log.d(TAG,"insertOrUpdate Motor"); Log.d(TAG, "insertOrUpdate Motor");
long rv = mDb.insertWithOnConflict(DATABASE_TABLE, null, initialValues,SQLiteDatabase.CONFLICT_REPLACE); long rv = mDb.insertWithOnConflict(DATABASE_TABLE, null, initialValues, SQLiteDatabase.CONFLICT_REPLACE);
return rv; return rv;
} }
@ -127,7 +128,7 @@ public class MotorDao {
*/ */
public boolean deleteMotor(Long id) { public boolean deleteMotor(Long id) {
boolean rv = mDb.delete(DATABASE_TABLE, ID + "=" + id, null) > 0; boolean rv = mDb.delete(DATABASE_TABLE, ID + "=" + id, null) > 0;
return rv; return rv;
} }
@ -137,14 +138,14 @@ public class MotorDao {
* @param groupVal * @param groupVal
* @return * @return
*/ */
public Cursor fetchAllInGroups( String groupCol, String groupVal ) { public Cursor fetchAllInGroups(String groupCol, String groupVal) {
return mDb.query(DATABASE_TABLE, return mDb.query(DATABASE_TABLE,
/* columns */ ALL_COLS, /* columns */ALL_COLS,
/* selection */groupCol + "=?", /* selection */groupCol + "=?",
/* selection args*/new String[] {groupVal}, /* selection args*/new String[] { groupVal },
/* groupby */null, /* groupby */null,
/* having*/null, /* having*/null,
/* orderby*/ DESIGNATION ); /* orderby*/DESIGNATION);
} }
@ -153,17 +154,17 @@ public class MotorDao {
* @param groupCol * @param groupCol
* @return * @return
*/ */
public Cursor fetchGroups( String groupCol ) { public Cursor fetchGroups(String groupCol) {
return mDb.query(true, DATABASE_TABLE, return mDb.query(true, DATABASE_TABLE,
/* columns */new String[] { /* columns */new String[] {
groupCol groupCol
}, },
/* selection */null, /* selection */null,
/* selection args*/null, /* selection args*/null,
/* groupby */null, /* groupby */null,
/* having*/null, /* having*/null,
/* orderby*/null, /* orderby*/null,
/* limit*/ null); /* limit*/null);
} }
@ -175,7 +176,7 @@ public class MotorDao {
public Cursor fetchAllMotors() { public Cursor fetchAllMotors() {
return mDb.query(DATABASE_TABLE, return mDb.query(DATABASE_TABLE,
/* columns */ ALL_COLS, /* columns */ALL_COLS,
/* selection */null, /* selection */null,
/* selection args*/null, /* selection args*/null,
/* groupby */null, /* groupby */null,
@ -183,7 +184,7 @@ public class MotorDao {
/* orderby*/null); /* orderby*/null);
} }
private ExtendedThrustCurveMotor hydrateMotor( Cursor mCursor ) throws Exception { private ExtendedThrustCurveMotor hydrateMotor(Cursor mCursor) throws Exception {
ExtendedThrustCurveMotor mi = new ExtendedThrustCurveMotor(); ExtendedThrustCurveMotor mi = new ExtendedThrustCurveMotor();
mi.setId(mCursor.getLong(mCursor.getColumnIndex(ID))); mi.setId(mCursor.getLong(mCursor.getColumnIndex(ID)));
@ -200,11 +201,16 @@ public class MotorDao {
double avgImpulse = mCursor.getDouble(mCursor.getColumnIndex(AVG_THRUST)); double avgImpulse = mCursor.getDouble(mCursor.getColumnIndex(AVG_THRUST));
double maxThrust = mCursor.getDouble(mCursor.getColumnIndex(MAX_THRUST)); double maxThrust = mCursor.getDouble(mCursor.getColumnIndex(MAX_THRUST));
double length = mCursor.getDouble(mCursor.getColumnIndex(LENGTH)); double length = mCursor.getDouble(mCursor.getColumnIndex(LENGTH));
Motor.Type type = Motor.Type.fromName( mCursor.getString(mCursor.getColumnIndex(TYPE))); Motor.Type type;
Manufacturer manufacturer = Manufacturer.getManufacturer( mCursor.getString( mCursor.getColumnIndex(MANUFACTURER))); try {
double[] thrustData = ConversionUtils.deserializeArrayOfDouble( mCursor.getBlob(mCursor.getColumnIndex(THRUST_DATA))); type = Enum.valueOf(Motor.Type.class, mCursor.getString(mCursor.getColumnIndex(TYPE)));
double[] timeData = ConversionUtils.deserializeArrayOfDouble( mCursor.getBlob(mCursor.getColumnIndex(TIME_DATA))); } catch (IllegalArgumentException e) {
Coordinate[] cgData = ConversionUtils.deserializeArrayOfCoordinate( mCursor.getBlob(mCursor.getColumnIndex(CG_DATA))); type = Motor.Type.UNKNOWN;
}
Manufacturer manufacturer = Manufacturer.getManufacturer(mCursor.getString(mCursor.getColumnIndex(MANUFACTURER)));
double[] thrustData = ConversionUtils.deserializeArrayOfDouble(mCursor.getBlob(mCursor.getColumnIndex(THRUST_DATA)));
double[] timeData = ConversionUtils.deserializeArrayOfDouble(mCursor.getBlob(mCursor.getColumnIndex(TIME_DATA)));
Coordinate[] cgData = ConversionUtils.deserializeArrayOfCoordinate(mCursor.getBlob(mCursor.getColumnIndex(CG_DATA)));
ThrustCurveMotor tcm = new ThrustCurveMotor(manufacturer, ThrustCurveMotor tcm = new ThrustCurveMotor(manufacturer,
designation, designation,
@ -224,15 +230,15 @@ public class MotorDao {
} }
public ExtendedThrustCurveMotor fetchMotor(Long id ) throws Exception { public ExtendedThrustCurveMotor fetchMotor(Long id) throws Exception {
Cursor mCursor = mDb.query(DATABASE_TABLE, Cursor mCursor = mDb.query(DATABASE_TABLE,
/* columns */ ALL_COLS, /* columns */ALL_COLS,
/* selection */ID + "="+id, /* selection */ID + "=" + id,
/* selection args*/null, /* selection args*/null,
/* groupby */null, /* groupby */null,
/* having*/null, /* having*/null,
/* orderby*/null); /* orderby*/null);
if ( mCursor == null ) { if (mCursor == null) {
return null; return null;
} }
try { try {
@ -241,22 +247,21 @@ public class MotorDao {
} }
mCursor.moveToFirst(); mCursor.moveToFirst();
return hydrateMotor(mCursor); return hydrateMotor(mCursor);
} } finally {
finally {
mCursor.close(); mCursor.close();
} }
} }
public ExtendedThrustCurveMotor fetchMotor(String manufacturerShortName, String designation ) throws Exception { public ExtendedThrustCurveMotor fetchMotor(String manufacturerShortName, String designation) throws Exception {
Cursor mCursor = mDb.query(DATABASE_TABLE, Cursor mCursor = mDb.query(DATABASE_TABLE,
/* columns */ ALL_COLS, /* columns */ALL_COLS,
/* selection */MANUFACTURER + "='"+manufacturerShortName + "' and "+DESIGNATION+"='"+designation+"'", /* selection */MANUFACTURER + "='" + manufacturerShortName + "' and " + DESIGNATION + "='" + designation + "'",
/* selection args*/null, /* selection args*/null,
/* groupby */null, /* groupby */null,
/* having*/null, /* having*/null,
/* orderby*/null); /* orderby*/null);
if ( mCursor == null ) { if (mCursor == null) {
return null; return null;
} }
try { try {
@ -265,8 +270,7 @@ public class MotorDao {
} }
mCursor.moveToFirst(); mCursor.moveToFirst();
return hydrateMotor(mCursor); return hydrateMotor(mCursor);
} } finally {
finally {
mCursor.close(); mCursor.close();
} }