Advertisement
Guest User

Config.java updated

a guest
Apr 8th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 KB | None | 0 0
  1. package com.kurzdev.vertretungsplanapp.basics;
  2.  
  3. import com.kurzdev.vertretungsplanapp.basics.UserHandler;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONObject;
  7. import java.io.*;
  8. import java.nio.file.Files;
  9. import java.nio.file.Paths;
  10. import java.util.ArrayList;
  11.  
  12. public class Config {
  13.  
  14.     public Config(){
  15.     }
  16.  
  17.     public ArrayList<String> loadDataUPC(){
  18.  
  19.         if(this.getData()){
  20.             return returnJSONDataUPC();
  21.         }else{
  22.             return null;
  23.         }
  24.     }
  25.  
  26.     public ArrayList<String> loadDataCourses(){
  27.  
  28.         if(this.getData()){
  29.             return returnJSONDataCourses();
  30.         }else{
  31.             return null;
  32.         }
  33.     }
  34.  
  35.     private String loadJSON(){
  36.  
  37.         try {
  38.             File json_file = new File(System.getProperty("user.dir") + "/src/Config.json");
  39.             String json_raw = new String(Files.readAllBytes(Paths.get(json_file.toURI())), "UTF-8");
  40.  
  41.             return json_raw;
  42.  
  43.         } catch(IOException e){
  44.             System.out.println("An IOException occured: " + e.getMessage());
  45.             return null;
  46.         } catch(Exception e){
  47.             System.out.println("An unknown exception occured: " + e.getMessage());
  48.             return null;
  49.         }
  50.     }
  51.  
  52.     /*
  53.     Method to write data to an desired JSON file
  54.     @param username: Argument used to log into Moodle
  55.     @param password: Argument to authenticate on Moodle
  56.     @param class_data: Argument regarding data about a certain class/grade
  57.     @param course_data: Argument/List consisting of one's courses - to-be-used in MyFilter
  58.      */
  59.     @SuppressWarnings("unchecked")
  60.     private void writeJSON(String username, String password, String class_data, ArrayList<String> course_data){
  61.  
  62.         JSONObject obj = new JSONObject();
  63.  
  64.         if(username.equals("")){
  65.             obj.put("username", "");
  66.         }else if(password.equals("")){
  67.             obj.put("password", "");
  68.         }else if(class_data.equals("")) {
  69.             obj.put("class", "");
  70.         }else if(course_data.isEmpty()){
  71.             obj.put("courses", "");
  72.         }else{
  73.             obj.put("username", username);
  74.             obj.put("password", password);
  75.             obj.put("class", class_data);
  76.  
  77.             JSONArray course_list = new JSONArray();
  78.             for(String elements : course_data){
  79.                 course_list.put(elements);
  80.             }
  81.             obj.put("courses", course_list);
  82.  
  83.             try(FileWriter file = new FileWriter(System.getProperty("user.dir") + "/src/Config.json")){
  84.                 file.write(obj.toString());
  85.                 file.flush();
  86.             }catch(IOException e){
  87.                 System.out.println("An IOException occured: " + e.getMessage());
  88.             }catch(Exception e){
  89.                 System.out.println("An unknown exception occured: " + e.getMessage());
  90.             }
  91.  
  92.         }
  93.     }
  94.  
  95.     /*
  96.      Method used to get desired data for JSON file
  97.      */
  98.     private Boolean getData(){
  99.         if(!this.checkJSON()){
  100.             UserHandler uHandler = new UserHandler();
  101.  
  102.             String username = uHandler.getUsername();
  103.             String password = uHandler.getPassword();
  104.             String classes = "3";
  105.  
  106.             ArrayList<String> course_list = new ArrayList<>();
  107.             course_list.add("Wer das liest ist dumm.");
  108.  
  109.             this.writeJSON(username, password, classes, course_list);
  110.             return true;
  111.         }else if(this.checkJSON()){
  112.             return true;
  113.         }else{
  114.             return false;
  115.         }
  116.     }
  117.  
  118.     private Boolean checkJSON(){
  119.             String raw_json = this.loadJSON();
  120.             if(raw_json == null){
  121.                 System.out.println("JSON is null.");
  122.                 return false;
  123.             }else {
  124.                 JSONObject jobj = new JSONObject(raw_json);
  125.  
  126.                 String class_data = jobj.get("class").toString();
  127.                 String username = jobj.get("username").toString();
  128.                 String password = jobj.get("password").toString();
  129.  
  130.                 JSONArray jarray = jobj.getJSONArray("courses");
  131.                 ArrayList<String> courses = new ArrayList<>();
  132.  
  133.                 for(int i = 0; i<jarray.length(); i++){
  134.                     courses.add(jarray.getString(i));
  135.                 }
  136.  
  137.                 if(username.equals("")){
  138.                     return false;
  139.                 }else if(password.equals("")){
  140.                     return false;
  141.                 }else if(class_data.equals("")) {
  142.                     return false;
  143.                 }else if(courses.isEmpty()){
  144.                     return false;
  145.                 }else {
  146.                     return true;
  147.                 }
  148.             }
  149.     }
  150.  
  151.     private ArrayList<String> returnJSONDataUPC(){
  152.  
  153.         String raw_json = this.loadJSON();
  154.         JSONObject jobj = new JSONObject(raw_json);
  155.  
  156.         String class_data = jobj.get("class").toString();
  157.         String username = jobj.get("username").toString();
  158.         String password = jobj.get("password").toString();
  159.  
  160.         final ArrayList<String> final_list = new ArrayList<>();
  161.         final_list.add(username);
  162.         final_list.add(password);
  163.         final_list.add(class_data);
  164.  
  165.         return final_list;
  166.     }
  167.  
  168.     private ArrayList<String> returnJSONDataCourses(){
  169.  
  170.         String raw_json = this.loadJSON();
  171.         JSONObject jobj = new JSONObject(raw_json);
  172.  
  173.         JSONArray jarray = jobj.getJSONArray("courses");
  174.         ArrayList<String> courses = new ArrayList<>();
  175.  
  176.         for(int i = 0; i<jarray.length(); i++){
  177.             courses.add(jarray.getString(i));
  178.         }
  179.  
  180.         return courses;
  181.     }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement