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;
|
2021-04-12 21:11:50 +02:00
|
|
|
import android.text.format.DateUtils;
|
2016-05-19 07:37:28 +02:00
|
|
|
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;
|
2016-05-19 07:37:28 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by jacques on 24/04/16.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
|
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;
|
2021-04-12 21:11:50 +02:00
|
|
|
private static final String DATABASE_NAME = "prescription.db";
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2021-06-18 20:10:07 +02:00
|
|
|
private static final String TABLE_DRUG = "drug";
|
|
|
|
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 = "name";
|
|
|
|
private static final String KEY_ADMIN = "administration_mode";
|
|
|
|
private static final String KEY_PRES = "presentation";
|
|
|
|
private static final String KEY_STOCK = "stock";
|
|
|
|
private static final String KEY_TAKE = "take";
|
|
|
|
private static final String KEY_THRESHOLD_WARN = "warning";
|
2021-04-18 18:39:55 +02:00
|
|
|
private static final String KEY_THRESHOLD_ALERT = "alert";
|
2021-06-18 20:10:07 +02:00
|
|
|
private static final String KEY_LAST_UPDATE = "last_update";
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
final List<Drug> drugs = 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();
|
|
|
|
|
2021-04-18 18:39:55 +02:00
|
|
|
private static final String[] COLUMS = {KEY_ID, KEY_CIS,KEY_CIP13, KEY_NAME, KEY_ADMIN, KEY_PRES, KEY_STOCK, KEY_TAKE,
|
|
|
|
KEY_THRESHOLD_WARN, KEY_THRESHOLD_ALERT, KEY_LAST_UPDATE};
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
DBHelper(Context context) {
|
2021-04-12 21:11:50 +02:00
|
|
|
super(context, DATABASE_NAME, 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, " +
|
2021-04-12 21:11:50 +02:00
|
|
|
"name TEXT, " +
|
|
|
|
"administration_mode TEXT, " +
|
2016-05-19 07:37:28 +02:00
|
|
|
"presentation TEXT, " +
|
|
|
|
"stock REAL, " +
|
2021-04-12 21:11:50 +02:00
|
|
|
"take REAL, " +
|
2016-05-19 07:37:28 +02:00
|
|
|
"warning INT, " +
|
2021-04-12 21:11:50 +02:00
|
|
|
"alert INT, " +
|
2020-12-07 11:18:15 +01:00
|
|
|
"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
|
|
|
/**
|
2021-04-12 21:11:50 +02:00
|
|
|
* Split drug values into database record and record it to the DB
|
|
|
|
* @param drug the drug object to be saved
|
2016-11-08 20:43:27 +01:00
|
|
|
*/
|
2021-04-12 21:11:50 +02:00
|
|
|
void addDrug(Drug drug) {
|
2016-05-19 07:37:28 +02:00
|
|
|
// Logging
|
2021-04-12 21:11:50 +02:00
|
|
|
Log.d(TAG, drug.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();
|
2021-04-12 21:11:50 +02:00
|
|
|
values.put(KEY_CIS, drug.getCis());
|
|
|
|
values.put(KEY_CIP13, drug.getCip13());
|
|
|
|
values.put(KEY_NAME, drug.getName());
|
|
|
|
values.put(KEY_ADMIN, drug.getAdministration_mode());
|
|
|
|
values.put(KEY_PRES, drug.getPresentation());
|
|
|
|
values.put(KEY_STOCK, drug.getStock());
|
2021-04-18 18:39:55 +02:00
|
|
|
values.put(KEY_TAKE, drug.getTake());
|
|
|
|
values.put(KEY_THRESHOLD_WARN, drug.getWarnThreshold());
|
|
|
|
values.put(KEY_THRESHOLD_ALERT, drug.getAlertThreshold());
|
2021-04-12 21:11:50 +02:00
|
|
|
values.put(KEY_LAST_UPDATE, drug.getDateLastUpdate());
|
|
|
|
|
|
|
|
// Calculate some drug's fields
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
/**
|
2021-04-12 21:11:50 +02:00
|
|
|
* return a drug from the DB with is id
|
|
|
|
* @param id of the drug we looking for (not used)
|
|
|
|
* @return return the found drug of null
|
2016-11-08 20:43:27 +01:00
|
|
|
*/
|
2021-04-12 21:11:50 +02:00
|
|
|
public Drug getDrug(int id) {
|
2016-05-19 07:37:28 +02:00
|
|
|
// Get reference to readable DB
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
|
|
|
// Build query
|
2021-04-12 21:11:50 +02:00
|
|
|
Cursor cursor = db.query(TABLE_DRUG, // Which table
|
|
|
|
COLUMS, // column names
|
|
|
|
" id = ?", // selections
|
|
|
|
new String[] { String.valueOf(id) }, // selections args
|
2016-05-19 07:37:28 +02:00
|
|
|
null, // group by
|
2021-04-12 21:11:50 +02:00
|
|
|
null, // having
|
2016-05-19 07:37:28 +02:00
|
|
|
null, // order by
|
2021-04-12 21:11:50 +02:00
|
|
|
null); // limits
|
2016-05-19 07:37:28 +02:00
|
|
|
|
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
|
2021-04-12 21:11:50 +02:00
|
|
|
Drug drug = new Drug();
|
2016-11-08 12:10:27 +01:00
|
|
|
if (cursor != null) {
|
|
|
|
cursor.moveToFirst();
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
// Build drug object
|
|
|
|
drug.setId(Integer.parseInt(cursor.getString(0)));
|
|
|
|
drug.setCis(cursor.getString(1));
|
|
|
|
drug.setCip13(cursor.getString(2));
|
2021-06-26 17:51:05 +02:00
|
|
|
drug.setName(cursor.getString(3));
|
2021-04-12 21:11:50 +02:00
|
|
|
drug.setAdministration_mode(cursor.getString(4));
|
|
|
|
drug.setPresentation(cursor.getString(5));
|
|
|
|
drug.setStock(Double.parseDouble(cursor.getString(6)));
|
|
|
|
drug.setTake(Double.parseDouble(cursor.getString(7)));
|
|
|
|
drug.setWarnThreshold(Integer.parseInt(cursor.getString(8)));
|
|
|
|
drug.setAlertThreshold(Integer.parseInt(cursor.getString(9)));
|
|
|
|
drug.setDateLastUpdate(Long.parseLong(cursor.getString(10)));
|
2016-11-08 12:10:27 +01:00
|
|
|
}
|
2016-05-19 07:37:28 +02:00
|
|
|
// Log
|
2021-04-12 21:11:50 +02:00
|
|
|
Log.d(TAG, "getDrug("+id+")" + drug.toString());
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2020-09-30 17:58:07 +02:00
|
|
|
assert cursor != null;
|
|
|
|
cursor.close();
|
|
|
|
db.close();
|
2021-04-12 21:11:50 +02:00
|
|
|
// Return drug
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
return drug;
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param cip13 drug id in French nomemclature
|
2021-04-12 21:11:50 +02:00
|
|
|
* @return the drug object found in DB or null
|
2016-11-08 12:10:27 +01:00
|
|
|
*/
|
2021-04-12 21:11:50 +02:00
|
|
|
public Drug getDrugByCIP13(String cip13) {
|
2016-05-19 07:37:28 +02:00
|
|
|
// 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
|
2021-04-12 21:11:50 +02:00
|
|
|
Drug drug = new Drug();
|
2016-11-08 12:10:27 +01:00
|
|
|
if (cursor != null) {
|
2016-05-19 07:37:28 +02:00
|
|
|
cursor.moveToFirst();
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
// Build drug object
|
|
|
|
drug.setId(Integer.parseInt(cursor.getString(0)));
|
|
|
|
drug.setCis(cursor.getString(1));
|
|
|
|
drug.setCip13(cursor.getString(2));
|
2021-06-26 17:51:05 +02:00
|
|
|
drug.setName(cursor.getString(3));
|
2021-04-12 21:11:50 +02:00
|
|
|
drug.setAdministration_mode(cursor.getString(4));
|
|
|
|
drug.setPresentation(cursor.getString(5));
|
|
|
|
drug.setStock(Double.parseDouble(cursor.getString(6)));
|
|
|
|
drug.setTake(Double.parseDouble(cursor.getString(7)));
|
|
|
|
drug.setWarnThreshold(Integer.parseInt(cursor.getString(8)));
|
|
|
|
drug.setAlertThreshold(Integer.parseInt(cursor.getString(9)));
|
|
|
|
drug.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
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
Log.d(TAG, "getDrug(" + cip13 + ")" + drug.toString());
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
return drug;
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
2016-11-08 12:10:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2021-04-18 18:39:55 +02:00
|
|
|
* @return a Sorted and updated by dateEndOfStock List of All drugs presents in database
|
2016-11-08 12:10:27 +01:00
|
|
|
*/
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
List<Drug> 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));
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
// For Each row, build a drug and add it to the list
|
|
|
|
Drug drug;
|
2016-05-19 07:37:28 +02:00
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
do {
|
2021-04-12 21:11:50 +02:00
|
|
|
drug = new Drug();
|
|
|
|
drug.setId(Integer.parseInt(cursor.getString(0)));
|
|
|
|
drug.setCis(cursor.getString(1));
|
|
|
|
drug.setCip13(cursor.getString(2));
|
2021-06-26 17:51:05 +02:00
|
|
|
drug.setName(cursor.getString(3));
|
2021-04-12 21:11:50 +02:00
|
|
|
drug.setAdministration_mode(cursor.getString(4));
|
|
|
|
drug.setPresentation(cursor.getString(5));
|
|
|
|
drug.setStock(Double.parseDouble(cursor.getString(6)));
|
|
|
|
drug.setTake(Double.parseDouble(cursor.getString(7)));
|
|
|
|
drug.setWarnThreshold(Integer.parseInt(cursor.getString(8)));
|
|
|
|
drug.setAlertThreshold(Integer.parseInt(cursor.getString(9)));
|
|
|
|
drug.setDateLastUpdate(Long.parseLong(cursor.getString(10)));
|
2016-05-19 07:37:28 +02:00
|
|
|
// Call calcul method
|
2021-04-12 21:11:50 +02:00
|
|
|
drug.setDateEndOfStock();
|
2020-12-07 11:18:15 +01:00
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2021-04-18 18:39:55 +02:00
|
|
|
// Add drug to Drugs
|
2021-04-12 21:11:50 +02:00
|
|
|
drugs.add(drug);
|
2016-05-19 07:37:28 +02:00
|
|
|
} while (cursor.moveToNext());
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor.close();
|
2020-09-30 17:58:07 +02:00
|
|
|
db.close();
|
2020-08-15 22:03:19 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
Drug currentDrug;
|
2020-08-15 22:03:19 +02:00
|
|
|
for (int position = 0 ; position < getCount() ; position++ ) {
|
2021-04-12 21:11:50 +02:00
|
|
|
currentDrug = getItem(position);
|
2020-12-23 09:48:08 +01:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
if (!DateUtils.isToday(currentDrug.getDateLastUpdate()))
|
|
|
|
{
|
|
|
|
currentDrug.newStock();
|
|
|
|
updateDrug(currentDrug);
|
|
|
|
}
|
2020-08-15 22:03:19 +02:00
|
|
|
}
|
|
|
|
|
2021-06-18 20:10:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Before sort == " + drugs.toString());
|
|
|
|
|
|
|
|
/*drugs.sort(Comparator.comparing(Drug::getDateEndOfStock)
|
|
|
|
.thenComparing(Drug::getStock));*/
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
drugs.sort(new Comparator<Drug>() {
|
2020-08-15 22:03:19 +02:00
|
|
|
@Override
|
2021-04-12 21:11:50 +02:00
|
|
|
public int compare(Drug lhs, Drug rhs) {
|
2021-06-18 20:10:07 +02:00
|
|
|
if (lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()) != 0)
|
|
|
|
return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock());
|
|
|
|
else
|
|
|
|
return (int) (lhs.getStock() - rhs.getStock());
|
2020-08-15 22:03:19 +02:00
|
|
|
}
|
|
|
|
});
|
2021-06-18 20:10:07 +02:00
|
|
|
Log.d(TAG, "After sort " + drugs.toString());
|
2020-08-15 22:03:19 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
// Move drug with prise = 0 at the end of the list
|
2021-06-26 17:51:05 +02:00
|
|
|
// todo: If some drug moved, must redo all the loop
|
2021-07-04 22:20:40 +02:00
|
|
|
int position = 0 ;
|
|
|
|
for ( int nbOps = 0; nbOps < getCount() ; nbOps ++ ) {
|
2021-04-12 21:11:50 +02:00
|
|
|
currentDrug = getItem(position);
|
|
|
|
double currentTake = currentDrug.getTake();
|
|
|
|
if (currentTake == 0)
|
2021-03-20 21:32:01 +01:00
|
|
|
{
|
2021-04-12 21:11:50 +02:00
|
|
|
drug = drugs.remove(position);
|
2021-06-26 17:51:05 +02:00
|
|
|
drugs.add(drug);
|
2021-07-04 22:20:40 +02:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
position++;
|
2021-03-20 21:32:01 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 21:11:50 +02:00
|
|
|
return drugs;
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
/**
|
|
|
|
*
|
2021-04-12 21:11:50 +02:00
|
|
|
* @param drug object to be updated in DB
|
2016-11-08 12:10:27 +01:00
|
|
|
*/
|
2021-04-12 21:11:50 +02:00
|
|
|
public void updateDrug(Drug drug) {
|
2020-05-03 15:14:56 +02:00
|
|
|
|
2021-06-18 20:10:07 +02:00
|
|
|
Log.d(TAG, "Update Drug == " + drug.toString());
|
2021-12-28 15:06:11 +01:00
|
|
|
Log.d(TAG, "drug last_update == " + UtilDate.convertDate(drug.getDateLastUpdate()));
|
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();
|
2021-06-18 20:10:07 +02:00
|
|
|
values.put(KEY_ID, drug.getId());
|
|
|
|
values.put(KEY_CIS, drug.getCis());
|
|
|
|
values.put(KEY_CIP13, drug.getCip13());
|
|
|
|
values.put(KEY_NAME, drug.getName());
|
|
|
|
values.put(KEY_ADMIN, drug.getAdministration_mode());
|
|
|
|
values.put(KEY_PRES, drug.getPresentation());
|
|
|
|
values.put(KEY_STOCK, drug.getStock());
|
|
|
|
values.put(KEY_TAKE, drug.getTake());
|
|
|
|
values.put(KEY_THRESHOLD_WARN, drug.getWarnThreshold());
|
2021-05-16 19:54:50 +02:00
|
|
|
values.put(KEY_THRESHOLD_ALERT, drug.getAlertThreshold());
|
2021-06-18 20:10:07 +02:00
|
|
|
values.put(KEY_LAST_UPDATE, drug.getDateLastUpdate());
|
2021-04-12 21:11:50 +02:00
|
|
|
|
|
|
|
String[] selectionArgs = { String.valueOf(drug.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();
|
2021-06-18 20:10:07 +02:00
|
|
|
Log.d(TAG, "values == " + values.toString());
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
/**
|
2021-04-12 21:11:50 +02:00
|
|
|
* Delete a drug object in database
|
|
|
|
* @param drug object to be delete in the DB
|
2016-11-08 12:10:27 +01:00
|
|
|
*/
|
2021-04-12 21:11:50 +02:00
|
|
|
public void deleteDrug(Drug drug) {
|
2016-05-19 07:37:28 +02:00
|
|
|
// Get writable database
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
|
|
// Delete record
|
|
|
|
db.delete(TABLE_DRUG, // table
|
|
|
|
KEY_ID+ " = ?", // selections
|
2021-04-12 21:11:50 +02:00
|
|
|
new String[] { String.valueOf(drug.getId()) } ); // selections args
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
// Close DB
|
|
|
|
db.close();
|
|
|
|
|
|
|
|
// log
|
2021-04-12 21:11:50 +02:00
|
|
|
Log.d(TAG, "delete drug "+ drug);
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:10:27 +01:00
|
|
|
/**
|
2021-04-12 21:11:50 +02:00
|
|
|
* Get count of all drug present in database
|
|
|
|
* @return number of drug in DB
|
2016-11-08 12:10:27 +01:00
|
|
|
*/
|
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
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
public Drug getItem(int position) {
|
|
|
|
return drugs.get(position);
|
2020-08-15 22:03:19 +02:00
|
|
|
}
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
boolean isDrugExist(String cip13) {
|
2020-06-24 06:33:56 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|