diff --git a/api/src/main/java/io/github/wulkanowy/api/Client.java b/api/src/main/java/io/github/wulkanowy/api/Client.java index f7a663ec..16b7982c 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Client.java +++ b/api/src/main/java/io/github/wulkanowy/api/Client.java @@ -5,44 +5,102 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.IOException; +import java.util.Date; import java.util.Map; +import java.util.concurrent.TimeUnit; + +import io.github.wulkanowy.api.login.Login; public class Client { - private String protocol; + private String protocol = "https"; - private String host; + private String host = "vulcan.net.pl"; - private String symbol; + private String email; + + private String password; + + private String symbol = "Default"; + + private Login login; + + private Date lastSuccessRequest = new Date(); private Cookies cookies = new Cookies(); - Client(String protocol, String host, String symbol) { - this.protocol = protocol; - this.host = host; + Client(String email, String password, String symbol) { + this.email = email; + this.password = password; this.symbol = symbol; + + setFullEndpointInfo(email); } - String getHost() { - return host; + private void setFullEndpointInfo(String info) { + String[] creds = info.split("\\\\"); + + email = info; + + if (creds.length > 2) { + String[] url = creds[0].split("://"); + + protocol = url[0]; + host = url[1]; + email = creds[2]; + } + } + + private void login() throws IOException, VulcanException { + if (isLoggedIn()) { + return; + } + + this.symbol = getLogin().login(email, password, symbol); + } + + private boolean isLoggedIn() { + return getCookies().size() > 0 && + 29 > TimeUnit.MILLISECONDS.toMinutes(new Date().getTime() - lastSuccessRequest.getTime()); + + } + + Login getLogin() { + if (null != login) { + return login; + } + + login = new Login(this); + + return login; + } + + public String getSymbol() { + return symbol; } public void setSymbol(String symbol) { this.symbol = symbol; } - Map getCookies() { + private Map getCookies() { return cookies.getItems(); } - private String getFilledUrl(String url) { + String getHost() { + return host; + } + + String getFilledUrl(String url) { return url .replace("{schema}", protocol) .replace("{host}", host.replace(":", "%253A")) - .replace("{symbol}", symbol == null ? "Default" : symbol); + .replace("{symbol}", symbol); } - Document getPageByUrl(String url) throws IOException { + Document getPageByUrl(String url) throws IOException, VulcanException { + login(); + Connection.Response response = Jsoup.connect(getFilledUrl(url)) .followRedirects(true) .cookies(getCookies()) @@ -50,10 +108,10 @@ public class Client { this.cookies.addItems(response.cookies()); - return response.parse(); + return checkForErrors(response.parse()); } - public Document postPageByUrl(String url, String[][] params) throws IOException { + public Document postPageByUrl(String url, String[][] params) throws IOException, VulcanException { Connection connection = Jsoup.connect(getFilledUrl(url)); for (String[] data : params) { @@ -68,10 +126,12 @@ public class Client { this.cookies.addItems(response.cookies()); - return response.parse(); + return checkForErrors(response.parse()); } - public String getJsonStringByUrl(String url) throws IOException { + public String getJsonStringByUrl(String url) throws IOException, VulcanException { + login(); + Connection.Response response = Jsoup.connect(getFilledUrl(url)) .followRedirects(true) .ignoreContentType(true) @@ -83,7 +143,9 @@ public class Client { return response.body(); } - public String postJsonStringByUrl(String url, String[][] params) throws IOException { + public String postJsonStringByUrl(String url, String[][] params) throws IOException, VulcanException { + login(); + Connection connection = Jsoup.connect(getFilledUrl(url)); for (String[] data : params) { @@ -101,4 +163,18 @@ public class Client { return response.body(); } + + Document checkForErrors(Document doc) throws VulcanException { + if ("Przerwa techniczna".equals(doc.select("title").text())) { + throw new VulcanOfflineException(); + } + + if ("Zaloguj się".equals(doc.select(".loginButton").text())) { + throw new NotLoggedInErrorException(); + } + + lastSuccessRequest = new Date(); + + return doc; + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/Cookies.java b/api/src/main/java/io/github/wulkanowy/api/Cookies.java index c97d9658..dfe4c4b5 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Cookies.java +++ b/api/src/main/java/io/github/wulkanowy/api/Cookies.java @@ -3,21 +3,15 @@ package io.github.wulkanowy.api; import java.util.HashMap; import java.util.Map; -public class Cookies { +class Cookies { private Map jar = new HashMap<>(); - public Map getItems() { + Map getItems() { return jar; } - public Cookies setItems(Map items) { - this.jar = items; - return this; - } - - public Cookies addItems(Map items) { - this.jar.putAll(items); - return this; + void addItems(Map items) { + jar.putAll(items); } } diff --git a/api/src/main/java/io/github/wulkanowy/api/NotLoggedInErrorException.java b/api/src/main/java/io/github/wulkanowy/api/NotLoggedInErrorException.java new file mode 100644 index 00000000..86372266 --- /dev/null +++ b/api/src/main/java/io/github/wulkanowy/api/NotLoggedInErrorException.java @@ -0,0 +1,4 @@ +package io.github.wulkanowy.api; + +public class NotLoggedInErrorException extends VulcanException { +} diff --git a/api/src/main/java/io/github/wulkanowy/api/SnP.java b/api/src/main/java/io/github/wulkanowy/api/SnP.java index 614b77b6..adbffd4b 100644 --- a/api/src/main/java/io/github/wulkanowy/api/SnP.java +++ b/api/src/main/java/io/github/wulkanowy/api/SnP.java @@ -6,19 +6,17 @@ import org.jsoup.nodes.Element; import java.io.IOException; import java.util.List; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; - public interface SnP { String getId(); - void storeContextCookies() throws IOException, NotLoggedInErrorException; + StudentAndParent storeContextCookies() throws IOException, VulcanException; String getRowDataChildValue(Element e, int index); - Document getSnPPageDocument(String url) throws IOException; + Document getSnPPageDocument(String url) throws IOException, VulcanException; - List getSemesters() throws IOException; + List getSemesters() throws IOException, VulcanException; List getSemesters(Document gradesPage); diff --git a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java index a8d61913..c014e96c 100644 --- a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java +++ b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java @@ -8,8 +8,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; - public class StudentAndParent implements SnP { private static final String START_PAGE_URL = "{schema}://uonetplus.{host}/{symbol}/Start.mvc/Index"; @@ -22,12 +20,8 @@ public class StudentAndParent implements SnP { private String id; - StudentAndParent(Client client) { - this.client = client; - } - StudentAndParent(Client client, String id) { - this(client); + this.client = client; this.id = id; } @@ -39,11 +33,12 @@ public class StudentAndParent implements SnP { return id; } - public void storeContextCookies() throws IOException, NotLoggedInErrorException { + public StudentAndParent storeContextCookies() throws IOException, VulcanException { client.getPageByUrl(getSnpHomePageUrl()); + return this; } - String getSnpHomePageUrl() throws IOException, NotLoggedInErrorException { + String getSnpHomePageUrl() throws IOException, VulcanException { if (null != getId()) { return getBaseUrl(); } @@ -77,11 +72,11 @@ public class StudentAndParent implements SnP { return e.select(".daneWiersz .wartosc").get(index - 1).text(); } - public Document getSnPPageDocument(String url) throws IOException { + public Document getSnPPageDocument(String url) throws IOException, VulcanException { return client.getPageByUrl(getBaseUrl() + url); } - public List getSemesters() throws IOException { + public List getSemesters() throws IOException, VulcanException { return getSemesters(getSnPPageDocument(GRADES_PAGE_URL)); } diff --git a/api/src/main/java/io/github/wulkanowy/api/Vulcan.java b/api/src/main/java/io/github/wulkanowy/api/Vulcan.java index a44e868f..c16ef088 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Vulcan.java +++ b/api/src/main/java/io/github/wulkanowy/api/Vulcan.java @@ -7,12 +7,6 @@ import io.github.wulkanowy.api.attendance.AttendanceTable; import io.github.wulkanowy.api.exams.ExamsWeek; 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.login.VulcanOfflineException; import io.github.wulkanowy.api.messages.Messages; import io.github.wulkanowy.api.notes.AchievementsList; import io.github.wulkanowy.api.notes.NotesList; @@ -26,171 +20,92 @@ public class Vulcan { private String id; - private String symbol; - private SnP snp; - private String protocolSchema = "https"; - - private String logHost = "vulcan.net.pl"; - - private String email; - private Client client; - private Login login; - - public void setClient(Client client) { - this.client = client; - } - - public void setLogin(Login login) { - this.login = login; - } - - public void login(String email, String password, String symbol) - throws BadCredentialsException, AccountPermissionException, - LoginErrorException, IOException, VulcanOfflineException { - - setFullEndpointInfo(email); - login = getLogin(); - - this.symbol = login.login(this.email, password, symbol); - } - - public Vulcan login(String email, String password, String symbol, String id) - throws BadCredentialsException, AccountPermissionException, - LoginErrorException, IOException, VulcanOfflineException { - login(email, password, symbol); + public void setCredentials(String email, String password, String symbol, String id) { + client = new Client(email, password, symbol); this.id = id; - - return this; } - String getProtocolSchema() { - return protocolSchema; - } - - String getLogHost() { - return logHost; - } - - public String getEmail() { - return email; - } - - public String getSymbol() { - return symbol; - } - - private void setFullEndpointInfo(String email) { - String[] creds = email.split("\\\\"); - - this.email = email; - - if (creds.length >= 2) { - String[] url = creds[0].split("://"); - - this.protocolSchema = url[0]; - this.logHost = url[1]; - this.email = creds[2]; + public Client getClient() throws NotLoggedInErrorException { + if (null == client) { + throw new NotLoggedInErrorException(); } - } - - protected Client getClient() { - if (null != client) { - return client; - } - - client = new Client(getProtocolSchema(), getLogHost(), symbol); return client; } - protected Login getLogin() { - if (null != login) { - return login; - } + public String getSymbol() throws NotLoggedInErrorException { + return getClient().getSymbol(); - login = new Login(getClient()); - - return login; } - public SnP getStudentAndParent() throws IOException, NotLoggedInErrorException { - if (0 == getClient().getCookies().size()) { - throw new NotLoggedInErrorException(); + public SnP getStudentAndParent() throws IOException, VulcanException { + if (null != this.snp) { + return this.snp; } - if (null != snp) { - return snp; - } + this.snp = new StudentAndParent(getClient(), id).storeContextCookies(); - snp = createSnp(getClient(), id); - - snp.storeContextCookies(); - - return snp; + return this.snp; } - SnP createSnp(Client client, String id) { - if (null == id) { - return new StudentAndParent(client); - } - - return new StudentAndParent(client, id); + public String getId() throws IOException, VulcanException { + return getStudentAndParent().getId(); } - public AttendanceStatistics getAttendanceStatistics() throws IOException, NotLoggedInErrorException { - return new AttendanceStatistics(getStudentAndParent()); - } - - public AttendanceTable getAttendanceTable() throws IOException, NotLoggedInErrorException { + public AttendanceTable getAttendanceTable() throws IOException, VulcanException { return new AttendanceTable(getStudentAndParent()); } - public ExamsWeek getExamsList() throws IOException, NotLoggedInErrorException { + public AttendanceStatistics getAttendanceStatistics() throws IOException, VulcanException { + return new AttendanceStatistics(getStudentAndParent()); + } + + public ExamsWeek getExamsList() throws IOException, VulcanException { return new ExamsWeek(getStudentAndParent()); } - public GradesList getGradesList() throws IOException, NotLoggedInErrorException { + public GradesList getGradesList() throws IOException, VulcanException { return new GradesList(getStudentAndParent()); } - public SubjectsList getSubjectsList() throws IOException, NotLoggedInErrorException { + public SubjectsList getSubjectsList() throws IOException, VulcanException { return new SubjectsList(getStudentAndParent()); } - public AchievementsList getAchievementsList() throws IOException, NotLoggedInErrorException { + public AchievementsList getAchievementsList() throws IOException, VulcanException { return new AchievementsList(getStudentAndParent()); } - public NotesList getNotesList() throws IOException, NotLoggedInErrorException { + public NotesList getNotesList() throws IOException, VulcanException { return new NotesList(getStudentAndParent()); } - public SchoolInfo getSchoolInfo() throws IOException, NotLoggedInErrorException { + public SchoolInfo getSchoolInfo() throws IOException, VulcanException { return new SchoolInfo(getStudentAndParent()); } - public TeachersInfo getTeachersInfo() throws IOException, NotLoggedInErrorException { + public TeachersInfo getTeachersInfo() throws IOException, VulcanException { return new TeachersInfo(getStudentAndParent()); } - public Timetable getTimetable() throws IOException, NotLoggedInErrorException { + public Timetable getTimetable() throws IOException, VulcanException { return new Timetable(getStudentAndParent()); } - public BasicInformation getBasicInformation() throws IOException, NotLoggedInErrorException { + public BasicInformation getBasicInformation() throws IOException, VulcanException { return new BasicInformation(getStudentAndParent()); } - public FamilyInformation getFamilyInformation() throws IOException, NotLoggedInErrorException { + public FamilyInformation getFamilyInformation() throws IOException, VulcanException { return new FamilyInformation(getStudentAndParent()); } - public Messages getMessages() { + public Messages getMessages() throws VulcanException { return new Messages(getClient()); } } diff --git a/api/src/main/java/io/github/wulkanowy/api/VulcanException.java b/api/src/main/java/io/github/wulkanowy/api/VulcanException.java new file mode 100644 index 00000000..0e7ed243 --- /dev/null +++ b/api/src/main/java/io/github/wulkanowy/api/VulcanException.java @@ -0,0 +1,4 @@ +package io.github.wulkanowy.api; + +public abstract class VulcanException extends Exception { +} diff --git a/api/src/main/java/io/github/wulkanowy/api/VulcanOfflineException.java b/api/src/main/java/io/github/wulkanowy/api/VulcanOfflineException.java new file mode 100644 index 00000000..497fba94 --- /dev/null +++ b/api/src/main/java/io/github/wulkanowy/api/VulcanOfflineException.java @@ -0,0 +1,4 @@ +package io.github.wulkanowy.api; + +public class VulcanOfflineException extends VulcanException { +} diff --git a/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceStatistics.java b/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceStatistics.java index 023a436e..29b6a6d9 100644 --- a/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceStatistics.java +++ b/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceStatistics.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.List; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.generic.Month; import io.github.wulkanowy.api.generic.Subject; @@ -22,15 +23,15 @@ public class AttendanceStatistics { this.snp = snp; } - public Types getTypesTable() throws IOException { + public Types getTypesTable() throws IOException, VulcanException { return getTypesTable(""); } - public Types getTypesTable(String tick) throws IOException { + public Types getTypesTable(String tick) throws IOException, VulcanException { return getTypesTable(tick, -1); } - public List getSubjectList() throws IOException { + public List getSubjectList() throws IOException, VulcanException { Element mainContainer = snp.getSnPPageDocument(attendancePageUrl) .select(".mainContainer #idPrzedmiot").first(); @@ -46,7 +47,7 @@ public class AttendanceStatistics { return subjectList; } - public Types getTypesTable(String tick, Integer subjectId) throws IOException { + public Types getTypesTable(String tick, Integer subjectId) throws IOException, VulcanException { Element mainContainer = snp.getSnPPageDocument((attendancePageUrl + "?data={tick}&idPrzedmiot={subject}") .replace("{tick}", tick) diff --git a/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceTable.java b/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceTable.java index 677a6de0..c45cbe6a 100644 --- a/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceTable.java +++ b/api/src/main/java/io/github/wulkanowy/api/attendance/AttendanceTable.java @@ -12,26 +12,28 @@ import java.util.List; import java.util.Locale; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.generic.Day; import io.github.wulkanowy.api.generic.Lesson; import io.github.wulkanowy.api.generic.Week; public class AttendanceTable { - private SnP snp; - private final static String ATTENDANCE_PAGE_URL = "Frekwencja.mvc?data="; + private SnP snp; + public AttendanceTable(SnP snp) { this.snp = snp; } - public Week getWeekTable() throws IOException, ParseException { + public Week getWeekTable() throws IOException, ParseException, VulcanException { return getWeekTable(""); } - public Week getWeekTable(String tick) throws IOException, ParseException { + public Week getWeekTable(String tick) throws IOException, ParseException, VulcanException { Element table = snp.getSnPPageDocument(ATTENDANCE_PAGE_URL + tick) + .select(".mainContainer .presentData").first(); Elements headerCells = table.select("thead th"); diff --git a/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java b/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java index b4921b8b..3dd056d9 100644 --- a/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java +++ b/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.List; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.generic.Week; public class ExamsWeek { @@ -21,11 +22,11 @@ public class ExamsWeek { this.snp = snp; } - public Week getCurrent() throws IOException { + public Week getCurrent() throws IOException, VulcanException { return getWeek("", true); } - public Week getWeek(String tick, final boolean onlyNotEmpty) throws IOException { + public Week getWeek(String tick, final boolean onlyNotEmpty) throws IOException, VulcanException { Document examsPage = snp.getSnPPageDocument(EXAMS_PAGE_URL + tick); Elements examsDays = examsPage.select(".mainContainer > div:not(.navigation)"); diff --git a/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java b/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java index 44c45c9a..ce8c0f77 100644 --- a/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java +++ b/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java @@ -16,6 +16,7 @@ import java.util.regex.Pattern; import io.github.wulkanowy.api.Semester; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; public class GradesList { @@ -33,11 +34,11 @@ public class GradesList { return GRADES_PAGE_URL; } - public List getAll() throws IOException, ParseException { + public List getAll() throws IOException, ParseException, VulcanException { return getAll(""); } - public List getAll(String semester) throws IOException, ParseException { + public List getAll(String semester) throws IOException, ParseException, VulcanException { Document gradesPage = snp.getSnPPageDocument(getGradesPageUrl() + semester); Elements gradesRows = gradesPage.select(".ocenySzczegoly-table > tbody > tr"); Semester currentSemester = snp.getCurrentSemester(snp.getSemesters(gradesPage)); diff --git a/api/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java b/api/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java index decf4468..99359413 100644 --- a/api/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java +++ b/api/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.List; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; public class SubjectsList { @@ -20,7 +21,7 @@ public class SubjectsList { this.snp = snp; } - public List getAll() throws IOException { + public List getAll() throws IOException, VulcanException { Document subjectPage = snp.getSnPPageDocument(SUBJECTS_PAGE_URL); Elements rows = subjectPage.select(".ocenyZwykle-table > tbody > tr"); diff --git a/api/src/main/java/io/github/wulkanowy/api/login/AccountPermissionException.java b/api/src/main/java/io/github/wulkanowy/api/login/AccountPermissionException.java index 15be359c..99feb86f 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/AccountPermissionException.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/AccountPermissionException.java @@ -1,4 +1,6 @@ package io.github.wulkanowy.api.login; -public class AccountPermissionException extends Exception { +import io.github.wulkanowy.api.VulcanException; + +public class AccountPermissionException extends VulcanException { } diff --git a/api/src/main/java/io/github/wulkanowy/api/login/BadCredentialsException.java b/api/src/main/java/io/github/wulkanowy/api/login/BadCredentialsException.java index 4502bb15..1ac37f9c 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/BadCredentialsException.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/BadCredentialsException.java @@ -1,4 +1,6 @@ package io.github.wulkanowy.api.login; -public class BadCredentialsException extends Exception { +import io.github.wulkanowy.api.VulcanException; + +public class BadCredentialsException extends VulcanException { } diff --git a/api/src/main/java/io/github/wulkanowy/api/login/Login.java b/api/src/main/java/io/github/wulkanowy/api/login/Login.java index e680804a..7f88037d 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/Login.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/Login.java @@ -8,6 +8,7 @@ import org.jsoup.select.Elements; import java.io.IOException; import io.github.wulkanowy.api.Client; +import io.github.wulkanowy.api.VulcanException; public class Login { @@ -27,16 +28,13 @@ public class Login { this.client = client; } - public String login(String email, String password, String symbol) - throws BadCredentialsException, LoginErrorException, - AccountPermissionException, IOException, VulcanOfflineException { + public String login(String email, String password, String symbol) throws VulcanException, IOException { String certificate = sendCredentials(email, password, symbol); return sendCertificate(certificate, symbol); } - String sendCredentials(String email, String password, String symbol) - throws IOException, BadCredentialsException { + String sendCredentials(String email, String password, String symbol) throws IOException, VulcanException { this.symbol = symbol; Document html = client.postPageByUrl(LOGIN_PAGE_URL, new String[][]{ @@ -51,25 +49,20 @@ public class Login { return html.select("input[name=wresult]").attr("value"); } - String sendCertificate(String certificate, String defaultSymbol) - throws IOException, LoginErrorException, AccountPermissionException, VulcanOfflineException { + String sendCertificate(String certificate, String defaultSymbol) throws IOException, VulcanException { this.symbol = findSymbol(defaultSymbol, certificate); client.setSymbol(this.symbol); - Document html = client.postPageByUrl(LOGIN_ENDPOINT_PAGE_URL, new String[][]{ + String title = client.postPageByUrl(LOGIN_ENDPOINT_PAGE_URL, new String[][]{ {"wa", "wsignin1.0"}, {"wresult", certificate} - }); + }).select("title").text(); - if (html.getElementsByTag("title").text().equals("Logowanie")) { + if ("Logowanie".equals(title)) { throw new AccountPermissionException(); } - if (html.getElementsByTag("title").text().equals("Przerwa techniczna")) { - throw new VulcanOfflineException(); - } - - if (!html.select("title").text().equals("Uonet+")) { + if (!"Uonet+".equals(title)) { throw new LoginErrorException(); } @@ -85,7 +78,8 @@ public class Login { } String findSymbolInCertificate(String certificate) { - Elements els = Jsoup.parse(certificate.replaceAll(":", ""), "", Parser.xmlParser()) + Elements els = Jsoup + .parse(certificate.replaceAll(":", ""), "", Parser.xmlParser()) .select("[AttributeName=\"UserInstance\"] samlAttributeValue"); if (els.isEmpty()) { @@ -94,4 +88,4 @@ public class Login { return els.get(1).text(); } -} \ No newline at end of file +} diff --git a/api/src/main/java/io/github/wulkanowy/api/login/LoginErrorException.java b/api/src/main/java/io/github/wulkanowy/api/login/LoginErrorException.java index eec2b4e4..e264dc67 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/LoginErrorException.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/LoginErrorException.java @@ -1,4 +1,6 @@ package io.github.wulkanowy.api.login; +import io.github.wulkanowy.api.NotLoggedInErrorException; + public class LoginErrorException extends NotLoggedInErrorException { } diff --git a/api/src/main/java/io/github/wulkanowy/api/login/NotLoggedInErrorException.java b/api/src/main/java/io/github/wulkanowy/api/login/NotLoggedInErrorException.java deleted file mode 100644 index d33b892d..00000000 --- a/api/src/main/java/io/github/wulkanowy/api/login/NotLoggedInErrorException.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.github.wulkanowy.api.login; - -public class NotLoggedInErrorException extends Exception { -} diff --git a/api/src/main/java/io/github/wulkanowy/api/login/VulcanOfflineException.java b/api/src/main/java/io/github/wulkanowy/api/login/VulcanOfflineException.java deleted file mode 100644 index 45cf6458..00000000 --- a/api/src/main/java/io/github/wulkanowy/api/login/VulcanOfflineException.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.github.wulkanowy.api.login; - -public class VulcanOfflineException extends Exception { -} diff --git a/api/src/main/java/io/github/wulkanowy/api/messages/BadRequestException.java b/api/src/main/java/io/github/wulkanowy/api/messages/BadRequestException.java index 90af17ce..ed407b4d 100644 --- a/api/src/main/java/io/github/wulkanowy/api/messages/BadRequestException.java +++ b/api/src/main/java/io/github/wulkanowy/api/messages/BadRequestException.java @@ -1,4 +1,6 @@ package io.github.wulkanowy.api.messages; -class BadRequestException extends Exception { +import io.github.wulkanowy.api.VulcanException; + +class BadRequestException extends VulcanException { } diff --git a/api/src/main/java/io/github/wulkanowy/api/messages/Messages.java b/api/src/main/java/io/github/wulkanowy/api/messages/Messages.java index 4eb969cd..eb5f8bba 100644 --- a/api/src/main/java/io/github/wulkanowy/api/messages/Messages.java +++ b/api/src/main/java/io/github/wulkanowy/api/messages/Messages.java @@ -7,7 +7,8 @@ import java.io.IOException; import java.util.List; import io.github.wulkanowy.api.Client; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; +import io.github.wulkanowy.api.NotLoggedInErrorException; +import io.github.wulkanowy.api.VulcanException; public class Messages { @@ -37,19 +38,19 @@ public class Messages { this.client = client; } - public List getReceived() throws IOException, NotLoggedInErrorException, BadRequestException { + public List getReceived() throws IOException, VulcanException { return getMessages(RECEIVED_URL); } - public List getSent() throws IOException, NotLoggedInErrorException, BadRequestException { + public List getSent() throws IOException, VulcanException { return getMessages(SENT_URL); } - public List getDeleted() throws IOException, NotLoggedInErrorException, BadRequestException { + public List getDeleted() throws IOException, VulcanException { return getMessages(DELETED_URL); } - private List getMessages(String url) throws IOException, NotLoggedInErrorException, BadRequestException { + private List getMessages(String url) throws IOException, VulcanException { String res = client.getJsonStringByUrl(url); List messages; @@ -67,7 +68,7 @@ public class Messages { return messages; } - public Message getMessage(int id, int folder) throws IOException, BadRequestException, NotLoggedInErrorException { + public Message getMessage(int id, int folder) throws IOException, VulcanException { String res = client.postJsonStringByUrl(MESSAGE_URL, new String[][]{ {"idWiadomosc", String.valueOf(id)}, {"Folder", String.valueOf(folder)} diff --git a/api/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java b/api/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java index c821e8be..11d01342 100644 --- a/api/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java +++ b/api/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; public class AchievementsList { @@ -21,7 +22,7 @@ public class AchievementsList { this.snp = snp; } - public List getAllAchievements() throws IOException { + public List getAllAchievements() throws IOException, VulcanException { Element pageFragment = snp.getSnPPageDocument(NOTES_PAGE_URL) .select(".mainContainer > div").get(1); Elements items = pageFragment.select("article"); diff --git a/api/src/main/java/io/github/wulkanowy/api/notes/NotesList.java b/api/src/main/java/io/github/wulkanowy/api/notes/NotesList.java index 34b8540d..0c8a30b6 100644 --- a/api/src/main/java/io/github/wulkanowy/api/notes/NotesList.java +++ b/api/src/main/java/io/github/wulkanowy/api/notes/NotesList.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; public class NotesList { @@ -21,7 +22,7 @@ public class NotesList { this.snp = snp; } - public List getAllNotes() throws IOException { + public List getAllNotes() throws IOException, VulcanException { Element pageFragment = snp.getSnPPageDocument(NOTES_PAGE_URL) .select(".mainContainer > div").get(0); Elements items = pageFragment.select("article"); diff --git a/api/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java b/api/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java index 4eeb641c..51a7278b 100644 --- a/api/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java +++ b/api/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java @@ -5,6 +5,7 @@ import org.jsoup.nodes.Element; import java.io.IOException; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; public class SchoolInfo { @@ -16,7 +17,7 @@ public class SchoolInfo { this.snp = snp; } - public SchoolData getSchoolData() throws IOException { + public SchoolData getSchoolData() throws IOException, VulcanException { Element e = snp.getSnPPageDocument(SCHOOL_PAGE_URL) .select(".mainContainer > article").get(0); diff --git a/api/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java b/api/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java index e8af6744..bbf5f5d7 100644 --- a/api/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java +++ b/api/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.List; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; public class TeachersInfo { @@ -20,7 +21,7 @@ public class TeachersInfo { this.snp = snp; } - public TeachersData getTeachersData() throws IOException { + public TeachersData getTeachersData() throws IOException, VulcanException { Document doc = snp.getSnPPageDocument(SCHOOL_PAGE_URL); Elements rows = doc.select(".mainContainer > table tbody tr"); String description = doc.select(".mainContainer > p").first().text(); diff --git a/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java b/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java index 1d526494..f575edb3 100644 --- a/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java +++ b/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Locale; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.generic.Day; import io.github.wulkanowy.api.generic.Lesson; import io.github.wulkanowy.api.generic.Week; @@ -27,11 +28,11 @@ public class Timetable { this.snp = snp; } - public Week getWeekTable() throws IOException, ParseException { + public Week getWeekTable() throws IOException, ParseException, VulcanException { return getWeekTable(""); } - public Week getWeekTable(final String tick) throws IOException, ParseException { + public Week getWeekTable(final String tick) throws IOException, ParseException, VulcanException { Element table = snp.getSnPPageDocument(TIMETABLE_PAGE_URL + tick) .select(".mainContainer .presentData").first(); diff --git a/api/src/main/java/io/github/wulkanowy/api/user/BasicInformation.java b/api/src/main/java/io/github/wulkanowy/api/user/BasicInformation.java index 875cc8da..4edc5117 100644 --- a/api/src/main/java/io/github/wulkanowy/api/user/BasicInformation.java +++ b/api/src/main/java/io/github/wulkanowy/api/user/BasicInformation.java @@ -6,6 +6,7 @@ import org.jsoup.nodes.Element; import java.io.IOException; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; public class BasicInformation { @@ -21,7 +22,7 @@ public class BasicInformation { this.snp = snp; } - public Document getStudentDataPageDocument() throws IOException { + public Document getStudentDataPageDocument() throws IOException, VulcanException { if (null == studentDataPageDocument) { studentDataPageDocument = snp.getSnPPageDocument(STUDENT_DATA_PAGE_URL); } @@ -29,7 +30,7 @@ public class BasicInformation { return studentDataPageDocument; } - public PersonalData getPersonalData() throws IOException { + public PersonalData getPersonalData() throws IOException, VulcanException { Element e = getStudentDataPageDocument().select(CONTENT_QUERY).get(0); String name = snp.getRowDataChildValue(e, 1); @@ -48,7 +49,7 @@ public class BasicInformation { .setParentsNames(snp.getRowDataChildValue(e, 7)); } - public AddressData getAddressData() throws IOException { + public AddressData getAddressData() throws IOException, VulcanException { Element e = getStudentDataPageDocument().select(CONTENT_QUERY).get(1); return new AddressData() @@ -58,7 +59,7 @@ public class BasicInformation { } - public ContactDetails getContactDetails() throws IOException { + public ContactDetails getContactDetails() throws IOException, VulcanException { Element e = getStudentDataPageDocument().select(CONTENT_QUERY).get(2); return new ContactDetails() diff --git a/api/src/main/java/io/github/wulkanowy/api/user/FamilyInformation.java b/api/src/main/java/io/github/wulkanowy/api/user/FamilyInformation.java index 555de866..f5a459ee 100644 --- a/api/src/main/java/io/github/wulkanowy/api/user/FamilyInformation.java +++ b/api/src/main/java/io/github/wulkanowy/api/user/FamilyInformation.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; import io.github.wulkanowy.api.SnP; +import io.github.wulkanowy.api.VulcanException; public class FamilyInformation { @@ -19,7 +20,7 @@ public class FamilyInformation { this.snp = snp; } - public List getFamilyMembers() throws IOException { + public List getFamilyMembers() throws IOException, VulcanException { Elements membersElements = snp.getSnPPageDocument(STUDENT_DATA_PAGE_URL) .select(".mainContainer > article:nth-of-type(n+4)"); diff --git a/api/src/test/java/io/github/wulkanowy/api/ClientTest.java b/api/src/test/java/io/github/wulkanowy/api/ClientTest.java new file mode 100644 index 00000000..4aa1be91 --- /dev/null +++ b/api/src/test/java/io/github/wulkanowy/api/ClientTest.java @@ -0,0 +1,80 @@ +package io.github.wulkanowy.api; + +import org.hamcrest.CoreMatchers; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.junit.Assert; +import org.junit.Test; + +import io.github.wulkanowy.api.login.Login; + +public class ClientTest { + + private String getFixtureAsString(String fixtureFileName) { + return FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)); + } + + @Test + public void setFullEndpointInfoTest() throws Exception { + Client client = new Client("http://fakelog.net\\\\admin", "pass", "Default"); + + Assert.assertEquals("fakelog.net", client.getHost()); + Assert.assertEquals("Default", client.getSymbol()); + } + + @Test + public void checkForNoErrorsTest() throws Exception { + Client client = new Client("", "", ""); + + Document doc = Jsoup.parse(getFixtureAsString("login/Logowanie-success.html")); + + Assert.assertEquals(doc, client.checkForErrors(doc)); + } + + @Test(expected = VulcanOfflineException.class) + public void checkForErrorsOffline() throws Exception { + Client client = new Client("", "", ""); + + Document doc = Jsoup.parse(getFixtureAsString("login/PrzerwaTechniczna.html")); + + client.checkForErrors(doc); + } + + @Test(expected = NotLoggedInErrorException.class) + public void checkForErrors() throws Exception { + Client client = new Client("", "", ""); + + Document doc = Jsoup.parse(getFixtureAsString("login/Logowanie-notLoggedIn.html")); + + client.checkForErrors(doc); + } + + @Test + public void getClientTest() throws Exception { + Client client = new Client("", "", ""); + + Assert.assertThat(client.getLogin(), CoreMatchers.instanceOf(Login.class)); + } + + @Test + public void getClientTwiceTest() throws Exception { + Client client = new Client("", "", ""); + + Assert.assertEquals(client.getLogin(), client.getLogin()); + } + + @Test + public void getFilledUrlTest() throws Exception { + Client client = new Client("http://fakelog.cf\\\\admin", "", "symbol123"); + + Assert.assertEquals("http://uonetplus.fakelog.cf/symbol123/LoginEndpoint.aspx", + client.getFilledUrl("{schema}://uonetplus.{host}/{symbol}/LoginEndpoint.aspx")); + } + + @Test + public void getSymbolTest() throws Exception { + Client client = new Client("", "", "symbol4321"); + + Assert.assertEquals("symbol4321", client.getSymbol()); + } +} diff --git a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java index 89a5b190..5cde9b64 100644 --- a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java @@ -10,8 +10,6 @@ import org.mockito.Mockito; import java.util.ArrayList; import java.util.List; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; - public class StudentAndParentTest { private Client client; @@ -45,7 +43,7 @@ public class StudentAndParentTest { Mockito.when(client.getHost()).thenReturn("vulcan.net.pl"); Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(startPageDocument); - StudentAndParent snp = new StudentAndParent(client); + StudentAndParent snp = new StudentAndParent(client, null); Assert.assertEquals("https://uonetplus-opiekun.vulcan.net.pl/symbol/534213/Start/Index/", snp.getSnpHomePageUrl()); @@ -58,7 +56,7 @@ public class StudentAndParentTest { ); Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(wrongPageDocument); - StudentAndParent snp = new StudentAndParent(client); + StudentAndParent snp = new StudentAndParent(client, null); snp.getSnpHomePageUrl(); } diff --git a/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java b/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java index a80c798a..bda66e8d 100644 --- a/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java @@ -1,86 +1,31 @@ package io.github.wulkanowy.api; +import org.hamcrest.CoreMatchers; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; - -import java.util.HashMap; -import java.util.Map; - -import io.github.wulkanowy.api.login.Login; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; public class VulcanTest { - private Vulcan vulcan; + @Test(expected = NotLoggedInErrorException.class) + public void getClientWithoutLoginTest() throws Exception { + Vulcan vulcan = new Vulcan(); - @Before - public void setUp() throws Exception { - vulcan = new Vulcan(); - vulcan.setClient(Mockito.mock(Client.class)); - vulcan.setLogin(Mockito.mock(Login.class)); + vulcan.getClient(); } @Test - public void setFullEndpointInfoTest() throws Exception { - vulcan.login("http://fakelog.net\\\\admin", "pass", "Default", "123"); + public void getClientTest() throws Exception { + Vulcan vulcan = new Vulcan(); + vulcan.setCredentials("email", "password", "symbol", null); - Assert.assertEquals("http", vulcan.getProtocolSchema()); - Assert.assertEquals("fakelog.net", vulcan.getLogHost()); - Assert.assertEquals("admin", vulcan.getEmail()); + Assert.assertThat(vulcan.getClient(), CoreMatchers.instanceOf(Client.class)); } @Test public void getClientTwiceTest() throws Exception { Vulcan vulcan = new Vulcan(); - Assert.assertTrue(vulcan.getClient().equals(vulcan.getClient())); - } + vulcan.setCredentials("email", "password", "symbol", null); - @Test - public void getLoginTwiceTest() throws Exception { - Vulcan vulcan = new Vulcan(); - Assert.assertTrue(vulcan.getLogin().equals(vulcan.getLogin())); - } - - @Test(expected = NotLoggedInErrorException.class) - public void getStudentAndParentNotLoggedInTest() throws Exception { - vulcan.getStudentAndParent(); - } - - @Test - public void getStudentAndParentTwiceTest() throws Exception { - Client client = Mockito.mock(Client.class); - Map cookies = new HashMap<>(); - cookies.put("test", "test"); - Mockito.when(client.getCookies()).thenReturn(cookies); - - SnP snp = Mockito.mock(StudentAndParent.class); - Mockito.doNothing().when(snp).storeContextCookies(); - - Vulcan vulcan = Mockito.mock(Vulcan.class); - Mockito.when(vulcan.getClient()).thenReturn(client); - Mockito.when(vulcan.getStudentAndParent()).thenCallRealMethod(); - Mockito.when(vulcan.createSnp(Mockito.any(Client.class), Mockito.any())).thenReturn(snp); - - vulcan.getStudentAndParent(); - vulcan.getStudentAndParent(); - } - - @Test - public void createSnPTest() throws Exception { - vulcan.login("wulkanowy@wulkanowy.io", "wulkanowy123", "wulkan"); - - SnP snp1 = vulcan.createSnp(Mockito.mock(Client.class), null); - Assert.assertEquals(null, snp1.getId()); - - SnP snp2 = vulcan.createSnp(Mockito.mock(Client.class), "wulkan"); - Assert.assertEquals("wulkan", snp2.getId()); - - } - - @Test(expected = NotLoggedInErrorException.class) - public void getAttendanceExceptionText() throws Exception { - vulcan.getAttendanceTable(); + Assert.assertEquals(vulcan.getClient(), vulcan.getClient()); } } diff --git a/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java b/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java index 335ab84b..69118018 100644 --- a/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java @@ -1,6 +1,5 @@ package io.github.wulkanowy.api.login; - import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.junit.Assert; @@ -79,13 +78,6 @@ public class LoginTest { login.sendCertificate(getFixtureAsString("cert.xml"), "demo123"); } - @Test(expected = VulcanOfflineException.class) - public void sendCertificateVulcanOfflineTest() throws Exception { - Login login = new Login(getClient("PrzerwaTechniczna.html")); - - login.sendCertificate(getFixtureAsString("cert.xml"), "demo123"); - } - @Test public void findSymbolInCertificateTest() throws Exception { Login login = new Login(getClient("Logowanie-certyfikat.html")); diff --git a/api/src/test/java/io/github/wulkanowy/api/messages/MessagesTest.java b/api/src/test/java/io/github/wulkanowy/api/messages/MessagesTest.java index 44880a88..14fa4627 100644 --- a/api/src/test/java/io/github/wulkanowy/api/messages/MessagesTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/messages/MessagesTest.java @@ -8,7 +8,7 @@ import java.util.List; import io.github.wulkanowy.api.Client; import io.github.wulkanowy.api.FixtureHelper; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; +import io.github.wulkanowy.api.NotLoggedInErrorException; public class MessagesTest { diff --git a/api/src/test/resources/io/github/wulkanowy/api/login/Logowanie-notLoggedIn.html b/api/src/test/resources/io/github/wulkanowy/api/login/Logowanie-notLoggedIn.html new file mode 100644 index 00000000..f961bf82 --- /dev/null +++ b/api/src/test/resources/io/github/wulkanowy/api/login/Logowanie-notLoggedIn.html @@ -0,0 +1,22 @@ + + + + + Dziennik UONET+ + + +
+
+
+
+
+ + Zaloguj się +
+
+
+
Uonet+ wersja 17.09.0007.26300
+
+
+ + diff --git a/app/src/main/java/io/github/wulkanowy/data/Repository.java b/app/src/main/java/io/github/wulkanowy/data/Repository.java index 2637b08c..d25be8b5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/Repository.java +++ b/app/src/main/java/io/github/wulkanowy/data/Repository.java @@ -7,10 +7,7 @@ import java.util.List; import javax.inject.Inject; import javax.inject.Singleton; -import io.github.wulkanowy.api.login.AccountPermissionException; -import io.github.wulkanowy.api.login.BadCredentialsException; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; -import io.github.wulkanowy.api.login.VulcanOfflineException; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.data.db.dao.entities.Account; import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson; import io.github.wulkanowy.data.db.dao.entities.DaoSession; @@ -21,8 +18,8 @@ import io.github.wulkanowy.data.db.dao.entities.WeekDao; import io.github.wulkanowy.data.db.resources.ResourcesContract; import io.github.wulkanowy.data.db.shared.SharedPrefContract; import io.github.wulkanowy.data.sync.SyncContract; +import io.github.wulkanowy.data.sync.account.AccountSyncContract; import io.github.wulkanowy.data.sync.attendance.AttendanceSyncContract; -import io.github.wulkanowy.data.sync.login.LoginSyncContract; import io.github.wulkanowy.data.sync.timetable.TimetableSyncContract; import io.github.wulkanowy.di.annotations.SyncGrades; import io.github.wulkanowy.di.annotations.SyncSubjects; @@ -37,7 +34,7 @@ public class Repository implements RepositoryContract { private final DaoSession daoSession; - private final LoginSyncContract loginSync; + private final AccountSyncContract accountSync; private final AttendanceSyncContract attendanceSync; @@ -51,7 +48,7 @@ public class Repository implements RepositoryContract { Repository(SharedPrefContract sharedPref, ResourcesContract resources, DaoSession daoSession, - LoginSyncContract loginSync, + AccountSyncContract accountSync, AttendanceSyncContract attendanceSync, TimetableSyncContract timetableSync, @SyncGrades SyncContract gradeSync, @@ -59,7 +56,7 @@ public class Repository implements RepositoryContract { this.sharedPref = sharedPref; this.resources = resources; this.daoSession = daoSession; - this.loginSync = loginSync; + this.accountSync = accountSync; this.attendanceSync = attendanceSync; this.timetableSync = timetableSync; this.gradeSync = gradeSync; @@ -92,50 +89,48 @@ public class Repository implements RepositoryContract { } @Override - public void loginUser(String email, String password, String symbol) - throws NotLoggedInErrorException, AccountPermissionException, IOException, - CryptoException, VulcanOfflineException, BadCredentialsException { - loginSync.loginUser(email, password, symbol); + public void registerUser(String email, String password, String symbol) throws VulcanException, + IOException, CryptoException { + accountSync.registerUser(email, password, symbol); } @Override - public void loginCurrentUser() throws NotLoggedInErrorException, AccountPermissionException, - IOException, CryptoException, VulcanOfflineException, BadCredentialsException { - loginSync.loginCurrentUser(); + public void initLastUser() throws VulcanException, IOException, CryptoException { + accountSync.initLastUser(); } @Override - public void syncGrades() throws NotLoggedInErrorException, IOException, ParseException { + public void syncGrades() throws VulcanException, IOException, ParseException { gradeSync.sync(); } @Override - public void syncSubjects() throws NotLoggedInErrorException, IOException, ParseException { + public void syncSubjects() throws VulcanException, IOException, ParseException { subjectSync.sync(); } @Override - public void syncAttendance() throws NotLoggedInErrorException, ParseException, IOException { + public void syncAttendance() throws ParseException, IOException, VulcanException { attendanceSync.syncAttendance(); } @Override - public void syncAttendance(String date) throws NotLoggedInErrorException, ParseException, IOException { + public void syncAttendance(String date) throws ParseException, IOException, VulcanException { attendanceSync.syncAttendance(date); } @Override - public void syncTimetable() throws NotLoggedInErrorException, IOException, ParseException { + public void syncTimetable() throws VulcanException, IOException, ParseException { timetableSync.syncTimetable(); } @Override - public void syncTimetable(String date) throws NotLoggedInErrorException, IOException, ParseException { + public void syncTimetable(String date) throws VulcanException, IOException, ParseException { timetableSync.syncTimetable(date); } @Override - public void syncAll() throws NotLoggedInErrorException, IOException, ParseException { + public void syncAll() throws VulcanException, IOException, ParseException { syncSubjects(); syncGrades(); syncAttendance(); diff --git a/app/src/main/java/io/github/wulkanowy/data/RepositoryContract.java b/app/src/main/java/io/github/wulkanowy/data/RepositoryContract.java index 90aaeef8..f06c4762 100644 --- a/app/src/main/java/io/github/wulkanowy/data/RepositoryContract.java +++ b/app/src/main/java/io/github/wulkanowy/data/RepositoryContract.java @@ -6,26 +6,26 @@ import java.util.List; import javax.inject.Singleton; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.data.db.dao.entities.Account; import io.github.wulkanowy.data.db.dao.entities.Grade; import io.github.wulkanowy.data.db.dao.entities.Week; import io.github.wulkanowy.data.db.resources.ResourcesContract; +import io.github.wulkanowy.data.sync.account.AccountSyncContract; import io.github.wulkanowy.data.sync.attendance.AttendanceSyncContract; -import io.github.wulkanowy.data.sync.login.LoginSyncContract; import io.github.wulkanowy.data.sync.timetable.TimetableSyncContract; @Singleton -public interface RepositoryContract extends ResourcesContract, LoginSyncContract, +public interface RepositoryContract extends ResourcesContract, AccountSyncContract, AttendanceSyncContract, TimetableSyncContract { long getCurrentUserId(); - void syncGrades() throws NotLoggedInErrorException, IOException, ParseException; + void syncGrades() throws VulcanException, IOException, ParseException; - void syncSubjects() throws NotLoggedInErrorException, IOException, ParseException; + void syncSubjects() throws VulcanException, IOException, ParseException; - void syncAll() throws NotLoggedInErrorException, IOException, ParseException; + void syncAll() throws VulcanException, IOException, ParseException; Account getCurrentUser(); diff --git a/app/src/main/java/io/github/wulkanowy/data/db/resources/AppResources.java b/app/src/main/java/io/github/wulkanowy/data/db/resources/AppResources.java index e9b85464..349ac177 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/resources/AppResources.java +++ b/app/src/main/java/io/github/wulkanowy/data/db/resources/AppResources.java @@ -11,8 +11,8 @@ import javax.inject.Inject; import javax.inject.Singleton; import io.github.wulkanowy.R; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; -import io.github.wulkanowy.api.login.VulcanOfflineException; +import io.github.wulkanowy.api.NotLoggedInErrorException; +import io.github.wulkanowy.api.VulcanOfflineException; import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson; import io.github.wulkanowy.di.annotations.ApplicationContext; import io.github.wulkanowy.utils.AppConstant; diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/SyncContract.java b/app/src/main/java/io/github/wulkanowy/data/sync/SyncContract.java index 43b37b7e..d9850ab1 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/SyncContract.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/SyncContract.java @@ -3,9 +3,9 @@ package io.github.wulkanowy.data.sync; import java.io.IOException; import java.text.ParseException; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; +import io.github.wulkanowy.api.VulcanException; public interface SyncContract { - void sync() throws NotLoggedInErrorException, IOException, ParseException; + void sync() throws VulcanException, IOException, ParseException; } diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/login/LoginSync.java b/app/src/main/java/io/github/wulkanowy/data/sync/account/AccountSync.java similarity index 56% rename from app/src/main/java/io/github/wulkanowy/data/sync/login/LoginSync.java rename to app/src/main/java/io/github/wulkanowy/data/sync/account/AccountSync.java index 71838ff5..d56f9990 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/login/LoginSync.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/account/AccountSync.java @@ -1,4 +1,4 @@ -package io.github.wulkanowy.data.sync.login; +package io.github.wulkanowy.data.sync.account; import android.content.Context; @@ -8,10 +8,7 @@ import javax.inject.Inject; import javax.inject.Singleton; 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.NotLoggedInErrorException; -import io.github.wulkanowy.api.login.VulcanOfflineException; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.data.db.dao.entities.Account; import io.github.wulkanowy.data.db.dao.entities.DaoSession; import io.github.wulkanowy.data.db.shared.SharedPrefContract; @@ -21,7 +18,7 @@ import io.github.wulkanowy.utils.security.CryptoException; import io.github.wulkanowy.utils.security.Scrambler; @Singleton -public class LoginSync implements LoginSyncContract { +public class AccountSync implements AccountSyncContract { private final DaoSession daoSession; @@ -32,8 +29,8 @@ public class LoginSync implements LoginSyncContract { private final Context context; @Inject - LoginSync(DaoSession daoSession, SharedPrefContract sharedPref, - Vulcan vulcan, @ApplicationContext Context context) { + AccountSync(DaoSession daoSession, SharedPrefContract sharedPref, + Vulcan vulcan, @ApplicationContext Context context) { this.daoSession = daoSession; this.sharedPref = sharedPref; this.vulcan = vulcan; @@ -41,13 +38,12 @@ public class LoginSync implements LoginSyncContract { } @Override - public void loginUser(String email, String password, String symbol) - throws NotLoggedInErrorException, AccountPermissionException, IOException, - CryptoException, VulcanOfflineException, BadCredentialsException { + public void registerUser(String email, String password, String symbol) + throws VulcanException, IOException, CryptoException { - LogUtils.debug("Login new user email=" + email); + LogUtils.debug("Register new user email=" + email); - vulcan.login(email, password, symbol); + vulcan.setCredentials(email, password, symbol, null); Account account = new Account() .setName(vulcan.getBasicInformation().getPersonalData().getFirstAndLastName()) @@ -56,24 +52,25 @@ public class LoginSync implements LoginSyncContract { .setSymbol(vulcan.getSymbol()) .setSnpId(vulcan.getStudentAndParent().getId()); - sharedPref.setCurrentUserId(daoSession.getAccountDao().insert(account)); + daoSession.getAccountDao().insert(account); + + sharedPref.setCurrentUserId(account.getId()); } @Override - public void loginCurrentUser() throws NotLoggedInErrorException, AccountPermissionException, - IOException, CryptoException, VulcanOfflineException, BadCredentialsException { + public void initLastUser() throws VulcanException, IOException, CryptoException { long userId = sharedPref.getCurrentUserId(); if (userId == 0) { - throw new IOException("Can't find logged user"); + throw new IOException("Can't find saved user"); } - LogUtils.debug("Login current user id=" + userId); + LogUtils.debug("Initialization current user id=" + userId); Account account = daoSession.getAccountDao().load(userId); - vulcan.login(account.getEmail(), + vulcan.setCredentials(account.getEmail(), Scrambler.decrypt(account.getEmail(), account.getPassword()), account.getSymbol(), account.getSnpId()); diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/account/AccountSyncContract.java b/app/src/main/java/io/github/wulkanowy/data/sync/account/AccountSyncContract.java new file mode 100644 index 00000000..799d320e --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/data/sync/account/AccountSyncContract.java @@ -0,0 +1,16 @@ +package io.github.wulkanowy.data.sync.account; + +import java.io.IOException; + +import io.github.wulkanowy.api.VulcanException; +import io.github.wulkanowy.utils.security.CryptoException; + +public interface AccountSyncContract { + + void registerUser(String email, String password, String symbol) + throws VulcanException, IOException, + CryptoException; + + void initLastUser() throws VulcanException, IOException, + CryptoException; +} diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/attendance/AttendanceSync.java b/app/src/main/java/io/github/wulkanowy/data/sync/attendance/AttendanceSync.java index 19de88ef..325fff19 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/attendance/AttendanceSync.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/attendance/AttendanceSync.java @@ -9,8 +9,8 @@ import javax.inject.Inject; import javax.inject.Singleton; import io.github.wulkanowy.api.Vulcan; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.generic.Lesson; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson; import io.github.wulkanowy.data.db.dao.entities.AttendanceLessonDao; import io.github.wulkanowy.data.db.dao.entities.DaoSession; @@ -42,12 +42,12 @@ public class AttendanceSync implements AttendanceSyncContract { } @Override - public void syncAttendance() throws IOException, NotLoggedInErrorException, ParseException { + public void syncAttendance() throws IOException, ParseException, VulcanException { syncAttendance(null); } @Override - public void syncAttendance(String date) throws IOException, NotLoggedInErrorException, ParseException { + public void syncAttendance(String date) throws IOException, ParseException, VulcanException { this.userId = sharedPref.getCurrentUserId(); io.github.wulkanowy.api.generic.Week weekApi = getWeekFromApi(getNormalizedDate(date)); @@ -59,7 +59,7 @@ public class AttendanceSync implements AttendanceSyncContract { daoSession.getAttendanceLessonDao().saveInTx(lessonList); - LogUtils.debug("Synchronization lessons (amount = " + lessonList.size() + ")"); + LogUtils.debug("Synchronization attendance lessons (amount = " + lessonList.size() + ")"); } private String getNormalizedDate(String date) throws ParseException { @@ -67,7 +67,7 @@ public class AttendanceSync implements AttendanceSyncContract { } private io.github.wulkanowy.api.generic.Week getWeekFromApi(String date) - throws IOException, NotLoggedInErrorException, ParseException { + throws IOException, ParseException, VulcanException { return vulcan.getAttendanceTable().getWeekTable(date); } diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/attendance/AttendanceSyncContract.java b/app/src/main/java/io/github/wulkanowy/data/sync/attendance/AttendanceSyncContract.java index 503086ba..dfb4c1b4 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/attendance/AttendanceSyncContract.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/attendance/AttendanceSyncContract.java @@ -3,11 +3,11 @@ package io.github.wulkanowy.data.sync.attendance; import java.io.IOException; import java.text.ParseException; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; +import io.github.wulkanowy.api.VulcanException; public interface AttendanceSyncContract { - void syncAttendance(String date) throws NotLoggedInErrorException, IOException, ParseException; + void syncAttendance(String date) throws IOException, ParseException, VulcanException; - void syncAttendance() throws NotLoggedInErrorException, IOException, ParseException; + void syncAttendance() throws IOException, ParseException, VulcanException; } diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/grades/GradeSync.java b/app/src/main/java/io/github/wulkanowy/data/sync/grades/GradeSync.java index e0b38884..b132ac1b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/grades/GradeSync.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/grades/GradeSync.java @@ -9,7 +9,7 @@ import javax.inject.Inject; import javax.inject.Singleton; import io.github.wulkanowy.api.Vulcan; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.data.db.dao.entities.Account; import io.github.wulkanowy.data.db.dao.entities.DaoSession; import io.github.wulkanowy.data.db.dao.entities.Grade; @@ -29,6 +29,8 @@ public class GradeSync implements SyncContract { private final SharedPrefContract sharedPref; + private Long userId; + @Inject GradeSync(DaoSession daoSession, SharedPrefContract sharedPref, Vulcan vulcan) { this.daoSession = daoSession; @@ -37,36 +39,53 @@ public class GradeSync implements SyncContract { } @Override - public void sync() throws IOException, NotLoggedInErrorException, ParseException { + public void sync() throws IOException, VulcanException, ParseException { - long userId = sharedPref.getCurrentUserId(); + userId = sharedPref.getCurrentUserId(); Account account = daoSession.getAccountDao().load(userId); - account.resetGradeList(); - account.resetSubjectList(); + resetAccountRelations(account); - List gradesFromNet = DataObjectConverter - .gradesToGradeEntities(vulcan.getGradesList().getAll()); - List gradesFromDb = account.getGradeList(); - - List updatedGrades = EntitiesCompare.compareGradeList(gradesFromNet, gradesFromDb); - - daoSession.getGradeDao().deleteInTx(gradesFromDb); - - List lastList = new ArrayList<>(); - - for (Grade grade : updatedGrades) { - grade.setUserId(userId); - grade.setSubjectId(daoSession.getSubjectDao().queryBuilder() - .where(SubjectDao.Properties.Name.eq(grade.getSubject()), - SubjectDao.Properties.UserId.eq(userId)) - .build() - .uniqueOrThrow().getId()); - lastList.add(grade); - } + List lastList = getUpdatedList(getComparedList(account)); + daoSession.getGradeDao().deleteInTx(account.getGradeList()); daoSession.getGradeDao().insertInTx(lastList); LogUtils.debug("Synchronization grades (amount = " + lastList.size() + ")"); } + + private void resetAccountRelations(Account account) { + account.resetSubjectList(); + account.resetGradeList(); + } + + private List getUpdatedList(List comparedList) { + List updatedList = new ArrayList<>(); + + for (Grade grade : comparedList) { + grade.setUserId(userId); + grade.setSubjectId(getSubjectId(grade.getSubject())); + updatedList.add(grade); + } + return updatedList; + } + + private List getComparedList(Account account) throws IOException, VulcanException, + ParseException { + List gradesFromNet = DataObjectConverter + .gradesToGradeEntities(vulcan.getGradesList().getAll()); + + List gradesFromDb = account.getGradeList(); + + return EntitiesCompare.compareGradeList(gradesFromNet, gradesFromDb); + } + + private Long getSubjectId(String subjectName) { + return daoSession.getSubjectDao().queryBuilder() + .where(SubjectDao.Properties.Name.eq(subjectName), + SubjectDao.Properties.UserId.eq(userId)) + .build() + .uniqueOrThrow() + .getId(); + } } diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/login/LoginSyncContract.java b/app/src/main/java/io/github/wulkanowy/data/sync/login/LoginSyncContract.java deleted file mode 100644 index 1aed8b1b..00000000 --- a/app/src/main/java/io/github/wulkanowy/data/sync/login/LoginSyncContract.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.github.wulkanowy.data.sync.login; - -import java.io.IOException; - -import io.github.wulkanowy.api.login.AccountPermissionException; -import io.github.wulkanowy.api.login.BadCredentialsException; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; -import io.github.wulkanowy.api.login.VulcanOfflineException; -import io.github.wulkanowy.utils.security.CryptoException; - -public interface LoginSyncContract { - - void loginUser(String email, String password, String symbol) - throws NotLoggedInErrorException, AccountPermissionException, IOException, - CryptoException, VulcanOfflineException, BadCredentialsException; - - void loginCurrentUser() throws NotLoggedInErrorException, AccountPermissionException, IOException, - CryptoException, VulcanOfflineException, BadCredentialsException; -} diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/subjects/SubjectSync.java b/app/src/main/java/io/github/wulkanowy/data/sync/subjects/SubjectSync.java index d888090b..1c5bfc71 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/subjects/SubjectSync.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/subjects/SubjectSync.java @@ -9,10 +9,10 @@ import javax.inject.Inject; import javax.inject.Singleton; import io.github.wulkanowy.api.Vulcan; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; +import io.github.wulkanowy.api.VulcanException; +import io.github.wulkanowy.data.db.dao.entities.Account; import io.github.wulkanowy.data.db.dao.entities.DaoSession; import io.github.wulkanowy.data.db.dao.entities.Subject; -import io.github.wulkanowy.data.db.dao.entities.SubjectDao; import io.github.wulkanowy.data.db.shared.SharedPrefContract; import io.github.wulkanowy.data.sync.SyncContract; import io.github.wulkanowy.utils.DataObjectConverter; @@ -21,41 +21,51 @@ import io.github.wulkanowy.utils.LogUtils; @Singleton public class SubjectSync implements SyncContract { - private final SubjectDao subjectDao; + private final DaoSession daoSession; private final Vulcan vulcan; private final SharedPrefContract sharedPref; + private Long userId; + @Inject SubjectSync(DaoSession daoSession, SharedPrefContract sharedPref, Vulcan vulcan) { - this.subjectDao = daoSession.getSubjectDao(); + this.daoSession = daoSession; this.sharedPref = sharedPref; this.vulcan = vulcan; } @Override - public void sync() throws NotLoggedInErrorException, IOException, ParseException { + public void sync() throws VulcanException, IOException, ParseException { - long userId = sharedPref.getCurrentUserId(); + userId = sharedPref.getCurrentUserId(); - List subjectsFromNet = DataObjectConverter - .subjectsToSubjectEntities(vulcan.getSubjectsList().getAll()); + List lastList = getUpdatedList(getSubjectsFromNet()); - subjectDao.deleteInTx(subjectDao.queryBuilder() - .where(SubjectDao.Properties.UserId.eq(userId)) - .build() - .list()); - - List lastList = new ArrayList<>(); - - for (Subject subject : subjectsFromNet) { - subject.setUserId(userId); - lastList.add(subject); - } - - subjectDao.insertInTx(lastList); + daoSession.getSubjectDao().deleteInTx(getSubjectsFromDb()); + daoSession.getSubjectDao().insertInTx(lastList); LogUtils.debug("Synchronization subjects (amount = " + lastList.size() + ")"); } + + private List getSubjectsFromNet() throws VulcanException, IOException { + return DataObjectConverter.subjectsToSubjectEntities(vulcan.getSubjectsList().getAll()); + } + + private List getSubjectsFromDb() { + Account account = daoSession.getAccountDao().load(userId); + account.resetSubjectList(); + return account.getSubjectList(); + } + + private List getUpdatedList(List subjectsFromNet) { + List updatedList = new ArrayList<>(); + + for (Subject subject : subjectsFromNet) { + subject.setUserId(userId); + updatedList.add(subject); + } + return updatedList; + } } diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/timetable/TimetableSync.java b/app/src/main/java/io/github/wulkanowy/data/sync/timetable/TimetableSync.java index 11819bed..da6b143f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/timetable/TimetableSync.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/timetable/TimetableSync.java @@ -9,8 +9,8 @@ import javax.inject.Inject; import javax.inject.Singleton; import io.github.wulkanowy.api.Vulcan; +import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.generic.Lesson; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; import io.github.wulkanowy.data.db.dao.entities.DaoSession; import io.github.wulkanowy.data.db.dao.entities.Day; import io.github.wulkanowy.data.db.dao.entities.DayDao; @@ -42,12 +42,12 @@ public class TimetableSync implements TimetableSyncContract { } @Override - public void syncTimetable() throws NotLoggedInErrorException, IOException, ParseException { + public void syncTimetable() throws IOException, ParseException, VulcanException { syncTimetable(null); } @Override - public void syncTimetable(String date) throws NotLoggedInErrorException, IOException, ParseException { + public void syncTimetable(String date) throws IOException, ParseException, VulcanException { this.userId = sharedPref.getCurrentUserId(); io.github.wulkanowy.api.generic.Week weekApi = getWeekFromApi(getNormalizedDate(date)); @@ -59,7 +59,7 @@ public class TimetableSync implements TimetableSyncContract { daoSession.getTimetableLessonDao().saveInTx(lessonList); - LogUtils.debug("Synchronization lessons (amount = " + lessonList.size() + ")"); + LogUtils.debug("Synchronization timetable lessons (amount = " + lessonList.size() + ")"); } private String getNormalizedDate(String date) throws ParseException { @@ -67,7 +67,7 @@ public class TimetableSync implements TimetableSyncContract { } private io.github.wulkanowy.api.generic.Week getWeekFromApi(String date) - throws IOException, NotLoggedInErrorException, ParseException { + throws IOException, VulcanException, ParseException { return vulcan.getTimetable().getWeekTable(date); } diff --git a/app/src/main/java/io/github/wulkanowy/data/sync/timetable/TimetableSyncContract.java b/app/src/main/java/io/github/wulkanowy/data/sync/timetable/TimetableSyncContract.java index 4c37d2ec..9bba209a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/sync/timetable/TimetableSyncContract.java +++ b/app/src/main/java/io/github/wulkanowy/data/sync/timetable/TimetableSyncContract.java @@ -3,11 +3,11 @@ package io.github.wulkanowy.data.sync.timetable; import java.io.IOException; import java.text.ParseException; -import io.github.wulkanowy.api.login.NotLoggedInErrorException; +import io.github.wulkanowy.api.VulcanException; public interface TimetableSyncContract { - void syncTimetable(String date) throws NotLoggedInErrorException, IOException, ParseException; + void syncTimetable(String date) throws VulcanException, IOException, ParseException; - void syncTimetable() throws NotLoggedInErrorException, IOException, ParseException; + void syncTimetable() throws VulcanException, IOException, ParseException; } diff --git a/app/src/main/java/io/github/wulkanowy/di/modules/ApplicationModule.java b/app/src/main/java/io/github/wulkanowy/di/modules/ApplicationModule.java index 46744c90..e7d2bcc6 100644 --- a/app/src/main/java/io/github/wulkanowy/di/modules/ApplicationModule.java +++ b/app/src/main/java/io/github/wulkanowy/di/modules/ApplicationModule.java @@ -21,11 +21,11 @@ import io.github.wulkanowy.data.db.resources.ResourcesContract; import io.github.wulkanowy.data.db.shared.SharedPref; import io.github.wulkanowy.data.db.shared.SharedPrefContract; import io.github.wulkanowy.data.sync.SyncContract; +import io.github.wulkanowy.data.sync.account.AccountSync; +import io.github.wulkanowy.data.sync.account.AccountSyncContract; import io.github.wulkanowy.data.sync.attendance.AttendanceSync; import io.github.wulkanowy.data.sync.attendance.AttendanceSyncContract; import io.github.wulkanowy.data.sync.grades.GradeSync; -import io.github.wulkanowy.data.sync.login.LoginSync; -import io.github.wulkanowy.data.sync.login.LoginSyncContract; import io.github.wulkanowy.data.sync.subjects.SubjectSync; import io.github.wulkanowy.data.sync.timetable.TimetableSync; import io.github.wulkanowy.data.sync.timetable.TimetableSyncContract; @@ -100,8 +100,8 @@ public class ApplicationModule { @Singleton @Provides - LoginSyncContract provideLoginSync(LoginSync loginSync) { - return loginSync; + AccountSyncContract provideLoginSync(AccountSync accountSync) { + return accountSync; } @SyncGrades diff --git a/app/src/main/java/io/github/wulkanowy/services/SyncJob.java b/app/src/main/java/io/github/wulkanowy/services/SyncJob.java index ab26fcee..e7b0908f 100644 --- a/app/src/main/java/io/github/wulkanowy/services/SyncJob.java +++ b/app/src/main/java/io/github/wulkanowy/services/SyncJob.java @@ -62,7 +62,7 @@ public class SyncJob extends SimpleJobService { @Override public int onRunJob(JobParameters job) { try { - repository.loginCurrentUser(); + repository.initLastUser(); repository.syncAll(); gradeList = repository.getNewGrades(); diff --git a/app/src/main/java/io/github/wulkanowy/ui/login/LoginContract.java b/app/src/main/java/io/github/wulkanowy/ui/login/LoginContract.java index fa074896..debfbbde 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/login/LoginContract.java +++ b/app/src/main/java/io/github/wulkanowy/ui/login/LoginContract.java @@ -42,6 +42,8 @@ public interface LoginContract { void onStartAsync(); + void onDoInBackground(int stepNumber) throws Exception; + void onLoginProgress(int step); void onEndAsync(boolean success, Exception exception); diff --git a/app/src/main/java/io/github/wulkanowy/ui/login/LoginPresenter.java b/app/src/main/java/io/github/wulkanowy/ui/login/LoginPresenter.java index b4e2e4e9..2b6d173d 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/login/LoginPresenter.java +++ b/app/src/main/java/io/github/wulkanowy/ui/login/LoginPresenter.java @@ -17,6 +17,12 @@ public class LoginPresenter extends BasePresenter private LoginTask loginAsync; + private String email; + + private String password; + + private String symbol; + @Inject LoginPresenter(RepositoryContract repository) { super(repository); @@ -26,17 +32,17 @@ public class LoginPresenter extends BasePresenter public void attemptLogin(String email, String password, String symbol) { getView().resetViewErrors(); + this.email = email; + this.password = password; + this.symbol = getNormalizedSymbol(symbol); + if (!isAllFieldCorrect(password, email)) { getView().showSoftInput(); return; } if (getView().isNetworkConnected()) { - // Dopóki używamy AsyncTask presenter będzie musiał "wiedzieć" o AsyncTaskach - loginAsync = new LoginTask(this, - email, - password, - getNormalizedSymbol(symbol)); + loginAsync = new LoginTask(this); loginAsync.execute(); } else { @@ -53,6 +59,18 @@ public class LoginPresenter extends BasePresenter } } + @Override + public void onDoInBackground(int stepNumber) throws Exception { + switch (stepNumber) { + case 1: + getRepository().registerUser(email, password, symbol); + break; + case 2: + getRepository().syncAll(); + break; + } + } + @Override public void onLoginProgress(int step) { if (step == 1) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/login/LoginTask.java b/app/src/main/java/io/github/wulkanowy/ui/login/LoginTask.java index 441b8d8d..b22194da 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/login/LoginTask.java +++ b/app/src/main/java/io/github/wulkanowy/ui/login/LoginTask.java @@ -4,21 +4,12 @@ import android.os.AsyncTask; public class LoginTask extends AsyncTask { - private String email; - - private String password; - - private String symbol; - private LoginContract.Presenter presenter; private Exception exception; - LoginTask(LoginContract.Presenter presenter, String email, String password, String symbol) { + LoginTask(LoginContract.Presenter presenter) { this.presenter = presenter; - this.email = email; - this.password = password; - this.symbol = symbol; } @Override @@ -30,10 +21,10 @@ public class LoginTask extends AsyncTask { protected Boolean doInBackground(Void... params) { try { publishProgress(1); - presenter.getRepository().loginUser(email, password, symbol); + presenter.onDoInBackground(1); publishProgress(2); - presenter.getRepository().syncAll(); + presenter.onDoInBackground(2); } catch (Exception e) { exception = e; return false; diff --git a/app/src/main/java/io/github/wulkanowy/ui/main/attendance/AttendanceTabPresenter.java b/app/src/main/java/io/github/wulkanowy/ui/main/attendance/AttendanceTabPresenter.java index 7f4766c9..d0d448ca 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/main/attendance/AttendanceTabPresenter.java +++ b/app/src/main/java/io/github/wulkanowy/ui/main/attendance/AttendanceTabPresenter.java @@ -153,7 +153,6 @@ public class AttendanceTabPresenter extends BasePresenter @Override public void onDoInBackgroundRefresh() throws Exception { - getRepository().loginCurrentUser(); getRepository().syncSubjects(); getRepository().syncGrades(); } diff --git a/app/src/main/java/io/github/wulkanowy/ui/main/timetable/TimetableTabPresenter.java b/app/src/main/java/io/github/wulkanowy/ui/main/timetable/TimetableTabPresenter.java index 00dcef6a..e5eb499f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/main/timetable/TimetableTabPresenter.java +++ b/app/src/main/java/io/github/wulkanowy/ui/main/timetable/TimetableTabPresenter.java @@ -157,7 +157,6 @@ public class TimetableTabPresenter extends BasePresenter implements SplashContract.Presenter { @@ -23,7 +24,12 @@ public class SplashPresenter extends BasePresenter if (getRepository().getCurrentUserId() == 0) { getView().openLoginActivity(); } else { - getView().openMainActivity(); + try { + getRepository().initLastUser(); + getView().openMainActivity(); + } catch (Exception e) { + LogUtils.error("An error occurred when the application was started", e); + } } } } diff --git a/app/src/test/java/io/github/wulkanowy/utils/DataObjectConverterTest.java b/app/src/test/java/io/github/wulkanowy/utils/DataObjectConverterTest.java index f32877c5..b0335f62 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/DataObjectConverterTest.java +++ b/app/src/test/java/io/github/wulkanowy/utils/DataObjectConverterTest.java @@ -6,10 +6,10 @@ import org.junit.Test; import java.util.ArrayList; import java.util.List; -import io.github.wulkanowy.api.grades.Grade; -import io.github.wulkanowy.api.grades.Subject; import io.github.wulkanowy.api.generic.Day; import io.github.wulkanowy.api.generic.Lesson; +import io.github.wulkanowy.api.grades.Grade; +import io.github.wulkanowy.api.grades.Subject; import io.github.wulkanowy.data.db.dao.entities.TimetableLesson; public class DataObjectConverterTest {