]> git.gag.com Git - debian/openrocket/blob - android/src/net/sf/openrocket/android/db/MotorDao.java
motor updates
[debian/openrocket] / android / src / net / sf / openrocket / android / db / MotorDao.java
1 package net.sf.openrocket.android.db;\r
2 \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
12 \r
13 public class MotorDao {\r
14         \r
15         private static final String TAG = "MotorDao";\r
16         \r
17         private SQLiteDatabase mDb;\r
18         \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
25                                         "digest string, " +\r
26                                         "designation text, " +\r
27                                         "delays 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
33                                         "length number," +\r
34                                         "prop_mass_g number," +\r
35                                         "tot_mass_g number," +\r
36                                         "case_info text," +\r
37                                         "manufacturer text," +\r
38                                         "type text," +\r
39                                         "impulse_class text," +\r
40                                         "thrust_data blob," +\r
41                                         "time_data blob," +\r
42                                         "cg_data blob" +\r
43                                         ");";\r
44         \r
45         MotorDao(SQLiteDatabase mDb) {\r
46                 this.mDb = mDb;\r
47         }\r
48         \r
49         static String[] create() {\r
50                 return new String[] { CREATE_TABLE };\r
51         }\r
52         \r
53         static String[] update(int oldVer, int newVer) {\r
54                 return new String[] { DROP_TABLE, CREATE_TABLE };\r
55         }\r
56         \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
75         \r
76         private final static String[] ALL_COLS = new String[] {\r
77                         ID,\r
78                         DIGEST,\r
79                         DESIGNATION,\r
80                         DELAYS,\r
81                         DIAMETER,\r
82                         TOTAL_IMPULSE,\r
83                         AVG_THRUST,\r
84                         MAX_THRUST,\r
85                         BURN_TIME,\r
86                         LENGTH,\r
87                         CASE_INFO,\r
88                         TYPE,\r
89                         IMPULSE_CLASS,\r
90                         MANUFACTURER,\r
91                         THRUST_DATA,\r
92                         TIME_DATA,\r
93                         CG_DATA\r
94         };\r
95         \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
117                 \r
118                 Log.d(TAG, "insertOrUpdate Motor");\r
119                 long rv = mDb.insertWithOnConflict(DATABASE_TABLE, null, initialValues, SQLiteDatabase.CONFLICT_REPLACE);\r
120                 return rv;\r
121         }\r
122         \r
123         /**\r
124          * Delete the motor and burn data with the given rowId\r
125          * \r
126          * @param name name of motor to delete\r
127          * @return true if deleted, false otherwise\r
128          */\r
129         public boolean deleteMotor(Long id) {\r
130                 \r
131                 boolean rv = mDb.delete(DATABASE_TABLE, ID + "=" + id, null) > 0;\r
132                 return rv;\r
133         }\r
134         \r
135         /**\r
136          * \r
137          * @param groupCol\r
138          * @param groupVal\r
139          * @return\r
140          */\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
146                                 /* groupby */null,\r
147                                 /* having*/null,\r
148                                 /* orderby*/DESIGNATION);\r
149                 \r
150         }\r
151         \r
152         /**\r
153          * Fetch the groups based on groupCol\r
154          * @param groupCol\r
155          * @return\r
156          */\r
157         public Cursor fetchGroups(String groupCol) {\r
158                 return mDb.query(true, DATABASE_TABLE,\r
159                                 /* columns */new String[] {\r
160                                 groupCol\r
161                                 },\r
162                                 /* selection */null,\r
163                                 /* selection args*/null,\r
164                                 /* groupby */null,\r
165                                 /* having*/null,\r
166                                 /* orderby*/null,\r
167                                 /* limit*/null);\r
168                 \r
169         }\r
170         \r
171         /**\r
172          * Return a Cursor over the list of all motors\r
173          * \r
174          * @return Cursor over all notes\r
175          */\r
176         public Cursor fetchAllMotors() {\r
177                 \r
178                 return mDb.query(DATABASE_TABLE,\r
179                                 /* columns */ALL_COLS,\r
180                                 /* selection */null,\r
181                                 /* selection args*/null,\r
182                                 /* groupby */null,\r
183                                 /* having*/null,\r
184                                 /* orderby*/null);\r
185         }\r
186         \r
187         private ExtendedThrustCurveMotor hydrateMotor(Cursor mCursor) throws Exception {\r
188                 ExtendedThrustCurveMotor mi = new ExtendedThrustCurveMotor();\r
189                 \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
193                 \r
194                 {\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
204                         Motor.Type type;\r
205                         try {\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
209                         }\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
214                         \r
215                         ThrustCurveMotor tcm = new ThrustCurveMotor(manufacturer,\r
216                                         designation,\r
217                                         "",\r
218                                         type,\r
219                                         delays,\r
220                                         diameter,\r
221                                         length,\r
222                                         timeData,\r
223                                         thrustData,\r
224                                         cgData,\r
225                                         digest\r
226                                         );\r
227                         mi.setThrustCurveMotor(tcm);\r
228                 }\r
229                 return mi;\r
230                 \r
231         }\r
232         \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
238                                 /* groupby */null,\r
239                                 /* having*/null,\r
240                                 /* orderby*/null);\r
241                 if (mCursor == null) {\r
242                         return null;\r
243                 }\r
244                 try {\r
245                         if (mCursor.getCount() == 0) {\r
246                                 return null;\r
247                         }\r
248                         mCursor.moveToFirst();\r
249                         return hydrateMotor(mCursor);\r
250                 } finally {\r
251                         mCursor.close();\r
252                 }\r
253                 \r
254         }\r
255         \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
261                                 /* groupby */null,\r
262                                 /* having*/null,\r
263                                 /* orderby*/null);\r
264                 if (mCursor == null) {\r
265                         return null;\r
266                 }\r
267                 try {\r
268                         if (mCursor.getCount() == 0) {\r
269                                 return null;\r
270                         }\r
271                         mCursor.moveToFirst();\r
272                         return hydrateMotor(mCursor);\r
273                 } finally {\r
274                         mCursor.close();\r
275                 }\r
276                 \r
277         }\r
278         \r
279 }\r