Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F98073010
AddPatientActivity.java
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Jan 9, 12:22
Size
8 KB
Mime Type
text/x-java
Expires
Sat, Jan 11, 12:22 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
23498920
Attached To
R3229 Genome Privacy - SHCS App
AddPatientActivity.java
View Options
package
ch.epfl.pharma
;
import
java.sql.SQLException
;
import
android.app.Activity
;
import
android.app.AlertDialog
;
import
android.app.Dialog
;
import
android.app.DialogFragment
;
import
android.content.Context
;
import
android.content.DialogInterface
;
import
android.os.AsyncTask
;
import
android.os.Bundle
;
import
android.os.Handler
;
import
android.util.Log
;
import
android.view.Menu
;
import
android.view.MenuItem
;
import
android.view.View
;
import
android.widget.EditText
;
import
android.widget.Toast
;
import
ch.epfl.pharma.database.DatabaseHelper
;
import
ch.epfl.pharma.framework.Patient
;
import
ch.epfl.pharma.framework.Patient.Sex
;
import
ch.epfl.pharma.ui.ErrorDialogFragment
;
import
crypto.elgamal.PartialSecret
;
import
database.DBconnectorClient
;
import
database.DatabaseConfig
;
public
class
AddPatientActivity
extends
Activity
{
@SuppressWarnings
(
"unused"
)
private
static
final
String
TAG
=
AddPatientActivity
.
class
.
getName
();
private
DatabaseHelper
mDbHelper
=
null
;
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_add_patient
);
String
dbName
=
getResources
().
getString
(
R
.
string
.
database_name
);
mDbHelper
=
new
DatabaseHelper
(
this
,
dbName
);
}
@Override
public
boolean
onCreateOptionsMenu
(
Menu
menu
)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater
().
inflate
(
R
.
menu
.
add_patient
,
menu
);
return
true
;
}
@Override
public
boolean
onOptionsItemSelected
(
MenuItem
item
)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int
id
=
item
.
getItemId
();
if
(
id
==
R
.
id
.
action_settings
)
{
return
true
;
}
return
super
.
onOptionsItemSelected
(
item
);
}
public
void
addPatient
(
View
view
)
{
EditText
editPatientId
=
(
EditText
)
findViewById
(
R
.
id
.
edittext_addpatient_patientid
);
EditText
editLastname
=
(
EditText
)
findViewById
(
R
.
id
.
edittext_addpatient_lastname
);
EditText
editFirstname
=
(
EditText
)
findViewById
(
R
.
id
.
edittext_addpatient_firstname
);
EditText
editAddress
=
(
EditText
)
findViewById
(
R
.
id
.
edittext_addpatient_address
);
String
strPatientId
=
editPatientId
.
getText
().
toString
();
String
lastname
=
editLastname
.
getText
().
toString
();
String
firstname
=
editFirstname
.
getText
().
toString
();
String
strAddress
=
editAddress
.
getText
().
toString
();
InvalidInputField
inputStatus
=
getInputFieldStatus
(
strPatientId
,
lastname
,
firstname
,
strAddress
);
if
(
inputStatus
==
InvalidInputField
.
NONE
)
{
int
patientId
=
Integer
.
valueOf
(
strPatientId
);
Patient
patient
=
new
Patient
(
patientId
,
firstname
,
lastname
,
Sex
.
NA
,
null
,
strAddress
,
null
);
AsyncAddPatient
asyncTask
=
new
AsyncAddPatient
(
this
);
asyncTask
.
execute
(
patient
);
}
else
{
// invalid fields
InvalidInputDialogFragment
invalidDialog
=
new
InvalidInputDialogFragment
(
inputStatus
);
invalidDialog
.
show
(
getFragmentManager
(),
"invalid_input_argument"
);
switch
(
inputStatus
)
{
case
PATIENT_ID:
editPatientId
.
requestFocus
();
break
;
case
LASTNAME:
editLastname
.
requestFocus
();
break
;
case
FIRSTNAME:
editFirstname
.
requestFocus
();
break
;
case
ADDRESS:
editAddress
.
requestFocus
();
break
;
case
NONE:
default
:
break
;
}
}
}
private
InvalidInputField
getInputFieldStatus
(
String
strPatientId
,
String
strLastname
,
String
strFirstname
,
String
strAddress
)
{
if
(
strPatientId
.
isEmpty
())
{
return
InvalidInputField
.
PATIENT_ID
;
}
if
(
strLastname
.
isEmpty
())
{
return
InvalidInputField
.
LASTNAME
;
}
if
(
strFirstname
.
isEmpty
())
{
return
InvalidInputField
.
FIRSTNAME
;
}
if
(
strAddress
.
isEmpty
())
{
return
InvalidInputField
.
ADDRESS
;
}
return
InvalidInputField
.
NONE
;
}
private
enum
InvalidInputField
{
NONE
,
PATIENT_ID
,
LASTNAME
,
FIRSTNAME
,
ADDRESS
}
private
class
InvalidInputDialogFragment
extends
DialogFragment
{
private
InvalidInputField
reason
;
public
InvalidInputDialogFragment
(
InvalidInputField
reason
)
{
this
.
reason
=
reason
;
}
@Override
public
Dialog
onCreateDialog
(
Bundle
savedInstanceState
)
{
AlertDialog
.
Builder
builder
=
new
AlertDialog
.
Builder
(
getActivity
());
builder
.
setTitle
(
"Invalid input"
);
builder
.
setIcon
(
android
.
R
.
drawable
.
ic_dialog_alert
);
builder
.
setMessage
(
getReasonMessage
());
builder
.
setPositiveButton
(
"OK"
,
new
DialogInterface
.
OnClickListener
()
{
@Override
public
void
onClick
(
DialogInterface
dialog
,
int
which
)
{
dialog
.
dismiss
();
}
});
return
builder
.
create
();
}
private
String
getReasonMessage
()
{
String
message
=
"What is patient's "
;
switch
(
reason
)
{
case
ADDRESS:
message
+=
"address"
;
break
;
case
FIRSTNAME:
message
+=
"firstname"
;
break
;
case
LASTNAME:
message
+=
"lastname"
;
break
;
case
PATIENT_ID:
message
+=
"id"
;
break
;
case
NONE:
throw
new
IllegalStateException
(
"Not a consistant InvalidInputfield."
);
}
message
+=
"?"
;
return
message
;
};
}
private
class
AsyncAddPatient
extends
AsyncTask
<
Patient
,
Void
,
AddPatientState
>
{
private
final
String
TAG
=
AsyncAddPatient
.
class
.
getName
();
private
Activity
parent
;
private
Handler
mHandler
;
public
AsyncAddPatient
(
Activity
parent
)
{
super
();
this
.
parent
=
parent
;
this
.
mHandler
=
new
Handler
();
}
@Override
protected
void
onPreExecute
()
{
// TODO: show progress bar circle
super
.
onPreExecute
();
}
@Override
protected
AddPatientState
doInBackground
(
Patient
...
patients
)
{
Patient
patient
=
patients
[
0
];
int
patientId
=
patient
.
getPatientId
();
// status of operation
AddPatientState
status
;
// check if patient already exists in database
mDbHelper
.
openDatabase
();
boolean
patientExists
=
mDbHelper
.
patientExists
(
patient
);
if
(
patientExists
)
{
status
=
AddPatientState
.
EXISTS
;
}
else
{
// request crypto keys from server
PartialSecret
secret
;
try
{
secret
=
getCryptoKey
(
patientId
);
// insert patient and key into database
mDbHelper
.
insertPatient
(
patient
);
mDbHelper
.
insertPrivateKey
(
patientId
,
secret
);
status
=
AddPatientState
.
ADDED
;
Log
.
d
(
TAG
,
"Patient inserted: "
+
patientId
);
}
catch
(
SQLException
e
)
{
Log
.
e
(
TAG
,
e
.
getMessage
(),
e
);
status
=
AddPatientState
.
FAILED
;
}
}
mDbHelper
.
close
();
return
status
;
}
@Override
protected
void
onPostExecute
(
AddPatientState
status
)
{
switch
(
status
)
{
case
ADDED:
displayToastSuccessAndQuit
();
break
;
case
EXISTS:
displayWarningPatientExists
();
break
;
case
FAILED:
displayError
();
break
;
default
:
throw
new
IllegalStateException
(
"Unallowed AddPatient state in adding operation"
);
}
}
private
void
displayWarningPatientExists
()
{
ErrorDialogFragment
warningFrag
=
new
ErrorDialogFragment
(
"Patient already exists"
,
"The patient specified already exists. Specify a new one or cancel operation"
);
warningFrag
.
show
(
getFragmentManager
(),
"database_patient_exists"
);
}
private
void
displayError
()
{
// display error message
ErrorDialogFragment
errorFrag
=
new
ErrorDialogFragment
(
"Connection error"
,
"Failed to establish connection with distant service to fetch cryptographic key."
);
errorFrag
.
show
(
getFragmentManager
(),
"database_access_crypto_failed"
);
}
private
void
displayToastSuccessAndQuit
()
{
Context
mContext
=
getApplicationContext
();
CharSequence
text
=
"Patient addedd successfully"
;
int
duration
=
Toast
.
LENGTH_LONG
;
Toast
toast
=
Toast
.
makeText
(
mContext
,
text
,
duration
);
toast
.
show
();
// after showing toast, close activity
mHandler
.
postDelayed
(
new
Runnable
()
{
@Override
public
void
run
()
{
parent
.
finish
();
}
},
duration
);
}
private
PartialSecret
getCryptoKey
(
int
patientId
)
throws
SQLException
{
String
dbIp
=
getResources
().
getString
(
R
.
string
.
db_client_ip
);
String
dbName
=
getResources
().
getString
(
R
.
string
.
db_client_dbname
);
String
dbUser
=
getResources
().
getString
(
R
.
string
.
db_client_user
);
String
dbPwd
=
getResources
().
getString
(
R
.
string
.
db_client_password
);
DatabaseConfig
dbConfig
=
new
DatabaseConfig
(
dbIp
,
dbName
,
dbUser
,
dbPwd
);
DBconnectorClient
dbClient
=
new
DBconnectorClient
(
dbConfig
);
dbClient
.
openConnection
();
PartialSecret
secret
=
dbClient
.
getPrivateKey
(
patientId
);
dbClient
.
closeConnection
();
return
secret
;
}
}
private
enum
AddPatientState
{
ADDED
,
EXISTS
,
FAILED
}
}
Event Timeline
Log In to Comment