Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kurzdev.vertretungsplanapp.basics;
- import com.kurzdev.vertretungsplanapp.basics.UserHandler;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- public class Config {
- public Config(){
- }
- public ArrayList<String> loadDataUPC(){
- if(this.getData()){
- return returnJSONDataUPC();
- }else{
- return null;
- }
- }
- public ArrayList<String> loadDataCourses(){
- if(this.getData()){
- return returnJSONDataCourses();
- }else{
- return null;
- }
- }
- private String loadJSON(){
- try {
- File json_file = new File(System.getProperty("user.dir") + "/src/Config.json");
- String json_raw = new String(Files.readAllBytes(Paths.get(json_file.toURI())), "UTF-8");
- return json_raw;
- } catch(IOException e){
- System.out.println("An IOException occured: " + e.getMessage());
- return null;
- } catch(Exception e){
- System.out.println("An unknown exception occured: " + e.getMessage());
- return null;
- }
- }
- /*
- Method to write data to an desired JSON file
- @param username: Argument used to log into Moodle
- @param password: Argument to authenticate on Moodle
- @param class_data: Argument regarding data about a certain class/grade
- @param course_data: Argument/List consisting of one's courses - to-be-used in MyFilter
- */
- @SuppressWarnings("unchecked")
- private void writeJSON(String username, String password, String class_data, ArrayList<String> course_data){
- JSONObject obj = new JSONObject();
- if(username.equals("")){
- obj.put("username", "");
- }else if(password.equals("")){
- obj.put("password", "");
- }else if(class_data.equals("")) {
- obj.put("class", "");
- }else if(course_data.isEmpty()){
- obj.put("courses", "");
- }else{
- obj.put("username", username);
- obj.put("password", password);
- obj.put("class", class_data);
- JSONArray course_list = new JSONArray();
- for(String elements : course_data){
- course_list.put(elements);
- }
- obj.put("courses", course_list);
- try(FileWriter file = new FileWriter(System.getProperty("user.dir") + "/src/Config.json")){
- file.write(obj.toString());
- file.flush();
- }catch(IOException e){
- System.out.println("An IOException occured: " + e.getMessage());
- }catch(Exception e){
- System.out.println("An unknown exception occured: " + e.getMessage());
- }
- }
- }
- /*
- Method used to get desired data for JSON file
- */
- private Boolean getData(){
- if(!this.checkJSON()){
- UserHandler uHandler = new UserHandler();
- String username = uHandler.getUsername();
- String password = uHandler.getPassword();
- String classes = "3";
- ArrayList<String> course_list = new ArrayList<>();
- course_list.add("Wer das liest ist dumm.");
- this.writeJSON(username, password, classes, course_list);
- return true;
- }else if(this.checkJSON()){
- return true;
- }else{
- return false;
- }
- }
- private Boolean checkJSON(){
- String raw_json = this.loadJSON();
- if(raw_json == null){
- System.out.println("JSON is null.");
- return false;
- }else {
- JSONObject jobj = new JSONObject(raw_json);
- String class_data = jobj.get("class").toString();
- String username = jobj.get("username").toString();
- String password = jobj.get("password").toString();
- JSONArray jarray = jobj.getJSONArray("courses");
- ArrayList<String> courses = new ArrayList<>();
- for(int i = 0; i<jarray.length(); i++){
- courses.add(jarray.getString(i));
- }
- if(username.equals("")){
- return false;
- }else if(password.equals("")){
- return false;
- }else if(class_data.equals("")) {
- return false;
- }else if(courses.isEmpty()){
- return false;
- }else {
- return true;
- }
- }
- }
- private ArrayList<String> returnJSONDataUPC(){
- String raw_json = this.loadJSON();
- JSONObject jobj = new JSONObject(raw_json);
- String class_data = jobj.get("class").toString();
- String username = jobj.get("username").toString();
- String password = jobj.get("password").toString();
- final ArrayList<String> final_list = new ArrayList<>();
- final_list.add(username);
- final_list.add(password);
- final_list.add(class_data);
- return final_list;
- }
- private ArrayList<String> returnJSONDataCourses(){
- String raw_json = this.loadJSON();
- JSONObject jobj = new JSONObject(raw_json);
- JSONArray jarray = jobj.getJSONArray("courses");
- ArrayList<String> courses = new ArrayList<>();
- for(int i = 0; i<jarray.length(); i++){
- courses.add(jarray.getString(i));
- }
- return courses;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement