public class DataBaseHelper extends Object
Modifier and Type | Field and Description |
---|---|
private Crypto |
crypto
Cryptographic object
|
private DataFramework |
db
Field for access to the database backend
|
private int |
DB_KEY_LENGTH
Database passphrase length
|
private String |
DBKey
Database passphrase
|
private Context |
mCtx
Application context
|
Constructor and Description |
---|
DataBaseHelper(Context ctx)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
beginTransaction()
Begin a database transaction
|
void |
cleanTables()
Clean data of all tables from database.
|
void |
clearDB()
Delete all tables from database
|
void |
clearOldNotifications(int size)
Clear old notifications
|
void |
close()
Closes the database
|
(package private) void |
compactDB()
Compact the database
|
private Model |
createObjectByTable(String table,
Entity ent)
Creates a Model's subclass object looking at the table selected
|
void |
deleteTable(String table)
Delete table from database
|
void |
emptyTable(String table)
Empty table from database
|
void |
encryptNotifications()
Encrypts the notifications data
|
void |
encryptUsers()
Encrypts the users data
|
void |
endTransaction()
End a database transaction
|
List<Model> |
getAllRows(String table)
Gets all rows of specified table
|
List<Model> |
getAllRows(String table,
String where,
String orderby)
Gets the rows of specified table that matches "where" condition.
|
Cursor |
getCursor(String table,
String where,
String orderby) |
Cursor |
getCursorGroupsOfType(long groupTypeCode) |
Cursor |
getCursorGroupType(long courseCode) |
DataFramework |
getDb()
Gets DB object
|
String |
getFieldOfLastNotification(String field)
Gets a field of last notification
|
Group |
getGroup(long groupId)
Gets the group which code is given
|
List<Long> |
getGroupCodesCourse(long courseCode)
Gets the group codes in the selected course
|
List<Group> |
getGroups(long courseCode,
String... filter)
Gets the groups that owns the selected course
|
private List<Long> |
getGroupsCodesOfType(long groupTypeCode)
Gets the codes of groups belonging the selected type of group
|
List<Group> |
getGroupsOfType(long groupTypeCode)
Gets the groups belonging to the selected type of group
|
(package private) long |
getGroupTypeCodeFromGroup(long groupCode)
Gets the code of the group type belonging the group with the given group code
|
GroupType |
getGroupTypeFromGroup(long groupCode) |
List<TestTag> |
getOrderedCourseTags(long selectedCourseCode)
Gets the tags of specified course ordered by tagInd field
|
Cursor |
getPracticeGroups(long courseCode)
Gets the practice groups in the selected course
|
PracticeSession |
getPracticeSessionInProgress(long courseCode,
long groupId)
Gets the actual practice session in progress for the selected course and group
|
List<PracticeSession> |
getPracticeSessions(long courseCode,
long groupId)
Gets practice sessions for the selected course and group
|
List<TestQuestion> |
getRandomCourseQuestionsByTagAndAnswerType(long selectedCourseCode,
List<TestTag> tagsList,
List<String> answerTypesList,
int maxQuestions)
Gets the questions of specified course and tags
|
Model |
getRow(String table,
String fieldName,
Object fieldValue)
Gets a row of specified table
|
List<Long> |
getStudentsAtSession(long sessionId)
Gets the user codes of students in the selected session
|
String |
getTimeOfLastTestUpdate(long selectedCourseCode)
Gets last time the test was updated
|
User |
getUser(String fieldName,
Object fieldValue)
Gets an user
|
List<Group> |
getUserLoggedGroups(long courseCode)
Get groups belonging to the referred course to which the logged user is enrolled
|
List<Long> |
getUsersCourse(long courseCode)
Gets the id of users enrolled in the selected course
|
boolean |
hasAttendedSession(long userCode,
long sessionId) |
void |
initializeDB()
Initializes the database structure for the first use
|
boolean |
insertCollection(String table,
List<Model> currentModels,
long... courseCode) |
void |
insertCourse(Course c)
Inserts a course in database
|
(package private) boolean |
insertEntity(String tableName,
Model currentModel,
Entity... ents)
Inserts a new row or updates an existing one in the table named @a tableName with the data that contains @a currentModel
|
boolean |
insertGroup(Group g,
long courseCode)
Inserts a group in database
|
boolean |
insertGroupType(GroupType gt)
Insert a new group type in the database or updated if the group type exits already in the data base
|
void |
insertNotification(SWADNotification n)
Inserts a notification in database
|
(package private) void |
insertPairTable(PairTable<?,?> p)
Inserts a relation in database
|
boolean |
insertPracticeSession(long courseCode,
long groupCode,
String startDate,
String endDate,
String site,
String description)
Inserts a practice session in database
|
void |
insertRollcallData(long l,
long sessCode)
Inserts the rollcall data of a student to a practice session in database
|
void |
insertTestAnswer(TestAnswer a,
int qstCod)
Inserts a test answer in database
|
void |
insertTestConfig(Test t)
Inserts a test config in database
|
void |
insertTestQuestion(TestQuestion q,
long selectedCourseCode)
Inserts a test question in database
|
void |
insertTestTag(TestTag t)
Inserts a test tag in database
|
void |
insertUser(User u)
Inserts a user in database or updates it if already exists
|
void |
insertUserCourse(long userID,
long courseCode,
long groupCode)
Inserts a new record in database indicating that the user belongs
to the course and group specified, or updates it if already exists
|
boolean |
isUserEnrolledCourse(String userID,
long selectedCourseCode)
Checks if the specified user is enrolled in the selected course
|
void |
reencryptNotifications()
Reencrypts the notifications data
|
(package private) void |
removeAllRows(String table,
String fieldName,
long value)
Removes all rows from a database table where fieldName has the given value as value
|
void |
removeGroup(Group g)
Removes a Group from database
|
void |
removePairTable(PairTable<?,?> p)
Removes a PairTable from database
|
void |
removeRow(String table,
long id)
Removes a row from a database table
|
void |
removeUser(User u)
Removes a User from database and user photo from external storage
|
private Pair<String,String> |
selectParamsPairTable(String table)
Selects the appropriated parameters for access a table
|
void |
setDb(DataFramework db)
Sets DB object
|
void |
updateAllNotifications(String field,
String value)
Updates all notifications in database
|
void |
updateCourse(Course prev,
Course actual)
Updates a course in database
|
void |
updateCourse(long id,
Course actual)
Updates a course in database
|
void |
updateGroup(Group prev,
Group currentGroup)
Updates a group in database
|
boolean |
updateGroup(long groupCode,
long courseCode,
Group currentGroup,
long... groupTypeCode)
Updates a Group and the relationship between Groups and Courses
|
private boolean |
updateGroupType(GroupType prv,
GroupType current)
Updates an existing group type
|
void |
updateNotification(long id,
String field,
String value)
Updates a notification in database
|
void |
updateNotification(long id,
SWADNotification actual)
Updates a notification in database
|
void |
updateNotification(SWADNotification prev,
SWADNotification actual)
Updates a notification in database
|
(package private) void |
updatePairTable(PairTable<?,?> prev,
PairTable<?,?> actual)
Updates a relation in database
|
private <T> boolean |
updateRelationship(Pair<String,String> tables,
Pair<String,String> idsTables,
String relationTable,
Pair<String,T> remainField,
Pair<String,T> changedField) |
void |
updateTestAnswer(TestAnswer prev,
TestAnswer actual,
int qstCod)
Updates a test answer in database
|
void |
updateTestConfig(long id,
Test actual)
Updates a test config in database
|
void |
updateTestQuestion(TestQuestion prev,
TestQuestion actual,
long selectedCourseCode)
Updates a test question in database
|
void |
updateTestTag(TestTag prev,
TestTag actual)
Updates a test tag in database
|
void |
updateUser(User prev,
User actual)
Updates a user in database
|
void |
upgradeDB(Context context)
Upgrades the database structure
|
private DataFramework db
private final Context mCtx
private String DBKey
private final int DB_KEY_LENGTH
private final Crypto crypto
public DataBaseHelper(Context ctx) throws XmlPullParserException, IOException
IOException
XmlPullParserException
public void close()
public DataFramework getDb()
public void setDb(DataFramework db)
db
- DataFramework DB objectprivate Pair<String,String> selectParamsPairTable(String table)
table
- Table to be accessedprivate Model createObjectByTable(String table, Entity ent)
table
- Table selectedent
- Cursor to the table rowspublic List<Model> getAllRows(String table)
table
- Table containing the rowspublic List<Model> getAllRows(String table, String where, String orderby)
table
- Table containing the rowswhere
- Where condition of SQL sentenceorderby
- Orderby part of SQL sentencepublic Model getRow(String table, String fieldName, Object fieldValue)
table
- Table containing the rowsfieldName
- Field's namefieldValue
- Field's valuepublic User getUser(String fieldName, Object fieldValue)
fieldName
- Field's namefieldValue
- Field's valuepublic List<Long> getUsersCourse(long courseCode)
courseCode
- Course code to be referencedpublic PracticeSession getPracticeSessionInProgress(long courseCode, long groupId)
courseCode
- Course code to be referencedgroupId
- Group code to be referencedpublic List<PracticeSession> getPracticeSessions(long courseCode, long groupId)
courseCode
- Course code to be referencedgroupId
- Group code to be referencedpublic Group getGroup(long groupId)
groupId
- long that identifies uniquely the searched grouppublic List<Long> getGroupCodesCourse(long courseCode)
courseCode
- Course code to be referencedpublic List<Group> getGroupsOfType(long groupTypeCode)
groupTypeCode
- group type code to be referencedpublic Cursor getCursorGroupsOfType(long groupTypeCode)
public Cursor getCursorGroupType(long courseCode)
public GroupType getGroupTypeFromGroup(long groupCode)
long getGroupTypeCodeFromGroup(long groupCode)
groupCode
- long that specifies the code of the groupprivate List<Long> getGroupsCodesOfType(long groupTypeCode)
groupTypeCode
- group type code to be referencedpublic List<Group> getUserLoggedGroups(long courseCode)
courseCode
- course code to which the groups belongpublic Cursor getPracticeGroups(long courseCode)
courseCode
- Course code to be referencedpublic List<Long> getStudentsAtSession(long sessionId)
sessionId
- Session code to be referencedpublic boolean hasAttendedSession(long userCode, long sessionId)
public boolean isUserEnrolledCourse(String userID, long selectedCourseCode)
selectedCourseCode
- Course code to be referencedpublic List<Group> getGroups(long courseCode, String... filter)
courseCode
- Course code to be referencedpublic void insertCourse(Course c)
c
- Course to be insertedpublic void insertNotification(SWADNotification n)
n
- Notification to be insertedpublic void insertTestQuestion(TestQuestion q, long selectedCourseCode)
q
- Test question to be insertedselectedCourseCode
- Course code to be referencedpublic void insertTestAnswer(TestAnswer a, int qstCod)
a
- Test answer to be insertedqstCod
- Test question code to be referencedpublic void insertTestTag(TestTag t)
t
- Test tag to be insertedpublic void insertTestConfig(Test t)
t
- Test config to be insertedvoid insertPairTable(PairTable<?,?> p)
p
- Relation to be insertedboolean insertEntity(String tableName, Model currentModel, Entity... ents)
tableName
- string with the table namecurrentModel
- model with the new dataents
- vector of entities. In case this param is given, the entities given will be modifiedpublic void insertUser(User u)
u
- User to be insertedpublic boolean insertGroup(Group g, long courseCode)
g
- Group to be insertedcourseCode
- Course code to be referencedpublic boolean insertGroupType(GroupType gt)
gt
- Group Type to be insertedpublic boolean insertCollection(String table, List<Model> currentModels, long... courseCode)
public void insertRollcallData(long l, long sessCode)
l
- User code of studentsessCode
- Practice session codepublic boolean insertPracticeSession(long courseCode, long groupCode, String startDate, String endDate, String site, String description)
courseCode
- Course code to be referencedgroupCode
- Group code to be referencedstartDate
- Start date-time of sessionendDate
- End date-time of sessionsite
- Site where session takes placedescription
- Optional description of practice sessionpublic void insertUserCourse(long userID, long courseCode, long groupCode)
userID
- User to be insertedcourseCode
- Course code to be referencedgroupCode
- Group code to be referencedpublic void updateCourse(Course prev, Course actual)
prev
- Course to be updatedactual
- Updated coursepublic void updateCourse(long id, Course actual)
id
- Course code of course to be updatedactual
- Updated coursepublic void updateAllNotifications(String field, String value)
field
- Field to be updatedvalue
- New field valuepublic void updateNotification(long id, String field, String value)
id
- Notification code of notification to be updatedfield
- Field to be updatedvalue
- New field valuepublic void updateNotification(long id, SWADNotification actual)
id
- Notification code of notification to be updatedactual
- Updated notificationpublic void updateNotification(SWADNotification prev, SWADNotification actual)
prev
- Notification to be updatedactual
- Updated notificationpublic void updateTestQuestion(TestQuestion prev, TestQuestion actual, long selectedCourseCode)
prev
- Test question to be updatedactual
- Updated test questionselectedCourseCode
- Course code to be referencedpublic void updateTestAnswer(TestAnswer prev, TestAnswer actual, int qstCod)
prev
- Test answer to be updatedactual
- Updated test answerqstCod
- Test question code to be referencedpublic void updateTestTag(TestTag prev, TestTag actual)
prev
- Test tag to be updatedactual
- Updated test tagpublic void updateTestConfig(long id, Test actual)
id
- ID of the test prior to updateactual
- Updated testvoid updatePairTable(PairTable<?,?> prev, PairTable<?,?> actual)
prev
- Relation to be updatedactual
- Updated relationpublic void updateUser(User prev, User actual)
prev
- User to be updatedactual
- Updated userpublic boolean updateGroup(long groupCode, long courseCode, Group currentGroup, long... groupTypeCode)
groupCode
- code of the group to be updatedcourseCode
- current code of the course related to the groupcurrentGroup
- updated groupprivate <T> boolean updateRelationship(Pair<String,String> tables, Pair<String,String> idsTables, String relationTable, Pair<String,T> remainField, Pair<String,T> changedField)
public void updateGroup(Group prev, Group currentGroup)
prev
- Group to be updatedprivate boolean updateGroupType(GroupType prv, GroupType current)
public void removeUser(User u)
u
- User to be removedpublic void removeGroup(Group g)
g
- Group to be removedpublic void removeRow(String table, long id)
id
- Identifier of row to be removedvoid removeAllRows(String table, String fieldName, long value)
fieldName
- Name field to searchvalue
- Value field of row to be removedpublic void removePairTable(PairTable<?,?> p)
p
- PairTable to be removedpublic String getFieldOfLastNotification(String field)
field
- A field of last notificationpublic String getTimeOfLastTestUpdate(long selectedCourseCode)
selectedCourseCode
- Test's coursepublic List<TestTag> getOrderedCourseTags(long selectedCourseCode)
selectedCourseCode
- Test's coursepublic List<TestQuestion> getRandomCourseQuestionsByTagAndAnswerType(long selectedCourseCode, List<TestTag> tagsList, List<String> answerTypesList, int maxQuestions)
selectedCourseCode
- Test's coursetagsList
- Tag's list of the questions to be extractedpublic void clearOldNotifications(int size)
size
- Max table sizepublic void encryptNotifications()
public void encryptUsers()
public void reencryptNotifications()
public void emptyTable(String table)
table
- Table to be emptiedpublic void deleteTable(String table)
table
- Table to be deletedpublic void clearDB()
public void cleanTables()
public void beginTransaction()
public void endTransaction()
void compactDB()
public void initializeDB()
public void upgradeDB(Context context)