aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com/example/portfolio2/Database.java
blob: 8b8f93a0070db3145186d6e2785f5f1a8f69c02c (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. private MyDB db = new MyDB();
  15. /// default constructor
  16. // (declared explicitly only to silence javadoc)
  17. Database() { }
  18. /// student object
  19. // TODO: replace this dummy placeholder with database query
  20. private Person student;
  21. /// clear the participation database at program launch
  22. void initialize() {
  23. clearParticipation();
  24. }
  25. /// Add student
  26. ///
  27. /// @param name Name of student
  28. // TODO: replace this dummy placeholder with database query
  29. public final void addStudent(final String name) {
  30. student = new Person(name);
  31. }
  32. /// Get student name
  33. ///
  34. /// @return name of student
  35. // TODO: replace this dummy placeholder with database query
  36. public final String getStudentName() {
  37. return student.name;
  38. }
  39. /// resolve activity index from name
  40. ///
  41. /// @param name activity name
  42. /// @return index of activity as integer
  43. int getActivityIndeks(final String name) {
  44. if (name == null) {
  45. return -1;
  46. }
  47. ArrayList<String> result = db.query(
  48. "SELECT indeks FROM activity"
  49. + " WHERE name IS '" + name + "';",
  50. "indeks");
  51. return Integer.parseInt(result.getFirst());
  52. }
  53. /// insert activity into participation
  54. ///
  55. /// @param activityIndex index of activity
  56. void addParticipation(final int activityIndex) {
  57. db.cmd("INSERT INTO participation VALUES(123, "
  58. + activityIndex + ");");
  59. }
  60. /// list currently participating activities
  61. ///
  62. /// @param program programme name
  63. /// @return names of participating activities
  64. ArrayList<String> getParticipation(final String program) {
  65. return db.query(
  66. "SELECT name FROM participation p"
  67. + " INNER JOIN activity a ON p.indeks = a.indeks"
  68. + " WHERE program IS '" + program + "';",
  69. "name");
  70. }
  71. /// purge participation database
  72. void clearParticipation() {
  73. db.cmd("DELETE FROM participation");
  74. }
  75. /// list activities within a program
  76. ///
  77. /// @param program programme name
  78. /// @return names of contained activities
  79. ArrayList<String> selectProgram(final String program) {
  80. return db.query(
  81. "SELECT name FROM activity"
  82. + " WHERE program IS '" + program + "';",
  83. "name");
  84. }
  85. /// sum of ECTS points under the given category
  86. ///
  87. /// @param program programme name
  88. /// @return ECTS points as String
  89. String getSumEcts(final String program) {
  90. if (program == null) {
  91. return "0";
  92. }
  93. ArrayList<String> result = db.query(
  94. "SELECT SUM(activity.ects)"
  95. + " AS total_ects,student.name"
  96. + " FROM student LEFT OUTER JOIN participation"
  97. + " ON student.studid = participation.studid"
  98. + " INNER JOIN activity"
  99. + " ON participation.indeks = activity.indeks"
  100. + " WHERE program IS '" + program + "'"
  101. + " GROUP BY student.studid ;",
  102. "total_ects");
  103. if (result.isEmpty()) {
  104. return "0";
  105. }
  106. return result.getFirst();
  107. }
  108. /// list of available subject modules
  109. ///
  110. /// @return names of all subject modules as list of strings
  111. List<String> getAllSubjects() {
  112. return db.query(
  113. "SELECT DISTINCT program FROM activity"
  114. + " WHERE program NOT IN ("
  115. + " SELECT program from activity"
  116. + " WHERE name LIKE 'BP1 %')",
  117. "program");
  118. }
  119. /// list of available activities
  120. ///
  121. /// @return names of all activities as list of strings
  122. ArrayList<String> getAllActivities() {
  123. return db.query("SELECT name FROM activity;", "name");
  124. }
  125. }