working on GUI refactorization (vi)

parent 83d95dca
......@@ -31,7 +31,7 @@ android {
debug {
resValue "string", "db_name", "PCB.db"
resValue "bool", "force_db_create", "false"
resValue "bool", "force_db_create", "true"
resValue "bool", "ssl_connect", "false"
resValue "bool", "force_img_download", "false"
resValue "integer", "netservice_timing", "20"
......
......@@ -211,7 +211,7 @@ public class Device extends SQLiteOpenHelper {
return user;
}
private Vector<User> recoverStudents(Integer id_sup) throws JSONException{
public Vector<User> recoverStudents(Integer id_sup) throws JSONException{
SQLiteDatabase db = this.getReadableDatabase();
Vector<User> users = new Vector<>();
Cursor cursor = db.query("users_detail", null, null, null, null, null, null, null);
......@@ -290,33 +290,13 @@ public class Device extends SQLiteOpenHelper {
return users;
}
private void updateUser(User user, SQLiteDatabase db) {
db.execSQL("UPDATE users_detail SET " +
"nickname_stu='" + user.get_nickname_stu() + "', " +
"pwd_stu='" + user.get_pwd_stu() + "', " +
"name_stu='" + user.get_name_stu() + "', " +
"surname_stu='" + user.get_surname_stu() + "', " +
"url_img_stu='" + user.get_url_img_stu() + "', " +
"gender_stu='" + user.get_gender_stu() + "', " +
"lang_stu='" + user.get_lang_stu() + "', " +
"attributes_stu='" + user.get_json_attrs() + "', " +
"name_sup='" + user.get_name_sup() + "', " +
"email_sup='" + user.get_email_sup() + "', " +
"pwd_sup='" + user.get_pwd_sup() + "', " +
"surname_sup='" + user.get_surname_sup() + "', " +
"url_img_sup='" + user.get_url_img_sup() + "', " +
"gender_sup='" + user.get_gender_sup() + "', " +
"lang_sup='" + user.get_lang_sup() + "', " +
"tts_engine_sup='" + user.get_tts_engine_sup() + "'" +
" WHERE id_stu=" + user.get_id_stu() + " AND id_sup=" + user.get_id_sup());
}
private void insertUser(User user, SQLiteDatabase db) {
public void insertUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("INSERT INTO users_detail values (" +
user.get_id_stu() + ", " +
"'" + user.get_nickname_stu() + "', " +
"'" + user.get_pwd_stu() + "', " +
"'" + user.get_name_stu() + "', " +
user.get_pwd_stu()==null ? "" : ("'" + user.get_pwd_stu() + "', " +
"'") + user.get_name_stu() + "', " +
"'" + user.get_surname_stu() + "', " +
"'" + user.get_url_img_stu() + "', " +
"'" + user.get_gender_stu() + "', " +
......@@ -332,43 +312,11 @@ public class Device extends SQLiteOpenHelper {
"'" + user.get_lang_sup() + "'," +
"'" + user.get_tts_engine_sup() + "'" +
")");
}
/**
* Set the current set of users, a hashtable with the whole users who have been registered. The hashcode is calculated as {@link User#hashCode()} depicts.
* For each user:
* if the user exists in the db, he/she is updated
* if the user doesn't exits in the db, he/she is included
* Users who are no longer registered are fully deleted from the DB, even the pictos in case of not any more users using the given picto. Nevertheless, the actions are preserved.
* Student and Supervisor profile images are updated from the server
*
* @param users a hashtable with the set of available users. The key of the hashtable is the same User object
* @see User
* @see com.yottacode.pictogram.dao.PCBDBHelper
*/
public void synchronizeUsers(Vector<User> users, iImgDownloaderListener imgListener) throws JSONException {
Vector<User> current_users = getUsers();
Vector<Img> imgs = new Vector<Img>(users.size() * 2);
SQLiteDatabase db = this.getWritableDatabase();
for (User user : users) {
if (current_users.contains(user)) {
updateUser(user, db);
} else
insertUser(user, db);
if (!imgs.contains(user.get_Img_stu())) imgs.add(user.get_Img_stu());
if (!imgs.contains(user.get_Img_sup())) imgs.add(user.get_Img_sup());
}
db.close();
ImgDownloader downloader = new ImgDownloader(this.context, imgListener, ImgDownloader.tsource.remote );
downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,imgs);
}
/**
* delete the list of images (students, supervisors, pictograms) which are no longer used
*/
......
......@@ -41,33 +41,15 @@ import java.util.Vector;
public class LoginActivity extends FragmentActivity {
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_SURNAME = "surname";
private static final String TAG_GENDER = "gender";
private static final String TAG_PIC = "pic";
private static final String TAG_LANG = "lang";
private static final String TAG_ATTRIBUTES = "attributes";
private static final String TAG_SUPERVISION = "supervision";
private int sup_id;
// String constant for logs
private final String LOG_TAG = this.getClass().getSimpleName(); // Or .getCanonicalName()
//A ProgressDialog object
private ProgressDialog progressDialog;
private String token=null;
private String username = null;
private String password = null;
private choose_student(Vector<User> students) {
downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,imgs);
}
private void show_login_failed(int msg) {
protected void show_login_failed(int msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage(getString(msg))
......@@ -92,7 +74,8 @@ public class LoginActivity extends FragmentActivity {
Intent pictogramActivity = new Intent(this, PictogramActivity.class);
startActivity(pictogramActivity);
} else{
getIntent().putExtra("sup_id", localUsers.elementAt(0).get_id_sup());
getIntent().putExtra("sup_id", localUsers.elementAt(0).get_id_sup()); //TODO: comprobar, ¿esto no está hecho ya?
setContentView(R.layout.activity_login);
}
} catch (JSONException e) {
e.printStackTrace();
......@@ -121,94 +104,7 @@ public class LoginActivity extends FragmentActivity {
if (offline)
manage_offline_supervisor(intent.getStringExtra("username"), intent.getStringExtra("password"));
else {
// Modo ONLINE
sup_id = getIntent().getExtras().getInt("sup_id");
token = getIntent().getExtras().getString("token");
RestapiWrapper wrapper = new RestapiWrapper(
getApplicationContext().getResources().getString(R.string.server), token);
String operation = "sup/" + sup_id + "/students";
wrapper.ask(operation, new iRestapiListener() {
@Override
public void error(Exception e) {
Log.e(this.getClass().getName(), " Server restapi error: " + e.getLocalizedMessage());
}
@Override
public void preExecute() {
progressDialog = ProgressDialog.show(LoginActivity.this, getString(R.string.userLoadingTxt),
getString(R.string.userLoadingTxt), false, false);
}
@Override
public void result(JSONArray students_supervisors) {
try {
// Saco los pares estudiante-supervisor
final Vector<User> users = new Vector<>(students_supervisors.length());
Vector<Img> imgs = new Vector<>(users.size());
if (students_supervisors.length() == 0)
show_login_failed(R.string.noStudentsError);
User student = null;
for (int i = 0; i < students_supervisors.length(); i++) {
JSONObject st = students_supervisors.getJSONObject(i);
Integer st_id = st.getInt(TAG_ID);
String st_name = st.getString(TAG_NAME);
String st_surname = st.getString(TAG_SURNAME);
String st_pic = st.getString(TAG_PIC);
String st_gender = st.getString(TAG_GENDER);
String st_lang = st.getString(TAG_LANG);
String st_attributes = st.getString(TAG_ATTRIBUTES);
Integer st_supervision = st.getInt(TAG_SUPERVISION);
// Añado un par <student-supervisor>
int st_id_int = st_id.intValue();
student = new User(st_id_int, "", "", st_name, st_surname, st_pic, st_gender, st_lang, st_attributes,
sup_id,
username,
password,
intent.getStringExtra("name"),
intent.getStringExtra("surname"),
intent.getStringExtra("pic"),
intent.getStringExtra("gender"),
intent.getStringExtra("lang"),
"");
users.add(student);
Img img=new Img(st_id,st_pic,Img.STUDENT);
if (!imgs.contains(img)) imgs.add(img); //it's required to download student's images
} //for
ImgDownloader downloader = new ImgDownloader(LoginActivity.this, new iImgDownloaderListener() {
@Override
public void loadComplete() {
progressDialog.dismiss();
if (users.size() > 1)
setContentView(R.layout.activity_login);
}
@Override
public void loadImg(Img image) {
}
}, ImgDownloader.tsource.remote);
downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,imgs);
// Si sólo hay 1 estudiante paso a Pictogram
if (users.size() == 1) {
Intent pictogramActivity = new Intent(LoginActivity.this, PictogramActivity.class);
startActivity(pictogramActivity);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void result(JSONObject result) {
}
});
setContentView(R.layout.activity_login);
}
}
@Override
......
......@@ -3,7 +3,6 @@ package com.yottacode.pictogram.gui;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
......@@ -57,7 +56,7 @@ public class SerialActivity extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
private void manageSupervisorLogin(final String username, final String password, boolean online) {
private void manageSupervisorLogin(final String email, final String password, boolean online) {
final Intent loginActivity = new Intent(SerialActivity.this, LoginActivity.class);
if (online) {
......@@ -66,7 +65,7 @@ public class SerialActivity extends Activity {
final String operation = "sup/login";
Hashtable<String, String> postDataParams = new Hashtable<String, String>();
postDataParams.put("email", username);
postDataParams.put("email", email);
postDataParams.put("password", password);
PCBcontext.getRestapiWrapper().ask(operation, postDataParams, "post", new iRestapiListener() {
......@@ -104,13 +103,14 @@ public class SerialActivity extends Activity {
loginActivity.putExtra("offline", false);
loginActivity.putExtra("sup_id", su_id_int);
loginActivity.putExtra("token", jsonToken);
loginActivity.putExtra("username", username);
loginActivity.putExtra("email", email);
loginActivity.putExtra("password", password);
loginActivity.putExtra("name", user.getString(TAG_NAME));
loginActivity.putExtra("surname", user.getString(TAG_SURNAME));
loginActivity.putExtra("pic", user.getString(TAG_PIC));
loginActivity.putExtra("gender", user.getString(TAG_GENDER));
loginActivity.putExtra("lang", user.getString(TAG_LANG));
Log.i(this.getClass().getCanonicalName(),"FERNANDO arrancando loginActivity");
startActivity(loginActivity);
} catch (JSONException e) {
......@@ -127,7 +127,7 @@ public class SerialActivity extends Activity {
});
} else{
loginActivity.putExtra("offline", true);
loginActivity.putExtra("username", username);
loginActivity.putExtra("username", email);
loginActivity.putExtra("password", password);
startActivity(loginActivity);
}
......@@ -244,10 +244,11 @@ public class SerialActivity extends Activity {
private boolean checkLogin(JSONObject result) {
String error;
try {
error = result.getString("error");
error=result.has("error") ? result.getString("error")
: null;
} catch (JSONException e) {
e.printStackTrace();
error= null;
error=null;
}
if (error!=null){
......
......@@ -196,6 +196,7 @@ END
CREATE TRIGGER trg_insert_users_detail
INSTEAD OF INSERT ON users_detail
FOR EACH ROW
WHEN NEW.pwd_stu IS NOT NULL
BEGIN
INSERT OR REPLACE INTO student VALUES (NEW.id_stu, NEW.nickname_stu, NEW.pwd_stu, NEW.name_stu, NEW.surname_stu, NEW.url_img_stu, NEW.gender_stu, NEW.lang_stu, NEW.attributes_stu);
INSERT OR REPLACE INTO supervisor VALUES (NEW.id_sup, NEW.email_sup, NEW.pwd_sup, NEW.name_sup, NEW.surname_sup, NEW.url_img_sup, NEW.gender_sup, NEW.lang_sup, NEW.tts_engine_sup);
......@@ -203,6 +204,28 @@ BEGIN
END
;--
CREATE TRIGGER trg_insert_users_detail_nostudentpwd
INSTEAD OF INSERT ON users_detail
FOR EACH ROW
WHEN NEW.pwd_stu IS NULL
BEGIN
UPDATE
student
SET
nickname=NEW.nickname_stu,
name=NEW.name_stu,
surname=NEW.surname_stu,
url_img=NEW.url_img_stu,
gender=NEW.gender_stu,
lang=NEW.lang_stu,
attributes=NEW.attributes_stu
WHERE
id=NEW.id_stu;
INSERT OR REPLACE INTO supervisor VALUES (NEW.id_sup, NEW.email_sup, NEW.pwd_sup, NEW.name_sup, NEW.surname_sup, NEW.url_img_sup, NEW.gender_sup, NEW.lang_sup, NEW.tts_engine_sup);
INSERT INTO users VALUES (NEW.id_stu,NEW.id_sup);
END
;--
CREATE TRIGGER trg_update_users_detail
INSTEAD OF UPDATE ON users_detail
FOR EACH ROW
......
......@@ -22,7 +22,8 @@
<string name="userErrorMsg">The user is not correct. Try again.</string>
<string name="userLoadingTxt">Loading</string>
<string name="userLoadingMsg">Loading students. Please wait.</string>
<string name="noStudentsError">El usuario indicado no tiene alumnos asignados. Asigne estudiantes desde el panel de control</string>
<string name="imguserLoadingMsg">Loading images students. Please wait.</string>
<string name="noStudentsError">User without any student available</string>
<!-- Alert dialogs -->
<string name="exitPictogram">Exit Pictogram</string>
<string name="enterEscapeCode">Type escape code</string>
......
......@@ -23,6 +23,7 @@
<string name="userErrorMsg">El usuario indicado no es correcto. Inténtelo de nuevo.</string>
<string name="userLoadingTxt">Cargando</string>
<string name="userLoadingMsg">Cargando alumnos. Por favor espere.</string>
<string name="imguserLoadingMsg">Cargando imágenes de los alumnos. Por favor espere.</string>
<string name="noStudentsError">El usuario indicado no tiene alumnos asignados. Asigne estudiantes desde el panel de control</string>
<!-- Alert dialogs -->
......@@ -62,3 +63,4 @@
<string name="pictogram_offline">Compruebe si tiene conexión a Internet. </string>
<string name="pictogram_online">Conexón con el servidor establecida. </string>
</resources>
......@@ -23,6 +23,7 @@
<string name="userErrorMsg">El usuario indicado no es correcto. Inténtelo de nuevo.</string>
<string name="userLoadingTxt">Cargando</string>
<string name="userLoadingMsg">Cargando alumnos. Por favor espere.</string>
<string name="imguserLoadingMsg">Cargando imágenes de los alumnos. Por favor espere.</string>
<string name="noStudentsError">El usuario indicado no tiene alumnos asignados. Asigne estudiantes desde el panel de control</string>
<!-- Alert dialogs -->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment