Sort grades by date (#20)

This commit is contained in:
Mikołaj Pich 2017-09-02 14:58:08 +02:00 committed by RicomenPL
parent 935b79e8cf
commit 7b087144e9
5 changed files with 36 additions and 14 deletions

View File

@ -5,8 +5,12 @@ import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -31,11 +35,11 @@ public class GradesList extends Vulcan {
return gradesPageUrl;
}
public List<Grade> getAll() throws IOException, LoginErrorException {
public List<Grade> getAll() throws IOException, LoginErrorException, ParseException {
return getAll("");
}
public List<Grade> getAll(String semester) throws IOException, LoginErrorException {
public List<Grade> getAll(String semester) throws IOException, LoginErrorException, ParseException {
Document gradesPage = snp.getSnPPageDocument(getGradesPageUrl() + semester);
Elements gradesRows = gradesPage.select(".ocenySzczegoly-table > tbody > tr");
Semester currentSemester = snp.getCurrentSemester(snp.getSemesters(gradesPage));
@ -54,6 +58,10 @@ public class GradesList extends Vulcan {
.attr("style"));
String color = matcher.find() ? matcher.group(1) : "";
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT);
Date d = sdf.parse(row.select("td:nth-child(5)").text());
sdf.applyPattern("yyyy-MM-dd");
grades.add(new Grade()
.setSubject(row.select("td:nth-child(1)").text())
.setValue(row.select("td:nth-child(2)").text())
@ -61,7 +69,7 @@ public class GradesList extends Vulcan {
.setSymbol(symbol)
.setDescription(description)
.setWeight(row.select("td:nth-child(4)").text())
.setDate(row.select("td:nth-child(5)").text())
.setDate(sdf.format(d))
.setTeacher(row.select("td:nth-child(6)").text())
.setSemester(currentSemester.getNumber())
);

View File

@ -10,7 +10,7 @@ public class DatabaseAdapter {
private final String DATABASE_NAME = "accountdatabase.db";
private final int DATABASE_VERSION = 4;
private final int DATABASE_VERSION = 5;
public static SQLiteDatabase database;

View File

@ -35,7 +35,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
"symbol TEXT, " +
"description TEXT, " +
"weight TEXT, " +
"date TEXT, " +
"date DATE, " +
"teacher TEXT, " +
"semester INTEGER, " +
"isNew INTEGER );";

View File

@ -20,17 +20,29 @@ import io.github.wulkanowy.database.subjects.SubjectsDatabase;
public class GradesDatabase extends DatabaseAdapter {
private String userIdText = "userID";
private String subjectIdText = "subjectID";
private String subject = "subject";
private String value = "value";
private String color = "color";
private String symbol = "symbol";
private String description = "description";
private String weight = "weight";
private String date = "date";
private String teacher = "teacher";
private String isNew = "isNew";
private String semester = "semester";
private String grades = "grades";
public GradesDatabase(Context context) {
@ -84,11 +96,13 @@ public class GradesDatabase extends DatabaseAdapter {
public List<GradeItem> getSubjectGrades(long userId, long subjectId) throws SQLException {
String whereExec = "SELECT * FROM " + grades + " WHERE " + userIdText + "=? AND " + subjectIdText + "=?";
String exec = "SELECT " + grades + ".*, strftime('%d.%m.%Y', " + date + ") " +
"FROM " + grades + " WHERE " + userIdText + "=? AND "
+ subjectIdText + "=? ORDER BY " + date + " DESC";
List<GradeItem> gradesList = new ArrayList<>();
Cursor cursor = database.rawQuery(whereExec, new String[]{String.valueOf(userId), String.valueOf(subjectId)});
Cursor cursor = database.rawQuery(exec, new String[]{String.valueOf(userId), String.valueOf(subjectId)});
while (cursor.moveToNext()) {
GradeItem grade = new GradeItem();
@ -101,7 +115,7 @@ public class GradesDatabase extends DatabaseAdapter {
grade.setSymbol(cursor.getString(6));
grade.setDescription(cursor.getString(7));
grade.setWeight(cursor.getString(8));
grade.setDate(cursor.getString(9));
grade.setDate(cursor.getString(13)); // last, because reformatted date is last
grade.setTeacher(cursor.getString(10));
grade.setSemester(cursor.getString(11));
grade.setIsNew(cursor.getInt(12) != 0);
@ -116,7 +130,8 @@ public class GradesDatabase extends DatabaseAdapter {
List<Grade> gradesList = new ArrayList<>();
String exec = "SELECT * FROM " + grades + " WHERE " + userIdText + "=?";
String exec = "SELECT " + grades + ".*, strftime('%d.%m.%Y', " + date + ") " +
" FROM " + grades + " WHERE " + userIdText + "=? ORDER BY " + date + " DESC";
Cursor cursor = database.rawQuery(exec, new String[]{String.valueOf(context.getSharedPreferences("LoginData", context.MODE_PRIVATE).getLong("isLogin", 0))});
@ -138,4 +153,3 @@ public class GradesDatabase extends DatabaseAdapter {
return gradesList;
}
}

View File

@ -31,7 +31,7 @@ public class GradesListTest extends GradesTest {
Assert.assertEquals("A1", grade1.getSymbol());
Assert.assertEquals("Dzień Kobiet w naszej klasie", grade1.getDescription());
Assert.assertEquals("1,00", grade1.getWeight());
Assert.assertEquals("21.03.2017", grade1.getDate());
Assert.assertEquals("2017-03-21", grade1.getDate());
Assert.assertEquals("Patryk Maciejewski", grade1.getTeacher());
Assert.assertEquals("7654321", grade1.getSemester());
@ -42,7 +42,7 @@ public class GradesListTest extends GradesTest {
Assert.assertEquals("BW3", grade2.getSymbol());
Assert.assertEquals("Writing", grade2.getDescription());
Assert.assertEquals("3,00", grade2.getWeight());
Assert.assertEquals("02.06.2017", grade2.getDate());
Assert.assertEquals("2017-06-02", grade2.getDate());
Assert.assertEquals("Oliwia Woźniak", grade2.getTeacher());
Assert.assertEquals("7654321", grade2.getSemester());
@ -53,7 +53,7 @@ public class GradesListTest extends GradesTest {
Assert.assertEquals("STR", grade3.getSymbol());
Assert.assertEquals("", grade3.getDescription());
Assert.assertEquals("8,00", grade3.getWeight());
Assert.assertEquals("02.04.2017", grade3.getDate());
Assert.assertEquals("2017-04-02", grade3.getDate());
Assert.assertEquals("Klaudia Dziedzic", grade3.getTeacher());
Assert.assertEquals("7654321", grade3.getSemester());
@ -64,7 +64,7 @@ public class GradesListTest extends GradesTest {
Assert.assertEquals("K", grade4.getSymbol());
Assert.assertEquals("Kordian", grade4.getDescription());
Assert.assertEquals("5,00", grade4.getWeight());
Assert.assertEquals("06.02.2017", grade4.getDate());
Assert.assertEquals("2017-02-06", grade4.getDate());
Assert.assertEquals("Amelia Stępień", grade4.getTeacher());
Assert.assertEquals("7654321", grade4.getSemester());
}