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;
|
2020-09-30 17:58:07 +02:00
|
|
|
import android.database.DatabaseUtils;
|
2016-05-19 07:37:28 +02:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2021-03-20 21:32:01 +01:00
|
|
|
import java.util.ArrayList;
|
2020-08-15 22:03:19 +02:00
|
|
|
import java.util.Comparator;
|
2021-03-20 21:32:01 +01:00
|
|
|
import java.util.Iterator;
|
2016-05-19 07:37:28 +02:00
|
|
|
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;
|
2020-07-03 21:44:20 +02:00
|
|
|
private static final ThreadLocal<String> DATABASE_NAME = ThreadLocal.withInitial(() -> "ordonnance.db");
|
2016-05-19 07:37:28 +02:00
|
|
|
|
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";
|
2020-12-07 11:18:15 +01:00
|
|
|
private static final String KEY_LAST_UPDATE = "last_update";
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2021-03-20 21:32:01 +01:00
|
|
|
final List<Medicament> medicaments = new ArrayList<>();
|
2016-05-19 07:37:28 +02:00
|
|
|
|
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,
|
2020-12-07 11:18:15 +01:00
|
|
|
KEY_SEUIL_WARN, KEY_SEUIL_ALERT, KEY_LAST_UPDATE};
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
DBHelper(Context context) {
|
2020-07-03 21:44:20 +02:00
|
|
|
super(context, DATABASE_NAME.get(), null, DATABASE_VERSION);
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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, " +
|
2020-12-07 11:18:15 +01:00
|
|
|
"alerte INT, " +
|
|
|
|
"last_update LONG)";
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
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());
|
2020-12-07 11:18:15 +01:00
|
|
|
values.put(KEY_SEUIL_ALERT, medicament.getAlertThreshold());
|
|
|
|
values.put(KEY_LAST_UPDATE, medicament.getDateLastUpdate());
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2020-09-30 17:58:07 +02:00
|
|
|
Log.d(TAG, "Cursor == " + DatabaseUtils.dumpCursorToString(cursor));
|
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
// 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)));
|
2020-12-07 11:18:15 +01:00
|
|
|
medicament.setDateLastUpdate(Long.parseLong(cursor.getString(10)));
|
2016-11-08 12:10:27 +01:00
|
|
|
}
|
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
|
|
|
|
2020-09-30 17:58:07 +02:00
|
|
|
assert cursor != null;
|
|
|
|
cursor.close();
|
|
|
|
db.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)));
|
2020-12-07 11:18:15 +01:00
|
|
|
medicament.setDateLastUpdate(Long.parseLong(cursor.getString(10)));
|
2016-11-08 12:10:27 +01:00
|
|
|
}
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2020-09-30 17:58:07 +02:00
|
|
|
assert cursor != null;
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2020-08-15 22:03:19 +02:00
|
|
|
* @return a Sorted and updated by dateEndOfStock List of All medicaments presents in database
|
2016-11-08 12:10:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
List<Medicament> getAllDrugs() {
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
// Build the query
|
|
|
|
String query = "SELECT * FROM " + TABLE_DRUG;
|
|
|
|
|
2020-08-15 22:03:19 +02:00
|
|
|
// Get reference to readable DB
|
2016-05-19 07:37:28 +02:00
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
Cursor cursor = db.rawQuery(query, null);
|
|
|
|
|
2020-09-30 17:58:07 +02:00
|
|
|
Log.d(TAG, "Cursor == " + DatabaseUtils.dumpCursorToString(cursor));
|
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
// 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)));
|
2020-12-07 11:18:15 +01:00
|
|
|
medicament.setDateLastUpdate(Long.parseLong(cursor.getString(10)));
|
2016-05-19 07:37:28 +02:00
|
|
|
// Call calcul method
|
|
|
|
medicament.setDateEndOfStock();
|
2020-12-07 11:18:15 +01:00
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
// Add medicament to medicaments
|
|
|
|
medicaments.add(medicament);
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor.close();
|
2020-09-30 17:58:07 +02:00
|
|
|
db.close();
|
2020-08-15 22:03:19 +02:00
|
|
|
|
2020-09-30 17:58:07 +02:00
|
|
|
Medicament currentMedicament;
|
2020-08-15 22:03:19 +02:00
|
|
|
for (int position = 0 ; position < getCount() ; position++ ) {
|
|
|
|
currentMedicament = getItem(position);
|
2020-12-23 09:48:08 +01:00
|
|
|
|
|
|
|
// if (!DateUtils.isToday(currentMedicament.getDateLastUpdate()))
|
|
|
|
// {
|
|
|
|
currentMedicament.newStock();
|
|
|
|
updateDrug(currentMedicament);
|
|
|
|
//}
|
2020-08-15 22:03:19 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:48:08 +01:00
|
|
|
medicaments.sort(new Comparator<Medicament>() {
|
2020-08-15 22:03:19 +02:00
|
|
|
@Override
|
|
|
|
public int compare(Medicament lhs, Medicament rhs) {
|
|
|
|
return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-20 21:32:01 +01:00
|
|
|
// Move medicament with prise = 0 at the end of the list
|
|
|
|
for (int position = 0 ; position < getCount() ; position++ ) {
|
|
|
|
currentMedicament = getItem(position);
|
|
|
|
double currentPrise = currentMedicament.getPrise();
|
|
|
|
if (currentPrise == 0)
|
|
|
|
{
|
|
|
|
medicament = medicaments.remove(position);
|
|
|
|
medicaments.add(medicaments.size(), medicament);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2020-09-30 17:58:07 +02:00
|
|
|
values.put(KEY_ID, medicament.getId());
|
2016-05-19 07:37:28 +02:00
|
|
|
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-07-20 18:08:04 +02:00
|
|
|
values.put(KEY_PRISE, medicament.getPrise());
|
2020-12-07 11:18:15 +01:00
|
|
|
values.put(KEY_LAST_UPDATE, medicament.getDateLastUpdate());
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2020-09-30 17:58:07 +02:00
|
|
|
String[] selectionArgs = { String.valueOf(medicament.getId()) };
|
2020-12-07 11:18:15 +01:00
|
|
|
|
|
|
|
db.update(TABLE_DRUG, // table
|
2016-05-19 07:37:28 +02:00
|
|
|
values, // column/value
|
2020-12-07 11:18:15 +01:00
|
|
|
KEY_ID + " = ?", // selections
|
|
|
|
selectionArgs);
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
// Close DB
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
|
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();
|
2020-09-30 17:58:07 +02:00
|
|
|
db.close();
|
2016-05-19 07:37:28 +02:00
|
|
|
return count;
|
|
|
|
}
|
2020-08-15 22:03:19 +02:00
|
|
|
|
|
|
|
public Medicament getItem(int position) {
|
|
|
|
return medicaments.get(position);
|
|
|
|
}
|
|
|
|
|
2020-06-24 06:33:56 +02:00
|
|
|
boolean isMedicamentExist(String cip13) {
|
|
|
|
boolean value = false;
|
|
|
|
try {
|
|
|
|
Cursor c = this.getReadableDatabase().rawQuery("SELECT * FROM "+ TABLE_DRUG + " where cip13 = "+cip13, null);
|
|
|
|
|
|
|
|
if(c.getCount()>0)
|
|
|
|
{
|
|
|
|
value = true;
|
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
} catch(Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
|
|
|
|