Added Groups to Database

This commit is contained in:
Helena Rodriguez 2012-05-23 19:24:11 +02:00
parent 6e01503b9b
commit b59db74adb
5 changed files with 147 additions and 20 deletions

View File

@ -259,4 +259,40 @@
obligatory="true"
type="foreign-key" />
</table>
<table
name="groups"
new-in-version="11"
to-string="%name%" >
<field
name="groupCode"
obligatory="true"
type="integer" />
<field
name="groupName"
obligatory="true"
type="text" />
<field
name="groupTypeCode"
obligatory="true"
type="integer" />
<field
name="groupTypeName"
obligatory="true"
type="text" />
</table>
<table
name="group_course"
new-in-version="11"
to-string="%name%" >
<field
name="grpCod"
foreign-table="groups"
obligatory="true"
type="foreign-key" />
<field
name="crsCod"
foreign-table="courses"
obligatory="true"
type="foreign-key" />
</table>
</database>

View File

@ -223,9 +223,12 @@ public class SWADMain extends MenuExpandableListActivity {
activity = new Intent(getBaseContext(), Attendance.class);
startActivityForResult(activity, Global.ATTENDANCE_REQUEST_CODE);
} else if(keyword.equals(getString(R.string.documentsDownloadModuleLabel))){
activity = new Intent(getBaseContext(), DownloadsManager.class);
activity.putExtra("downloadsAreaCode", Global.DOCUMENTS_AREA_CODE);
startActivityForResult(activity,Global.DOWNLOADSMANAGER_REQUEST_CODE);
activity = new Intent(getBaseContext(),Groups.class);
startActivityForResult(activity,Global.GROUPS_REQUEST_CODE);
//activity = new Intent(getBaseContext(), DownloadsManager.class);
//activity.putExtra("downloadsAreaCode", Global.DOCUMENTS_AREA_CODE);
//startActivityForResult(activity,Global.DOWNLOADSMANAGER_REQUEST_CODE);
}else if(keyword.equals(getString(R.string.sharedsDownloadModuleLabel))){
activity = new Intent(getBaseContext(), DownloadsManager.class);
@ -359,6 +362,8 @@ public class SWADMain extends MenuExpandableListActivity {
createSpinnerAdapter();
createMenu();
break;
case Global.GROUPS_REQUEST_CODE:
break;
}
}
}

View File

