2016-05-19 14:20:52 +02:00
|
|
|
package net.foucry.pilldroid;
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by jacques on 24/04/16.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-11-08 20:43:27 +01:00
|
|
|
class DBHelper extends SQLiteOpenHelper {
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
private static final int DATABASE_VERSION = 1;
|
|
|
|
private static String DATABASE_NAME = "ordonnance.db";
|
|
|
|
|
2020-04-12 19:57:10 +02:00
|
|
|
private static final String TABLE_DRUG = "drug";
|
2016-11-08 20:43:27 +01:00
|
|
|
private static final String KEY_ID = "id";
|
|
|
|
private static final String KEY_CIS = "cis";
|
|
|
|
private static final String KEY_CIP13 = "cip13";
|
|
|
|
private static final String KEY_NAME = "nom";
|
|
|
|
private static final String KEY_ADMIN = "mode_administration";
|
|
|
|
private static final String KEY_PRES = "presentation";
|
|
|
|
private static final String KEY_STOCK = "stock";
|
|
|
|
private static final String KEY_PRISE = "prise";
|
|
|
|
private static final String KEY_SEUIL_WARN = "warning";
|
2016-05-19 07:37:28 +02:00
|
|
|
private static final String KEY_SEUIL_ALERT = "alerte";
|
|
|
|
|
|
|
|
private static DBHelper sInstance;
|
|
|
|
|
2016-08-10 21:03:28 +02:00
|
|
|
private static final String TAG = DBHelper.class.getName();
|
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
private static final String[] COLUMS = {KEY_ID, KEY_CIS,KEY_CIP13, KEY_NAME, KEY_ADMIN, KEY_PRES, KEY_STOCK, KEY_PRISE,
|
|
|
|
KEY_SEUIL_WARN, KEY_SEUIL_ALERT};
|
|
|
|
|
|
|
|
public static synchronized DBHelper getInstance(Context context) {
|
|
|
|
if (sInstance == null) {
|
|
|
|
sInstance = new DBHelper(context.getApplicationContext());
|
|
|
|
}
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
DBHelper(Context context) {
|
2016-05-19 07:37:28 +02:00
|
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
String CREATE_DRUG_TABLE = "CREATE TABLE drug ( " +
|
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
|
|
|
|
"cis TEXT, " +
|
|
|
|
"cip13 TEXT, " +
|
|
|
|
"nom TEXT, " +
|
|
|
|
"mode_administration TEXT, " +
|
|
|
|
"presentation TEXT, " +
|
|
|
|
"stock REAL, " +
|
|
|
|
"prise REAL, " +
|
|
|
|
"warning INT, " +
|
|
|
|
"alerte INT)";
|
|
|
|
|
|
|
|
db.execSQL(CREATE_DRUG_TABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
// Drop old database
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS drug");
|
|
|
|
|
|
|
|
// Create fresh book table
|
|
|
|
this.onCreate(db);
|
|
|
|
}
|
|
|
|
|
2016-11-08 20:43:27 +01:00
|
|
|
/**
|
|
|
|
* Drop current database. Debug code only
|
|
|
|
*/
|
2016-11-08 12:10:27 +01:00
|
|
|
void dropDrug() {
|
2016-05-19 07:37:28 +02:00
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
2016-08-10 21:03:28 +02:00
|
|
|
Log.d(TAG, "Drop drug table");
|
2016-05-19 07:37:28 +02:00
|
|
|
db.execSQL("DROP TABLE IF EXISTS drug");
|
|
|
|
|
|
|
|
this.onCreate(db);
|
|
|
|
}
|
|
|
|
|
2016-11-08 20:43:27 +01:00
|
|
|
/**
|
|
|
|
* Split medicament values into database record and record it to the DB
|
|
|
|
* @param medicament the medicament object to be saved
|
|
|
|
*/
|
2016-11-08 12:10:27 +01:00
|
|
|
void addDrug(Medicament medicament) {
|
2016-05-19 07:37:28 +02:00
|
|
|
// Logging
|
2016-08-10 21:03:28 +02:00
|
|
|
Log.d(TAG, medicament.toString());
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
// Get reference to writable DB
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
|
|
// Create ContentValues to add key "column"/value
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(KEY_CIS, medicament.getCis());
|
|
|
|
values.put(KEY_CIP13, medicament.getCip13());
|
|
|
|
values.put(KEY_NAME, medicament.getNom());
|
|
|
|
values.put(KEY_ADMIN, medicament.getMode_administration());
|
|
|
|
values.put(KEY_PRES, medicament.getPresentation());
|
|
|
|
values.put(KEY_STOCK, medicament.getStock());
|
|
|
|
values.put(KEY_PRISE, medicament.getPrise());
|
|
|
|
values.put(KEY_SEUIL_WARN, medicament.getWarnThreshold());
|
|
|
|
values.put(KEY_SEUIL_ALERT,medicament.getAlertThreshold());
|
|
|
|
|
|
|
|
// Calculate some medicament's fields
|
|
|
|
|
|
|
|
// Insert
|
|
|
|
db.insert(TABLE_DRUG, // table
|
|
|
|
null, // colunms list not needed
|
|
|
|
values); // key/value
|
|
|
|
|
|
|
|
// Close database
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
|
2016-11-08 20:43:27 +01:00
|
|
|
/**
|
|
|
|
* return a medicament from the DB with is id
|
|
|
|
* @param id of the medicament we looking for (not used)
|
|
|
|
* @return return the found medicament of null
|
|
|
|
*/
|
2016-05-19 07:37:28 +02:00
|
|
|
public Medicament getDrug(int id) {
|
|
|
|
// Get reference to readable DB
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
|
|
|
// Build query
|
|
|
|
Cursor cursor = db.query(TABLE_DRUG, // Which table
|
|
|
|
COLUMS, // column names
|
|
|
|
" id = ?", // selections
|
|
|
|
new String[] { String.valueOf(id) }, // selections args
|
|
|
|
null, // group by
|
|
|
|
null, // having
|
|
|
|
null, // order by
|
|
|
|
null); // limits
|
|
|
|
|
|
|
|
// if case we got result, go to the first one
|
|
|
|
Medicament medicament = new Medicament();
|
2016-11-08 12:10:27 +01:00
|
|
|
if (cursor != null) {
|
|
|
|
cursor.moveToFirst();
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
// Build medicament object
|
|
|
|
medicament.setId(Integer.parseInt(cursor.getString(0)));
|
|
|
|
medicament.setCis(cursor.getString(1));
|
|
|
|
medicament.setCip13(cursor.getString(2));
|
|
|
|
medicament.setNom(cursor.getString(3));
|
|
|
|
medicament.setMode_administration(cursor.getString(4));
|
|
|
|
medicament.setPresentation(cursor.getString(5));
|
|
|
|
medicament.setStock(Double.parseDouble(cursor.getString(6)));
|
|
|
|
medicament.setPrise(Double.parseDouble(cursor.getString(7)));
|
|
|
|
medicament.setWarnThreshold(Integer.parseInt(cursor.getString(8)));
|
|
|
|
medicament.setAlertThreshold(Integer.parseInt(cursor.getString(9)));
|
|
|
|
}
|
2016-05-19 07:37:28 +02:00
|
|
|
// Log
|
2016-08-10 21:03:28 +02:00
|
|
|
Log.d(TAG, "getDrug("+id+")" + medicament.toString());
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
if (null != cursor) cursor.close();
|
2016-05-19 07:37:28 +02:00
|
|
|
// Return medicament
|
|
|
|
|
|
|
|
return medicament;
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param cip13 drug id in French nomemclature
|
|
|
|
* @return the medicament object found in DB or null
|
|
|
|
*/
|
2016-05-19 07:37:28 +02:00
|
|
|
public Medicament getDrugByCIP13(String cip13) {
|
|
|
|
// Get reference to readable DB
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
|
|
|
// Build query
|
|
|
|
Cursor cursor = db.query(TABLE_DRUG, // Which table
|
|
|
|
COLUMS, // column names
|
|
|
|
" cip13 = ?", // selections
|
2016-11-08 12:10:27 +01:00
|
|
|
new String[]{String.valueOf(cip13)}, // selections args
|
2016-05-19 07:37:28 +02:00
|
|
|
null, // group by
|
|
|
|
null, // having
|
|
|
|
null, // order by
|
|
|
|
null); // limits
|
|
|
|
|
|
|
|
// if case we got result, go to the first one
|
2016-11-08 12:10:27 +01:00
|
|
|
Medicament medicament = new Medicament();
|
|
|
|
if (cursor != null) {
|
2016-05-19 07:37:28 +02:00
|
|
|
cursor.moveToFirst();
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
// Build medicament object
|
|
|
|
medicament.setId(Integer.parseInt(cursor.getString(0)));
|
|
|
|
medicament.setCis(cursor.getString(1));
|
|
|
|
medicament.setCip13(cursor.getString(2));
|
|
|
|
medicament.setNom(cursor.getString(3));
|
|
|
|
medicament.setMode_administration(cursor.getString(4));
|
|
|
|
medicament.setPresentation(cursor.getString(5));
|
|
|
|
medicament.setStock(Double.parseDouble(cursor.getString(6)));
|
|
|
|
medicament.setPrise(Double.parseDouble(cursor.getString(7)));
|
|
|
|
medicament.setWarnThreshold(Integer.parseInt(cursor.getString(8)));
|
|
|
|
medicament.setAlertThreshold(Integer.parseInt(cursor.getString(9)));
|
|
|
|
}
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
if (null != cursor) cursor.close();
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
Log.d(TAG, "getDrug(" + cip13 + ")" + medicament.toString());
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
return medicament;
|
|
|
|
}
|
2016-11-08 12:10:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return a List of All medicaments presents in database
|
|
|
|
*/
|
|
|
|
|
|
|
|
List<Medicament> getAllDrugs() {
|
2016-05-19 07:37:28 +02:00
|
|
|
List<Medicament> medicaments = new LinkedList<Medicament>();
|
|
|
|
|
|
|
|
// Build the query
|
|
|
|
String query = "SELECT * FROM " + TABLE_DRUG;
|
|
|
|
|
|
|
|
// Get reference to readable DB (tutorial parle de writable, mais bof... on verra)
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
Cursor cursor = db.rawQuery(query, null);
|
|
|
|
|
|
|
|
// For Each row, build a medicament and add it to the list
|
2016-11-08 12:10:27 +01:00
|
|
|
Medicament medicament;
|
2016-05-19 07:37:28 +02:00
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
do {
|
|
|
|
medicament = new Medicament();
|
|
|
|
medicament.setId(Integer.parseInt(cursor.getString(0)));
|
|
|
|
medicament.setCis(cursor.getString(1));
|
|
|
|
medicament.setCip13(cursor.getString(2));
|
|
|
|
medicament.setNom(cursor.getString(3));
|
|
|
|
medicament.setMode_administration(cursor.getString(4));
|
|
|
|
medicament.setPresentation(cursor.getString(5));
|
|
|
|
medicament.setStock(Double.parseDouble(cursor.getString(6)));
|
|
|
|
medicament.setPrise(Double.parseDouble(cursor.getString(7)));
|
|
|
|
medicament.setWarnThreshold(Integer.parseInt(cursor.getString(8)));
|
|
|
|
medicament.setAlertThreshold(Integer.parseInt(cursor.getString(9)));
|
|
|
|
|
|
|
|
// Call calcul method
|
|
|
|
medicament.setDateEndOfStock();
|
|
|
|
medicament.setDateLastUpdate();
|
|
|
|
|
|
|
|
// Add medicament to medicaments
|
|
|
|
medicaments.add(medicament);
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor.close();
|
2016-08-10 21:03:28 +02:00
|
|
|
Log.d(TAG, "getAllDrugs " + medicaments.toString());
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
return medicaments;
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param medicament object to be updated in DB
|
|
|
|
*/
|
2020-04-28 22:34:47 +02:00
|
|
|
public void updateDrug(Medicament medicament) {
|
2020-05-03 15:14:56 +02:00
|
|
|
|
2020-05-03 20:32:23 +02:00
|
|
|
Log.d(TAG, "Update Drug == " + medicament);
|
2020-05-03 15:14:56 +02:00
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
// Get reference to writable DB
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
|
|
// Create ContentValues to add columnm/value
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(KEY_CIS, medicament.getCis());
|
|
|
|
values.put(KEY_CIP13, medicament.getCip13());
|
|
|
|
values.put(KEY_NAME, medicament.getNom());
|
|
|
|
values.put(KEY_ADMIN, medicament.getMode_administration());
|
|
|
|
values.put(KEY_PRES, medicament.getPresentation());
|
|
|
|
values.put(KEY_STOCK, medicament.getStock());
|
|
|
|
|
2020-05-03 20:32:23 +02:00
|
|
|
Log.d(TAG, "values are " +values.toString());
|
2016-05-19 07:37:28 +02:00
|
|
|
// Update row
|
|
|
|
int i = db.update(TABLE_DRUG, // table
|
|
|
|
values, // column/value
|
2020-04-25 12:05:00 +02:00
|
|
|
KEY_ID+" = ?", // selections
|
2016-05-19 07:37:28 +02:00
|
|
|
new String[] {String.valueOf(medicament.getId()) } ); // selections args
|
|
|
|
|
2020-05-03 15:14:56 +02:00
|
|
|
Log.d(TAG, "Return update = " + i);
|
2016-05-19 07:37:28 +02:00
|
|
|
// Close DB
|
|
|
|
db.close();
|
|
|
|
|
2020-05-03 20:32:23 +02:00
|
|
|
// DEBUG
|
|
|
|
Medicament toto = getDrug(medicament.getId());
|
|
|
|
Log.d(TAG, "toto stock== " + toto.getStock());
|
|
|
|
|
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
/**
|
2016-11-08 20:43:27 +01:00
|
|
|
* Delete a medicament object in database
|
2016-11-08 12:10:27 +01:00
|
|
|
* @param medicament object to be delete in the DB
|
|
|
|
*/
|
2016-05-19 07:37:28 +02:00
|
|
|
public void deleteDrug(Medicament medicament) {
|
|
|
|
// Get writable database
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
|
|
// Delete record
|
|
|
|
db.delete(TABLE_DRUG, // table
|
|
|
|
KEY_ID+ " = ?", // selections
|
|
|
|
new String[] { String.valueOf(medicament.getId()) } ); // selections args
|
|
|
|
|
|
|
|
// Close DB
|
|
|
|
db.close();
|
|
|
|
|
|
|
|
// log
|
2020-05-03 20:32:23 +02:00
|
|
|
Log.d(TAG, "delete drug "+medicament);
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
/**
|
|
|
|
* Get count of all medicament present in database
|
|
|
|
* @return number of medicament in DB
|
|
|
|
*/
|
2016-11-08 20:43:27 +01:00
|
|
|
int getCount() {
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
String query = "SELECT count (*) FROM " + TABLE_DRUG;
|
|
|
|
|
|
|
|
// Get reference to readable DB (tutorial parle de writable, mais bof... on verra)
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
Cursor mCount = db.rawQuery(query, null);
|
|
|
|
mCount.moveToFirst();
|
|
|
|
int count = mCount.getInt(0);
|
|
|
|
|
|
|
|
mCount.close();
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|