Cleanning code, remove almost warning

This commit is contained in:
Jacques Foucry 2016-11-08 12:10:27 +01:00
parent 88322fe1bc
commit f88a7157b8
5 changed files with 121 additions and 96 deletions

View file

@ -46,7 +46,7 @@ public class DBHelper extends SQLiteOpenHelper {
return sInstance;
}
public DBHelper(Context context) {
DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@ -76,7 +76,7 @@ public class DBHelper extends SQLiteOpenHelper {
this.onCreate(db);
}
public void dropDrug() {
void dropDrug() {
SQLiteDatabase db = this.getWritableDatabase();
Log.d(TAG, "Drop drug table");
db.execSQL("DROP TABLE IF EXISTS drug");
@ -84,7 +84,7 @@ public class DBHelper extends SQLiteOpenHelper {
this.onCreate(db);
}
public void addDrug(Medicament medicament) {
void addDrug(Medicament medicament) {
// Logging
Log.d(TAG, medicament.toString());
@ -129,30 +129,36 @@ public class DBHelper extends SQLiteOpenHelper {
null); // limits
// if case we got result, go to the first one
if (cursor != null)
Medicament medicament = new Medicament();
if (cursor != null) {
cursor.moveToFirst();
// Build medicament object
Medicament 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)));
// 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)));
}
// Log
Log.d(TAG, "getDrug("+id+")" + medicament.toString());
if (null != cursor) cursor.close();
// Return medicament
return medicament;
}
/**
*
* @param cip13 drug id in French nomemclature
* @return the medicament object found in DB or null
*/
public Medicament getDrugByCIP13(String cip13) {
// Get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
@ -161,37 +167,43 @@ public class DBHelper extends SQLiteOpenHelper {
Cursor cursor = db.query(TABLE_DRUG, // Which table
COLUMS, // column names
" cip13 = ?", // selections
new String[] { String.valueOf(cip13) }, // selections args
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
if (cursor != null)
Medicament medicament = new Medicament();
if (cursor != null) {
cursor.moveToFirst();
// Build medicament object
Medicament 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)));
// 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)));
}
// Log
Log.d(TAG, "getDrug("+cip13+")" + medicament.toString());
if (null != cursor) cursor.close();
// Return medicament
Log.d(TAG, "getDrug(" + cip13 + ")" + medicament.toString());
return medicament;
}
public List<Medicament> getAllDrugs() {
/**
*
* @return a List of All medicaments presents in database
*/
List<Medicament> getAllDrugs() {
List<Medicament> medicaments = new LinkedList<Medicament>();
// Build the query
@ -202,7 +214,7 @@ public class DBHelper extends SQLiteOpenHelper {
Cursor cursor = db.rawQuery(query, null);
// For Each row, build a medicament and add it to the list
Medicament medicament = null;
Medicament medicament;
if (cursor.moveToFirst()) {
do {
medicament = new Medicament();
@ -229,10 +241,14 @@ public class DBHelper extends SQLiteOpenHelper {
cursor.close();
Log.d(TAG, "getAllDrugs " + medicaments.toString());
// return
return medicaments;
}
/**
*
* @param medicament object to be updated in DB
* @return code of update operation (should be 0)
*/
public int updateDrug(Medicament medicament) {
// Get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
@ -258,6 +274,10 @@ public class DBHelper extends SQLiteOpenHelper {
return i;
}
/**
* Delete a medicament object in datebase
* @param medicament object to be delete in the DB
*/
public void deleteDrug(Medicament medicament) {
// Get writable database
SQLiteDatabase db = this.getWritableDatabase();
@ -274,6 +294,10 @@ public class DBHelper extends SQLiteOpenHelper {
Log.d(TAG, "delete drug "+medicament.toString());
}
/**
* Get count of all medicament present in database
* @return number of medicament in DB
*/
public int getCount() {
String query = "SELECT count (*) FROM " + TABLE_DRUG;

View file

@ -16,7 +16,7 @@ import java.io.OutputStream;
/**
* Created by jfoucry on 5/25/16.
*/
public class DBMedoc extends SQLiteOpenHelper{
class DBMedoc extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
@ -37,7 +37,7 @@ public class DBMedoc extends SQLiteOpenHelper{
private static final String TAG = DBMedoc.class.getName();
public DBMedoc(Context context) {
DBMedoc(Context context) {
super(context, dbName, null, DATABASE_VERSION);
this.myContext = context;
}
@ -86,7 +86,7 @@ public class DBMedoc extends SQLiteOpenHelper{
}
}
public void openDatabase() throws SQLiteException {
void openDatabase() throws SQLiteException {
Log.e(TAG, "openDatabase called");
String myPath = DATABASE_PATH + dbName;
@ -100,9 +100,12 @@ public class DBMedoc extends SQLiteOpenHelper{
}
}
private DBMedoc dbMedoc;
public Medicament getMedocByCIP13(String cip13) {
/**
* Lookup in the DB for a record corresponding to cpi1
* @param cip13 string representing the object we're looking for
* @return return a medicament objet
*/
Medicament getMedocByCIP13(String cip13) {
Log.e(TAG, "getNedocByCIP13 - " + cip13);
SQLiteDatabase db = this.getReadableDatabase();
@ -110,8 +113,8 @@ public class DBMedoc extends SQLiteOpenHelper{
// Build query
Cursor cursor = db.query(TABLE_NAME, // Which table
COLUMNS_NAMES, // column names
" cip13 =?", // selections
new String[]{cip13}, // selections args
" cip13 =?", // selections
new String[]{cip13}, // selections args
null, // group by
null, // having
null, // order by

View file

@ -7,6 +7,7 @@ import java.lang.String;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import static net.foucry.pilldroid.UtilDate.*;
@ -31,12 +32,9 @@ public class Medicament implements Serializable {
/* calculate part */
private Date dateEndOfStock;
private static final String[] COLUMN_LIST = {"id","cis", "cip13", "nom", "mode_administration", "presentation", "stock", "prise",
"seuil_warn", "seuil_alert", "dateLastUpdate"};
Medicament() {}
public Medicament() {}
public Medicament(final String cis, final String cip13, final String nom, final String mode_administration, final String presentation,
Medicament(final String cis, final String cip13, final String nom, final String mode_administration, final String presentation,
double stock, double prise, int warn, int alert) {
super();
@ -56,39 +54,37 @@ public class Medicament implements Serializable {
return id;
}
public String getNom() {
String getNom() {
return nom;
}
public String getCip13() {
String getCip13() {
return cip13;
}
public String getCis() {
String getCis() {
return cis;
}
public String getMode_administration() {
String getMode_administration() {
return mode_administration;
}
public String getPresentation() {
String getPresentation() {
return presentation;
}
public double getStock() {
return stock;
}
double getStock() { return stock; }
public double getPrise() {
double getPrise() {
return prise;
}
public int getAlertThreshold() {
int getAlertThreshold() {
return alertThreshold;
}
public int getWarnThreshold() {
int getWarnThreshold() {
return warnThreshold;
}
@ -96,7 +92,7 @@ public class Medicament implements Serializable {
return dateLastUpdate;
}
public Date getDateEndOfStock() {
Date getDateEndOfStock() {
return dateEndOfStock;
}
@ -104,51 +100,51 @@ public class Medicament implements Serializable {
this.id = id;
}
public void setNom(String nom) {
void setNom(String nom) {
this.nom = nom;
}
public void setCip13(String cip13) {
void setCip13(String cip13) {
this.cip13 = cip13;
}
public void setCis(String cis) {
void setCis(String cis) {
this.cis = cis;
}
public void setMode_administration(String mode_administration) {
void setMode_administration(String mode_administration) {
this.mode_administration = mode_administration;
}
public void setPresentation(String presentation) {
void setPresentation(String presentation) {
this.presentation = presentation;
}
public void setStock(double stock) {
void setStock(double stock) {
this.stock = stock;
}
public void setPrise(double prise) {
void setPrise(double prise) {
this.prise = prise;
}
public void setWarnThreshold(int warn) {
void setWarnThreshold(int warn) {
if (warn == 0)
warn = 14;
this.warnThreshold = warn;
}
public void setAlertThreshold(int alert) {
void setAlertThreshold(int alert) {
if (alert == 0)
alert = 7;
this.alertThreshold = alert;
}
public void setDateLastUpdate() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
void setDateLastUpdate() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRANCE);
this.dateLastUpdate = date2String(dateAtNoon(new Date()), dateFormat);
}
public void setDateEndOfStock() {
void setDateEndOfStock() {
int numberDayOfPrise;
if (this.prise > 0) {
numberDayOfPrise = (int) Math.floor(this.stock / this.prise);
@ -164,7 +160,7 @@ public class Medicament implements Serializable {
this.dateEndOfStock = calendar.getTime();
}
public double newStock(double currentStock) {
double newStock(double currentStock) {
Date lastUpdate = string2Date(this.dateLastUpdate);
int numberOfDays = nbOfDaysBetweenDateAndToday(lastUpdate);
double takeDuringPeriod = this.prise * numberOfDays;

View file

@ -27,7 +27,7 @@ public class NotificationPublisher extends BroadcastReceiver {
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID,0);
notificationManager.notify(id, notification);
Vibrator vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(400);
}

View file

@ -7,20 +7,22 @@ import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* Created by jacques on 05/05/16.
*/
public class UtilDate {
class UtilDate {
private static final String TAG = UtilDate.class.getName();
/**
*
* @param aDate
* @return date
* @param aDate anydate
* @return date the same date as input but at noon (12:00:00)
*
* set date time at Noon
*/
public static Date dateAtNoon(Date aDate) {
static Date dateAtNoon(Date aDate) {
// Log.d(TAG, "dateAtNoon " + aDate);
@ -34,13 +36,13 @@ public class UtilDate {
}
/**
*
* @param days
* @param date
* @param days number of days to remove to the ate
* @param date date before day removing
* @return date
*
* Substract days to date and return a new date
*/
public static Date removeDaysToDate(int days, Date date) {
static Date removeDaysToDate(int days, Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, -days);
@ -50,12 +52,12 @@ public class UtilDate {
/**
*
* @param date
* @return String
* @param date Date to be converted
* @return String of the converted date
*
* Convert a date to a String using a SimpleDateFormat
*/
public static String date2String(Date date, DateFormat dateFormat) {
static String date2String(Date date, DateFormat dateFormat) {
Log.d(TAG, "date == " + date);
@ -67,25 +69,25 @@ public class UtilDate {
/**
*
* @param dateString
* @return date
* @param dateString string representing a Date to be conveted
* @return date Date after convertion
*
* Convert String date into Date
*/
public static Date string2Date(String dateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static Date string2Date(String dateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRANCE);
ParsePosition pos = new ParsePosition(0);
return dateFormat.parse(dateString,pos);
}
/**
*
* @param date
* @return int
* @param date start date
* @return int numbers of days between date and today
*
* Number of days between date (older than today) and today
*/
public static int nbOfDaysBetweenDateAndToday(Date date) {
static int nbOfDaysBetweenDateAndToday(Date date) {
Date oldDate = dateAtNoon(date); // Be sure that the old date is at Noon
Date todayDate = dateAtNoon(new Date()); // Be sure that we use today at Noon
@ -97,7 +99,7 @@ public class UtilDate {
* return int
*/
public static long tomorrowAtNoonInMillis() {
static long tomorrowAtNoonInMillis() {
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
@ -108,8 +110,8 @@ public class UtilDate {
return (tomorrowAtNoon.getTime() - now.getTime());
}
public static String convertDate(long dateInMilliseconds) {
DateFormat formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
static String convertDate(long dateInMilliseconds) {
DateFormat formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss", Locale.FRANCE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(dateInMilliseconds);
return formatter.format(calendar.getTime());