aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com/example/portfolio2/Database.java
blob: a6a144a8ba0346e3ac2417e1fe6b39682e20c264 (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(final String name) {
  25. if (name == null) {
  26. return -1;
  27. }
  28. ArrayList<String> result = db.query("select indeks from activity a where name is '" + name + "';", "indeks");
  29. return Integer.parseInt(result.getFirst());
  30. }
  31. /// insert activity into participation
  32. ///
  33. /// @param activityIndex index of activity
  34. void addParticipation(final int activityIndex) {
  35. db.cmd("insert into participation values(123, " + activityIndex + ");");
  36. }
  37. /// list currently participating activities
  38. ///
  39. /// @param program programme name
  40. /// @return names of participating activities
  41. ArrayList<String> getParticipation(final String program) {
  42. return db.query("select name from participation p inner join activity a on p.indeks = a.indeks where program is '" + program + "';", "name");
  43. }
  44. /// purge participation database
  45. void clearParticipation() {
  46. db.cmd("delete from participation");
  47. }
  48. /// list activities within a program
  49. ///
  50. /// @param program programme name
  51. /// @return names of contained activities
  52. ArrayList<String> selectProgram(final String program) {
  53. return db.query("select name from activity where program is '" + program + "';", "name");
  54. }
  55. /// sum of ECTS points under the given category
  56. ///
  57. /// @param program programme name
  58. /// @return ECTS points as String
  59. String getSumEcts(final String program) {
  60. if (program == null) {
  61. return "0";
  62. }
  63. 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");
  64. if (result.isEmpty()) {
  65. return "0";
  66. }
  67. return result.getFirst();
  68. }
  69. /// sum of ECTS points under the given category
  70. ///
  71. /// @return names of all activities as list of String
  72. ArrayList<String> getAllActivities() {
  73. return db.query("select name from activity;", "name");
  74. }
  75. }