Add support for multiple SnP sites (#140)

This commit is contained in:
Mikołaj Pich
2018-07-14 16:52:09 +02:00
committed by Rafał Borcz
parent 5a4b8b22f3
commit 378aba9716
26 changed files with 630 additions and 219 deletions

View File

@ -93,7 +93,7 @@ play {
}
greendao {
schemaVersion 28
schemaVersion 29
generateTests = true
}

View File

@ -0,0 +1,22 @@
package io.github.wulkanowy.data.db.dao.entities;
import org.greenrobot.greendao.test.AbstractDaoTestLongPk;
import io.github.wulkanowy.data.db.dao.entities.School;
import io.github.wulkanowy.data.db.dao.entities.SchoolDao;
public class SchoolTest extends AbstractDaoTestLongPk<SchoolDao, School> {
public SchoolTest() {
super(SchoolDao.class);
}
@Override
protected School createEntity(Long key) {
School entity = new School();
entity.setId(key);
entity.setCurrent(false);
return entity;
}
}

View File

@ -17,6 +17,8 @@ public interface DbContract {
List<Grade> getNewGrades(int semesterName);
long getCurrentSchoolId();
long getCurrentStudentId();
long getCurrentSymbolId();

View File

@ -21,6 +21,7 @@ import io.github.wulkanowy.data.db.dao.migrations.Migration23;
import io.github.wulkanowy.data.db.dao.migrations.Migration26;
import io.github.wulkanowy.data.db.dao.migrations.Migration27;
import io.github.wulkanowy.data.db.dao.migrations.Migration28;
import io.github.wulkanowy.data.db.dao.migrations.Migration29;
import io.github.wulkanowy.data.db.shared.SharedPrefContract;
import timber.log.Timber;
@ -79,6 +80,7 @@ public class DbHelper extends DaoMaster.OpenHelper {
migrations.add(new Migration26());
migrations.add(new Migration27());
migrations.add(new Migration28());
migrations.add(new Migration29());
// Sorting just to be safe, in case other people add migrations in the wrong order.
Comparator<Migration> migrationComparator = new Comparator<Migration>() {

View File

@ -11,6 +11,7 @@ import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.DiaryDao;
import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.data.db.dao.entities.GradeDao;
import io.github.wulkanowy.data.db.dao.entities.SchoolDao;
import io.github.wulkanowy.data.db.dao.entities.Semester;
import io.github.wulkanowy.data.db.dao.entities.SemesterDao;
import io.github.wulkanowy.data.db.dao.entities.StudentDao;
@ -72,10 +73,18 @@ public class DbRepository implements DbContract {
return getCurrentSymbol().getId();
}
@Override
public long getCurrentSchoolId() {
return daoSession.getSchoolDao().queryBuilder().where(
SchoolDao.Properties.SymbolId.eq(getCurrentSymbolId()),
SchoolDao.Properties.Current.eq(true)
).unique().getId();
}
@Override
public long getCurrentStudentId() {
return daoSession.getStudentDao().queryBuilder().where(
StudentDao.Properties.SymbolId.eq(getCurrentSymbolId()),
StudentDao.Properties.SchoolId.eq(getCurrentSchoolId()),
StudentDao.Properties.Current.eq(true)
).unique().getId();
}

View File

@ -0,0 +1,179 @@
package io.github.wulkanowy.data.db.dao.entities;
import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.ToMany;
import java.util.List;
@Entity(
nameInDb = "Schools",
active = true
)
public class School {
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = "symbol_id")
private Long symbolId;
@Property(nameInDb = "current")
private boolean current;
@Property(nameInDb = "real_id")
private String realId;
@Property(nameInDb = "name")
private String name;
@ToMany(referencedJoinProperty = "schoolId")
private List<Student> studentList;
/**
* Used to resolve relations
*/
@Generated(hash = 2040040024)
private transient DaoSession daoSession;
/**
* Used for active entity operations.
*/
@Generated(hash = 1796006707)
private transient SchoolDao myDao;
@Generated(hash = 975562398)
public School(Long id, Long symbolId, boolean current, String realId,
String name) {
this.id = id;
this.symbolId = symbolId;
this.current = current;
this.realId = realId;
this.name = name;
}
@Generated(hash = 1579966795)
public School() {
}
public Long getId() {
return this.id;
}
public School setId(Long id) {
this.id = id;
return this;
}
public Long getSymbolId() {
return this.symbolId;
}
public School setSymbolId(Long symbolId) {
this.symbolId = symbolId;
return this;
}
public boolean getCurrent() {
return this.current;
}
public School setCurrent(boolean current) {
this.current = current;
return this;
}
public String getRealId() {
return this.realId;
}
public School setRealId(String realId) {
this.realId = realId;
return this;
}
public String getName() {
return this.name;
}
public School setName(String name) {
this.name = name;
return this;
}
/**
* To-many relationship, resolved on first access (and after reset).
* Changes to to-many relations are not persisted, make changes to the target entity.
*/
@Generated(hash = 180118651)
public List<Student> getStudentList() {
if (studentList == null) {
final DaoSession daoSession = this.daoSession;
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
StudentDao targetDao = daoSession.getStudentDao();
List<Student> studentListNew = targetDao._querySchool_StudentList(id);
synchronized (this) {
if (studentList == null) {
studentList = studentListNew;
}
}
}
return studentList;
}
/**
* Resets a to-many relationship, making the next get call to query for a fresh result.
*/
@Generated(hash = 1628625923)
public synchronized void resetStudentList() {
studentList = null;
}
/**
* Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
* Entity must attached to an entity context.
*/
@Generated(hash = 128553479)
public void delete() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.delete(this);
}
/**
* Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
* Entity must attached to an entity context.
*/
@Generated(hash = 1942392019)
public void refresh() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.refresh(this);
}
/**
* Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
* Entity must attached to an entity context.
*/
@Generated(hash = 713229351)
public void update() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.update(this);
}
/** called by internal mechanisms, do not call yourself. */
@Generated(hash = 234091322)
public void __setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
myDao = daoSession != null ? daoSession.getSchoolDao() : null;
}
}

View File

@ -18,8 +18,8 @@ public class Student {
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = "symbol_id")
private Long symbolId;
@Property(nameInDb = "school_id")
private Long schoolId;
@Property(nameInDb = "current")
private boolean current;
@ -45,10 +45,10 @@ public class Student {
@Generated(hash = 1943931642)
private transient StudentDao myDao;
@Generated(hash = 1334215952)
public Student(Long id, Long symbolId, boolean current, String realId, String name) {
@Generated(hash = 470181623)
public Student(Long id, Long schoolId, boolean current, String realId, String name) {
this.id = id;
this.symbolId = symbolId;
this.schoolId = schoolId;
this.current = current;
this.realId = realId;
this.name = name;
@ -66,12 +66,12 @@ public class Student {
this.id = id;
}
public Long getSymbolId() {
return this.symbolId;
public Long getSchoolId() {
return this.schoolId;
}
public Student setSymbolId(Long symbolId) {
this.symbolId = symbolId;
public Student setSchoolId(Long schoolId) {
this.schoolId = schoolId;
return this;
}

View File

@ -24,9 +24,6 @@ public class Symbol {
@Property(nameInDb = "host")
private String host;
@Property(nameInDb = "school_id")
private String schoolId;
@Property(nameInDb = "symbol")
private String symbol;
@ -34,7 +31,7 @@ public class Symbol {
private String type;
@ToMany(referencedJoinProperty = "symbolId")
private List<Student> studentList;
private List<School> schoolList;
/**
* Used to resolve relations
@ -48,13 +45,11 @@ public class Symbol {
@Generated(hash = 684907977)
private transient SymbolDao myDao;
@Generated(hash = 242774339)
public Symbol(Long id, Long userId, String host, String schoolId, String symbol,
String type) {
@Generated(hash = 1034469460)
public Symbol(Long id, Long userId, String host, String symbol, String type) {
this.id = id;
this.userId = userId;
this.host = host;
this.schoolId = schoolId;
this.symbol = symbol;
this.type = type;
}
@ -89,15 +84,6 @@ public class Symbol {
return this;
}
public String getSchoolId() {
return this.schoolId;
}
public Symbol setSchoolId(String schoolId) {
this.schoolId = schoolId;
return this;
}
public String getSymbol() {
return this.symbol;
}
@ -120,30 +106,28 @@ public class Symbol {
* To-many relationship, resolved on first access (and after reset).
* Changes to to-many relations are not persisted, make changes to the target entity.
*/
@Generated(hash = 604366458)
public List<Student> getStudentList() {
if (studentList == null) {
@Generated(hash = 1733082867)
public List<School> getSchoolList() {
if (schoolList == null) {
final DaoSession daoSession = this.daoSession;
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
StudentDao targetDao = daoSession.getStudentDao();
List<Student> studentListNew = targetDao._querySymbol_StudentList(id);
SchoolDao targetDao = daoSession.getSchoolDao();
List<School> schoolListNew = targetDao._querySymbol_SchoolList(id);
synchronized (this) {
if (studentList == null) {
studentList = studentListNew;
if (schoolList == null) {
schoolList = schoolListNew;
}
}
}
return studentList;
return schoolList;
}
/**
* Resets a to-many relationship, making the next get call to query for a fresh result.
*/
@Generated(hash = 1628625923)
public synchronized void resetStudentList() {
studentList = null;
/** Resets a to-many relationship, making the next get call to query for a fresh result. */
@Generated(hash = 1757777300)
public synchronized void resetSchoolList() {
schoolList = null;
}
/**

View File

@ -15,7 +15,7 @@ public class Migration27 implements DbHelper.Migration {
}
@Override
public void runMigration(Database db, SharedPrefContract sharedPref, Vulcan vulcan) throws Exception {
public void runMigration(Database db, SharedPrefContract sharedPref, Vulcan vulcan) {
ExamDao.dropTable(db, true);
ExamDao.createTable(db, true);

View File

@ -0,0 +1,60 @@
package io.github.wulkanowy.data.db.dao.migrations
import android.database.Cursor
import org.greenrobot.greendao.database.Database
import io.github.wulkanowy.api.Vulcan
import io.github.wulkanowy.data.db.dao.DbHelper
import io.github.wulkanowy.data.db.shared.SharedPrefContract
class Migration29 : DbHelper.Migration {
override fun getVersion(): Int? {
return 29
}
override fun runMigration(db: Database, sharedPref: SharedPrefContract, vulcan: Vulcan) {
createSchoolsTable(db)
modifyStudents(db)
insertSchool(db, getRealSchoolId(db))
}
private fun createSchoolsTable(db: Database) {
db.execSQL("DROP TABLE IF EXISTS \"Schools\";")
db.execSQL("CREATE TABLE IF NOT EXISTS \"Schools\" (" + //
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
"\"symbol_id\" INTEGER," + // 1: symbolId
"\"current\" INTEGER NOT NULL ," + // 2: current
"\"real_id\" TEXT," + // 3: realId
"\"name\" TEXT);") // 4: name
}
private fun modifyStudents(db: Database) {
db.execSQL("ALTER TABLE Students ADD COLUMN school_id INTEGER")
db.execSQL("UPDATE Students SET (school_id) = ('1')")
}
private fun getRealSchoolId(db: Database): String {
var cursor: Cursor? = null
try {
cursor = db.rawQuery("SELECT school_id FROM Symbols WHERE _id=?", arrayOf("1"))
return if (cursor!!.count > 0) {
cursor.moveToFirst()
cursor.getString(cursor.getColumnIndex("school_id"))
} else ""
} finally {
cursor!!.close()
}
}
private fun insertSchool(db: Database, realId: String) {
db.execSQL("INSERT INTO Schools(symbol_id, current, real_id, name) VALUES(" +
"\"1\"," +
"\"1\"," +
"\"" + realId + "\"," +
"\"Uczeń\"" +
")")
}
}

View File

@ -17,6 +17,8 @@ import io.github.wulkanowy.data.db.dao.entities.DaoMaster;
import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.Diary;
import io.github.wulkanowy.data.db.dao.entities.DiaryDao;
import io.github.wulkanowy.data.db.dao.entities.School;
import io.github.wulkanowy.data.db.dao.entities.SchoolDao;
import io.github.wulkanowy.data.db.dao.entities.Semester;
import io.github.wulkanowy.data.db.dao.entities.Student;
import io.github.wulkanowy.data.db.dao.entities.StudentDao;
@ -57,12 +59,15 @@ public class AccountSync {
daoSession.getDatabase().beginTransaction();
Timber.i("Register start");
try {
Account account = insertAccount(email, password);
Symbol symbolEntity = insertSymbol(account);
insertStudents(symbolEntity);
insertDiaries(symbolEntity);
insertSemesters();
School schoolEntity = insertSchools(symbolEntity);
Student student = insertStudents(schoolEntity);
Diary diary = insertDiaries(student);
insertSemesters(diary);
sharedPref.setCurrentUserId(account.getId());
@ -70,6 +75,8 @@ public class AccountSync {
} finally {
daoSession.getDatabase().endTransaction();
}
Timber.i("Register end");
}
private Account insertAccount(String email, String password) throws CryptoException {
@ -82,44 +89,64 @@ public class AccountSync {
}
private Symbol insertSymbol(Account account) throws VulcanException, IOException {
String schoolId = vulcan.getStudentAndParent().getSchoolID();
Timber.d("Register symbol %s", vulcan.getSymbol());
vulcan.getSchools();
Timber.d("Register symbol (%s)", vulcan.getSymbol());
Symbol symbol = new Symbol()
.setUserId(account.getId())
.setSchoolId(schoolId)
.setSymbol(vulcan.getSymbol());
daoSession.getSymbolDao().insert(symbol);
return symbol;
}
private void insertStudents(Symbol symbol) throws VulcanException, IOException {
List<Student> studentList = DataObjectConverter.studentsToStudentEntities(
vulcan.getStudentAndParent().getStudents(),
private School insertSchools(Symbol symbol) throws VulcanException, IOException {
List<School> schoolList = DataObjectConverter.schoolsToSchoolsEntities(
vulcan.getSchools(),
symbol.getId()
);
Timber.d("Register students %s", studentList.size());
daoSession.getStudentDao().insertInTx(studentList);
Timber.d("Register schools (%s)", schoolList.size());
daoSession.getSchoolDao().insertInTx(schoolList);
return daoSession.getSchoolDao().queryBuilder().where(
SchoolDao.Properties.SymbolId.eq(symbol.getId()),
SchoolDao.Properties.Current.eq(true)
).unique();
}
private void insertDiaries(Symbol symbolEntity) throws VulcanException, IOException {
private Student insertStudents(School school) throws VulcanException, IOException {
List<Student> studentList = DataObjectConverter.studentsToStudentEntities(
vulcan.getStudentAndParent().getStudents(),
school.getId()
);
Timber.d("Register students (%s)", studentList.size());
daoSession.getStudentDao().insertInTx(studentList);
return daoSession.getStudentDao().queryBuilder().where(
StudentDao.Properties.SchoolId.eq(school.getId()),
StudentDao.Properties.Current.eq(true)
).unique();
}
private Diary insertDiaries(Student student) throws VulcanException, IOException {
List<Diary> diaryList = DataObjectConverter.diariesToDiaryEntities(
vulcan.getStudentAndParent().getDiaries(),
daoSession.getStudentDao().queryBuilder().where(
StudentDao.Properties.SymbolId.eq(symbolEntity.getId()),
StudentDao.Properties.Current.eq(true)
).unique().getId());
Timber.d("Register diaries %s", diaryList.size());
student.getId()
);
Timber.d("Register diaries (%s)", diaryList.size());
daoSession.getDiaryDao().insertInTx(diaryList);
return daoSession.getDiaryDao().queryBuilder().where(
DiaryDao.Properties.StudentId.eq(student.getId()),
DiaryDao.Properties.Current.eq(true)
).unique();
}
private void insertSemesters() throws VulcanException, IOException {
private void insertSemesters(Diary diary) throws VulcanException, IOException {
List<Semester> semesterList = DataObjectConverter.semestersToSemesterEntities(
vulcan.getStudentAndParent().getSemesters(),
daoSession.getDiaryDao().queryBuilder().where(
DiaryDao.Properties.Current.eq(true)
).unique().getId());
Timber.d("Register semesters %s", semesterList.size());
diary.getId()
);
Timber.d("Register semesters (%s)", semesterList.size());
daoSession.getSemesterDao().insertInTx(semesterList);
}
@ -138,8 +165,11 @@ public class AccountSync {
Symbol symbol = daoSession.getSymbolDao().queryBuilder().where(
SymbolDao.Properties.UserId.eq(account.getId())).unique();
School school = daoSession.getSchoolDao().queryBuilder().where(
SchoolDao.Properties.SymbolId.eq(symbol.getId())).unique();
Student student = daoSession.getStudentDao().queryBuilder().where(
StudentDao.Properties.SymbolId.eq(symbol.getId()),
StudentDao.Properties.SchoolId.eq(school.getId()),
StudentDao.Properties.Current.eq(true)
).unique();
@ -152,7 +182,7 @@ public class AccountSync {
account.getEmail(),
Scrambler.decrypt(account.getEmail(), account.getPassword()),
symbol.getSymbol(),
symbol.getSchoolId(),
school.getRealId(),
student.getRealId(),
diary.getValue()
);

View File

@ -9,6 +9,7 @@ import io.github.wulkanowy.data.db.dao.entities.Day;
import io.github.wulkanowy.data.db.dao.entities.Diary;
import io.github.wulkanowy.data.db.dao.entities.Exam;
import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.data.db.dao.entities.School;
import io.github.wulkanowy.data.db.dao.entities.Semester;
import io.github.wulkanowy.data.db.dao.entities.Student;
import io.github.wulkanowy.data.db.dao.entities.Subject;
@ -21,7 +22,22 @@ public final class DataObjectConverter {
throw new IllegalStateException("Utility class");
}
public static List<Student> studentsToStudentEntities(List<io.github.wulkanowy.api.generic.Student> students, Long symbolId) {
public static List<School> schoolsToSchoolsEntities(List<io.github.wulkanowy.api.generic.School> schools, Long symbolId) {
List<School> studentList = new ArrayList<>();
for (io.github.wulkanowy.api.generic.School school : schools) {
studentList.add(new School()
.setName(school.getName())
.setCurrent(school.getCurrent())
.setRealId(school.getId())
.setSymbolId(symbolId)
);
}
return studentList;
}
public static List<Student> studentsToStudentEntities(List<io.github.wulkanowy.api.generic.Student> students, Long schoolId) {
List<Student> studentList = new ArrayList<>();
for (io.github.wulkanowy.api.generic.Student student : students) {
@ -29,7 +45,7 @@ public final class DataObjectConverter {
.setName(student.getName())
.setCurrent(student.isCurrent())
.setRealId(student.getId())
.setSymbolId(symbolId)
.setSchoolId(schoolId)
);
}