@ -478,20 +478,33 @@ public class DataBaseHelper {
* @param q Test question to be inserted
* @param selectedCourseCode Course code to be referenced
*/
public void insertGroup(Group g, long selectedCourseCode)
public boolean insertGroup(Group g, long courseCode)
{
Entity ent = new Entity(Global.DB_TABLE_GROUPS);
List<Entity> rows = db.getEntityList(Global.DB_TABLE_GROUPS, "groupCode = " + g.getId());
ent.setValue("groupCode", g.getId());
ent.setValue("groupName", g.getGroupName());
ent.setValue("groupTypeCode", g.getGroupTypeCode());
ent.setValue("groupTypeName", g.getGroupTypeName());
ent.save();
ent = new Entity(Global.DB_TABLE_GROUPS_COURSES) ;
ent.setValue("grpCod", g.getId());
ent.setValue("crsCod", selectedCourseCode);
ent.save();
if(rows.isEmpty()) {
Entity ent = new Entity(Global.DB_TABLE_GROUPS);
ent.setValue("groupCode", g.getId());
ent.setValue("groupName", g.getGroupName());
ent.setValue("groupTypeCode", g.getGroupTypeCode());
ent.setValue("groupTypeName", g.getGroupTypeName());
ent.save();
rows = db.getEntityList(Global.DB_TABLE_GROUPS_COURSES,"grpCod =" + g.getId());
if(rows.isEmpty()){
ent = new Entity(Global.DB_TABLE_GROUPS_COURSES);
ent.setValue("grpCod", g.getId());
ent.setValue("crsCod", courseCode);
ent.save();
}else{
rows.get(0).setValue("crsCod", courseCode);
rows.get(0).save();
}
return true;
} else
return false;
}
/**
@ -711,6 +724,38 @@ public class DataBaseHelper {
ent.setValue("userRole", actual.getUserRole());
ent.save();
}
/**
* Updates a Group and the relationship between Groups and Courses
* @param groupCode code of the group to be updated
* @param courseCode current code of the course related to the group
* @param currentGroup updated group
* */
public boolean updateGroup(long groupCode, long courseCode, Group currentGroup){
List<Entity> rows = db.getEntityList(Global.DB_TABLE_GROUPS, "groupCode =" + groupCode);
if(!rows.isEmpty()){
Entity ent = rows.get(0);
//ent.setValue("groupCode", g.getId());
ent.setValue("groupName", currentGroup.getGroupName());
ent.setValue("groupTypeCode", currentGroup.getGroupTypeCode());
ent.setValue("groupTypeName", currentGroup.getGroupTypeName());
ent.save();
rows = db.getEntityList(Global.DB_TABLE_GROUPS_COURSES,"grpCod =" + groupCode);
if(rows.isEmpty()){
ent = new Entity(Global.DB_TABLE_GROUPS_COURSES);
ent.setValue("grpCod", groupCode);
ent.setValue("crsCod", courseCode);
ent.save();
}else{
rows.get(0).setValue("crsCod", courseCode);
rows.get(0).save();
}
return true;
}else
return false;
}
/**
* Removes a User from database
@ -730,6 +775,19 @@ public class DataBaseHelper {
Entity ent = rows.get(0);
ent.delete();
}
/**
* Removes all rows from a database table where fieldName has the given value as value
* @param fieldName Name field to search
* @param value Value field of row to be removed
*/
public void removeAllRow(String table, String fieldName ,long value)
{
List<Entity> rows = db.getEntityList(table, fieldName + "= " + value);
for(int i = 0; i < rows.size(); ++i){
Entity ent = rows.get(i);
ent.delete();
}
}
/**
* Removes a PairTable from database
@ -986,6 +1044,9 @@ public class DataBaseHelper {
emptyTable(Global.DB_TABLE_TEST_TAGS);
emptyTable(Global.DB_TABLE_USERS_COURSES);
emptyTable(Global.DB_TABLE_USERS);
emptyTable(Global.DB_TABLE_GROUPS);
emptyTable(Global.DB_TABLE_GROUPS_COURSES);
compactDB();
}

View File

@ -16,7 +16,6 @@ public class Group extends Model {
private int groupTypeCode;
private String groupTypeName;
private static PropertyInfo PI_id = new PropertyInfo();
private static PropertyInfo PI_groupName = new PropertyInfo();
private static PropertyInfo PI_groupTypeCode = new PropertyInfo();
@ -28,8 +27,7 @@ public class Group extends Model {
PI_id,
PI_groupName,
PI_groupTypeCode,
PI_groupTypeName
PI_groupTypeName,
};
@ -116,6 +114,7 @@ public class Group extends Model {
public int getGroupTypeCode(){
return groupTypeCode;
}
@Override
public int hashCode() {

View File

@ -2,6 +2,8 @@ package es.ugr.swad.swadroid.modules;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.ksoap2.SoapFault;
@ -14,6 +16,7 @@ import android.widget.Toast;
import es.ugr.swad.swadroid.Global;
import es.ugr.swad.swadroid.model.Course;
import es.ugr.swad.swadroid.model.Group;
import es.ugr.swad.swadroid.model.Model;
/**
* Groups module gets user's groups inside the current course
@ -34,9 +37,15 @@ public class Groups extends Module {
if(result != null){
//Stores groups data returned by webservice response
List<Model> groupsSWAD = new ArrayList<Model>();
Vector<?> res = (Vector <?>) result;
SoapObject soap = (SoapObject) res.get(1);
int csSize = soap.getPropertyCount();
for (int i = 0; i < csSize; i++) {
SoapObject pii = (SoapObject)soap.getProperty(i);
long id = Long.parseLong(pii.getProperty("groupCode").toString());
@ -44,15 +53,32 @@ public class Groups extends Module {
int groupTypeCode = Integer.parseInt(pii.getProperty("groupTypeCode").toString());
String groupTypeName = pii.getProperty("groupTypeName").toString();
Group g = new Group(id,groupName,groupTypeCode,groupTypeName);
//coursesSWAD.add(c);
groupsSWAD.add(g);
if(isDebuggable){
Log.i(TAG, g.toString());
}
}
//TODO remove obsolete groups
for(int i = 0; i < groupsSWAD.size(); ++i){
Group g = (Group) groupsSWAD.get(i);
//boolean isAdded = dbHelper.insertGroup(g,Global.getSelectedCourseCode());
//if(!isAdded){
if(!dbHelper.insertGroup(g,Global.getSelectedCourseCode())){
Log.i(TAG, "group to update");
dbHelper.updateGroup(g.getId(), Global.getSelectedCourseCode(), g);
Log.i(TAG, "group updated");
}
}
//Request finalized without errors
setResult(RESULT_OK);
}
}
@Override