Correction d'un bug dans la copie de la base de médicaments

This commit is contained in:
Jacques Foucry 2016-08-29 22:01:03 +02:00
parent 943aeb7307
commit 11d54c36d2

View file

@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log; import android.util.Log;
import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -19,16 +20,16 @@ public class DBMedoc extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1; private static final int DATABASE_VERSION = 1;
private static String DATABASE_PATH = "/data/data/net.foucry.pilldroid/databases/"; File dbFile;
private static String DATABASE_NAME = "medicaments.db"; private final static String DB_PATH = "/data/data/net.foucry.pilldroid/databases/";
private static String dbName = "medicaments.db";
private SQLiteDatabase myDataBase; private SQLiteDatabase myDataBase;
private final Context myContext; Context myContext;
private static final String TABLE_NAME = "medicaments"; private static final String TABLE_NAME = "medicaments";
// private static final String MEDOC_ID = "id";
private static final String MEDOC_CIS = "cis"; private static final String MEDOC_CIS = "cis";
private static final String MEDOC_CIP13 = "cip13"; private static final String MEDOC_CIP13 = "cip13";
// private static final String MEDOC_CIP7 = "cip7";
private static final String MEDOC_ADMIN = "mode_administration"; private static final String MEDOC_ADMIN = "mode_administration";
private static final String MEDOC_NOM = "nom"; private static final String MEDOC_NOM = "nom";
private static final String MEDOC_PRES = "presentation"; private static final String MEDOC_PRES = "presentation";
@ -36,78 +37,57 @@ public class DBMedoc extends SQLiteOpenHelper{
private static final String[] COLUMNS_NAMES = {MEDOC_CIS, MEDOC_CIP13, MEDOC_ADMIN, MEDOC_NOM, MEDOC_PRES}; private static final String[] COLUMNS_NAMES = {MEDOC_CIS, MEDOC_CIP13, MEDOC_ADMIN, MEDOC_NOM, MEDOC_PRES};
public DBMedoc(Context context) { public DBMedoc(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); super(context, dbName, null, DATABASE_VERSION);
this.myContext = context; this.myContext = context;
}
@Override
public synchronized SQLiteDatabase getWritableDatabase() {
if (!dbFile.exists()) {
SQLiteDatabase db = super.getWritableDatabase();
copyDatabase(db.getPath());
}
return super.getWritableDatabase();
}
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
if (!dbFile.exists()) {
SQLiteDatabase db = super.getReadableDatabase();
copyDatabase(db.getPath());
}
return super.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
private void copyDatabase(String dbPath) {
try { try {
createDatabase(); InputStream assestDB = myContext.getAssets().open(dbName);
OutputStream appDB = new FileOutputStream(dbPath, false);
byte[] buffer = new byte[1024];
int length;
while ((length = assestDB.read(buffer)) > 0) {
appDB.write(buffer,0, length);
}
appDB.flush();
appDB.close();
assestDB.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void createDatabase() throws IOException {
Log.e(MedicamentListActivity.Constants.TAG, "createDatabase called");
boolean dbExist = checkDatabase();
if (dbExist) {
// Nothing to do, DB already exist
} else {
this.getDatabaseName();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error coping Database");
}
}
}
private boolean checkDatabase() {
if (BuildConfig.DEBUG) {
Log.e(MedicamentListActivity.Constants.TAG, "checkDatabase called");
}
SQLiteDatabase checkDB = null;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does not exists
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDatabase() throws IOException {
Log.e(MedicamentListActivity.Constants.TAG, "copyDatabase called");
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer,0,length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDatabase() throws SQLiteException { public void openDatabase() throws SQLiteException {
Log.e(MedicamentListActivity.Constants.TAG, "openDatabase called"); Log.e(MedicamentListActivity.Constants.TAG, "openDatabase called");
String myPath = DATABASE_PATH + DATABASE_NAME; String myPath = DB_PATH + dbName;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} }
@ -118,17 +98,8 @@ public class DBMedoc extends SQLiteOpenHelper{
myDataBase.close(); myDataBase.close();
} }
} }
@Override
public void onCreate(SQLiteDatabase db) {
} // private DBMedoc dbMedoc;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private DBMedoc dbMedoc;
public Medicament getMedocByCIP13(String cip13) { public Medicament getMedocByCIP13(String cip13) {
Log.e(MedicamentListActivity.Constants.TAG, "getNedocByCIP13 - " + cip13); Log.e(MedicamentListActivity.Constants.TAG, "getNedocByCIP13 - " + cip13);
@ -151,6 +122,7 @@ public class DBMedoc extends SQLiteOpenHelper{
// Build medicament object // Build medicament object
Medicament medicament = new Medicament(); Medicament medicament = new Medicament();
// medicament.setId(Integer.parseInt(cursor.getString(0))); // medicament.setId(Integer.parseInt(cursor.getString(0)));
assert cursor != null;
medicament.setCis(cursor.getString(0)); medicament.setCis(cursor.getString(0));
medicament.setCip13(cursor.getString(1)); medicament.setCip13(cursor.getString(1));
medicament.setMode_administration(cursor.getString(2)); medicament.setMode_administration(cursor.getString(2));
@ -172,6 +144,7 @@ public class DBMedoc extends SQLiteOpenHelper{
// Return medicament // Return medicament
cursor.close();
return medicament; return medicament;
} }
} }