aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com/example/portfolio2/Model.java
blob: 73f2f6b573203171ab7d6126e25a99cf8efad16c (plain)
  1. // SPDX-FileCopyrightText: <Alexander Marthin Klemensen stud-marthin@ruc.dk>
  2. // SPDX-FileCopyrightText: <Ian Valentin Christensen stud-ianc@ruc.dk>
  3. // SPDX-FileCopyrightText: <Zahed Noos zahed@ruc.dk>
  4. // SPDX-License-Identifier: GPL-3.0-or-later
  5. package com.example.portfolio2;
  6. import java.util.ArrayList;
  7. class Model{ // The model handles all interactions with the database
  8. MyDB db=new MyDB();
  9. Model(){}
  10. void initialize() { // When running the program, it clears the participation database
  11. clearParticipation();
  12. }
  13. int getActivityIndeks(String name) { // Returns the integer value of the activity's index
  14. if(name ==null) return -1;
  15. ArrayList<String> result = db.query("select indeks from activity a where name is '"+name+"';", "indeks");
  16. return Integer.parseInt(result.getFirst());
  17. }
  18. void addParticipation(int activityIndex) { // Inserts the given activity into participation using the activity's index
  19. db.cmd("insert into participation values(123, "+activityIndex+");");
  20. }
  21. ArrayList<String> getParticipation(String program) { // Returns all activity names from activities currently in participation
  22. return db.query("select name from participation p inner join activity a on p.indeks = a.indeks where program is '" + program + "';", "name");
  23. }
  24. void clearParticipation() { // Removes all entries in the participation database
  25. db.cmd("delete from participation");
  26. }
  27. ArrayList<String> selectProgram(String program) { // Returns an arraylist of activities within the given program
  28. return db.query("select name from activity where program is '" + program + "';", "name");
  29. }
  30. String getSumEcts(String program){ // Returns the sum of ECTS points under the given category from the student as a string
  31. if(program==null)return "0";
  32. ArrayList<String> result = db.query("select sum(activity.ects) as total_ects,student.name from student left outer join participation on student.studid = participation.studid inner join activity on participation.indeks = activity.indeks where program is '"+program+"' group by student.studid ;", "total_ects");
  33. if (result.isEmpty()) return "0";
  34. return result.getFirst();
  35. }
  36. ArrayList<String> getAllActivities() {
  37. return db.query("select name from activity;", "name");
  38. }
  39. }