1
0

API facade (#27)

*Create API facade

* Refactor API tests

* Implementation of the facade API and add tests for synchronization
This commit is contained in:
Mikołaj Pich
2017-09-23 20:28:35 +02:00
committed by Rafał Borcz
parent c876d114e3
commit 1f5a03fba7
56 changed files with 1791 additions and 1076 deletions

View File

@ -9,14 +9,17 @@ import android.widget.Toast;
import java.io.IOException;
import io.github.wulkanowy.R;
import io.github.wulkanowy.activity.WulkanowyApp;
import io.github.wulkanowy.activity.dashboard.DashboardActivity;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.dao.entities.DaoSession;
import io.github.wulkanowy.security.CryptoException;
import io.github.wulkanowy.services.LoginSession;
import io.github.wulkanowy.services.VulcanSynchronization;
import io.github.wulkanowy.services.jobs.GradesSync;
import io.github.wulkanowy.services.synchronisation.DataSynchronisation;
import io.github.wulkanowy.services.synchronisation.VulcanSynchronisation;
import io.github.wulkanowy.utilities.ConnectionUtilities;
public class LoginTask extends AsyncTask<String, Integer, Integer> {
@ -44,21 +47,22 @@ public class LoginTask extends AsyncTask<String, Integer, Integer> {
protected Integer doInBackground(String... credentials) {
if (ConnectionUtilities.isOnline(activity)) {
VulcanSynchronisation vulcanSynchronisation = new VulcanSynchronisation();
VulcanSynchronization vulcanSynchronization = new VulcanSynchronization(new LoginSession());
DaoSession daoSession = ((WulkanowyApp) activity.getApplication()).getDaoSession();
try {
vulcanSynchronisation.loginNewUser(credentials[0], credentials[1], credentials[2], activity);
vulcanSynchronization
.loginNewUser(credentials[0], credentials[1], credentials[2], activity, daoSession, new Vulcan());
} catch (BadCredentialsException e) {
return R.string.login_bad_credentials_text;
} catch (AccountPermissionException e) {
return R.string.login_bad_account_permission_text;
} catch (CryptoException e) {
return R.string.encrypt_failed_text;
} catch (LoginErrorException | IOException e) {
} catch (NotLoggedInErrorException | IOException e) {
return R.string.login_denied_text;
}
DataSynchronisation dataSynchronisation = new DataSynchronisation(activity);
dataSynchronisation.syncSubjectsAndGrades(vulcanSynchronisation);
vulcanSynchronization.syncSubjectsAndGrades();
return R.string.login_accepted_text;

View File

@ -0,0 +1,37 @@
package io.github.wulkanowy.api;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.Map;
public abstract class Api {
protected Cookies cookies;
public Cookies getCookiesObject() {
return cookies;
}
public Map<String, String> getCookies() {
return cookies.getItems();
}
public Cookies setCookies(Map<String, String> cookies) {
this.cookies.setItems(cookies);
return this.cookies;
}
public Cookies addCookies(Map<String, String> cookies) {
this.cookies.addItems(cookies);
return this.cookies;
}
public Document getPageByUrl(String url) throws IOException {
return Jsoup.connect(url)
.followRedirects(true)
.cookies(getCookies())
.get();
}
}

View File

@ -10,9 +10,9 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
public class StudentAndParent extends Vulcan {
public class StudentAndParent extends Api {
private String startPageUrl = "https://uonetplus.vulcan.net.pl/{symbol}/Start.mvc/Index";
@ -20,37 +20,32 @@ public class StudentAndParent extends Vulcan {
private String gradesPageUrl = baseUrl + "Oceny/Wszystkie";
private String symbol = "";
private String symbol;
private String id = "";
private String id;
public StudentAndParent(Cookies cookies, String locID) throws IOException, LoginErrorException {
public StudentAndParent(Cookies cookies, String symbol) {
this.cookies = cookies;
this.symbol = locID;
this.symbol = symbol;
}
// get link to uonetplus-opiekun.vulcan.net.pl module
Document startPage = getPageByUrl(startPageUrl.replace("{symbol}", symbol));
Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first();
String uonetPlusOpiekunUrl = studentTileLink.attr("href");
//get context module cookie
Connection.Response res = Jsoup.connect(uonetPlusOpiekunUrl)
.followRedirects(true)
.cookies(getCookies())
.execute();
cookies.addItems(res.cookies());
this.id = getCalculatedID(uonetPlusOpiekunUrl);
this.baseUrl = baseUrl
.replace("{symbol}", getSymbol())
.replace("{ID}", getId());
public StudentAndParent(Cookies cookies, String symbol, String id) {
this(cookies, symbol);
this.id = id;
}
public String getGradesPageUrl() {
return gradesPageUrl;
}
public String getBaseUrl() {
return baseUrl;
}
public String getStartPageUrl() {
return startPageUrl;
}
public String getSymbol() {
return symbol;
}
@ -59,11 +54,41 @@ public class StudentAndParent extends Vulcan {
return id;
}
public String getCalculatedID(String uonetPlusOpiekunUrl) throws LoginErrorException {
String[] path = uonetPlusOpiekunUrl.split("vulcan.net.pl/")[1].split("/");
public void storeContextCookies() throws IOException, NotLoggedInErrorException {
//get context cookie
Connection.Response res = Jsoup.connect(getSnpPageUrl())
.followRedirects(true)
.cookies(getCookies())
.execute();
cookies.addItems(res.cookies());
}
public String getSnpPageUrl() throws IOException, NotLoggedInErrorException {
if (null != getId()) {
return getBaseUrl().replace("{symbol}", getSymbol()).replace("{ID}", getId());
}
// get url to uonetplus-opiekun.vulcan.net.pl
Document startPage = getPageByUrl(getStartPageUrl().replace("{symbol}", getSymbol()));
Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first();
if (null == studentTileLink) {
throw new NotLoggedInErrorException();
}
String snpPageUrl = studentTileLink.attr("href");
this.id = getExtractedIdFromUrl(snpPageUrl);
return snpPageUrl;
}
public String getExtractedIdFromUrl(String snpPageUrl) throws NotLoggedInErrorException {
String[] path = snpPageUrl.split("vulcan.net.pl/")[1].split("/");
if (4 != path.length) {
throw new LoginErrorException();
throw new NotLoggedInErrorException();
}
return path[1];
@ -74,7 +99,9 @@ public class StudentAndParent extends Vulcan {
}
public Document getSnPPageDocument(String url) throws IOException {
return getPageByUrl(baseUrl + url);
return getPageByUrl(getBaseUrl()
.replace("{symbol}", getSymbol())
.replace("{ID}", getId()) + url);
}
public List<Semester> getSemesters() throws IOException {

View File

@ -1,37 +1,111 @@
package io.github.wulkanowy.api;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.Map;
public abstract class Vulcan {
import io.github.wulkanowy.api.attendance.AttendanceStatistics;
import io.github.wulkanowy.api.attendance.AttendanceTable;
import io.github.wulkanowy.api.grades.GradesList;
import io.github.wulkanowy.api.grades.SubjectsList;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.Login;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.notes.AchievementsList;
import io.github.wulkanowy.api.notes.NotesList;
import io.github.wulkanowy.api.school.SchoolInfo;
import io.github.wulkanowy.api.school.TeachersInfo;
import io.github.wulkanowy.api.timetable.Timetable;
import io.github.wulkanowy.api.user.BasicInformation;
import io.github.wulkanowy.api.user.FamilyInformation;
protected Cookies cookies;
public class Vulcan extends Api {
public Cookies getCookiesObject() {
return cookies;
private String id;
private String symbol;
private StudentAndParent snp;
public void login(String email, String password, String symbol)
throws BadCredentialsException, AccountPermissionException, LoginErrorException {
Login login = new Login(new Cookies());
login.login(email, password, symbol);
this.symbol = symbol;
this.cookies = login.getCookiesObject();
}
public Map<String, String> getCookies() {
return cookies.getItems();
public void login(String email, String password, String symbol, String id)
throws BadCredentialsException, AccountPermissionException, LoginErrorException {
login(email, password, symbol);
this.id = id;
}
public Cookies setCookies(Map<String, String> cookies) {
this.cookies.setItems(cookies);
return this.cookies;
public StudentAndParent getStudentAndParent() throws IOException, NotLoggedInErrorException {
if (null == getCookiesObject()) {
throw new NotLoggedInErrorException();
}
if (null != snp) {
return snp;
}
if (null == id) {
snp = new StudentAndParent(cookies, symbol);
} else {
snp = new StudentAndParent(cookies, symbol, id);
}
snp.storeContextCookies();
this.cookies = snp.getCookiesObject();
return snp;
}
public Cookies addCookies(Map<String, String> cookies) {
this.cookies.addItems(cookies);
return this.cookies;
public AttendanceStatistics getAttendanceStatistics() throws IOException, NotLoggedInErrorException {
return new AttendanceStatistics(getStudentAndParent());
}
public Document getPageByUrl(String url) throws IOException {
return Jsoup.connect(url)
.followRedirects(true)
.cookies(getCookies())
.get();
public AttendanceTable getAttendanceTable() throws IOException, NotLoggedInErrorException {
return new AttendanceTable(getStudentAndParent());
}
public GradesList getGradesList() throws IOException, NotLoggedInErrorException {
return new GradesList(getStudentAndParent());
}
public SubjectsList getSubjectsList() throws IOException, NotLoggedInErrorException {
return new SubjectsList(getStudentAndParent());
}
public AchievementsList getAchievementsList() throws IOException, NotLoggedInErrorException {
return new AchievementsList(getStudentAndParent());
}
public NotesList getNotesList() throws IOException, NotLoggedInErrorException {
return new NotesList(getStudentAndParent());
}
public SchoolInfo getSchoolInfo() throws IOException, NotLoggedInErrorException {
return new SchoolInfo(getStudentAndParent());
}
public TeachersInfo getTeachersInfo() throws IOException, NotLoggedInErrorException {
return new TeachersInfo(getStudentAndParent());
}
public Timetable getTimetable() throws IOException, NotLoggedInErrorException {
return new Timetable(getStudentAndParent());
}
public BasicInformation getBasicInformation() throws IOException, NotLoggedInErrorException {
return new BasicInformation(getStudentAndParent());
}
public FamilyInformation getFamilyInformation() throws IOException, NotLoggedInErrorException {
return new FamilyInformation(getStudentAndParent());
}
}

View File

@ -10,13 +10,13 @@ import java.util.List;
import io.github.wulkanowy.api.StudentAndParent;
public class Statistics {
public class AttendanceStatistics {
private StudentAndParent snp;
private String attendancePageUrl = "Frekwencja.mvc";
public Statistics(StudentAndParent snp) {
public AttendanceStatistics(StudentAndParent snp) {
this.snp = snp;
}

View File

@ -9,13 +9,13 @@ import java.util.List;
import io.github.wulkanowy.api.StudentAndParent;
public class Table {
public class AttendanceTable {
private StudentAndParent snp;
private String attendancePageUrl = "Frekwencja.mvc?data=";
public Table(StudentAndParent snp) {
public AttendanceTable(StudentAndParent snp) {
this.snp = snp;
}

View File

@ -2,10 +2,10 @@ package io.github.wulkanowy.api.grades;
public class Grade {
private String subject = "";
protected String value = "";
private String subject = "";
private String color = "";
private String symbol = "";

View File

@ -16,10 +16,9 @@ import java.util.regex.Pattern;
import io.github.wulkanowy.api.Semester;
import io.github.wulkanowy.api.StudentAndParent;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.LoginErrorException;
public class GradesList extends Vulcan {
public class GradesList {
private StudentAndParent snp = null;

View File

@ -9,10 +9,9 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.StudentAndParent;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.LoginErrorException;
public class SubjectsList extends Vulcan {
public class SubjectsList {
private StudentAndParent snp = null;

View File

@ -6,17 +6,17 @@ import org.jsoup.nodes.Document;
import java.io.IOException;
import io.github.wulkanowy.api.Api;
import io.github.wulkanowy.api.Cookies;
import io.github.wulkanowy.api.Vulcan;
public class Login extends Vulcan {
public class Login extends Api {
private String loginPageUrl = "https://cufs.vulcan.net.pl/{symbol}/Account/LogOn";
private String certificatePageUrl = "https://cufs.vulcan.net.pl/{symbol}"
+ "/FS/LS?wa=wsignin1.0&wtrealm=https://uonetplus.vulcan.net.pl/{symbol}"
+ "/LoginEndpoint.aspx&wctx=https://uonetplus.vulcan.net.pl/{symbol}"
+ "/LoginEndpoint.aspx";
+ "/FS/LS?wa=wsignin1.0&wtrealm=https://uonetplus.vulcan.net.pl/{symbol}"
+ "/LoginEndpoint.aspx&wctx=https://uonetplus.vulcan.net.pl/{symbol}"
+ "/LoginEndpoint.aspx";
private String loginEndpointPageUrl =
"https://uonetplus.vulcan.net.pl/{symbol}/LoginEndpoint.aspx";

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.api.login;
public class LoginErrorException extends Exception {
public class LoginErrorException extends NotLoggedInErrorException {
}

View File

@ -0,0 +1,5 @@
package io.github.wulkanowy.api.login;
public class NotLoggedInErrorException extends Exception {
}

View File

@ -5,10 +5,9 @@ import org.jsoup.nodes.Element;
import java.io.IOException;
import io.github.wulkanowy.api.StudentAndParent;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.LoginErrorException;
public class SchoolInfo extends Vulcan {
public class SchoolInfo {
private StudentAndParent snp = null;

View File

@ -9,10 +9,9 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.StudentAndParent;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.LoginErrorException;
public class TeachersInfo extends Vulcan {
public class TeachersInfo {
private StudentAndParent snp = null;

View File

@ -9,16 +9,15 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.StudentAndParent;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.LoginErrorException;
public class Table extends Vulcan {
public class Timetable {
private StudentAndParent snp;
private String timetablePageurl = "Lekcja.mvc/PlanLekcji?data=";
private String timetablePageUrl = "Lekcja.mvc/PlanLekcji?data=";
public Table(StudentAndParent snp) {
public Timetable(StudentAndParent snp) {
this.snp = snp;
}
@ -27,7 +26,7 @@ public class Table extends Vulcan {
}
public Week getWeekTable(String tick) throws IOException, LoginErrorException {
Element table = snp.getSnPPageDocument(timetablePageurl + tick)
Element table = snp.getSnPPageDocument(timetablePageUrl + tick)
.select(".mainContainer .presentData").first();
Elements tableHeaderCells = table.select("thead th");
@ -95,6 +94,12 @@ public class Table extends Vulcan {
lesson.setTeacher(spans.get(1).text());
lesson.setRoom(spans.get(2).text());
// okienko dla uczniów
if (5 == spans.size()) {
lesson.setTeacher(spans.get(2).text());
lesson.setRoom(spans.get(3).text());
}
lesson = getLessonGroupDivisionInfo(lesson, spans);
lesson = getLessonTypeInfo(lesson, spans);
lesson = getLessonDescriptionInfo(lesson, spans);

View File

@ -8,10 +8,9 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.StudentAndParent;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.LoginErrorException;
public class FamilyInformation extends Vulcan {
public class FamilyInformation {
private StudentAndParent snp;

View File

@ -17,7 +17,7 @@ public class EntitiesCompare {
.removeAll(newList, addedOrUpdatedGradeList));
for (Grade grade : addedOrUpdatedGradeList) {
grade.setNew(true);
grade.setIsNew(true);
updatedList.add(grade);
}

View File

@ -280,15 +280,6 @@ public class Grade implements Parcelable {
return this;
}
public boolean isNew() {
return isNew;
}
public Grade setNew(boolean aNew) {
isNew = aNew;
return this;
}
public boolean getIsNew() {
return this.isNew;
}

View File

@ -29,19 +29,19 @@ import javax.security.auth.x500.X500Principal;
public class Scrambler {
private KeyStore keyStore;
public final static String DEBUG_TAG = "WulkanowySecurity";
private static final String ANDROID_KEYSTORE = "AndroidKeyStore";
public final static String DEBUG_TAG = "WulkanowySecurity";
protected Context context;
public Context context;
private KeyStore keyStore;
public Scrambler(Context context) {
protected Scrambler(Context context) {
this.context = context;
}
public void loadKeyStore() throws CryptoException {
protected void loadKeyStore() throws CryptoException {
try {
keyStore = KeyStore.getInstance(ANDROID_KEYSTORE);
@ -54,7 +54,7 @@ public class Scrambler {
}
@TargetApi(18)
public void generateNewKey(String alias) throws CryptoException {
protected void generateNewKey(String alias) throws CryptoException {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
@ -105,7 +105,7 @@ public class Scrambler {
}
public String encryptString(String alias, String text) throws CryptoException {
protected String encryptString(String alias, String text) throws CryptoException {
if (!alias.isEmpty() && !text.isEmpty()) {
try {
@ -135,7 +135,7 @@ public class Scrambler {
}
}
public String decryptString(String alias, String text) throws CryptoException {
protected String decryptString(String alias, String text) throws CryptoException {
if (!alias.isEmpty() && !text.isEmpty()) {
try {

View File

@ -0,0 +1,41 @@
package io.github.wulkanowy.services;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.dao.entities.DaoSession;
public class LoginSession {
private Long userId;
private Vulcan vulcan;
private DaoSession daoSession;
public Long getUserId() {
return userId;
}
public LoginSession setUserId(Long userId) {
this.userId = userId;
return this;
}
public Vulcan getVulcan() {
return vulcan;
}
public LoginSession setVulcan(Vulcan vulcan) {
this.vulcan = vulcan;
return this;
}
public DaoSession getDaoSession() {
return daoSession;
}
public LoginSession setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
return this;
}
}

View File

@ -0,0 +1,67 @@
package io.github.wulkanowy.services;
import android.content.Context;
import android.util.Log;
import java.io.IOException;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.dao.entities.DaoSession;
import io.github.wulkanowy.security.CryptoException;
import io.github.wulkanowy.services.jobs.VulcanSync;
import io.github.wulkanowy.services.synchronisation.AccountSynchronisation;
import io.github.wulkanowy.services.synchronisation.GradesSynchronisation;
import io.github.wulkanowy.services.synchronisation.SubjectsSynchronisation;
public class VulcanSynchronization {
private LoginSession loginSession;
public VulcanSynchronization(LoginSession loginSession) {
this.loginSession = loginSession;
}
public void loginCurrentUser(Context context, DaoSession daoSession, Vulcan vulcan)
throws CryptoException, BadCredentialsException, AccountPermissionException, IOException, LoginErrorException {
AccountSynchronisation accountSynchronisation = new AccountSynchronisation();
loginSession = accountSynchronisation.loginCurrentUser(context, daoSession, vulcan);
}
public void loginNewUser(String email, String password, String symbol,
Context context, DaoSession daoSession, Vulcan vulcan)
throws BadCredentialsException, NotLoggedInErrorException, AccountPermissionException, IOException, CryptoException {
AccountSynchronisation accountSynchronisation = new AccountSynchronisation();
loginSession = accountSynchronisation.loginNewUser(email, password, symbol, context, daoSession, vulcan);
}
public boolean syncGrades() {
GradesSynchronisation gradesSynchronisation = new GradesSynchronisation();
try {
gradesSynchronisation.sync(loginSession);
return true;
} catch (Exception e) {
Log.e(VulcanSync.DEBUG_TAG, "Synchronisation of grades failed", e);
return false;
}
}
public boolean syncSubjectsAndGrades() {
SubjectsSynchronisation subjectsSynchronisation = new SubjectsSynchronisation();
try {
subjectsSynchronisation.sync(loginSession);
syncGrades();
return true;
} catch (Exception e) {
Log.e(VulcanSync.DEBUG_TAG, "Synchronisation of subjects failed", e);
return false;
}
}
}

View File

@ -10,13 +10,14 @@ import com.firebase.jobdispatcher.Trigger;
import java.io.IOException;
import io.github.wulkanowy.activity.WulkanowyApp;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.dao.entities.DaoSession;
import io.github.wulkanowy.security.CryptoException;
import io.github.wulkanowy.services.synchronisation.DataSynchronisation;
import io.github.wulkanowy.services.synchronisation.VulcanSynchronisation;
import io.github.wulkanowy.services.LoginSession;
import io.github.wulkanowy.services.VulcanSynchronization;
public class GradesSync extends VulcanSync {
@ -44,14 +45,13 @@ public class GradesSync extends VulcanSync {
@Override
public void workToBePerformed() throws CryptoException, BadCredentialsException,
LoginErrorException, AccountPermissionException, IOException {
NotLoggedInErrorException, AccountPermissionException, IOException {
DaoSession daoSession = ((WulkanowyApp) getApplication()).getDaoSession();
VulcanSynchronisation vulcanSynchronisation = new VulcanSynchronisation();
DataSynchronisation dataSynchronisation = new DataSynchronisation(daoSession);
vulcanSynchronisation.loginCurrentUser(getApplicationContext(), daoSession);
dataSynchronisation.syncGrades(vulcanSynchronisation);
VulcanSynchronization vulcanSynchronization = new VulcanSynchronization(new LoginSession());
vulcanSynchronization.loginCurrentUser(getApplicationContext(), daoSession, new Vulcan());
vulcanSynchronization.syncGrades();
}
}
}

View File

@ -10,13 +10,14 @@ import com.firebase.jobdispatcher.Trigger;
import java.io.IOException;
import io.github.wulkanowy.activity.WulkanowyApp;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.dao.entities.DaoSession;
import io.github.wulkanowy.security.CryptoException;
import io.github.wulkanowy.services.synchronisation.DataSynchronisation;
import io.github.wulkanowy.services.synchronisation.VulcanSynchronisation;
import io.github.wulkanowy.services.LoginSession;
import io.github.wulkanowy.services.VulcanSynchronization;
public class SubjectsSync extends VulcanSync {
@ -44,14 +45,13 @@ public class SubjectsSync extends VulcanSync {
@Override
public void workToBePerformed() throws CryptoException, BadCredentialsException,
LoginErrorException, AccountPermissionException, IOException {
NotLoggedInErrorException, AccountPermissionException, IOException {
DaoSession daoSession = ((WulkanowyApp) getApplication()).getDaoSession();
VulcanSynchronisation vulcanSynchronisation = new VulcanSynchronisation();
DataSynchronisation dataSynchronisation = new DataSynchronisation(daoSession);
vulcanSynchronisation.loginCurrentUser(getApplicationContext(), daoSession);
dataSynchronisation.syncSubjectsAndGrades(vulcanSynchronisation);
VulcanSynchronization vulcanSynchronization = new VulcanSynchronization(new LoginSession());
vulcanSynchronization.loginCurrentUser(getApplicationContext(), daoSession, new Vulcan());
vulcanSynchronization.syncSubjectsAndGrades();
}
}

View File

@ -10,7 +10,7 @@ import java.io.IOException;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.security.CryptoException;
public abstract class VulcanJob extends JobService {
@ -32,7 +32,7 @@ public abstract class VulcanJob extends JobService {
}
public abstract void workToBePerformed() throws CryptoException, BadCredentialsException,
LoginErrorException, AccountPermissionException, IOException;
NotLoggedInErrorException, AccountPermissionException, IOException;
private class SyncTask extends AsyncTask<JobParameters, Void, Void> {

View File

@ -0,0 +1,85 @@
package io.github.wulkanowy.services.synchronisation;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import java.io.IOException;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.user.PersonalData;
import io.github.wulkanowy.dao.entities.Account;
import io.github.wulkanowy.dao.entities.AccountDao;
import io.github.wulkanowy.dao.entities.DaoSession;
import io.github.wulkanowy.security.CryptoException;
import io.github.wulkanowy.security.Safety;
import io.github.wulkanowy.services.LoginSession;
import io.github.wulkanowy.services.jobs.VulcanSync;
public class AccountSynchronisation {
public LoginSession loginCurrentUser(Context context, DaoSession daoSession, Vulcan vulcan) throws CryptoException,
BadCredentialsException, AccountPermissionException, IOException, LoginErrorException {
AccountDao accountDao = daoSession.getAccountDao();
long userId = context.getSharedPreferences("LoginData", Context.MODE_PRIVATE).getLong("userId", 0);
if (userId != 0) {
Log.d(VulcanSync.DEBUG_TAG, "Login current user id=" + String.valueOf(userId));
Safety safety = new Safety(context);
Account account = accountDao.load(userId);
vulcan.login(
account.getEmail(),
safety.decrypt(account.getEmail(), account.getPassword()),
account.getSymbol()
);
return new LoginSession()
.setDaoSession(daoSession)
.setUserId(userId)
.setVulcan(vulcan);
} else {
Log.wtf(VulcanSync.DEBUG_TAG, "loginCurrentUser - USERID IS EMPTY");
throw new IOException("Can't find user with index 0");
}
}
public LoginSession loginNewUser(String email, String password, String symbol, Context context, DaoSession daoSession, Vulcan vulcan)
throws BadCredentialsException, NotLoggedInErrorException, AccountPermissionException, IOException, CryptoException {
long userId;
vulcan.login(email, password, symbol);
PersonalData personalData = vulcan.getBasicInformation().getPersonalData();
AccountDao accountDao = daoSession.getAccountDao();
Safety safety = new Safety(context);
Account account = new Account()
.setName(personalData.getFirstAndLastName())
.setEmail(email)
.setPassword(safety.encrypt(email, password))
.setSymbol(symbol);
userId = accountDao.insert(account);
Log.d(VulcanSync.DEBUG_TAG, "Login and save new user id=" + String.valueOf(userId));
SharedPreferences sharedPreferences = context.getSharedPreferences("LoginData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("userId", userId);
editor.apply();
return new LoginSession()
.setVulcan(vulcan)
.setUserId(userId)
.setDaoSession(daoSession);
}
}

View File

@ -1,42 +0,0 @@
package io.github.wulkanowy.services.synchronisation;
import android.app.Activity;
import android.util.Log;
import io.github.wulkanowy.activity.WulkanowyApp;
import io.github.wulkanowy.dao.entities.DaoSession;
import io.github.wulkanowy.services.jobs.VulcanSync;
public class DataSynchronisation {
private DaoSession daoSession;
public DataSynchronisation(DaoSession daoSession) {
this.daoSession = daoSession;
}
public DataSynchronisation(Activity activity) {
daoSession = ((WulkanowyApp) activity.getApplication()).getDaoSession();
}
public void syncGrades(VulcanSynchronisation vulcanSynchronisation) {
GradesSynchronisation gradesSynchronisation = new GradesSynchronisation();
try {
gradesSynchronisation.sync(vulcanSynchronisation, daoSession);
} catch (Exception e) {
Log.e(VulcanSync.DEBUG_TAG, "Synchronisation of grades failed", e);
}
}
public void syncSubjectsAndGrades(VulcanSynchronisation vulcanSynchronisation) {
SubjectsSynchronisation subjectsSynchronisation = new SubjectsSynchronisation();
try {
subjectsSynchronisation.sync(vulcanSynchronisation, daoSession);
syncGrades(vulcanSynchronisation);
} catch (Exception e) {
Log.e(VulcanSync.DEBUG_TAG, "Synchronisation of subjects failed", e);
}
}
}

View File

@ -10,30 +10,30 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.grades.GradesList;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.dao.EntitiesCompare;
import io.github.wulkanowy.dao.entities.Account;
import io.github.wulkanowy.dao.entities.AccountDao;
import io.github.wulkanowy.dao.entities.DaoSession;
import io.github.wulkanowy.dao.entities.Grade;
import io.github.wulkanowy.dao.entities.GradeDao;
import io.github.wulkanowy.dao.entities.Subject;
import io.github.wulkanowy.dao.entities.SubjectDao;
import io.github.wulkanowy.services.LoginSession;
import io.github.wulkanowy.services.jobs.VulcanSync;
import io.github.wulkanowy.utilities.ConversionVulcanObject;
public class GradesSynchronisation {
public void sync(VulcanSynchronisation vulcanSynchronisation, DaoSession daoSession) throws IOException,
ParseException, LoginErrorException {
public void sync(LoginSession loginSession) throws IOException,
ParseException, NotLoggedInErrorException {
GradesList gradesList = new GradesList(vulcanSynchronisation.getStudentAndParent());
GradesList gradesList = loginSession.getVulcan().getGradesList();
GradeDao gradeDao = daoSession.getGradeDao();
AccountDao accountDao = daoSession.getAccountDao();
SubjectDao subjectDao = daoSession.getSubjectDao();
GradeDao gradeDao = loginSession.getDaoSession().getGradeDao();
AccountDao accountDao = loginSession.getDaoSession().getAccountDao();
SubjectDao subjectDao = loginSession.getDaoSession().getSubjectDao();
Account account = accountDao.load(vulcanSynchronisation.getUserId());
Account account = accountDao.load(loginSession.getUserId());
List<Grade> gradesFromDb = account.getGradeList();
List<Grade> gradeEntitiesList = ConversionVulcanObject.gradesToGradeEntities(gradesList.getAll());
@ -49,7 +49,7 @@ public class GradesSynchronisation {
.where(SubjectDao.Properties.Name.eq(grade.getSubject()))
.build();
grade.setUserId(vulcanSynchronisation.getUserId());
grade.setUserId(loginSession.getUserId());
grade.setSubjectId((subjectQuery.uniqueOrThrow()).getId());
lastList.add(grade);

View File

@ -8,26 +8,26 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.grades.SubjectsList;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.dao.entities.DaoSession;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.dao.entities.Subject;
import io.github.wulkanowy.dao.entities.SubjectDao;
import io.github.wulkanowy.services.LoginSession;
import io.github.wulkanowy.services.jobs.VulcanSync;
import io.github.wulkanowy.utilities.ConversionVulcanObject;
public class SubjectsSynchronisation {
public void sync(VulcanSynchronisation vulcanSynchronisation, DaoSession daoSession) throws IOException,
ParseException, LoginErrorException {
public void sync(LoginSession loginSession) throws IOException,
ParseException, NotLoggedInErrorException {
SubjectsList subjectsList = new SubjectsList(vulcanSynchronisation.getStudentAndParent());
SubjectDao subjectDao = daoSession.getSubjectDao();
SubjectsList subjectsList = loginSession.getVulcan().getSubjectsList();
SubjectDao subjectDao = loginSession.getDaoSession().getSubjectDao();
List<Subject> subjectEntitiesList = ConversionVulcanObject.subjectsToSubjectEntities(subjectsList.getAll());
List<Subject> preparedList = new ArrayList<>();
for (Subject subject : subjectEntitiesList) {
subject.setUserId(vulcanSynchronisation.getUserId());
subject.setUserId(loginSession.getUserId());
preparedList.add(subject);
}

View File

@ -1,122 +0,0 @@
package io.github.wulkanowy.services.synchronisation;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import java.io.IOException;
import java.util.Map;
import io.github.wulkanowy.activity.WulkanowyApp;
import io.github.wulkanowy.api.Cookies;
import io.github.wulkanowy.api.StudentAndParent;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.Login;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.api.user.BasicInformation;
import io.github.wulkanowy.api.user.PersonalData;
import io.github.wulkanowy.dao.entities.Account;
import io.github.wulkanowy.dao.entities.AccountDao;
import io.github.wulkanowy.dao.entities.DaoSession;
import io.github.wulkanowy.security.CryptoException;
import io.github.wulkanowy.security.Safety;
import io.github.wulkanowy.services.jobs.VulcanSync;
public class VulcanSynchronisation {
private StudentAndParent studentAndParent;
private Long userId = 0L;
public void loginCurrentUser(Context context, DaoSession daoSession) throws CryptoException,
BadCredentialsException, LoginErrorException, AccountPermissionException, IOException {
AccountDao accountDao = daoSession.getAccountDao();
userId = context.getSharedPreferences("LoginData", Context.MODE_PRIVATE).getLong("userId", 0);
if (userId != 0) {
Log.d(VulcanSync.DEBUG_TAG, "Login current user id=" + String.valueOf(userId));
Safety safety = new Safety(context);
Account account = accountDao.load(userId);
Login login = loginUser(
account.getEmail(),
safety.decrypt(account.getEmail(), account.getPassword()),
account.getSymbol());
getAndSetStudentAndParentFromApi(account.getSymbol(), login.getCookies());
} else {
Log.wtf(VulcanSync.DEBUG_TAG, "loginCurrentUser - USERID IS EMPTY");
}
}
public void loginNewUser(String email, String password, String symbol, Context context, DaoSession daoSession)
throws BadCredentialsException, LoginErrorException, AccountPermissionException, IOException, CryptoException {
AccountDao accountDao = daoSession.getAccountDao();
Login login = loginUser(email, password, symbol);
Safety safety = new Safety(context);
BasicInformation basicInformation = new BasicInformation(getAndSetStudentAndParentFromApi(symbol, login.getCookies()));
PersonalData personalData = basicInformation.getPersonalData();
Account account = new Account()
.setName(personalData.getFirstAndLastName())
.setEmail(email)
.setPassword(safety.encrypt(email, password))
.setSymbol(symbol);
userId = accountDao.insert(account);
Log.d(VulcanSync.DEBUG_TAG, "Login and save new user id=" + String.valueOf(userId));
SharedPreferences sharedPreferences = context.getSharedPreferences("LoginData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("userId", userId);
editor.apply();
}
public void loginNewUser(String email, String password, String symbol, Activity activity)
throws BadCredentialsException, LoginErrorException, AccountPermissionException, IOException, CryptoException {
loginNewUser(email, password, symbol, activity, ((WulkanowyApp) activity.getApplication()).getDaoSession());
}
private Login loginUser(String email, String password, String symbol) throws BadCredentialsException,
LoginErrorException, AccountPermissionException {
Cookies cookies = new Cookies();
Login login = new Login(cookies);
login.login(email, password, symbol);
return login;
}
public Long getUserId() {
return userId;
}
public StudentAndParent getStudentAndParent() {
return studentAndParent;
}
private StudentAndParent getAndSetStudentAndParentFromApi(String symbol, Map<String, String> cookiesMap)
throws IOException, LoginErrorException {
if (studentAndParent == null) {
Cookies cookies = new Cookies();
cookies.setItems(cookiesMap);
StudentAndParent snp = new StudentAndParent(cookies, symbol);
studentAndParent = snp;
return snp;
} else {
return studentAndParent;
}
}
}