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