aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com/example/portfolio2/Database.java
blob: 2712e3dc2c9d2ffb49de2f7c4cc65ac1b65f7883 (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. /// Bachelorizer - database model
  8. ///
  9. /// This model handles all interaction with the database.
  10. class Database {
  11. /// database singleton
  12. MyDB db = new MyDB();
  13. /// default constructor
  14. // (declared explicitly only to silence javadoc)
  15. Database() {}
  16. /// clear the participation database at program launch
  17. void initialize() {
  18. clearParticipation();
  19. }
  20. /// resolve activity index from name
  21. ///
  22. /// @param name activity name
  23. /// @return index of activity as integer
  24. int getActivityIndeks(String name) {
  25. if(name ==null) return -1;
  26. ArrayList<String> result = db.query("select indeks from activity a where name is '"+name+"';", "indeks");
  27. return Integer.parseInt(result.getFirst());
  28. }
  29. /// insert activity into participation
  30. ///
  31. /// @param activityIndex index of activity
  32. void addParticipation(int activityIndex) {
  33. db.cmd("insert into participation values(123, "+activityIndex+");");
  34. }
  35. /// list currently participating activities
  36. ///
  37. /// @param program programme name
  38. /// @return names of participating activities
  39. ArrayList<String> getParticipation(String program) {
  40. return db.query("select name from participation p inner join activity a on p.indeks = a.indeks where program is '" + program + "';", "name");
  41. }
  42. /// purge participation database
  43. void clearParticipation() {
  44. db.cmd("delete from participation");
  45. }
  46. /// list activities within a program
  47. ///
  48. /// @param program programme name
  49. /// @return names of contained activities
  50. ArrayList<String> selectProgram(String program) {
  51. return db.query("select name from activity where program is '" + program + "';", "name");
  52. }
  53. /// sum of ECTS points under the given category
  54. ///
  55. /// @param program programme name
  56. /// @return ECTS points as String
  57. String getSumEcts(String program){
  58. if(program==null)return "0";
  59. 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");
  60. if (result.isEmpty()) return "0";
  61. return result.getFirst();
  62. }
  63. /// sum of ECTS points under the given category
  64. ///
  65. /// @return names of all activities as list of String
  66. ArrayList<String> getAllActivities() {
  67. return db.query("select name from activity;", "name");
  68. }
  69. }