aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com/example/portfolio2/Database.java
blob: d978e83d5254a0c949dc81a8487abcfea56420e3 (plain)
  1. // SPDX-FileCopyrightText: 2025 Jonas Smedegaard <dr@jones.dk>
  2. // SPDX-FileCopyrightText: 2025 <Alexander Marthin Klemensen stud-marthin@ruc.dk>
  3. // SPDX-FileCopyrightText: 2025 <Ian Valentin Christensen stud-ianc@ruc.dk>
  4. // SPDX-FileCopyrightText: 2025 <Zahed Noos zahed@ruc.dk>
  5. // SPDX-License-Identifier: GPL-3.0-or-later
  6. package com.example.portfolio2;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /// Bachelorizer - database model
  10. ///
  11. /// This model handles all interaction with the database.
  12. class Database {
  13. /// database singleton
  14. MyDB db = new MyDB();
  15. /// available subject modules
  16. // TODO: replace this dummy placeholder with database query
  17. private List<String> Modules = List.of(
  18. "Computer Science",
  19. "Informatik",
  20. "Astrology"
  21. );
  22. /// default constructor
  23. // (declared explicitly only to silence javadoc)
  24. Database() { }
  25. /// clear the participation database at program launch
  26. void initialize() {
  27. clearParticipation();
  28. }
  29. /// resolve subject module index from name
  30. ///
  31. /// @param name subject module name
  32. /// @return index of subject module as integer
  33. // TODO: replace this dummy placeholder with database query
  34. int getModuleIndeks(final String name) {
  35. if (name == null) {
  36. return -1;
  37. }
  38. return Modules.indexOf(name);
  39. }
  40. /// resolve activity index from name
  41. ///
  42. /// @param name activity name
  43. /// @return index of activity as integer
  44. int getActivityIndeks(final String name) {
  45. if (name == null) {
  46. return -1;
  47. }
  48. ArrayList<String> result = db.query(
  49. "select indeks from activity"
  50. + " where name is '" + name + "';",
  51. "indeks");
  52. return Integer.parseInt(result.getFirst());
  53. }
  54. /// insert activity into participation
  55. ///
  56. /// @param activityIndex index of activity
  57. void addParticipation(final int activityIndex) {
  58. db.cmd("insert into participation values(123, "
  59. + activityIndex + ");");
  60. }
  61. /// list currently participating activities
  62. ///
  63. /// @param program programme name
  64. /// @return names of participating activities
  65. ArrayList<String> getParticipation(final String program) {
  66. return db.query(
  67. "select name from participation p"
  68. + " inner join activity a on p.indeks = a.indeks"
  69. + " where program is '" + program + "';",
  70. "name");
  71. }
  72. /// purge participation database
  73. void clearParticipation() {
  74. db.cmd("delete from participation");
  75. }
  76. /// list activities within a program
  77. ///
  78. /// @param program programme name
  79. /// @return names of contained activities
  80. ArrayList<String> selectProgram(final String program) {
  81. return db.query(
  82. "select name from activity"
  83. + " where program is '" + program + "';",
  84. "name");
  85. }
  86. /// sum of ECTS points under the given category
  87. ///
  88. /// @param program programme name
  89. /// @return ECTS points as String
  90. String getSumEcts(final String program) {
  91. if (program == null) {
  92. return "0";
  93. }
  94. ArrayList<String> result = db.query(
  95. "select sum(activity.ects)"
  96. + " as total_ects,student.name"
  97. + " from student left outer join participation"
  98. + " on student.studid = participation.studid"
  99. + " inner join activity"
  100. + " on participation.indeks = activity.indeks"
  101. + " where program is '" + program + "'"
  102. + " group by student.studid ;",
  103. "total_ects");
  104. if (result.isEmpty()) {
  105. return "0";
  106. }
  107. return result.getFirst();
  108. }
  109. /// list of available subject modules
  110. ///
  111. /// @return names of all subject moduless as list of strings
  112. List<String> getAllModules() {
  113. return Modules;
  114. }
  115. /// list of available activities
  116. ///
  117. /// @return names of all activities as list of strings
  118. ArrayList<String> getAllActivities() {
  119. return db.query("select name from activity;", "name");
  120. }
  121. }