Cambios en BBDD del PCB, añadida tabla scene, editada tabla collection y vista…

Cambios en BBDD del PCB, añadida tabla scene, editada tabla collection y vista collection_detail, asi como el trigger para insercion. Añadidos metodos a PCBDBHelper
parent 3ac0ef54
......@@ -142,6 +142,53 @@ public class PCBDBHelper extends SQLiteOpenHelper {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("action",null,null);
}
/**
* To insert into scene the data of scene that come from the server (Actually only insert the active scene)
* @param params example JSONObject{
* "supervisor": 23,
* "student": 105,
* "id": 135,
* "name": "sin",
* "active": true,
* "categories": false,
* "pictos": [.....] Pictos of that scene
*/
public void setActiveSceneForStudent(JSONObject params){
SQLiteDatabase db = this.getReadableDatabase();
try {
db.rawQuery("INSERT INTO scene VALUES ("
+params.getInt("id")
+","+params.getInt("supervisor")
+","+params.get("student")
+",'"+params.getString("name")
+"','"+params.getBoolean("active")
+"','"+params.getBoolean("categories")
+"')",null);
} catch (JSONException e) {
e.printStackTrace();
}
Cursor cursor = db.rawQuery("SELECT * FROM scene",null);
Log.i(LOG_TAG,"tam scen: "+cursor.getCount());
cursor.close();
}
/**
* Return the active scene for an student (Actually only find by id_stu cause table scene means activeScene of the student)
* @param id_stu
* @return
*/
public int getActiveSceneForStudent(int id_stu){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT id FROM scene WHERE id_stu = "+id_stu,null);
cursor.moveToFirst();
if(cursor.getCount() > 0){
return cursor.getInt(0);
}
return -1;
}
/**
* the collection (set of pictos) of the current student
*
......@@ -150,7 +197,8 @@ public class PCBDBHelper extends SQLiteOpenHelper {
public Vocabulary getStudentVocabulary(Vocabulary vocabulary ) throws JSONException {
int id_stu = this.getCurrentUser().get_id_stu();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("collection_detail", null, "id_stu=?", new String[]{String.valueOf(id_stu)}, null, null, null, null);
Cursor cursor = db.rawQuery("SELECT * FROM collection_detail WHERE id_stu = "+id_stu,null);
Log.i(LOG_TAG, "Local recovering " + cursor.getCount() + " pictos for student " + id_stu + " from local DB");
cursor.moveToFirst();
if (cursor.getCount()>0) do{
......@@ -163,9 +211,6 @@ public class PCBDBHelper extends SQLiteOpenHelper {
return vocabulary;
}
/**
* Set/update the set of pictos of the current student. Pictos which are no longer used are dropped from the DB
*
......@@ -176,17 +221,18 @@ public class PCBDBHelper extends SQLiteOpenHelper {
SQLiteDatabase db = this.getWritableDatabase();
int id_stu = this.getCurrentUser().get_id_stu();
int seconds1 = Calendar.getInstance().get(Calendar.SECOND);
Log.i("TAG_PRUEBAS","tamVoc to insert: "+vocabulary.size());
db.delete("collection", "id_stu=" + id_stu, null);
int newsize=0;
ContentValues values=new ContentValues(5);
ContentValues values=new ContentValues(6);
values.put("id_stu", id_stu);
db.beginTransaction();
for (Picto picto : vocabulary) {
//Log.e(LOG_TAG,"inserting "+picto.get_id()+":"+picto.get_translation()+":"+picto.get_json_attrs());
Log.e("TAG_PRUEBAS","inserting "+picto.get_id()+":"+picto.get_translation()+":"+picto.get_json_attrs());
newsize++;
values.put("id_picto", picto.get_id());
values.put("id_scene", 135/*getActiveSceneForStudent(id_stu)*/);
values.put("url", picto.get_url());
values.put("translation",picto.get_translation());
values.put("attributes",picto.get_json_attrs());
......@@ -194,7 +240,7 @@ public class PCBDBHelper extends SQLiteOpenHelper {
}
int seconds2 = Calendar.getInstance().get(Calendar.SECOND);
Log.i(LOG_TAG, " Local student vocabulary updated, id:" + id_stu + ", cats: " + vocabulary.size() + " time:" + (seconds2 - seconds1) + " secs. Size: " + newsize + " read only?" + db.isReadOnly());
//Log.i(LOG_TAG, " Local student vocabulary updated, id:" + id_stu + ", cats: " + vocabulary.size() + " time:" + (seconds2 - seconds1) + " secs. Size: " + newsize + " read only?" + db.isReadOnly());
db.setTransactionSuccessful();
db.endTransaction();
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
......@@ -212,6 +258,7 @@ public class PCBDBHelper extends SQLiteOpenHelper {
ContentValues values=new ContentValues(6);
values.put("id_stu", id_stu);
values.put("id_picto", picto.get_id());
//values.put("id_scene",picto.get_scene());
values.put("url", picto.get_url());
values.put("translation",picto.get_translation());
values.put("attributes",picto.get_json_attrs());
......
......@@ -256,6 +256,16 @@ public class User {
}
/**
* Change the value wich knows if the user has categories or not
*/
public void set_has_categories(boolean categories){
try {
this.attributes_stu.put(JSON_STUDENT_ATTTRS.CATEGORIES, true);
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
*
* @return input feedback of the student configuration (default: "vibration")
*/
......
......@@ -175,7 +175,6 @@ public class Vocabulary implements Iterable<Picto> {
//final String picto_str = "/pictos";
final String picto_str = "/activeScene";
String operation = PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + picto_str;
Log.i("TAG_PRUEBAS","Ruta: "+operation);
PCBcontext.getRestapiWrapper().ask(operation, new RestapiWrapper.iRestapiListener() {
@Override
public void preExecute() {
......@@ -190,7 +189,7 @@ public class Vocabulary implements Iterable<Picto> {
@Override
public void result(JSONObject result) {
if (result != null) {
Log.i("TAG_PRUEBAS",result.toString());
//Log.i("TAG_PRUEBAS",result.toString());
/* final String jpicto = "picto";
final String jid = "id";
final String juri = "uri";
......@@ -199,8 +198,8 @@ public class Vocabulary implements Iterable<Picto> {
JSONObject picto, attributes;
JSONObject stupicto = null;
try {
PCBcontext.getPcbdb().getCurrentUser().set_has_categories(result.getBoolean("categories"));
JSONArray stu_pictos = result.getJSONArray("pictos"); //Obtengo el JSONArray de los pictos
Log.i("TAG_PRUEBAS",stu_pictos.toString());
Picto[] pictos = new Picto[stu_pictos.length()];
for (int i = 0; i < stu_pictos.length(); i++) {
......@@ -213,9 +212,11 @@ public class Vocabulary implements Iterable<Picto> {
attributes);
}
synchronizeImgs(pictos);
if (PCBcontext.is_user_logged())
//HASTA AQUI BIEN
if (PCBcontext.is_user_logged()) {
PCBcontext.getPcbdb().setStudentVocabulary(Vocabulary.this);
else
PCBcontext.getPcbdb().setActiveSceneForStudent(result); //Aqui inserto en scene los datos que llegan de la activa
}else
Log.i(this.getClass().getName(), "Downloaded images ended when the user comes to logout");
Log.i(this.getClass().getName(), " Pictos downloaded: " + result.length());
} catch (JSONException e) {
......@@ -255,19 +256,20 @@ public class Vocabulary implements Iterable<Picto> {
Vector<Img> imgs=new Vector<Img>(updated_collection.length);
this.pictos.clear();
for (Picto updated_picto: updated_collection) {
LinkedList<Picto> pictos_cat;
Log.i("TAG_PRUEBAS","ID: "+updated_picto.get_id());
//Log.i("TAG_PRUEBAS","URL: "+updated_picto.get_url());
Picto picto = new Picto(updated_picto.get_id(),
updated_picto.get_url(),
updated_picto.get_translation(),
updated_picto.get_json_attrs());
if (pictos.containsKey(picto.get_category())) {
pictos_cat = pictos.get(picto.get_category());
Log.i("TAG_PRUEBAS","Tiene categoria: "+pictos_cat);
}else {
pictos_cat = new LinkedList<>();
pictos.put(new Integer(picto.get_category()),pictos_cat);
pictos.put(picto.get_category(),pictos_cat);
}
pictos_cat.add(picto);
imgs.add(picto);
......
......@@ -21,13 +21,13 @@ public class VocabularyIterator implements Iterator<Picto> {
int location;
VocabularyIterator(Hashtable<Integer,LinkedList<Picto>> pictos) {
this.keys = pictos.keys();
if (pictos.size()>1) {
if (pictos.size()>=1) {
this.pictos=pictos;
this.category = this.keys.nextElement();
}
else {
this.category=-1;
this.category=-2;
this.pictos=new Hashtable<>(1);
this.pictos.put(this.category, new LinkedList<Picto>());
}
......@@ -47,7 +47,8 @@ public class VocabularyIterator implements Iterator<Picto> {
this.location = 0;
this.category = this.keys.nextElement();
}
picto=pictos.get(category).get(location++);
picto=pictos.get(category).get(location++);
return picto;
}
......
......@@ -68,14 +68,28 @@ constraint ck_users UNIQUE(id_stu,id_sup)
)
;--
DROP TABLE IF EXISTS scene
;--
CREATE TABLE scene (
id INTEGER PRIMARY KEY,
id_sup INTEGER NOT NULL REFERENCES supervisor,
id_stu INTEGER NOT NULL REFERENCES student,
name TEXT(100) NOT NULL,
active BOOLEAN NOT NULL,
categories BOOLEAN NOT NULL
)
;--
DROP TABLE IF EXISTS collection
;--
CREATE TABLE collection (
id_stu INTEGER NOT NULL REFERENCES student ON DELETE CASCADE,
id_picto INTEGER NOT NULL REFERENCES picto ON DELETE CASCADE,
id_scene INTEGER NOT NULL REFERENCES scene ON DELETE CASCADE,
attributes VARCHAR(1024),
constraint ck_collection UNIQUE(id_stu,id_picto,attributes)
constraint ck_collection UNIQUE(id_stu,id_picto,id_scene)
)
;--
......@@ -143,7 +157,7 @@ DROP VIEW IF EXISTS collection_detail
;--
CREATE VIEW collection_detail AS
SELECT id_stu, id_picto, b.url, translation,attributes
SELECT id_stu, id_picto, id_scene, b.url, translation, attributes
FROM collection a, picto b
WHERE a.id_picto=b.id
;--
......@@ -344,6 +358,6 @@ INSTEAD OF INSERT ON collection_detail
FOR EACH ROW
BEGIN
INSERT OR REPLACE INTO picto VALUES (NEW.id_picto,NEW.url, NEW.translation);
INSERT INTO collection VALUES (NEW.id_stu,NEW.id_picto,NEW.attributes);
INSERT INTO collection VALUES (NEW.id_stu,NEW.id_picto,NEW.id_scene,NEW.attributes);
END
;--
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