2016-05-25 17:25:20 +02:00
|
|
|
package net.foucry.pilldroid;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2016-08-29 22:01:03 +02:00
|
|
|
import java.io.File;
|
2016-05-25 17:25:20 +02:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
2020-06-18 20:52:37 +02:00
|
|
|
|
2016-05-25 17:25:20 +02:00
|
|
|
/**
|
|
|
|
* Created by jfoucry on 5/25/16.
|
|
|
|
*/
|
2021-04-12 21:11:50 +02:00
|
|
|
class DBDrugs extends SQLiteOpenHelper {
|
2016-05-25 17:25:20 +02:00
|
|
|
|
|
|
|
private static final int DATABASE_VERSION = 1;
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
private static final String dbName = "drugs.db";
|
2016-05-25 17:25:20 +02:00
|
|
|
private final Context myContext;
|
2020-12-07 11:19:24 +01:00
|
|
|
private final SQLiteDatabase myDataBase = null;
|
2016-05-25 17:25:20 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
private static final String TABLE_NAME = "drugs";
|
|
|
|
private static final String DRUG_CIS = "cis";
|
|
|
|
private static final String DRUG_CIP13 = "cip13";
|
|
|
|
private static final String DRUG_ADMIN = "administration_mode";
|
|
|
|
private static final String DRUG_NAME = "name";
|
|
|
|
private static final String DRUG_PRES = "presentation";
|
2016-05-25 17:25:20 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
private static final String[] COLUMNS_NAMES = {DRUG_CIS, DRUG_CIP13, DRUG_ADMIN, DRUG_NAME, DRUG_PRES};
|
2016-05-25 17:25:20 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
private static final String TAG = DBDrugs.class.getName();
|
2016-08-10 21:03:28 +02:00
|
|
|
|
2020-06-18 20:52:37 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
DBDrugs(Context context) {
|
2016-08-29 22:01:03 +02:00
|
|
|
super(context, dbName, null, DATABASE_VERSION);
|
2016-05-25 17:25:20 +02:00
|
|
|
this.myContext = context;
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
public boolean isDBFileExist(File database)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
myContext.getDatabasePath(String.valueOf(database));
|
|
|
|
} catch (final Exception exception) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
/* if (myContext.getDatabasePath(String.valueOf(database)) != null)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;*/
|
|
|
|
}
|
|
|
|
|
2016-08-29 22:01:03 +02:00
|
|
|
@Override
|
|
|
|
public synchronized SQLiteDatabase getWritableDatabase() {
|
2020-06-18 20:52:37 +02:00
|
|
|
|
|
|
|
File dbFile = myContext.getDatabasePath(dbName);
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
if (!isDBFileExist(dbFile)) {
|
2020-06-18 20:52:37 +02:00
|
|
|
copyDatabase(dbFile.getPath());
|
2016-05-25 17:25:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:36:34 +02:00
|
|
|
return super.getWritableDatabase();
|
2016-08-29 22:01:03 +02:00
|
|
|
}
|
2016-05-25 17:25:20 +02:00
|
|
|
|
2016-08-29 22:01:03 +02:00
|
|
|
@Override
|
2020-07-07 21:36:34 +02:00
|
|
|
public synchronized SQLiteDatabase getReadableDatabase() {
|
2020-06-18 20:52:37 +02:00
|
|
|
File dbFile = myContext.getDatabasePath(dbName);
|
|
|
|
|
2021-03-17 13:33:23 +01:00
|
|
|
PrefManager prefManager = new PrefManager(myContext);
|
|
|
|
int oldVersion = prefManager.getDatabaseVersion();
|
|
|
|
|
|
|
|
if (oldVersion == DATABASE_VERSION) return super.getReadableDatabase();
|
2020-06-18 20:52:37 +02:00
|
|
|
copyDatabase(dbFile.getPath());
|
2016-08-29 22:01:03 +02:00
|
|
|
return super.getReadableDatabase();
|
2016-05-25 17:25:20 +02:00
|
|
|
}
|
|
|
|
|
2016-08-29 22:01:03 +02:00
|
|
|
@Override
|
2020-07-07 21:36:34 +02:00
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
}
|
2016-05-25 17:25:20 +02:00
|
|
|
|
2016-08-29 22:01:03 +02:00
|
|
|
@Override
|
2020-07-07 21:36:34 +02:00
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
}
|
2016-05-25 17:25:20 +02:00
|
|
|
|
2016-08-29 22:01:03 +02:00
|
|
|
private void copyDatabase(String dbPath) {
|
2020-06-18 20:52:37 +02:00
|
|
|
Log.d(TAG, "try to copy database");
|
2016-08-29 22:01:03 +02:00
|
|
|
try {
|
2016-08-30 23:54:23 +02:00
|
|
|
InputStream assetDB = myContext.getAssets().open(dbName);
|
2016-08-29 22:01:03 +02:00
|
|
|
OutputStream appDB = new FileOutputStream(dbPath, false);
|
2016-05-25 17:25:20 +02:00
|
|
|
|
2016-08-29 22:01:03 +02:00
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
int length;
|
2016-08-30 23:54:23 +02:00
|
|
|
while ((length = assetDB.read(buffer)) > 0) {
|
2020-07-07 21:36:34 +02:00
|
|
|
appDB.write(buffer, 0, length);
|
2016-08-29 22:01:03 +02:00
|
|
|
}
|
2016-05-25 17:25:20 +02:00
|
|
|
|
2016-08-29 22:01:03 +02:00
|
|
|
appDB.flush();
|
|
|
|
appDB.close();
|
2016-08-30 23:54:23 +02:00
|
|
|
assetDB.close();
|
2016-08-29 22:01:03 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2016-05-25 17:25:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void close() {
|
|
|
|
if (myDataBase != null) {
|
|
|
|
myDataBase.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
/**
|
2020-09-30 17:58:32 +02:00
|
|
|
* Lookup in the DB for a record corresponding to cip13
|
2020-07-07 21:36:34 +02:00
|
|
|
*
|
2016-11-08 12:10:27 +01:00
|
|
|
* @param cip13 string representing the object we're looking for
|
2021-04-12 21:11:50 +02:00
|
|
|
* @return return a drug object
|
2016-11-08 12:10:27 +01:00
|
|
|
*/
|
2021-04-12 21:11:50 +02:00
|
|
|
Drug getDrugByCIP13(String cip13) {
|
2020-06-18 20:52:37 +02:00
|
|
|
Log.e(TAG, "CIP13 - " + cip13);
|
2016-05-25 17:25:20 +02:00
|
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
|
|
|
// Build query
|
2020-07-18 20:15:48 +02:00
|
|
|
Cursor cursor = db.query(TABLE_NAME, // Which table
|
2016-05-25 17:25:20 +02:00
|
|
|
COLUMNS_NAMES, // column names
|
2020-07-18 20:15:48 +02:00
|
|
|
" cip13 =?", // selections
|
2016-11-08 12:10:27 +01:00
|
|
|
new String[]{cip13}, // selections args
|
2020-07-18 20:15:48 +02:00
|
|
|
null, // group by
|
|
|
|
null, // having
|
|
|
|
null, // order by
|
|
|
|
null); // limits
|
|
|
|
//Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " where cip13 = " + cip13, null);
|
2020-07-07 21:36:34 +02:00
|
|
|
if (cursor.getCount() != 0) {
|
|
|
|
|
2020-07-18 20:15:48 +02:00
|
|
|
cursor.moveToFirst();
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
// Build drug object
|
|
|
|
Drug drug = new Drug();
|
|
|
|
// drug.setId(Integer.parseInt(cursor.getString(0)));
|
|
|
|
drug.setCis(cursor.getString(0));
|
|
|
|
drug.setCip13(cursor.getString(1));
|
|
|
|
drug.setAdministration_mode(cursor.getString(2));
|
|
|
|
drug.setNama(cursor.getString(3));
|
|
|
|
drug.setPresentation(cursor.getString(4));
|
2020-07-07 21:36:34 +02:00
|
|
|
|
|
|
|
// Set default values
|
2021-04-12 21:11:50 +02:00
|
|
|
drug.setStock(0);
|
|
|
|
drug.setTake(0);
|
|
|
|
drug.setWarnThreshold(14);
|
|
|
|
drug.setAlertThreshold(7);
|
2020-07-07 21:36:34 +02:00
|
|
|
|
|
|
|
// Log
|
2021-04-12 21:11:50 +02:00
|
|
|
Log.d(TAG, "getDrug(" + cip13 + ")" + drug.toString());
|
2020-07-07 21:36:34 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
// Return drug
|
2020-07-07 21:36:34 +02:00
|
|
|
|
|
|
|
cursor.close();
|
2021-04-12 21:11:50 +02:00
|
|
|
return drug;
|
2020-07-07 21:36:34 +02:00
|
|
|
} else
|
|
|
|
return null;
|
2016-05-25 17:25:20 +02:00
|
|
|
}
|
|
|
|
}
|