Funciones de acceso a bbdd local

parent a3bd956f
......@@ -178,14 +178,32 @@ public class Device extends SQLiteOpenHelper {
return user;
}
/**
* Para saber si un supervisor esta en la bbdd local
* @param id_sup
* @return
* @throws JSONException
*/
public Boolean isSupervisorOnLocal(int id_sup) throws JSONException{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(" SELECT * FROM supervisor WHERE id = " + id_sup, null);
if(cursor.moveToFirst())
return true;
cursor.close();
return false;
}
//TODO: Recuperar los users (supervisors + students) de un alumno
public Vector<User> recoverSupervisors(Integer id_stu) throws JSONException{
SQLiteDatabase db = this.getReadableDatabase();
Vector<User> users = new Vector<>();
Cursor cursor = db.query("users_detail", null, null, null, null, null, null, null);
Cursor cursor = db.rawQuery(" SELECT * FROM supervisor,student WHERE "+id_stu+ " = (SELECT id_stu from users WHERE id_stu = " +id_stu+")" ,null);
while (cursor.moveToNext())
if (cursor.getInt(0) == id_stu)
Log.i("TAG_PRUEBAS","Fila: "+cursor.getInt(0)+cursor.getString(1)+ cursor.getString(2)+ cursor.getString(3)+ cursor.getString(4)+ cursor.getString(5)+ cursor.getString(6)+ cursor.getString(7)+ cursor.getString(8)+
cursor.getInt(9)+ cursor.getString(10)+ cursor.getString(11)+ cursor.getString(12)+ cursor.getString(13)+ cursor.getString(14)+ cursor.getString(15)+ cursor.getString(16)+ cursor.getString(17)+ cursor.getString(18));
/*if (cursor.getInt(9) == id_stu)
users.add(new User(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),
cursor.getInt(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17), cursor.getString(18)));
cursor.getInt(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17), cursor.getString(18)));*/
cursor.close();
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
return users;
......@@ -268,6 +286,48 @@ public class Device extends SQLiteOpenHelper {
return users;
}
/**
* To insert a tuple on users
* @param id_stu
* @param id_sup
*/
public void insertUserOnUsers(int id_stu, int id_sup){
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
db.execSQL("INSERT INTO users values (" +
id_stu + ", " +
id_sup +
")");
db.setTransactionSuccessful();
db.endTransaction();
}
/**
* For insert a new supervisor on the local database
*
*/
public void insertSupervisor(int id, String email,String pwd,String name,String surname,String url_image,String gender,String lang,String tts_engine,String office) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
db.execSQL("INSERT INTO supervisor values (" +
id+ ", " +
"'" + email + "', " +
(pwd==null || pwd.length()==0 ? "NULL" : "'"+pwd+"'") + ", "+
"'" + name + "', " +
"'" + surname + "', " +
"'" + url_image + "', " +
"'" + gender + "', " +
"'" + lang + "', " +
"'" + tts_engine + "', " +
"'" + office + "')");
db.setTransactionSuccessful();
db.endTransaction();
Log.i("TAG_PRUEBAS","Supervisor: "+email+" insertado");
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
}
public void insertUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
......@@ -297,10 +357,14 @@ public class Device extends SQLiteOpenHelper {
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
}
public void deleteUser(User user) {
/**
* Delete all the pairs <id_sup,id_stu> of a student
* @param stu_id
*/
public void deleteSupervisorOfStudentUser(int stu_id) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
db.execSQL("DELETE FROM users_detail WHERE id_sup = " + user.get_id_sup());
db.execSQL("DELETE FROM users WHERE id_stu = "+stu_id);
db.setTransactionSuccessful();
db.endTransaction();
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
......
......@@ -226,38 +226,41 @@ public class StudentFragmentGrid extends Fragment{
@Override
public void result(JSONArray supervisors) {
Vector<Integer> idSupervisoresJSON = new Vector<Integer>();
//TODO: Obtener supervisores del JSON
//Vector<Integer> idSupervisoresJSON = new Vector<Integer>();
for (int i=0;i<supervisors.length();i++) {
JSONObject supervisor = null;
try {
supervisor = supervisors.getJSONObject(i);
idSupervisoresJSON.add((Integer) supervisor.get("id"));
User user = PCBcontext.getDevice().findUser(stu_id,(int) supervisor.get("id")); //Para comprobar si ya existe en local
if(user==null) {
PCBcontext.getDevice().insertUser(new User(stu_id, PCBcontext.getPcbdb().getCurrentUser().get_nickname_stu(), PCBcontext.getPcbdb().getCurrentUser().get_pwd_stu()
//idSupervisoresJSON.add((Integer) supervisor.get("id"));
//User user = ;
if(!PCBcontext.getDevice().isSupervisorOnLocal((int) supervisor.get("id"))) {//Para comprobar si ya existe en local
PCBcontext.getDevice().insertSupervisor(supervisor.getInt("id"),supervisor.getString("email"),supervisor.getString("name"),null,supervisor.getString("surname"),
supervisor.getString("pic"),supervisor.getString("gender"),supervisor.getString("lang"),supervisor.getString("ttsEngine"),supervisor.getString("office"));
/*PCBcontext.getDevice().insertUser(new User(stu_id, PCBcontext.getPcbdb().getCurrentUser().get_nickname_stu(), PCBcontext.getPcbdb().getCurrentUser().get_pwd_stu()
, PCBcontext.getPcbdb().getCurrentUser().get_name_stu(), PCBcontext.getPcbdb().getCurrentUser().get_surname_stu(), PCBcontext.getPcbdb().getCurrentUser().get_url_img_stu()
, PCBcontext.getPcbdb().getCurrentUser().get_gender_stu(), PCBcontext.getPcbdb().getCurrentUser().get_lang_stu(), PCBcontext.getPcbdb().getCurrentUser().get_json_attrs(),
(int) supervisor.get("id"), supervisor.get("email").toString(), null, supervisor.get("name").toString(), supervisor.get("surname").toString(), supervisor.get("pic").toString(),
supervisor.get("gender").toString(), supervisor.get("lang").toString(), supervisor.get("ttsEngine").toString(), supervisor.get("office").toString()));
supervisor.get("gender").toString(), supervisor.get("lang").toString(), supervisor.get("ttsEngine").toString(), supervisor.get("office").toString()));*/
}else{
Log.i("TAG_PRUEBAS","Usuario con id: "+supervisor.getString("email")+" ya existe");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
/*try {
Vector<User> supervisorsLocal = PCBcontext.getDevice().recoverSupervisors(stu_id);
for(User user: supervisorsLocal){
if(!idSupervisoresJSON.contains(user.get_id_sup())){
PCBcontext.getDevice().deleteUser(user);
PCBcontext.getDevice().deleteUser(stu_id);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}*/
//PCBcontext.getPcbdb().getCurrentUser().set_Supervisors(supervisorsFormat);
}
......
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