Merge branch 'bugfix/ScanCip7' into develop

This commit is contained in:
jacques 2021-12-30 11:22:57 +01:00
commit 5d41ab8807
4 changed files with 105 additions and 10 deletions

View file

@ -2,6 +2,7 @@ package net.foucry.pilldroid;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
@ -27,11 +28,12 @@ class DBDrugs extends SQLiteOpenHelper {
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_CIP7 = "cip7";
private static final String DRUG_ADMIN = "administration_mode";
private static final String DRUG_NAME = "name";
private static final String DRUG_PRES = "presentation";
private static final String[] COLUMNS_NAMES = {DRUG_CIS, DRUG_CIP13, DRUG_ADMIN, DRUG_NAME, DRUG_PRES};
private static final String[] COLUMNS_NAMES = {DRUG_CIS, DRUG_CIP13, DRUG_CIP7, DRUG_ADMIN, DRUG_NAME, DRUG_PRES};
private static final String TAG = DBDrugs.class.getName();
@ -141,9 +143,9 @@ class DBDrugs extends SQLiteOpenHelper {
// drug.setId(Integer.parseInt(cursor.getString(0)));
drug.setCis(cursor.getString(0));
drug.setCip13(cursor.getString(1));
drug.setAdministration_mode(cursor.getString(2));
drug.setName(cursor.getString(3));
drug.setPresentation(cursor.getString(4));
drug.setAdministration_mode(cursor.getString(3));
drug.setName(cursor.getString(4));
drug.setPresentation(cursor.getString(5));
// Set default values
drug.setStock(0);
@ -161,4 +163,72 @@ class DBDrugs extends SQLiteOpenHelper {
} else
return null;
}
String getCIP13FromCIP7(String cip7) {
String cip13 = null;
try {
Cursor c = this.getReadableDatabase().rawQuery("SELECT cip13 FROM "+ TABLE_NAME + " where cip7 = "+cip7, null);
Log.d(TAG, "Cursor == " + DatabaseUtils.dumpCursorToString(c));
c.moveToFirst();
if(c.getCount()>0)
{
cip13 = c.getString(0);
}
c.close();
} catch(Exception e)
{
e.printStackTrace();
}
return cip13;
}
Drug getDrugByCIP7(String cip7) {
Log.d(TAG, "CIP7 - " + cip7);
SQLiteDatabase db = this.getReadableDatabase();
// Build query
Cursor cursor = db.query(TABLE_NAME, // Which table
COLUMNS_NAMES, // column names
" cip7 =?", // selections
new String[]{cip7}, // selections args
null, // group by
null, // having
null, // order by
null); // limits
//Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " where cip13 = " + cip13, null);
if (cursor.getCount() != 0) {
cursor.moveToFirst();
// 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(3));
drug.setName(cursor.getString(4));
drug.setPresentation(cursor.getString(5));
// Set default values
drug.setStock(0);
drug.setTake(0);
drug.setWarnThreshold(14);
drug.setAlertThreshold(7);
// Log
Log.d(TAG, "getDrug(" + cip7 + ")" + drug.toString());
// Return drug
cursor.close();
return drug;
} else
return null;
}
}

View file

@ -274,10 +274,20 @@ public class DrugListActivity extends AppCompatActivity {
Log.d(TAG, "formatName = " + result.getFormatName());
if (result.getFormatName().equals("CODE_128") || (result.getFormatName().equals("EAN_13"))) { //CODE_128 || EAN 13
cip13 = result.getContents();
} else {
cip13 = result.getContents().substring(4, 17);
switch (result.getFormatName()) {
case "CODE_128":
case "EAN_13": //CODE_128 || EAN 13
cip13 = result.getContents();
break;
case "CODE_39":
cip13 = dbDrug.getCIP13FromCIP7(result.getContents());
break;
case "DATA_MATRIX":
cip13 = result.getContents().substring(4, 17);
break;
default:
scanNotOK();
return;
}
// Get Drug from database
@ -362,6 +372,21 @@ public class DrugListActivity extends AppCompatActivity {
dlg.show();
}
/**
* Tell user that the barre code cannot be interpreted
*/
private void scanNotOK()
{
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(getString(R.string.app_name));
dlg.setMessage(R.string.notInterpreted);
dlg.setPositiveButton("OK", (dialog, which) -> {
// Nothing to do just dismiss dialog
});
dlg.show();
}
/**
* Add New drug to the user database
* @param aDrug Drug - drug to be added
@ -378,7 +403,6 @@ public class DrugListActivity extends AppCompatActivity {
startActivityForResult(intent, CUSTOMIZED_REQUEST_CODE);
overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
}
/**
* setupRecyclerView (list of drugs
* @param recyclerView RecyclerView
@ -389,7 +413,6 @@ public class DrugListActivity extends AppCompatActivity {
recyclerView.setAdapter(mAdapter);
}
// TODO: remove in release
private String getAppName() {
PackageManager packageManager = getApplicationContext().getPackageManager();
ApplicationInfo applicationInfo = null;

View file

@ -63,4 +63,5 @@
<string name="Value">Valeur</string>
<string name="label">Libellé</string>
<string name="drug_list">Attributs d\'un médicament</string>
<string name="notInterpreted">Le code barre ne peut pas être interprété</string>
</resources>

View file

@ -65,4 +65,5 @@
<string name="Value">Value</string>
<string name="label">Label</string>
<string name="drug_list">Drug\'s attributes</string>
<string name="notInterpreted">Barre code cannot be intrepreted</string>
</resources>