aboutsummaryrefslogtreecommitdiff
path: root/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Database.java
blob: f814b9230685e6fb045dadb0cbe70eb5903d9958 (plain)
  1. // SPDX-FileCopyrightText: 2025 <Alexander Marthin Klemensen stud-marthin@ruc.dk>
  2. // SPDX-FileCopyrightText: 2025 <Ian Valentin Christensen stud-ianc@ruc.dk>
  3. // SPDX-FileCopyrightText: 2025 Jonas Smedegaard <dr@jones.dk>
  4. // SPDX-FileCopyrightText: 2025 <Zahed Noos zahed@ruc.dk>
  5. // SPDX-License-Identifier: GPL-3.0-or-later
  6. package dk.biks.bachelorizer;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import com.example.portfolio2.MyDB;
  10. /// Bachelorizer - database storage model
  11. ///
  12. /// This model handles all interaction with the database.
  13. class Database extends Storage {
  14. /// database singleton
  15. private MyDB db = new MyDB();
  16. /// default constructor
  17. // (declared explicitly only to silence javadoc)
  18. Database() { }
  19. /// student object
  20. // TODO: replace this dummy placeholder with database query
  21. private Person student;
  22. /// clear the participation database at program launch
  23. void initialize() {
  24. clearParticipation();
  25. }
  26. /// add student
  27. ///
  28. /// @param name Name of student
  29. // TODO: replace this dummy placeholder with database query
  30. public final void addStudent(final String name) {
  31. student = new Person(name);
  32. }
  33. /// get student name
  34. ///
  35. /// @return name of student
  36. // TODO: replace this dummy placeholder with database query
  37. public final String getStudentName() {
  38. return student.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 choices
  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 modules as list of strings
  112. List<String> getAllSubjects() {
  113. return db.query(
  114. "SELECT DISTINCT program FROM activity"
  115. + " WHERE program NOT IN ("
  116. + " SELECT program from activity"
  117. + " WHERE name LIKE 'BP1 %')",
  118. "program");
  119. }
  120. /// list of available activities
  121. ///
  122. /// @return names of all activities as list of strings
  123. ArrayList<String> getAllActivities() {
  124. return db.query("SELECT name FROM activity;", "name");
  125. }
  126. }