diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Announcement.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Announcement.java index 6214621..be9577c 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Announcement.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Announcement.java @@ -6,11 +6,12 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import java.util.HashSet; import androidx.annotation.NonNull; import de.sebse.fuplanner.tools.UtilsJson; -public class Announcement implements UtilsJson.JsonConvertable { +public class Announcement implements UtilsJson.JsonConvertible { private final String id; private final String title; private final String body; @@ -27,6 +28,19 @@ public class Announcement implements UtilsJson.JsonConvertable { this.urls = urls; } + public Announcement(JSONObject json) throws JSONException { + this.id = json.getString("id"); + this.title = json.getString("title"); + this.body = json.getString("body"); + this.createdBy = json.getString("createdBy"); + this.createdOn = json.getLong("createdOn"); + ArrayList urls = new ArrayList<>(); + for (String url: UtilsJson.jsonArrayToIterableString(json.getJSONArray("urls"))) { + urls.add(url); + } + this.urls = urls; + } + public ArrayList getUrls() { return urls; } diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Assignment.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Assignment.java index 63d670e..aab37b1 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Assignment.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Assignment.java @@ -5,13 +5,12 @@ import com.google.android.gms.common.internal.Objects; import org.json.JSONException; import org.json.JSONObject; -import java.io.Serializable; import java.util.ArrayList; import androidx.annotation.NonNull; import de.sebse.fuplanner.tools.UtilsJson; -public class Assignment implements UtilsJson.JsonConvertable { +public class Assignment implements UtilsJson.JsonConvertible { private final String id; private final String title; private final long dueTime; diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Event.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Event.java index 6103ab8..a84f9a8 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Event.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Event.java @@ -5,7 +5,6 @@ import com.google.android.gms.common.internal.Objects; import org.json.JSONException; import org.json.JSONObject; -import java.io.Serializable; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -14,7 +13,7 @@ import androidx.annotation.NonNull; import de.sebse.fuplanner.tools.ColorRGB; import de.sebse.fuplanner.tools.UtilsJson; -public class Event implements UtilsJson.JsonConvertable { +public class Event implements UtilsJson.JsonConvertible { private final String siteId; private final String id; private final String type; diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Grade.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Grade.java index d35d233..b83fa6a 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Grade.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Grade.java @@ -5,12 +5,10 @@ import com.google.android.gms.common.internal.Objects; import org.json.JSONException; import org.json.JSONObject; -import java.io.Serializable; - import androidx.annotation.NonNull; import de.sebse.fuplanner.tools.UtilsJson; -public class Grade implements UtilsJson.JsonConvertable { +public class Grade implements UtilsJson.JsonConvertible { private final String itemName; private final double grade; private final double maxPoints; diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Lecturer.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Lecturer.java index 9940ec4..2684c18 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Lecturer.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Lecturer.java @@ -11,7 +11,7 @@ import java.util.regex.Pattern; import androidx.annotation.NonNull; import de.sebse.fuplanner.tools.UtilsJson; -public class Lecturer implements UtilsJson.JsonConvertable { +public class Lecturer implements UtilsJson.JsonConvertible { private final String firstName; private final String surname; private final String mail; @@ -29,6 +29,13 @@ public class Lecturer implements UtilsJson.JsonConvertable { this.isResponsible = matcher.group(4).equals("true"); } + public Lecturer(JSONObject json) throws JSONException { + this.firstName = json.getString("firstName"); + this.surname = json.getString("surname"); + this.mail = json.getString("mail"); + this.isResponsible = json.getBoolean("isResponsible"); + } + public String getFirstName() { return firstName; } diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Modules.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Modules.java index e66547d..aa6e89b 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Modules.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Modules.java @@ -11,11 +11,14 @@ import org.json.JSONObject; import java.io.BufferedReader; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.OutputStreamWriter; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -26,6 +29,7 @@ import androidx.annotation.Nullable; import de.sebse.fuplanner.tools.UtilsJson; import static de.sebse.fuplanner.tools.UtilsJson.collectionToJson; +import static de.sebse.fuplanner.tools.UtilsJson.jsonArrayToIterableObject; /** * Created by sebastian on 29.10.17. @@ -79,8 +83,7 @@ public class Modules implements Iterable { public static Modules load(Context context) throws IOException { String ret = ""; - InputStream inputStream = context.openFileInput(FILE_NAME_TIMESTAMP); - + InputStream inputStream = context.openFileInput(FILE_NAME); if (inputStream != null) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); @@ -97,11 +100,13 @@ public class Modules implements Iterable { JSONObject json = new JSONObject(ret); Modules mod = new Modules(json.getString("username")); JSONArray arr = json.getJSONArray("modules"); - - for (int i = 0; i < arr.length(); i++) { - mod.list.add(Module.); + for (JSONObject jsonObject: jsonArrayToIterableObject(arr)) { + mod.list.add(new Module(jsonObject)); } - return mod + FileInputStream ofi = context.openFileInput(FILE_NAME_TIMESTAMP); + ObjectInputStream inputStream1 = new ObjectInputStream(ofi); + mod.mLoadTime = inputStream1.readLong(); + return mod; } catch (JSONException e) { e.printStackTrace(); } @@ -110,18 +115,20 @@ public class Modules implements Iterable { public void save(Context context) throws IOException { JSONObject json = new JSONObject(); - JSONArray jModules = new JSONArray(); try { json.put("username", mUsername); json.put("modules", UtilsJson.collectionToJson(list)); + OutputStreamWriter osw = new OutputStreamWriter(context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE)); + osw.write(json.toString()); + osw.close(); } catch (JSONException e) { e.printStackTrace(); } mLoadTime = System.currentTimeMillis(); - OutputStreamWriter osw = new OutputStreamWriter(context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE)); - osw.write(json.toString()); - osw.close(); + FileOutputStream ofo = context.openFileOutput(FILE_NAME_TIMESTAMP, Context.MODE_PRIVATE); + ObjectOutputStream outputStream = new ObjectOutputStream(ofo); + outputStream.writeLong(mLoadTime); } public void delete(Context context) { @@ -162,7 +169,7 @@ public class Modules implements Iterable { return o1.hashCode() == o2.hashCode(); } - public class Module implements UtilsJson.JsonConvertable { + public static class Module implements UtilsJson.JsonConvertible { @Nullable public final Semester semester; @NotNull final HashSet lvNumber; @NotNull public final String title; @@ -226,25 +233,69 @@ public class Modules implements Iterable { this.ID = ID; } - private Module(JSONObject json) throws JSONException { - HashSet hs = new HashSet<>(); - Iterator it = UtilsJson.jsonArrayToIterator(json.getJSONArray("lvNumber")); - while (it.hasNext()) { - hs. + private Module(@NotNull JSONObject json) throws JSONException { + HashSet lv = new HashSet<>(); + for (String number: UtilsJson.jsonArrayToIterableString(json.getJSONArray("lvNumber"))) { + lv.add(number); } - Module(new Semester(json.getJSONObject("semester")), - json.getString("lvNumber"), - json.getString("title"), - json.getJSONObject("lvNumber"), + LinkedHashSet lecturer = new LinkedHashSet<>(); + for (JSONObject l: UtilsJson.jsonArrayToIterableObject(json.getJSONArray("lecturer"))) { + lecturer.add(new Lecturer(l)); + } + JSONObject obj = json.optJSONObject("semester"); - json.getString("type"), - json.getString("description"), - json.getString("ID")); - json.put("announcements", collectionToJson(announcements)); - json.put("assignments", collectionToJson(assignments)); - json.put("events", collectionToJson(events)); - json.put("gradebook", collectionToJson(gradebook)); - json.put("resources", collectionToJson(resources)); + this.semester = obj != null ? new Semester(obj) : null; + this.lvNumber = lv; + this.title = json.getString("title"); + this.lecturer = new ArrayList<>(lecturer); + this.type = json.getString("type"); + this.description = json.getString("description"); + this.ID = json.getString("ID"); + + JSONArray arr = json.optJSONArray("announcements"); + if (obj != null) { + ArrayList announce = new ArrayList<>(); + for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) { + announce.add(new Announcement(l)); + } + this.announcements = announce; + } + arr = json.optJSONArray("assignments"); + if (obj != null) { + AssignmentList assign = new AssignmentList(); + for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) { + assign.add(new Assignment(l)); + } + this.assignments = assign; + } + + arr = json.optJSONArray("events"); + if (obj != null) { + EventList events = new EventList(); + for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) { + events.add(new Event(l)); + } + this.events = events; + } + arr = json.optJSONArray("gradebook"); + if (obj != null) { + ArrayList grades = new ArrayList<>(); + for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) { + grades.add(new Grade(l)); + } + this.gradebook = grades; + } + arr = json.optJSONArray("resources"); + if (obj != null) { + ArrayList res = new ArrayList<>(); + for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) { + if (l.getString("restype").equals("file")) + res.add(new Resource.File(l)); + else + res.add(new Resource.Folder(l)); + } + this.resources = res; + } } @NonNull diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Resource.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Resource.java index 6c7820f..fb3013c 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Resource.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Resource.java @@ -5,7 +5,6 @@ import com.google.android.gms.common.internal.Objects; import org.json.JSONException; import org.json.JSONObject; -import java.io.Serializable; import java.util.ArrayList; import androidx.annotation.LayoutRes; @@ -16,7 +15,7 @@ import de.sebse.fuplanner.tools.ui.treeview.LayoutItemType; import de.sebse.fuplanner.tools.ui.treeview.TreeNode; -public abstract class Resource implements UtilsJson.JsonConvertable { +public abstract class Resource implements UtilsJson.JsonConvertible { final String author; final long modifiedDate; diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Semester.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Semester.java index 95a432d..3ec970d 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Semester.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/types/Semester.java @@ -3,13 +3,11 @@ package de.sebse.fuplanner.services.kvv.types; import org.json.JSONException; import org.json.JSONObject; -import java.io.Serializable; - import androidx.annotation.Nullable; import de.sebse.fuplanner.tools.Regex; import de.sebse.fuplanner.tools.UtilsJson; -public class Semester implements UtilsJson.JsonConvertable { +public class Semester implements UtilsJson.JsonConvertible { public static final int SEM_WS = 1; public static final int SEM_SS = 2; private int type; @@ -21,25 +19,17 @@ public class Semester implements UtilsJson.JsonConvertable { } public Semester(String semester_string) throws NoSuchFieldException { - /*Semester sem = null; - if (semester != null) { - sem = new Semester(semester) - semester = semester.replace("SS", "S"); - semester = semester.replaceAll("[0-9]{2}([0-9]{2})", "$1"); - }*/ - - - /*String s1type = Regex.regex("^(S|WS) ", a); - int s1year = Integer.parseInt(Regex.regex("^(S|WS) ([0-9]{2})", a, 2)); - String s2type = Regex.regex("^(S|WS) ", b); - int s2year = Integer.parseInt(Regex.regex("^(S|WS) ([0-9]{2})", b, 2));*/ - String type = Regex.regex("^(SS|WS) ", semester_string); String year = Regex.regex("^(SS|WS) ([0-9]{2})", semester_string, 2); this.type = type.equals("SS") ? SEM_SS : SEM_WS; this.year = Integer.parseInt(year); } + public Semester(JSONObject json) throws JSONException { + this.type = json.getInt("type"); + this.year = json.getInt("year"); + } + public int getType() { return type; } diff --git a/app/src/main/java/de/sebse/fuplanner/tools/UtilsJson.java b/app/src/main/java/de/sebse/fuplanner/tools/UtilsJson.java index 35f9cbc..58a782b 100644 --- a/app/src/main/java/de/sebse/fuplanner/tools/UtilsJson.java +++ b/app/src/main/java/de/sebse/fuplanner/tools/UtilsJson.java @@ -9,9 +9,8 @@ import java.util.Collection; import java.util.HashSet; import java.util.Iterator; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import de.sebse.fuplanner.services.kvv.types.Modules; -import de.sebse.fuplanner.services.kvv.types.SortedListModule; public class UtilsJson { public static JSONArray collectionToJson(@Nullable HashSet coll) { @@ -24,7 +23,7 @@ public class UtilsJson { return arr; } - public static JSONArray collectionToJson(@Nullable Collection coll) throws JSONException { + public static JSONArray collectionToJson(@Nullable Collection coll) throws JSONException { if (coll == null) return null; JSONArray arr = new JSONArray(); @@ -44,7 +43,7 @@ public class UtilsJson { return arr; } - public static JSONArray collectionToJson(@Nullable SortedList coll) throws JSONException { + public static JSONArray collectionToJson(@Nullable SortedList coll) throws JSONException { if (coll == null) return null; JSONArray arr = new JSONArray(); @@ -54,8 +53,8 @@ public class UtilsJson { return arr; } - public static Iterator jsonArrayToIterator(JSONArray array) { - return new Iterator() { + public static Iterator jsonArrayToIterator(JSONArray array, GetInterface func) { + return new Iterator() { int size = array == null ? 0 : array.length(); int pos = 0; @Override @@ -64,18 +63,47 @@ public class UtilsJson { } @Override - public Object next() { + public T next() { try { - return array.get(pos++); + return func.get(array, pos++); } catch (JSONException e) { e.printStackTrace(); return null; } } + }; + } + + @FunctionalInterface + private interface GetInterface { + T get(JSONArray array, int index) throws JSONException; + } + + public static JsonIterable jsonArrayToIterableObject(JSONArray array) { + return new JsonIterable<>(array, JSONArray::getJSONObject); + } + + public static JsonIterable jsonArrayToIterableString(JSONArray array) { + return new JsonIterable<>(array, JSONArray::getString); + } + + public static class JsonIterable implements Iterable { + private JSONArray array; + private GetInterface getInterface; + + JsonIterable(JSONArray array, GetInterface getInterface) { + this.array = array; + this.getInterface = getInterface; + } + @NonNull + @Override + public Iterator iterator() { + return UtilsJson.jsonArrayToIterator(array, getInterface); } } - public interface JsonConvertable { + public interface JsonConvertible { JSONObject toJSONObject() throws JSONException; } + }