Merge branch 'upstream' into debian
[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.android.util.AndroidLogWrapper;\r
5 import net.sf.openrocket.motor.Manufacturer;\r
6 import net.sf.openrocket.motor.Motor;\r
7 import net.sf.openrocket.motor.ThrustCurveMotor;\r
8 import net.sf.openrocket.util.Coordinate;\r
9 import android.content.ContentValues;\r
10 import android.database.Cursor;\r
11 import android.database.sqlite.SQLiteDatabase;\r
12 \r
13 public class MotorDao {\r
14         \r
15         private SQLiteDatabase mDb;\r
16         \r
17         private final static String DATABASE_TABLE = "motor";\r
18         private final static String DROP_TABLE = "DROP TABLE IF EXISTS " + DATABASE_TABLE;\r
19         private final static String CREATE_TABLE =\r
20                         "create table " + DATABASE_TABLE + " ( " +\r
21                                         "_id integer primary key, " +\r
22                                         "unique_name text unique, " +\r
23                                         "digest string, " +\r
24                                         "designation text, " +\r
25                                         "delays text, " +\r
26                                         "diameter number, " +\r
27                                         "tot_impulse_ns number, " +\r
28                                         "avg_thrust_n number, " +\r
29                                         "max_thrust_n number, " +\r
30                                         "burn_time_s number, " +\r
31                                         "length number," +\r
32                                         "prop_mass_g number," +\r
33                                         "tot_mass_g number," +\r
34                                         "case_info text," +\r
35                                         "manufacturer text," +\r
36                                         "type text," +\r
37                                         "impulse_class text," +\r
38                                         "thrust_data blob," +\r
39                                         "time_data blob," +\r
40                                         "cg_data blob" +\r
41                                         ");";\r
42         \r
43         MotorDao(SQLiteDatabase mDb) {\r
44                 this.mDb = mDb;\r
45         }\r
46         \r
47         static String[] create() {\r
48                 return new String[] { CREATE_TABLE };\r
49         }\r
50         \r
51         static String[] update(int oldVer, int newVer) {\r
52                 return new String[] { DROP_TABLE, CREATE_TABLE };\r
53         }\r
54         \r
55         public final static String ID = "_id";\r
56         public final static String UNIQUE_NAME = "unique_name";\r
57         public final static String DIGEST = "digest";\r
58         public final static String DESIGNATION = "designation";\r
59         public final static String DELAYS = "delays";\r
60         public final static String DIAMETER = "diameter";\r
61         public final static String TOTAL_IMPULSE = "tot_impulse_ns";\r
62         public final static String AVG_THRUST = "avg_thrust_n";\r
63         public final static String MAX_THRUST = "max_thrust_n";\r
64         public final static String BURN_TIME = "burn_time_s";\r
65         public final static String LENGTH = "length";\r
66         public final static String CASE_INFO = "case_info";\r
67         public final static String MANUFACTURER = "manufacturer";\r
68         public final static String TYPE = "type";\r
69         public final static String IMPULSE_CLASS = "impulse_class";\r
70         public final static String THRUST_DATA = "thrust_data";\r
71         public final static String TIME_DATA = "time_data";\r
72         public final static String CG_DATA = "cg_data";\r
73         \r
74         private final static String[] ALL_COLS = new String[] {\r
75                         ID,\r
76                         DIGEST,\r
77                         DESIGNATION,\r
78                         DELAYS,\r
79                         DIAMETER,\r
80                         TOTAL_IMPULSE,\r
81                         AVG_THRUST,\r
82                         MAX_THRUST,\r
83                         BURN_TIME,\r
84                         LENGTH,\r
85                         CASE_INFO,\r
86                         TYPE,\r
87                         IMPULSE_CLASS,\r
88                         MANUFACTURER,\r
89                         THRUST_DATA,\r
90                         TIME_DATA,\r
91                         CG_DATA\r
92         };\r
93         \r
94         public long insertOrUpdateMotor(ExtendedThrustCurveMotor mi) throws Exception {\r
95                 ContentValues initialValues = new ContentValues();\r
96                 final ThrustCurveMotor tcm = mi.getThrustCurveMotor();\r
97                 initialValues.put(ID, mi.getId());\r
98                 initialValues.put(UNIQUE_NAME, tcm.getManufacturer() + tcm.getDesignation());\r
99                 initialValues.put(DIGEST, tcm.getDigest());\r
100                 initialValues.put(DESIGNATION, tcm.getDesignation());\r
101                 initialValues.put(DELAYS, ConversionUtils.delaysToString(tcm.getStandardDelays()));\r
102                 initialValues.put(DIAMETER, tcm.getDiameter());\r
103                 initialValues.put(TOTAL_IMPULSE, tcm.getTotalImpulseEstimate());\r
104                 initialValues.put(AVG_THRUST, tcm.getAverageThrustEstimate());\r
105                 initialValues.put(MAX_THRUST, tcm.getMaxThrustEstimate());\r
106                 initialValues.put(BURN_TIME, tcm.getBurnTimeEstimate());\r
107                 initialValues.put(LENGTH, tcm.getLength());\r
108                 initialValues.put(CASE_INFO, mi.getCaseInfo());\r
109                 initialValues.put(TYPE, tcm.getMotorType().name());\r
110                 initialValues.put(IMPULSE_CLASS, mi.getImpulseClass());\r
111                 initialValues.put(MANUFACTURER, tcm.getManufacturer().getSimpleName());\r
112                 initialValues.put(THRUST_DATA, ConversionUtils.serializeArrayOfDouble(tcm.getThrustPoints()));\r
113                 initialValues.put(TIME_DATA, ConversionUtils.serializeArrayOfDouble(tcm.getTimePoints()));\r
114                 initialValues.put(CG_DATA, ConversionUtils.serializeArrayOfCoordinate(tcm.getCGPoints()));\r
115                 \r
116                 AndroidLogWrapper.d(MotorDao.class, "insertOrUpdate Motor");\r
117                 long rv = mDb.insertWithOnConflict(DATABASE_TABLE, null, initialValues, SQLiteDatabase.CONFLICT_REPLACE);\r
118                 return rv;\r
119         }\r
120         \r
121         /**\r
122          * Delete the motor and burn data with the given rowId\r
123          * \r
124          * @param name name of motor to delete\r
125          * @return true if deleted, false otherwise\r
126          */\r
127         public boolean deleteMotor(Long id) {\r
128                 \r
129                 boolean rv = mDb.delete(DATABASE_TABLE, ID + "=" + id, null) > 0;\r
130                 return rv;\r
131         }\r
132         \r
133         /**\r
134          * \r
135          * @param groupCol\r
136          * @param groupVal\r
137          * @return\r
138          */\r
139         public Cursor fetchAllInGroups(String groupCol, String groupVal) {\r
140                 return mDb.query(DATABASE_TABLE,\r
141                                 /* columns */ALL_COLS,\r
142                                 /* selection */groupCol + "=?",\r
143                                 /* selection args*/new String[] { groupVal },\r
144                                 /* groupby */null,\r
145                                 /* having*/null,\r
146                                 /* orderby*/DESIGNATION);\r
147                 \r
148         }\r
149         \r
150         /**\r
151          * Fetch the groups based on groupCol\r
152          * @param groupCol\r
153          * @return\r
154          */\r
155         public Cursor fetchGroups(String groupCol) {\r
156                 return mDb.query(true, DATABASE_TABLE,\r
157                                 /* columns */new String[] {\r
158                                 groupCol\r
159                                 },\r
160                                 /* selection */null,\r
161                                 /* selection args*/null,\r
162                                 /* groupby */null,\r
163                                 /* having*/null,\r
164                                 /* orderby*/null,\r
165                                 /* limit*/null);\r
166                 \r
167         }\r
168         \r
169         /**\r
170          * Return a Cursor over the list of all motors\r
171          * \r
172          * @return Cursor over all notes\r
173          */\r
174         public Cursor fetchAllMotors() {\r
175                 \r
176                 return mDb.query(DATABASE_TABLE,\r
177                                 /* columns */ALL_COLS,\r
178                                 /* selection */null,\r
179                                 /* selection args*/null,\r
180                                 /* groupby */null,\r
181                                 /* having*/null,\r
182                                 /* orderby*/null);\r
183         }\r
184         \r
185         private ExtendedThrustCurveMotor hydrateMotor(Cursor mCursor) throws Exception {\r
186                 ExtendedThrustCurveMotor mi = new ExtendedThrustCurveMotor();\r
187                 \r
188                 mi.setId(mCursor.getLong(mCursor.getColumnIndex(ID)));\r
189                 mi.setCaseInfo(mCursor.getString(mCursor.getColumnIndex(CASE_INFO)));\r
190                 mi.setImpulseClass(mCursor.getString(mCursor.getColumnIndex(IMPULSE_CLASS)));\r
191                 \r
192                 {\r
193                         String digest = mCursor.getString(mCursor.getColumnIndex(DIGEST));\r
194                         String designation = mCursor.getString(mCursor.getColumnIndex(DESIGNATION));\r
195                         String delayString = mCursor.getString(mCursor.getColumnIndex(DELAYS));\r
196                         double[] delays = ConversionUtils.stringToDelays(delayString);\r
197                         double diameter = mCursor.getDouble(mCursor.getColumnIndex(DIAMETER));\r
198                         double totImpulse = mCursor.getDouble(mCursor.getColumnIndex(TOTAL_IMPULSE));\r
199                         double avgImpulse = mCursor.getDouble(mCursor.getColumnIndex(AVG_THRUST));\r
200                         double maxThrust = mCursor.getDouble(mCursor.getColumnIndex(MAX_THRUST));\r
201                         double length = mCursor.getDouble(mCursor.getColumnIndex(LENGTH));\r
202                         Motor.Type type;\r
203                         try {\r
204                                 type = Enum.valueOf(Motor.Type.class, mCursor.getString(mCursor.getColumnIndex(TYPE)));\r
205                         } catch (IllegalArgumentException e) {\r
206                                 type = Motor.Type.UNKNOWN;\r
207                         }\r
208                         Manufacturer manufacturer = Manufacturer.getManufacturer(mCursor.getString(mCursor.getColumnIndex(MANUFACTURER)));\r
209                         double[] thrustData = ConversionUtils.deserializeArrayOfDouble(mCursor.getBlob(mCursor.getColumnIndex(THRUST_DATA)));\r
210                         double[] timeData = ConversionUtils.deserializeArrayOfDouble(mCursor.getBlob(mCursor.getColumnIndex(TIME_DATA)));\r
211                         Coordinate[] cgData = ConversionUtils.deserializeArrayOfCoordinate(mCursor.getBlob(mCursor.getColumnIndex(CG_DATA)));\r
212                         \r
213                         ThrustCurveMotor tcm = new ThrustCurveMotor(manufacturer,\r
214                                         designation,\r
215                                         "",\r
216                                         type,\r
217                                         delays,\r
218                                         diameter,\r
219                                         length,\r
220                                         timeData,\r
221                                         thrustData,\r
222                                         cgData,\r
223                                         digest\r
224                                         );\r
225                         mi.setThrustCurveMotor(tcm);\r
226                 }\r
227                 return mi;\r
228                 \r
229         }\r
230         \r
231         public ExtendedThrustCurveMotor fetchMotor(Long id) throws Exception {\r
232                 Cursor mCursor = mDb.query(DATABASE_TABLE,\r
233                                 /* columns */ALL_COLS,\r
234                                 /* selection */ID + "=" + id,\r
235                                 /* selection args*/null,\r
236                                 /* groupby */null,\r
237                                 /* having*/null,\r
238                                 /* orderby*/null);\r
239                 if (mCursor == null) {\r
240                         return null;\r
241                 }\r
242                 try {\r
243                         if (mCursor.getCount() == 0) {\r
244                                 return null;\r
245                         }\r
246                         mCursor.moveToFirst();\r
247                         return hydrateMotor(mCursor);\r
248                 } finally {\r
249                         mCursor.close();\r
250                 }\r
251                 \r
252         }\r
253         \r
254         public ExtendedThrustCurveMotor fetchMotor(String manufacturerShortName, String designation) throws Exception {\r
255                 Cursor mCursor = mDb.query(DATABASE_TABLE,\r
256                                 /* columns */ALL_COLS,\r
257                                 /* selection */MANUFACTURER + "='" + manufacturerShortName + "' and " + DESIGNATION + "='" + designation + "'",\r
258                                 /* selection args*/null,\r
259                                 /* groupby */null,\r
260                                 /* having*/null,\r
261                                 /* orderby*/null);\r
262                 if (mCursor == null) {\r
263                         return null;\r
264                 }\r
265                 try {\r
266                         if (mCursor.getCount() == 0) {\r
267                                 return null;\r
268                         }\r
269                         mCursor.moveToFirst();\r
270                         return hydrateMotor(mCursor);\r
271                 } finally {\r
272                         mCursor.close();\r
273                 }\r
274                 \r
275         }\r
276         \r
277 }\r