Pilldroid/app/src/main/java/net/foucry/pilldroid/DBHelper.java

393 lines
13 KiB
Java
Raw Normal View History

package net.foucry.pilldroid;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
2021-04-12 21:11:50 +02:00
import android.text.format.DateUtils;
import android.util.Log;
import java.util.ArrayList;
import java.util.Comparator;
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 {
private static final int DATABASE_VERSION = 1;
2021-04-12 21:11:50 +02:00
private static final String DATABASE_NAME = "prescription.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";
2021-04-12 21:11:50 +02:00
private static final String KEY_NAME = "name";
private static final String KEY_ADMIN = "administration_mode";
2016-11-08 20:43:27 +01:00
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";
private static final String KEY_THRESHOLD_ALERT = "alert";
2020-12-07 11:18:15 +01:00
private static final String KEY_LAST_UPDATE = "last_update";
2021-04-12 21:11:50 +02:00
final List<Drug> drugs = new ArrayList<>();
private static final String TAG = DBHelper.class.getName();
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-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);
}
@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, " +
"presentation TEXT, " +
"stock REAL, " +
2021-04-12 21:11:50 +02:00
"take REAL, " +
"warning INT, " +
2021-04-12 21:11:50 +02:00
"alert INT, " +
2020-12-07 11:18:15 +01:00
"last_update LONG)";
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() {
SQLiteDatabase db = this.getWritableDatabase();
Log.d(TAG, "Drop drug table");
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) {
// Logging
2021-04-12 21:11:50 +02:00
Log.d(TAG, drug.toString());
// 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());
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
// 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) {
// 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
null, // group by
2021-04-12 21:11:50 +02:00
null, // having
null, // order by
2021-04-12 21:11:50 +02:00
null); // limits
Log.d(TAG, "Cursor == " + DatabaseUtils.dumpCursorToString(cursor));
// 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();
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));
drug.setNama(cursor.getString(3));
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
}
// Log
2021-04-12 21:11:50 +02:00
Log.d(TAG, "getDrug("+id+")" + drug.toString());
assert cursor != null;
cursor.close();
db.close();
2021-04-12 21:11:50 +02:00
// Return drug
2021-04-12 21:11:50 +02:00
return drug;
}
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) {
// 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
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) {
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));
drug.setNama(cursor.getString(3));
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
}
assert cursor != null;
cursor.close();
2021-04-12 21:11:50 +02:00
Log.d(TAG, "getDrug(" + cip13 + ")" + drug.toString());
2021-04-12 21:11:50 +02:00
return drug;
}
2016-11-08 12:10:27 +01: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() {
// Build the query
String query = "SELECT * FROM " + TABLE_DRUG;
// Get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
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;
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));
drug.setNama(cursor.getString(3));
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)));
// Call calcul method
2021-04-12 21:11:50 +02:00
drug.setDateEndOfStock();
2020-12-07 11:18:15 +01:00
// Add drug to Drugs
2021-04-12 21:11:50 +02:00
drugs.add(drug);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
2021-04-12 21:11:50 +02:00
Drug currentDrug;
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);
}
}
2021-04-12 21:11:50 +02:00
drugs.sort(new Comparator<Drug>() {
@Override
2021-04-12 21:11:50 +02:00
public int compare(Drug lhs, Drug rhs) {
return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock());
}
});
2021-04-12 21:11:50 +02:00
// Move drug with prise = 0 at the end of the list
for (int position = 0 ; position < getCount() ; position++ ) {
2021-04-12 21:11:50 +02:00
currentDrug = getItem(position);
double currentTake = currentDrug.getTake();
if (currentTake == 0)
{
2021-04-12 21:11:50 +02:00
drug = drugs.remove(position);
drugs.add(drugs.size(), drug);
}
}
2021-04-12 21:11:50 +02:00
Log.d(TAG, "getAllDrugs " + drugs.toString());
2021-04-12 21:11:50 +02:00
return drugs;
}
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-04-12 21:11:50 +02:00
Log.d(TAG, "Update Drug == " + drug);
2020-05-03 15:14:56 +02:00
// Get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// Create ContentValues to add columnm/value
ContentValues values = new ContentValues();
2021-04-12 21:11:50 +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());
2021-04-12 21:11:50 +02:00
values.put(KEY_LAST_UPDATE, drug.getDateLastUpdate());
String[] selectionArgs = { String.valueOf(drug.getId()) };
2020-12-07 11:18:15 +01:00
db.update(TABLE_DRUG, // table
values, // column/value
2020-12-07 11:18:15 +01:00
KEY_ID + " = ?", // selections
selectionArgs);
// Close DB
db.close();
}
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) {
// 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
// Close DB
db.close();
// log
2021-04-12 21:11:50 +02:00
Log.d(TAG, "delete drug "+ drug);
}
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() {
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();
db.close();
return count;
}
2021-04-12 21:11:50 +02:00
public Drug getItem(int position) {
return drugs.get(position);
}
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;
}
}