Reimplemented database stuff
git-svn-id: https://forja.rediris.es/svn/cusl6-swadroid/trunk@62 5bc14d19-1e4b-4ba2-aa50-860af135f48c
This commit is contained in:
parent
dd194298dc
commit
c09e97a57c
Binary file not shown.
|
@ -55,5 +55,6 @@ public final class R {
|
|||
}
|
||||
public static final class xml {
|
||||
public static final int preferences=0x7f040000;
|
||||
public static final int tables=0x7f040001;
|
||||
}
|
||||
}
|
||||
|
|
BIN
SWADroid/libs/com.android.DataFramework.jar
Normal file
BIN
SWADroid/libs/com.android.DataFramework.jar
Normal file
Binary file not shown.
19
SWADroid/res/xml/tables.xml
Normal file
19
SWADroid/res/xml/tables.xml
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<database name="swadroid_db" version="1">
|
||||
<table name="courses" to-string="%name%">
|
||||
<field name="id" obligatory="true" type="integer"/>
|
||||
<field name="name" obligatory="true" type="text"/>
|
||||
</table>
|
||||
|
||||
<table name="notices" to-string="%description%">
|
||||
<field name="id" obligatory="true" type="integer"/>
|
||||
<field name="description" obligatory="true" type="text"/>
|
||||
<field name="timestamp" obligatory="true" type="integer"/>
|
||||
</table>
|
||||
|
||||
<table name="notices_courses" to-string="%description%">
|
||||
<field name="idcourse" type="foreign-key" foreign-table="courses" />
|
||||
<field name="idnotice" type="foreign-key" foreign-table="notices" />
|
||||
</table>
|
||||
|
||||
</database>
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
package es.ugr.swad.swadroid;
|
||||
|
||||
import com.android.dataframework.DataFramework;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Intent;
|
||||
|
@ -28,6 +30,7 @@ import android.view.Menu;
|
|||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import es.ugr.swad.swadroid.model.DataBaseHelper;
|
||||
import es.ugr.swad.swadroid.modules.Login;
|
||||
import es.ugr.swad.swadroid.ssl.SecureConnection;
|
||||
|
||||
|
@ -41,6 +44,16 @@ public class SWADMain extends Activity {
|
|||
*/
|
||||
protected static Preferences prefs = new Preferences();
|
||||
|
||||
/**
|
||||
* Database Helper.
|
||||
*/
|
||||
protected static DataBaseHelper dbHelper;
|
||||
|
||||
/**
|
||||
* Database Framework.
|
||||
*/
|
||||
protected static DataFramework db;
|
||||
|
||||
/**
|
||||
* Shows an error message.
|
||||
* @param message Error message to show.
|
||||
|
@ -136,6 +149,10 @@ public class SWADMain extends Activity {
|
|||
setContentView(R.layout.main);
|
||||
w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.swadroid);
|
||||
|
||||
db = DataFramework.getInstance();
|
||||
db.open(this, this.getPackageName());
|
||||
dbHelper = new DataBaseHelper(db);
|
||||
|
||||
prefs.getPreferences(getBaseContext());
|
||||
SecureConnection.initSecureConnection();
|
||||
} catch (Exception ex) {
|
||||
|
@ -144,4 +161,13 @@ public class SWADMain extends Activity {
|
|||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see android.app.Activity#onDestroy()
|
||||
*/
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
dbHelper.close();
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,4 +53,43 @@ public class Course extends Model {
|
|||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = super.hashCode();
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (!super.equals(obj))
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Course other = (Course) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Course [name=" + name + "]";
|
||||
}
|
||||
}
|
|
@ -18,169 +18,30 @@
|
|||
*/
|
||||
package es.ugr.swad.swadroid.model;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import es.ugr.swad.swadroid.Global;
|
||||
import es.ugr.swad.swadroid.R;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.SQLException;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.util.Log;
|
||||
|
||||
public class DataBaseHelper extends SQLiteOpenHelper {
|
||||
//The Android's default system path of your application database.
|
||||
private static String DB_PATH = "/data/data/es.ugr.swad.swadroid/databases/";
|
||||
private static String DB_NAME = "swadroid";
|
||||
private static final int DATABASE_VERSION = 1;
|
||||
private static final String TAG = "SWADroidDB";
|
||||
private SQLiteDatabase swadroidDataBase;
|
||||
private final Context context;
|
||||
import com.android.dataframework.DataFramework;
|
||||
import com.android.dataframework.Entity;
|
||||
|
||||
import es.ugr.swad.swadroid.Global;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
|
||||
* @param context
|
||||
* @author Juan Miguel Boyero Corral <juanmi1982@gmail.com>
|
||||
*
|
||||
*/
|
||||
public DataBaseHelper(Context context) {
|
||||
super(context, DB_NAME, null, DATABASE_VERSION);
|
||||
this.context = context;
|
||||
public class DataBaseHelper {
|
||||
private DataFramework db;
|
||||
|
||||
public DataBaseHelper(DataFramework database) {
|
||||
db = database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a empty database on the system and rewrites it with your own database.
|
||||
* @throws IOException
|
||||
*/
|
||||
public void createDataBase() throws IOException{
|
||||
boolean dbExist = checkDataBase();
|
||||
if(dbExist){
|
||||
//do nothing - database already exist
|
||||
Log.d("createDataBase", "Database already exists.");
|
||||
}else{
|
||||
//By calling this method and empty database will be created into the default system path
|
||||
//of your application so we are gonna be able to overwrite that database with our database.
|
||||
this.getReadableDatabase();
|
||||
try {
|
||||
Log.d("createDataBase", "Creating database...");
|
||||
copyDataBase();
|
||||
Log.d("createDataBase", "Database created successfully.");
|
||||
} catch (IOException e) {
|
||||
throw new Error(context.getString(R.string.errorCopyMsg_DB));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the database already exist to avoid re-copying the file each time you open the application.
|
||||
* @return true if it exists, false if it doesn't
|
||||
* @throws IOException
|
||||
*/
|
||||
private boolean checkDataBase() throws IOException{
|
||||
SQLiteDatabase checkDB = null;
|
||||
try{
|
||||
Log.d("checkDataBase", "Checking database...");
|
||||
String myPath = DB_PATH + DB_NAME;
|
||||
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
|
||||
Log.d("checkDataBase", "Database already exists.");
|
||||
}catch(SQLiteException e){
|
||||
//database doesn't exist yet.
|
||||
Log.d("checkDataBase", "Database doesn't exist yet.");
|
||||
createDataBase();
|
||||
}
|
||||
|
||||
if(checkDB != null){
|
||||
checkDB.close();
|
||||
}
|
||||
return checkDB != null ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies your database from your local assets-folder to the just created empty database in the
|
||||
* system folder, from where it can be accessed and handled.
|
||||
* This is done by transferring bytestream.
|
||||
* @throws IOException
|
||||
**/
|
||||
private void copyDataBase() throws IOException{
|
||||
Log.d("copyDataBase", "Copying database to destination...");
|
||||
|
||||
//Open your local db as the input stream
|
||||
InputStream myInput = context.getAssets().open(DB_NAME);
|
||||
// Path to the just created empty db
|
||||
String outFileName = DB_PATH + DB_NAME;
|
||||
//Open the empty db as the output stream
|
||||
OutputStream myOutput = new FileOutputStream(outFileName);
|
||||
|
||||
//transfer bytes from the inputfile to the outputfile
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = myInput.read(buffer))>0){
|
||||
myOutput.write(buffer, 0, length);
|
||||
}
|
||||
|
||||
//Close the streams
|
||||
myOutput.flush();
|
||||
myOutput.close();
|
||||
myInput.close();
|
||||
|
||||
Log.d("copyDataBase", "Database successfully copied.");
|
||||
}
|
||||
|
||||
public void openDataBase() throws SQLException, IOException {
|
||||
Log.d("openDataBase", "Opening database...");
|
||||
|
||||
checkDataBase();
|
||||
|
||||
//Open the database
|
||||
String myPath = DB_PATH + DB_NAME;
|
||||
swadroidDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
|
||||
|
||||
Log.d("openDataBase", "Database successfully opened.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if(swadroidDataBase != null)
|
||||
swadroidDataBase.close();
|
||||
super.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
|
||||
Log.w(TAG, context.getString(R.string.upgradeMsg_DB));
|
||||
//db.execSQL("");
|
||||
try {
|
||||
copyDataBase();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, context.getString(R.string.errorCopyMsg_DB));
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Add your public helper methods to access and get content from the database.
|
||||
// You could return cursors by doing "return swadroidDataBase.query(....)" so it'd be easy
|
||||
// to you to create adapters for your views.
|
||||
|
||||
/**
|
||||
* Gets all rows of specified table
|
||||
* @param table Table containing the rows
|
||||
* @return A cursor pointing to the rows
|
||||
*/
|
||||
public Cursor getCursorAllRows(String table)
|
||||
{
|
||||
String query = "SELECT * FROM " + table;
|
||||
return swadroidDataBase.rawQuery(query, null);
|
||||
db.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -219,76 +80,99 @@ public class DataBaseHelper extends SQLiteOpenHelper {
|
|||
return b ? "Y" : "N";
|
||||
}
|
||||
|
||||
private Pair<String, String> selectParamsPairTable(String table) {
|
||||
String firstParam = null;
|
||||
String secondParam = null;
|
||||
|
||||
if(table.equals(Global.DB_TABLE_NOTICES_COURSES)) {
|
||||
firstParam = "idcourse";
|
||||
secondParam = "idnotice";
|
||||
} else if(table.equals(Global.DB_TABLE_STUDENTS_COURSES)) {
|
||||
firstParam = "idcourse";
|
||||
secondParam = "idstudent";
|
||||
} else if(table.equals(Global.DB_TABLE_TEST_QUESTIONS_COURSES)) {
|
||||
firstParam = "crscod";
|
||||
secondParam = "qstcod";
|
||||
} else {
|
||||
Log.e("selectParamsPairTable", "Table " + table + " not exists");
|
||||
}
|
||||
|
||||
return new Pair<String, String>(firstParam, secondParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Model's subclass object looking at the table selected
|
||||
* @param table Table selected
|
||||
* @param rows Cursor to the table rows
|
||||
* @param ent Cursor to the table rows
|
||||
* @return A Model's subclass object
|
||||
*/
|
||||
private Model createObjectByTable(String table, Cursor rows) {
|
||||
private Model createObjectByTable(String table, Entity ent) {
|
||||
Model o = null;
|
||||
Pair<String, String> params;
|
||||
|
||||
if(table.equals(Global.DB_TABLE_COURSES)) {
|
||||
o = new Course((Integer) rows.getInt(0),
|
||||
(String) rows.getString(1));
|
||||
o = new Course(ent.getInt("id"),
|
||||
ent.getString("name"));
|
||||
} else if(table.equals(Global.DB_TABLE_NOTICES)) {
|
||||
o = new Notice((Integer) rows.getInt(0),
|
||||
(Integer) rows.getInt(1),
|
||||
(String) rows.getString(2));
|
||||
} else if(table.equals(Global.DB_TABLE_STUDENTS)) {
|
||||
o = new Student((Integer) rows.getInt(0),
|
||||
(String) rows.getString(1),
|
||||
(String) rows.getString(2),
|
||||
(String) rows.getString(3),
|
||||
(String) rows.getString(4));
|
||||
o = new Notice(ent.getInt("id"),
|
||||
ent.getInt("timestamp"),
|
||||
ent.getString("description"));
|
||||
/*} else if(table.equals(Global.DB_TABLE_STUDENTS)) {
|
||||
o = new Student((Integer) ent.getInt(0),
|
||||
(String) ent.getString(1),
|
||||
(String) ent.getString(2),
|
||||
(String) ent.getString(3),
|
||||
(String) ent.getString(4));
|
||||
} else if(table.equals(Global.DB_TABLE_TEST_ANSWERS)) {
|
||||
o = new TestAnswer((Integer) rows.getInt(0),
|
||||
(Boolean) parseIntBool(rows.getInt(1)),
|
||||
(String) rows.getString(2));
|
||||
o = new TestAnswer((Integer) ent.getInt(0),
|
||||
(Boolean) parseIntBool(ent.getInt(1)),
|
||||
(String) ent.getString(2));
|
||||
} else if(table.equals(Global.DB_TABLE_TEST_QUESTIONS)) {
|
||||
o = new TestQuestion((Integer) rows.getInt(0),
|
||||
(String) rows.getString(1),
|
||||
(String) rows.getString(2),
|
||||
(Integer) rows.getInt(3),
|
||||
(Boolean) parseStringBool(rows.getString(4)),
|
||||
(Float) rows.getFloat(5));
|
||||
o = new TestQuestion((Integer) ent.getInt(0),
|
||||
(String) ent.getString(1),
|
||||
(String) ent.getString(2),
|
||||
(Integer) ent.getInt(3),
|
||||
(Boolean) parseStringBool(ent.getString(4)),
|
||||
(Float) ent.getFloat(5));
|
||||
} else if(table.equals(Global.DB_TABLE_MSG_CONTENT)) {
|
||||
o = new MessageContent((Integer) rows.getInt(0),
|
||||
(String) rows.getString(1),
|
||||
(String) rows.getString(2),
|
||||
(Boolean) parseStringBool(rows.getString(3)),
|
||||
(Integer) rows.getInt(4));
|
||||
o = new MessageContent((Integer) ent.getInt(0),
|
||||
(String) ent.getString(1),
|
||||
(String) ent.getString(2),
|
||||
(Boolean) parseStringBool(ent.getString(3)),
|
||||
(Integer) ent.getInt(4));
|
||||
} else if(table.equals(Global.DB_TABLE_MSG_RCV)) {
|
||||
o = new MessageReceived((Integer) rows.getInt(0),
|
||||
(String) rows.getString(1),
|
||||
(String) rows.getString(2),
|
||||
(Integer) rows.getInt(3),
|
||||
(Boolean) parseStringBool(rows.getString(4)),
|
||||
(Boolean) parseStringBool(rows.getString(5)),
|
||||
(Boolean) parseStringBool(rows.getString(6)),
|
||||
(Boolean) parseStringBool(rows.getString(7)));
|
||||
o = new MessageReceived((Integer) ent.getInt(0),
|
||||
(String) ent.getString(1),
|
||||
(String) ent.getString(2),
|
||||
(Integer) ent.getInt(3),
|
||||
(Boolean) parseStringBool(ent.getString(4)),
|
||||
(Boolean) parseStringBool(ent.getString(5)),
|
||||
(Boolean) parseStringBool(ent.getString(6)),
|
||||
(Boolean) parseStringBool(ent.getString(7)));
|
||||
} else if(table.equals(Global.DB_TABLE_MSG_SNT)) {
|
||||
o = new MessageSent((Integer) rows.getInt(0),
|
||||
(String) rows.getString(1),
|
||||
(String) rows.getString(2),
|
||||
(Boolean) parseStringBool(rows.getString(3)),
|
||||
(Integer) rows.getInt(4),
|
||||
(Integer) rows.getInt(5),
|
||||
(String) rows.getString(6));
|
||||
o = new MessageSent((Integer) ent.getInt(0),
|
||||
(String) ent.getString(1),
|
||||
(String) ent.getString(2),
|
||||
(Boolean) parseStringBool(ent.getString(3)),
|
||||
(Integer) ent.getInt(4),
|
||||
(Integer) ent.getInt(5),
|
||||
(String) ent.getString(6));
|
||||
} else if(table.equals(Global.DB_TABLE_MARKS)) {
|
||||
o = new Mark((Integer) rows.getInt(0),
|
||||
(Integer) rows.getInt(1),
|
||||
(Integer) rows.getInt(2),
|
||||
(String) rows.getString(3),
|
||||
(Integer) rows.getInt(4),
|
||||
(Integer) rows.getInt(5));
|
||||
o = new Mark((Integer) ent.getInt(0),
|
||||
(Integer) ent.getInt(1),
|
||||
(Integer) ent.getInt(2),
|
||||
(String) ent.getString(3),
|
||||
(Integer) ent.getInt(4),
|
||||
(Integer) ent.getInt(5));*/
|
||||
} else if(table.equals(Global.DB_TABLE_NOTICES_COURSES) ||
|
||||
table.equals(Global.DB_TABLE_STUDENTS_COURSES) ||
|
||||
table.equals(Global.DB_TABLE_TEST_QUESTIONS_COURSES)) {
|
||||
|
||||
params = selectParamsPairTable(table);
|
||||
|
||||
o = new PairTable<Integer, Integer>(table,
|
||||
(Integer) rows.getInt(0),
|
||||
(Integer) rows.getInt(1));
|
||||
ent.getInt(params.getFirst()),
|
||||
ent.getInt(params.getSecond()));
|
||||
}
|
||||
|
||||
return o;
|
||||
|
@ -302,14 +186,14 @@ public class DataBaseHelper extends SQLiteOpenHelper {
|
|||
public List<Model> getAllRows(String table)
|
||||
{
|
||||
List<Model> result = new ArrayList<Model>();
|
||||
Cursor rows = getCursorAllRows(table);
|
||||
Model row = null;
|
||||
List<Entity> rows = db.getEntityList(table);
|
||||
Model row;
|
||||
|
||||
if(rows.moveToFirst()) {
|
||||
do {
|
||||
row = createObjectByTable(table, rows);
|
||||
Iterator<Entity> iter = rows.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Entity ent = iter.next();
|
||||
row = createObjectByTable(table, ent);
|
||||
result.add(row);
|
||||
} while (rows.moveToNext());
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -321,14 +205,10 @@ public class DataBaseHelper extends SQLiteOpenHelper {
|
|||
*/
|
||||
public void insertCourse(Course c)
|
||||
{
|
||||
String command = "INSERT INTO "
|
||||
+ Global.DB_TABLE_COURSES
|
||||
+ " (_id, name) VALUES ("
|
||||
+ c.getId() + ", \'"
|
||||
+ c.getName()
|
||||
+ "\')";
|
||||
Log.d("DB", command);
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
Entity ent = new Entity(Global.DB_TABLE_COURSES);
|
||||
ent.setValue("id", c.getId());
|
||||
ent.setValue("name", c.getName());
|
||||
ent.save();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -337,146 +217,11 @@ public class DataBaseHelper extends SQLiteOpenHelper {
|
|||
*/
|
||||
public void insertNotice(Notice n)
|
||||
{
|
||||
String command = "INSERT INTO "
|
||||
+ Global.DB_TABLE_NOTICES
|
||||
+ " (_id, timestamp, description) VALUES ("
|
||||
+ n.getId() + ", "
|
||||
+ n.getTimestamp() + ", \'"
|
||||
+ n.getDescription()
|
||||
+ "\')";
|
||||
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a student in database
|
||||
* @param s Student to be inserted
|
||||
*/
|
||||
public void insertStudent(Student s)
|
||||
{
|
||||
String command = "INSERT INTO "
|
||||
+ Global.DB_TABLE_STUDENTS
|
||||
+ " (_id, dni, firstname, surname1, surname2) VALUES ("
|
||||
+ s.getId() + ", "
|
||||
+ s.getDni() + ", \'"
|
||||
+ s.getFirstName() + "\', \'"
|
||||
+ s.getSurname1() + "\', \'"
|
||||
+ s.getSurname2()
|
||||
+ "\')";
|
||||
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a test answer in database
|
||||
* @param a test answer to be inserted
|
||||
*/
|
||||
public void insertTestAnswer(TestAnswer a)
|
||||
{
|
||||
String command = "INSERT INTO "
|
||||
+ Global.DB_TABLE_TEST_ANSWERS
|
||||
+ " (_id, answer, correct) VALUES ("
|
||||
+ a.getId() + ", \'"
|
||||
+ a.getAnswer() + "\', "
|
||||
+ parseBoolInt(a.getCorrect())
|
||||
+ ")";
|
||||
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a test question in database
|
||||
* @param q test question to be inserted
|
||||
*/
|
||||
public void insertTestQuestion(TestQuestion q)
|
||||
{
|
||||
String command = "INSERT INTO "
|
||||
+ Global.DB_TABLE_TEST_QUESTIONS
|
||||
+ " (_id, anstype, numhits, question, score, shuffle) VALUES ("
|
||||
+ q.getId() + ", "
|
||||
+ q.getAnstype() + ", "
|
||||
+ q.getNumhits() + ", \'"
|
||||
+ q.getQuestion() + "\', "
|
||||
+ q.getScore() + ", "
|
||||
+ parseBoolString(q.getShuffle())
|
||||
+ ")";
|
||||
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a message in database
|
||||
* @param m Message to be inserted
|
||||
*/
|
||||
public void insertMessageContent(MessageContent m)
|
||||
{
|
||||
String command = "INSERT INTO "
|
||||
+ Global.DB_TABLE_MSG_CONTENT
|
||||
+ " (msgcod, subject, content) VALUES ("
|
||||
+ m.getId() + ", \'"
|
||||
+ m.getSubject() + "\', \'"
|
||||
+ m.getContent()
|
||||
+ "\')";
|
||||
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a received message in database
|
||||
* @param m Message to be inserted
|
||||
*/
|
||||
public void insertMessageReceived(MessageReceived m)
|
||||
{
|
||||
String command = "INSERT INTO "
|
||||
+ Global.DB_TABLE_MSG_RCV
|
||||
+ " (msgcod, usrcod, notified, open, replied, expanded) VALUES ("
|
||||
+ m.getId() + ", "
|
||||
+ m.getUsrcod() + ", "
|
||||
+ parseBoolString(m.isNotified()) + ", "
|
||||
+ parseBoolString(m.isOpen()) + ", "
|
||||
+ parseBoolString(m.isReplied()) + ", "
|
||||
+ parseBoolString(m.isExpanded())
|
||||
+ ")";
|
||||
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a sent message in database
|
||||
* @param m Message to be inserted
|
||||
*/
|
||||
public void insertMessageSent(MessageSent m)
|
||||
{
|
||||
String command = "INSERT INTO "
|
||||
+ Global.DB_TABLE_MSG_SNT
|
||||
+ " (msgcod, crscod, usrcod, creattime, expanded) VALUES ("
|
||||
+ m.getId() + ", "
|
||||
+ m.getCrscod() + ", "
|
||||
+ m.getUsrcod() + ", "
|
||||
+ m.getCreattime() + ", "
|
||||
+ parseBoolString(m.isExpanded())
|
||||
+ ")";
|
||||
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a mark in database
|
||||
* @param m Mark to be inserted
|
||||
*/
|
||||
public void insertMark(Mark m)
|
||||
{
|
||||
String command = "INSERT INTO "
|
||||
+ Global.DB_TABLE_MARKS
|
||||
+ " (crscod, grpcod, path, header, footer) VALUES ("
|
||||
+ m.getCrscod() + ", "
|
||||
+ m.getGrpcod() + ", "
|
||||
+ m.getPath() + ", "
|
||||
+ m.getHeader() + ", "
|
||||
+ m.getFooter()
|
||||
+ ")";
|
||||
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
Entity ent = new Entity(Global.DB_TABLE_NOTICES);
|
||||
ent.setValue("id", n.getId());
|
||||
ent.setValue("timestamp", n.getTimestamp());
|
||||
ent.setValue("description", n.getDescription());
|
||||
ent.save();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -485,22 +230,121 @@ public class DataBaseHelper extends SQLiteOpenHelper {
|
|||
*/
|
||||
public void insertPairTable(PairTable<?, ?> p)
|
||||
{
|
||||
String command = "INSERT INTO ";
|
||||
String table = p.getTable();
|
||||
command += table + " ";
|
||||
Pair<String, String> params = selectParamsPairTable(table);
|
||||
|
||||
if(table.equals(Global.DB_TABLE_NOTICES_COURSES)) {
|
||||
command += " (idcourse, idnotice) VALUES (";
|
||||
} else if(table.equals(Global.DB_TABLE_STUDENTS_COURSES)) {
|
||||
command += " (idcourse, idstudent) VALUES (";
|
||||
} else if(table.equals(Global.DB_TABLE_TEST_QUESTIONS_COURSES)) {
|
||||
command += " (crscod, qstcod) VALUES (";
|
||||
Entity ent = new Entity(table);
|
||||
ent.setValue(params.getFirst(), p.getFirst());
|
||||
ent.setValue(params.getSecond(), p.getSecond());
|
||||
ent.save();
|
||||
}
|
||||
|
||||
command += p.getFirst() + ", "
|
||||
+ p.getSecond()
|
||||
+ ")";
|
||||
/**
|
||||
* Updates a course in database
|
||||
* @param prev Course to be updated
|
||||
* @param actual Updated course
|
||||
*/
|
||||
public void updateCourse(Course prev, Course actual)
|
||||
{
|
||||
List<Entity> rows = db.getEntityList(Global.DB_TABLE_COURSES, "id = " + prev.getId());
|
||||
Entity ent = rows.get(0);
|
||||
ent.setValue("id", actual.getId());
|
||||
ent.setValue("name", actual.getName());
|
||||
ent.save();
|
||||
}
|
||||
|
||||
swadroidDataBase.execSQL(command, null);
|
||||
/**
|
||||
* Updates a notice in database
|
||||
* @param prev Notice to be updated
|
||||
* @param actual Updated notice
|
||||
*/
|
||||
public void updateNotice(Notice prev, Notice actual)
|
||||
{
|
||||
List<Entity> rows = db.getEntityList(Global.DB_TABLE_NOTICES, "id = " + prev.getId());
|
||||
Entity ent = rows.get(0);
|
||||
ent.setValue("id", actual.getId());
|
||||
ent.setValue("timestamp", actual.getTimestamp());
|
||||
ent.setValue("description", actual.getDescription());
|
||||
ent.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a relation in database
|
||||
* @param prev Relation to be updated
|
||||
* @param actual Updated relation
|
||||
*/
|
||||
public void updatePairTable(PairTable<?, ?> prev, PairTable<?, ?> actual)
|
||||
{
|
||||
String table = prev.getTable();
|
||||
String where;
|
||||
Integer first = (Integer) prev.getFirst();
|
||||
Integer second = (Integer) prev.getSecond();
|
||||
Pair<String, String> params = selectParamsPairTable(table);
|
||||
|
||||
where = params.getFirst() + " = " + first + " AND " + params.getSecond() + " = " + second;
|
||||
|
||||
List<Entity> rows = db.getEntityList(table, where);
|
||||
Entity ent = rows.get(0);
|
||||
ent.setValue(params.getFirst(), actual.getFirst());
|
||||
ent.setValue(params.getSecond(), actual.getSecond());
|
||||
ent.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a course from database
|
||||
* @param id Identifier of Course to be removed
|
||||
*/
|
||||
public void removeCourse(int id)
|
||||
{
|
||||
List<Entity> rows = db.getEntityList(Global.DB_TABLE_COURSES, "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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a notice from database
|
||||
* @param id Identifier of Notice to be removed
|
||||
*/
|
||||
public void removeNotice(int id)
|
||||
{
|
||||
List<Entity> rows = db.getEntityList(Global.DB_TABLE_NOTICES, "id = " + id);
|
||||
Entity ent = rows.get(0);
|
||||
ent.delete();
|
||||
|
||||
rows = db.getEntityList(Global.DB_TABLE_NOTICES_COURSES, "idnotice = " + id);
|
||||
Iterator<Entity> iter = rows.iterator();
|
||||
while (iter.hasNext()) {
|
||||
ent = iter.next();
|
||||
ent.delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a PairTable from database
|
||||
* @param p PairTable to be removed
|
||||
*/
|
||||
public void removePairTable(PairTable p)
|
||||
{
|
||||
String table = p.getTable();
|
||||
Integer first = (Integer) p.getFirst();
|
||||
Integer second = (Integer) p.getSecond();
|
||||
String where;
|
||||
List<Entity> rows;
|
||||
Entity ent;
|
||||
Pair<String, String> params = selectParamsPairTable(table);
|
||||
|
||||
where = params.getFirst() + " = " + first + " AND " + params.getSecond() + " = " + second;
|
||||
|
||||
Log.d("removePairTable", where);
|
||||
rows = db.getEntityList(table, where);
|
||||
ent = rows.get(0);
|
||||
ent.delete();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,4 +48,40 @@ public class Model {
|
|||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + id;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Model other = (Model) obj;
|
||||
if (id != other.id)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Model [id=" + id + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,4 +75,48 @@ public class Notice extends Model {
|
|||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = super.hashCode();
|
||||
result = prime * result
|
||||
+ ((description == null) ? 0 : description.hashCode());
|
||||
result = prime * result + timestamp;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (!super.equals(obj))
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Notice other = (Notice) obj;
|
||||
if (description == null) {
|
||||
if (other.description != null)
|
||||
return false;
|
||||
} else if (!description.equals(other.description))
|
||||
return false;
|
||||
if (timestamp != other.timestamp)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Notice [timestamp=" + timestamp + ", description="
|
||||
+ description + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -63,10 +63,63 @@ public class Pair<FIRST, SECOND>
|
|||
}
|
||||
|
||||
/**
|
||||
* Converts this Pair into a string
|
||||
* Sets first value
|
||||
* param first First value
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return "(" + first.toString() + ", " + second.toString() + ")";
|
||||
public void setFirst(FIRST first) {
|
||||
this.first = first;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets second value
|
||||
* param second Second value
|
||||
*/
|
||||
public void setSecond(SECOND second) {
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((first == null) ? 0 : first.hashCode());
|
||||
result = prime * result + ((second == null) ? 0 : second.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Pair other = (Pair) obj;
|
||||
if (first == null) {
|
||||
if (other.first != null)
|
||||
return false;
|
||||
} else if (!first.equals(other.first))
|
||||
return false;
|
||||
if (second == null) {
|
||||
if (other.second != null)
|
||||
return false;
|
||||
} else if (!second.equals(other.second))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pair [first=" + first + ", second=" + second + "]";
|
||||
}
|
||||
}
|
|
@ -68,6 +68,22 @@ public class PairTable<FIRST, SECOND> extends Model {
|
|||
return this.values.getSecond();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets first value
|
||||
* param first First value
|
||||
*/
|
||||
public void setFirst(FIRST first) {
|
||||
this.values.setFirst(first);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets second value
|
||||
* param second Second value
|
||||
*/
|
||||
public void setSecond(SECOND second) {
|
||||
this.values.setSecond(second);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table who owns the pair of values
|
||||
* @return Table who owns the pair of values
|
||||
|
@ -75,4 +91,56 @@ public class PairTable<FIRST, SECOND> extends Model {
|
|||
public String getTable() {
|
||||
return table;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param table the table to set
|
||||
*/
|
||||
public void setTable(String table) {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = super.hashCode();
|
||||
result = prime * result + ((table == null) ? 0 : table.hashCode());
|
||||
result = prime * result + ((values == null) ? 0 : values.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (!super.equals(obj))
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
PairTable other = (PairTable) obj;
|
||||
if (table == null) {
|
||||
if (other.table != null)
|
||||
return false;
|
||||
} else if (!table.equals(other.table))
|
||||
return false;
|
||||
if (values == null) {
|
||||
if (other.values != null)
|
||||
return false;
|
||||
} else if (!values.equals(other.values))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PairTable [values=" + values + ", table=" + table + "]";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user