1 package net.sf.openrocket.android.db;
\r
3 import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;
\r
4 import net.sf.openrocket.motor.Manufacturer;
\r
5 import net.sf.openrocket.motor.Motor;
\r
6 import net.sf.openrocket.motor.ThrustCurveMotor;
\r
7 import net.sf.openrocket.util.Coordinate;
\r
8 import android.content.ContentValues;
\r
9 import android.database.Cursor;
\r
10 import android.database.sqlite.SQLiteDatabase;
\r
11 import android.util.Log;
\r
13 public class MotorDao {
\r
15 private static final String TAG = "MotorDao";
\r
17 private SQLiteDatabase mDb;
\r
19 private final static String DATABASE_TABLE = "motor";
\r
20 private final static String DROP_TABLE = "DROP TABLE IF EXISTS " + DATABASE_TABLE;
\r
21 private final static String CREATE_TABLE =
\r
22 "create table " + DATABASE_TABLE + " ( " +
\r
23 "_id integer primary key, " +
\r
24 "unique_name text unique, " +
\r
26 "designation text, " +
\r
28 "diameter number, " +
\r
29 "tot_impulse_ns number, " +
\r
30 "avg_thrust_n number, " +
\r
31 "max_thrust_n number, " +
\r
32 "burn_time_s number, " +
\r
34 "prop_mass_g number," +
\r
35 "tot_mass_g number," +
\r
37 "manufacturer text," +
\r
39 "impulse_class text," +
\r
40 "thrust_data blob," +
\r
45 MotorDao(SQLiteDatabase mDb) {
\r
49 static String[] create() {
\r
50 return new String[] { CREATE_TABLE };
\r
53 static String[] update(int oldVer, int newVer) {
\r
54 return new String[] { DROP_TABLE, CREATE_TABLE };
\r
57 public final static String ID = "_id";
\r
58 public final static String UNIQUE_NAME = "unique_name";
\r
59 public final static String DIGEST = "digest";
\r
60 public final static String DESIGNATION = "designation";
\r
61 public final static String DELAYS = "delays";
\r
62 public final static String DIAMETER = "diameter";
\r
63 public final static String TOTAL_IMPULSE = "tot_impulse_ns";
\r
64 public final static String AVG_THRUST = "avg_thrust_n";
\r
65 public final static String MAX_THRUST = "max_thrust_n";
\r
66 public final static String BURN_TIME = "burn_time_s";
\r
67 public final static String LENGTH = "length";
\r
68 public final static String CASE_INFO = "case_info";
\r
69 public final static String MANUFACTURER = "manufacturer";
\r
70 public final static String TYPE = "type";
\r
71 public final static String IMPULSE_CLASS = "impulse_class";
\r
72 public final static String THRUST_DATA = "thrust_data";
\r
73 public final static String TIME_DATA = "time_data";
\r
74 public final static String CG_DATA = "cg_data";
\r
76 private final static String[] ALL_COLS = new String[] {
\r
96 public long insertOrUpdateMotor(ExtendedThrustCurveMotor mi) throws Exception {
\r
97 ContentValues initialValues = new ContentValues();
\r
98 final ThrustCurveMotor tcm = mi.getThrustCurveMotor();
\r
99 initialValues.put(ID, mi.getId());
\r
100 initialValues.put(UNIQUE_NAME, tcm.getManufacturer() + tcm.getDesignation());
\r
101 initialValues.put(DIGEST, tcm.getDigest());
\r
102 initialValues.put(DESIGNATION, tcm.getDesignation());
\r
103 initialValues.put(DELAYS, ConversionUtils.delaysToString(tcm.getStandardDelays()));
\r
104 initialValues.put(DIAMETER, tcm.getDiameter());
\r
105 initialValues.put(TOTAL_IMPULSE, tcm.getTotalImpulseEstimate());
\r
106 initialValues.put(AVG_THRUST, tcm.getAverageThrustEstimate());
\r
107 initialValues.put(MAX_THRUST, tcm.getMaxThrustEstimate());
\r
108 initialValues.put(BURN_TIME, tcm.getBurnTimeEstimate());
\r
109 initialValues.put(LENGTH, tcm.getLength());
\r
110 initialValues.put(CASE_INFO, mi.getCaseInfo());
\r
111 initialValues.put(TYPE, tcm.getMotorType().name());
\r
112 initialValues.put(IMPULSE_CLASS, mi.getImpulseClass());
\r
113 initialValues.put(MANUFACTURER, tcm.getManufacturer().getSimpleName());
\r
114 initialValues.put(THRUST_DATA, ConversionUtils.serializeArrayOfDouble(tcm.getThrustPoints()));
\r
115 initialValues.put(TIME_DATA, ConversionUtils.serializeArrayOfDouble(tcm.getTimePoints()));
\r
116 initialValues.put(CG_DATA, ConversionUtils.serializeArrayOfCoordinate(tcm.getCGPoints()));
\r
118 Log.d(TAG, "insertOrUpdate Motor");
\r
119 long rv = mDb.insertWithOnConflict(DATABASE_TABLE, null, initialValues, SQLiteDatabase.CONFLICT_REPLACE);
\r
124 * Delete the motor and burn data with the given rowId
\r
126 * @param name name of motor to delete
\r
127 * @return true if deleted, false otherwise
\r
129 public boolean deleteMotor(Long id) {
\r
131 boolean rv = mDb.delete(DATABASE_TABLE, ID + "=" + id, null) > 0;
\r
141 public Cursor fetchAllInGroups(String groupCol, String groupVal) {
\r
142 return mDb.query(DATABASE_TABLE,
\r
143 /* columns */ALL_COLS,
\r
144 /* selection */groupCol + "=?",
\r
145 /* selection args*/new String[] { groupVal },
\r
148 /* orderby*/DESIGNATION);
\r
153 * Fetch the groups based on groupCol
\r
157 public Cursor fetchGroups(String groupCol) {
\r
158 return mDb.query(true, DATABASE_TABLE,
\r
159 /* columns */new String[] {
\r
162 /* selection */null,
\r
163 /* selection args*/null,
\r
172 * Return a Cursor over the list of all motors
\r
174 * @return Cursor over all notes
\r
176 public Cursor fetchAllMotors() {
\r
178 return mDb.query(DATABASE_TABLE,
\r
179 /* columns */ALL_COLS,
\r
180 /* selection */null,
\r
181 /* selection args*/null,
\r
187 private ExtendedThrustCurveMotor hydrateMotor(Cursor mCursor) throws Exception {
\r
188 ExtendedThrustCurveMotor mi = new ExtendedThrustCurveMotor();
\r
190 mi.setId(mCursor.getLong(mCursor.getColumnIndex(ID)));
\r
191 mi.setCaseInfo(mCursor.getString(mCursor.getColumnIndex(CASE_INFO)));
\r
192 mi.setImpulseClass(mCursor.getString(mCursor.getColumnIndex(IMPULSE_CLASS)));
\r
195 String digest = mCursor.getString(mCursor.getColumnIndex(DIGEST));
\r
196 String designation = mCursor.getString(mCursor.getColumnIndex(DESIGNATION));
\r
197 String delayString = mCursor.getString(mCursor.getColumnIndex(DELAYS));
\r
198 double[] delays = ConversionUtils.stringToDelays(delayString);
\r
199 double diameter = mCursor.getDouble(mCursor.getColumnIndex(DIAMETER));
\r
200 double totImpulse = mCursor.getDouble(mCursor.getColumnIndex(TOTAL_IMPULSE));
\r
201 double avgImpulse = mCursor.getDouble(mCursor.getColumnIndex(AVG_THRUST));
\r
202 double maxThrust = mCursor.getDouble(mCursor.getColumnIndex(MAX_THRUST));
\r
203 double length = mCursor.getDouble(mCursor.getColumnIndex(LENGTH));
\r
206 type = Enum.valueOf(Motor.Type.class, mCursor.getString(mCursor.getColumnIndex(TYPE)));
\r
207 } catch (IllegalArgumentException e) {
\r
208 type = Motor.Type.UNKNOWN;
\r
210 Manufacturer manufacturer = Manufacturer.getManufacturer(mCursor.getString(mCursor.getColumnIndex(MANUFACTURER)));
\r
211 double[] thrustData = ConversionUtils.deserializeArrayOfDouble(mCursor.getBlob(mCursor.getColumnIndex(THRUST_DATA)));
\r
212 double[] timeData = ConversionUtils.deserializeArrayOfDouble(mCursor.getBlob(mCursor.getColumnIndex(TIME_DATA)));
\r
213 Coordinate[] cgData = ConversionUtils.deserializeArrayOfCoordinate(mCursor.getBlob(mCursor.getColumnIndex(CG_DATA)));
\r
215 ThrustCurveMotor tcm = new ThrustCurveMotor(manufacturer,
\r
227 mi.setThrustCurveMotor(tcm);
\r
233 public ExtendedThrustCurveMotor fetchMotor(Long id) throws Exception {
\r
234 Cursor mCursor = mDb.query(DATABASE_TABLE,
\r
235 /* columns */ALL_COLS,
\r
236 /* selection */ID + "=" + id,
\r
237 /* selection args*/null,
\r
241 if (mCursor == null) {
\r
245 if (mCursor.getCount() == 0) {
\r
248 mCursor.moveToFirst();
\r
249 return hydrateMotor(mCursor);
\r
256 public ExtendedThrustCurveMotor fetchMotor(String manufacturerShortName, String designation) throws Exception {
\r
257 Cursor mCursor = mDb.query(DATABASE_TABLE,
\r
258 /* columns */ALL_COLS,
\r
259 /* selection */MANUFACTURER + "='" + manufacturerShortName + "' and " + DESIGNATION + "='" + designation + "'",
\r
260 /* selection args*/null,
\r
264 if (mCursor == null) {
\r
268 if (mCursor.getCount() == 0) {
\r
271 mCursor.moveToFirst();
\r
272 return hydrateMotor(mCursor);
\r