Fixed bug on test questions syncronization

This commit is contained in:
Juan Miguel Boyero Corral 2011-09-29 13:22:10 +02:00
parent e50253549d
commit 63bd6fd322
7 changed files with 147 additions and 78 deletions

View File

@ -95,7 +95,7 @@
momento.\n\n¿Desea abrir ahora la pantalla de configuración?</string>
<string name="initialDialogTitle">IMPORTANTE</string>
<string name="upgradeMsg">Debido a cambios en la base de datos todos los
datos de las notificaciones han sido borrados.\n\nDisculpe las
datos de las preguntas de test han sido borrados.\n\nDisculpe las
molestias.</string>
<string name="errorBadLoginMsg">Usuario o contraseña incorrectos</string>
<string name="errorServerResponseMsg">Error en respuesta del servidor</string>

View File

@ -93,7 +93,7 @@
menu anytime.\n\nDo you want to open the configuration screen now?
</string><string name="initialDialogTitle">IMPORTANT</string>
<string name="upgradeMsg">Due to changes in the database all notifications
<string name="upgradeMsg">Due to changes in the database all test questions
data have been deleted.\n\nSorry.</string>
<string name="errorBadLoginMsg">Incorrect user or password</string>
<string name="errorServerResponseMsg">Error in server response</string>

View File

@ -294,10 +294,13 @@ public class SWADMain extends ExpandableListActivity {
//If this is the first run, show configuration dialog
if(lastVersion == 0) {
showConfigurationDialog();
dbHelper.initializeDB();
prefs.setLastVersion(currentVersion);
//If this is an upgrade, show upgrade dialog
} else if(lastVersion < currentVersion) {
showUpgradeDialog();
dbHelper.upgradeDB(this);
prefs.setLastVersion(currentVersion);
}
} catch (Exception ex) {

View File

@ -150,7 +150,7 @@ public class DataBaseHelper {
if(t != null) {
o = new TestTag(id,
t.getQstCod(),
t.getQstCodList(),
ent.getString("tagTxt"),
ent.getInt("tagInd"));
} else {
@ -163,9 +163,11 @@ public class DataBaseHelper {
ent.getInt("max"),
ent.getString("feedback"),
ent.getLong("editTime"));
} else if(table.equals(Global.DB_TABLE_TEST_QUESTION_TAGS)) {
} else if(table.equals(Global.DB_TABLE_TEST_QUESTION_TAGS)) {
ArrayList<Integer> l = new ArrayList<Integer>();
l.add(ent.getInt("qstCod"));
o = new TestTag(ent.getInt("tagCod"),
ent.getInt("qstCod"),
l,
null,
ent.getInt("tagInd"));
}
@ -325,6 +327,7 @@ public class DataBaseHelper {
public void insertTestTag(TestTag t)
{
List<Entity> rows = db.getEntityList(Global.DB_TABLE_TEST_TAGS, "id = " + t.getId());
if(rows.isEmpty()) {
Entity ent = new Entity(Global.DB_TABLE_TEST_TAGS);
@ -332,11 +335,13 @@ public class DataBaseHelper {
ent.setValue("tagTxt", t.getTagTxt());
ent.save();
ent = new Entity(Global.DB_TABLE_TEST_QUESTION_TAGS);
ent.setValue("qstCod", t.getQstCod());
ent.setValue("tagCod", t.getId());
ent.setValue("tagInd", t.getTagInd());
ent.save();
for(Integer i : t.getQstCodList()) {
ent = new Entity(Global.DB_TABLE_TEST_QUESTION_TAGS);
ent.setValue("qstCod", i);
ent.setValue("tagCod", t.getId());
ent.setValue("tagInd", t.getTagInd());
ent.save();
}
} else {
throw new SQLException();
}
@ -474,20 +479,18 @@ public class DataBaseHelper {
{
List<Entity> rows = db.getEntityList(Global.DB_TABLE_TEST_TAGS, "id = " + prev.getId());
Entity ent = rows.get(0);
List<Integer> qstCodList = actual.getQstCodList();
ent.setValue("id", actual.getId());
ent.setValue("tagTxt", actual.getTagTxt());
ent.save();
rows = db.getEntityList(Global.DB_TABLE_TEST_QUESTION_TAGS, "tagCod = " + prev.getId());
Iterator<Entity> iter = rows.iterator();
while (iter.hasNext()) {
ent = iter.next();
ent.setValue("tagCod", actual.getId());
ent.setValue("qstCod", actual.getQstCod());
ent.setValue("tagInd", actual.getTagInd());
ent.save();
for(Integer i : qstCodList) {
db.getDB().execSQL("INSERT OR IGNORE INTO " + Global.DB_TABLE_TEST_QUESTION_TAGS + " VALUES (NULL,"
+ i + "," + actual.getId() + "," + actual.getTagInd() + ");");
}
}
/**
@ -540,13 +543,6 @@ public class DataBaseHelper {
List<Entity> rows = db.getEntityList(table, "id = " + id);
Entity ent = rows.get(0);
ent.delete();
/*rows = db.getEntityList(Global.DB_TABLE_NOTICES_COURSES, "idcourse = " + id);
Iterator<Entity> iter = rows.iterator();
while (iter.hasNext()) {
ent = iter.next();
ent.delete();
}*/
}
/**
@ -630,14 +626,27 @@ public class DataBaseHelper {
String orderby = " GROUP BY T.id ORDER BY Q.tagInd ASC";
Cursor dbCursor = db.getDB().rawQuery(select + tables + where + orderby, null);
List<TestTag> result = new ArrayList<TestTag>();
List<Integer> qstCodList;
int idOld = -1;
TestTag t = null;
while(dbCursor.moveToNext()) {
while(dbCursor.moveToNext()) {
int id = dbCursor.getInt(0);
String tagTxt = dbCursor.getString(1);
int qstCod = dbCursor.getInt(2);
int tagInd = dbCursor.getInt(3);
result.add(new TestTag(id, qstCod, tagTxt, tagInd));
if(id != idOld) {
qstCodList = new ArrayList<Integer>();
String tagTxt = dbCursor.getString(1);
qstCodList.add(dbCursor.getInt(2));
int tagInd = dbCursor.getInt(3);
t = new TestTag(id, qstCodList, tagTxt, tagInd);
result.add(t);
idOld = id;
} else {
t.addQstCod(dbCursor.getInt(2));
}
}
return result;
@ -656,7 +665,7 @@ public class DataBaseHelper {
String tables = " FROM " + Global.DB_TABLE_TEST_QUESTIONS + " AS Q, "
+ Global.DB_TABLE_TEST_QUESTIONS_COURSE + " AS C, "
+ Global.DB_TABLE_TEST_QUESTION_TAGS + " AS T";
String where = " WHERE Q.id=C.qstCod AND C.crsCod=" + selectedCourseCode;
String where = " WHERE Q.id=C.qstCod AND Q.id=T.qstCod AND C.crsCod=" + selectedCourseCode;
String orderby = " ORDER BY RANDOM()";
String limit = " LIMIT " + maxQuestions;
Cursor dbCursorQuestions, dbCursorAnswers;
@ -666,13 +675,14 @@ public class DataBaseHelper {
int answerTypesListSize = answerTypesList.size();
if(!tagsList.get(0).getTagTxt().equals("all")) {
where += " AND ";
where += " AND (";
for(int i=0; i<tagsListSize; i++) {
where += "T.tagCod=" + tagsList.get(i).getId();
if(i < tagsListSize-1) {
where += " OR ";
}
}
where += ")";
if(!answerTypesList.get(0).equals("all")) {
where += " AND ";
@ -773,13 +783,52 @@ public class DataBaseHelper {
db.deleteTables();
}
/**
* Begin a database transaction
*/
public void beginTransaction() {
db.getDB().execSQL("BEGIN;");
}
/**
* End a database transaction
*/
public void endTransaction() {
db.getDB().execSQL("END;");
}
/**
* Compact the database
*/
public void compactDB() {
db.getDB().execSQL("VACUUM;");
}
/**
* Initializes the database structure for the first use
*/
public void initializeDB() {
db.getDB().execSQL("CREATE UNIQUE INDEX " + Global.DB_TABLE_TEST_QUESTION_TAGS + "_unique on "
+ Global.DB_TABLE_TEST_QUESTION_TAGS + "(qstCod, tagCod);");
}
/**
* Upgrades the database structure
* @throws IOException
* @throws XmlPullParserException
*/
public void upgradeDB(Context context) throws XmlPullParserException, IOException {
db.getDB().execSQL("CREATE TEMPORARY TABLE __"
emptyTable(Global.DB_TABLE_TEST_QUESTION_ANSWERS);
emptyTable(Global.DB_TABLE_TEST_QUESTION_TAGS);
emptyTable(Global.DB_TABLE_TEST_QUESTIONS_COURSE);
emptyTable(Global.DB_TABLE_TEST_ANSWERS);
emptyTable(Global.DB_TABLE_TEST_CONFIG);
emptyTable(Global.DB_TABLE_TEST_QUESTIONS);
emptyTable(Global.DB_TABLE_TEST_TAGS);
initializeDB();
compactDB();
/*db.getDB().execSQL("CREATE TEMPORARY TABLE __"
+ Global.DB_TABLE_NOTIFICATIONS
+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER, eventType TEXT, eventTime TEXT,"
+ " userSurname1 TEXT, userSurname2 TEXT, userFirstname TEXT, location TEXT, summary TEXT,"
@ -803,6 +852,6 @@ public class DataBaseHelper {
db.getDB().execSQL("INSERT INTO " + Global.DB_TABLE_NOTIFICATIONS + " SELECT _id, id, eventType, eventTime, "
+ "userSurname1, userSurname2, userFirstname, location, summary, status, content FROM __"
+ Global.DB_TABLE_NOTIFICATIONS + ";"
+ "DROP TABLE __" + Global.DB_TABLE_NOTIFICATIONS + ";");
+ "DROP TABLE __" + Global.DB_TABLE_NOTIFICATIONS + ";");*/
}
}

View File

@ -18,7 +18,9 @@
*/
package es.ugr.swad.swadroid.model;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import org.ksoap2.serialization.PropertyInfo;
@ -28,9 +30,9 @@ import org.ksoap2.serialization.PropertyInfo;
*/
public class TestTag extends Model {
/**
* Question code;
* Question codes;
*/
private int qstCod;
private List<Integer> qstCodList;
/**
* Tag text
*/
@ -69,9 +71,9 @@ public class TestTag extends Model {
* @param tagTxt Tag text
* @param tagInd Tag index
*/
public TestTag(long id, int qstCod, String tagTxt, int tagInd) {
public TestTag(long id, List<Integer> qstCodList, String tagTxt, int tagInd) {
super(id);
this.qstCod = qstCod;
this.qstCodList = qstCodList;
this.tagTxt = tagTxt;
this.tagInd = tagInd;
}
@ -92,22 +94,6 @@ public class TestTag extends Model {
this.tagTxt = tagTxt;
}
/**
* Gets question code
* @return Question code
*/
public int getQstCod() {
return qstCod;
}
/**
* Sets question code
* @param qstCod question code
*/
public void setQstCod(int qstCod) {
this.qstCod = qstCod;
}
/**
* Gets tag index
* @return Tag index
@ -124,17 +110,10 @@ public class TestTag extends Model {
this.tagInd = tagInd;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + qstCod;
result = prime * result + tagInd;
result = prime * result + ((tagTxt == null) ? 0 : tagTxt.hashCode());
return result;
public String toString() {
return "TestTag [qstCodList=" + qstCodList + ", tagTxt=" + tagTxt
+ ", tagInd=" + tagInd + "]";
}
/* (non-Javadoc)
@ -151,15 +130,6 @@ public class TestTag extends Model {
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TestTag [qstCod=" + qstCod + ", tagTxt=" + tagTxt + ", tagInd="
+ tagInd + ", getId()=" + getId() + "]";
}
/* (non-Javadoc)
* @see org.ksoap2.serialization.KvmSerializable#getProperty(int)
*/
@ -214,4 +184,40 @@ public class TestTag extends Model {
}
}
/**
* Gets question codes
* @return Question codes
*/
public List<Integer> getQstCodList() {
return qstCodList;
}
/**
* Sets question codes
* @param qstCodList Question codes
*/
public void setQstCodList(List<Integer> qstCodList) {
this.qstCodList = qstCodList;
}
/**
* Gets the question code in position i
* @param i Position of question code
* @return Question code
*/
public Integer getQstCod(int i) {
return this.qstCodList.get(i);
}
/**
* Adds a question code to the list
* @param qstCod Question code to be added
*/
public void addQstCod(Integer qstCod) {
if(this.qstCodList == null) {
this.qstCodList = new ArrayList<Integer>();
}
this.qstCodList.add(qstCod);
}
}

View File

@ -205,7 +205,7 @@ public class TestsMake extends Module {
//If "All tags" item checked, add the whole list to the list of selected tags
if(checkedItems.get(0, false)) {
tagsList.add(new TestTag(0, 0, "all", 0));
tagsList.add(new TestTag(0, null, "all", 0));
//If "All tags" item not checked, add the selected items to the list of selected tags
} else {

View File

@ -114,7 +114,7 @@ public class TestsQuestionsDownload extends Module {
SoapObject pii = (SoapObject)tagsListObject.getProperty(i);
Integer tagCod = new Integer(pii.getProperty("tagCode").toString());
String tagTxt = pii.getProperty("tagText").toString();
TestTag tag = new TestTag(tagCod, 0, tagTxt, 0);
TestTag tag = new TestTag(tagCod, null, tagTxt, 0);
tagsList.add(tag);
if(isDebuggable)
@ -124,6 +124,8 @@ public class TestsQuestionsDownload extends Module {
Log.i(TAG, "Retrieved " + listSize + " tags");
//Read questions info from webservice response
dbHelper.beginTransaction();
listSize = questionsListObject.getPropertyCount();
for (int i = 0; i < listSize; i++) {
SoapObject pii = (SoapObject)questionsListObject.getProperty(i);
@ -150,9 +152,12 @@ public class TestsQuestionsDownload extends Module {
}
}
dbHelper.endTransaction();
Log.i(TAG, "Retrieved " + listSize + " questions");
//Read answers info from webservice response
dbHelper.beginTransaction();
listSize = answersListObject.getPropertyCount();
for (int i = 0; i < listSize; i++) {
SoapObject pii = (SoapObject)answersListObject.getProperty(i);
@ -179,9 +184,12 @@ public class TestsQuestionsDownload extends Module {
}
}
dbHelper.endTransaction();
Log.i(TAG, "Retrieved " + listSize + " answers");
//Read relationships between questions and tags from webservice response
dbHelper.beginTransaction();
listSize = questionTagsListObject.getPropertyCount();
for (int i = 0; i < listSize; i++) {
SoapObject pii = (SoapObject)questionTagsListObject.getProperty(i);
@ -189,7 +197,7 @@ public class TestsQuestionsDownload extends Module {
Integer tagCod = new Integer(pii.getProperty("tagCode").toString());
Integer tagIndex = new Integer(pii.getProperty("tagIndex").toString());
TestTag tag = tagsList.get(tagsList.indexOf(new TestTag(tagCod, "", 0)));
tag.setQstCod(qstCod);
tag.addQstCod(qstCod);
tag.setTagInd(tagIndex);
//If it's a new tag, insert in database
@ -203,13 +211,16 @@ public class TestsQuestionsDownload extends Module {
//If it's an updated tag, update it's rows in database
} catch (SQLException e) {
TestTag old = (TestTag) tagsListDB.get(tagsListDB.indexOf(tag));
tag.setQstCodList(old.getQstCodList());
tag.addQstCod(qstCod);
dbHelper.updateTestTag(old, tag);
if(isDebuggable)
Log.d(TAG, "UPDATED: " + tag.toString());
}
}
dbHelper.endTransaction();
Log.i(TAG, "Retrieved " + listSize + " relationships between questions and tags");
//Update last time test was updated