aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com/example/portfolio2/Model.java
blob: bafa3fc56ac0df77b7781ae7ac9c30b41b7f3313 (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. // The model handles all interactions with the database
  8. class Model{
  9. MyDB db=new MyDB();
  10. Model(){}
  11. // When running the program, it clears the participation database
  12. void initialize() {
  13. clearParticipation();
  14. }
  15. // Returns the integer value of the activity's index
  16. int getActivityIndeks(String name) {
  17. if(name ==null) return -1;
  18. ArrayList<String> result = db.query("select indeks from activity a where name is '"+name+"';", "indeks");
  19. return Integer.parseInt(result.getFirst());
  20. }
  21. // Inserts the given activity into participation using the activity's index
  22. void addParticipation(int activityIndex) {
  23. db.cmd("insert into participation values(123, "+activityIndex+");");
  24. }
  25. // Returns all activity names from activities currently in participation
  26. ArrayList<String> getParticipation(String program) {
  27. return db.query("select name from participation p inner join activity a on p.indeks = a.indeks where program is '" + program + "';", "name");
  28. }
  29. // Removes all entries in the participation database
  30. void clearParticipation() {
  31. db.cmd("delete from participation");
  32. }
  33. // Returns an arraylist of activities within the given program
  34. ArrayList<String> selectProgram(String program) {
  35. return db.query("select name from activity where program is '" + program + "';", "name");
  36. }
  37. // Returns the sum of ECTS points under the given category from the student as a string
  38. String getSumEcts(String program){
  39. if(program==null)return "0";
  40. 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");
  41. if (result.isEmpty()) return "0";
  42. return result.getFirst();
  43. }
  44. ArrayList<String> getAllActivities() {
  45. return db.query("select name from activity;", "name");
  46. }
  47. }