Skip to content

788 - Waiting list bookings are not auto promoted #788#401

Open
mariusmarin-dev wants to merge 3 commits intomainfrom
788_auto_promotion
Open

788 - Waiting list bookings are not auto promoted #788#401
mariusmarin-dev wants to merge 3 commits intomainfrom
788_auto_promotion

Conversation

@mariusmarin-dev
Copy link

No description provided.

// For Student events we only limit the number of students, other roles do not count against the capacity
Integer studentCount = roleCounts.getOrDefault(Role.STUDENT, 0);
return Math.max(0, numberOfPlaces - studentCount);
Integer placesAvailable = Math.max(0, numberOfPlaces - studentCount);

Check warning

Code scanning / CodeQL

Boxed variable is never null Warning

The variable 'placesAvailable' is only assigned values of primitive type and is never 'null', but it is declared with the boxed type 'Integer'.

Copilot Autofix

AI 4 days ago

In general, to fix this kind of issue you should declare local variables as primitive types (int, boolean, etc.) when they are never intended to hold null, even if the enclosing method returns a boxed type. Java will automatically box the primitive when returning it from a method with a boxed return type.

For this specific case in EventBookingManager.getPlacesAvailable, change the type of the local variables that are purely numeric counts/derived values and never assigned null from Integer to int. That includes placesAvailable, studentCount, and totalBooked. They are all initialized from non-null values (getOrDefault with a primitive default 0, stream reduce(0, Integer::sum), and Math.max(0, ...)), and only used in arithmetic and logging; Java will autobox them where an Object is needed (e.g. for the SLF4J logging call) and when returning from the method. This preserves existing functionality while eliminating unnecessary boxing/unboxing and aligning with the analysis warning.

Concretely:

  • In the student-event branch, change Integer studentCount to int studentCount and Integer placesAvailable to int placesAvailable.
  • In the non-student branch, change Integer totalBooked to int totalBooked and Integer placesAvailable to int placesAvailable.
    No new imports or methods are required.
Suggested changeset 1
src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java b/src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java
--- a/src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java
+++ b/src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java
@@ -944,16 +944,16 @@
     // Calculate remaining capacity with a lower bound of zero (no negatives)
     if (isStudentEvent) {
       // For Student events we only limit the number of students, other roles do not count against the capacity
-      Integer studentCount = roleCounts.getOrDefault(Role.STUDENT, 0);
-      Integer placesAvailable = Math.max(0, numberOfPlaces - studentCount);
+      int studentCount = roleCounts.getOrDefault(Role.STUDENT, 0);
+      int placesAvailable = Math.max(0, numberOfPlaces - studentCount);
 
       log.info("Event {} capacity: total={}, studentBooked={}, available={}, includeDeleted={}",
             event.getId(), numberOfPlaces, studentCount, placesAvailable, includeDeletedUsersInCounts);
       return placesAvailable;
     } else {
       // For other events, count all roles
-      Integer totalBooked = roleCounts.values().stream().reduce(0, Integer::sum);
-      Integer placesAvailable = Math.max(0, numberOfPlaces - totalBooked);
+      int totalBooked = roleCounts.values().stream().reduce(0, Integer::sum);
+      int placesAvailable = Math.max(0, numberOfPlaces - totalBooked);
       log.info("Event {} capacity: total={}, totalBooked={}, available={}, includeDeleted={}",
           event.getId(), numberOfPlaces, totalBooked, placesAvailable, includeDeletedUsersInCounts);
 
EOF
@@ -944,16 +944,16 @@
// Calculate remaining capacity with a lower bound of zero (no negatives)
if (isStudentEvent) {
// For Student events we only limit the number of students, other roles do not count against the capacity
Integer studentCount = roleCounts.getOrDefault(Role.STUDENT, 0);
Integer placesAvailable = Math.max(0, numberOfPlaces - studentCount);
int studentCount = roleCounts.getOrDefault(Role.STUDENT, 0);
int placesAvailable = Math.max(0, numberOfPlaces - studentCount);

log.info("Event {} capacity: total={}, studentBooked={}, available={}, includeDeleted={}",
event.getId(), numberOfPlaces, studentCount, placesAvailable, includeDeletedUsersInCounts);
return placesAvailable;
} else {
// For other events, count all roles
Integer totalBooked = roleCounts.values().stream().reduce(0, Integer::sum);
Integer placesAvailable = Math.max(0, numberOfPlaces - totalBooked);
int totalBooked = roleCounts.values().stream().reduce(0, Integer::sum);
int placesAvailable = Math.max(0, numberOfPlaces - totalBooked);
log.info("Event {} capacity: total={}, totalBooked={}, available={}, includeDeleted={}",
event.getId(), numberOfPlaces, totalBooked, placesAvailable, includeDeletedUsersInCounts);

Copilot is powered by AI and may make mistakes. Always verify output.
// For other events, count all roles
Integer totalBooked = roleCounts.values().stream().reduce(0, Integer::sum);
return Math.max(0, numberOfPlaces - totalBooked);
Integer placesAvailable = Math.max(0, numberOfPlaces - totalBooked);

Check warning

Code scanning / CodeQL

Boxed variable is never null Warning

The variable 'placesAvailable' is only assigned values of primitive type and is never 'null', but it is declared with the boxed type 'Integer'.

Copilot Autofix

AI 4 days ago

In general, to fix this kind of issue you should use the primitive type (int, boolean, etc.) for variables that are never assigned null and are only involved in primitive operations. This avoids unnecessary boxing/unboxing and makes it clear that the value cannot be null.

In this specific case, both studentCount and placesAvailable in the student-event branch, and totalBooked and placesAvailable in the non-student branch, are always non-null and participate purely in arithmetic. They can be safely declared as int instead of Integer. The method return type remains Integer so callers are unaffected: when returning an int value, Java will autobox it to Integer. The early return null; remains as-is for the “no capacity” situation. No imports or additional methods are required; we only change the local variable declarations on lines 947–948 and 955–956 to use int rather than Integer.

Concretely:

  • Change Integer studentCountint studentCount
  • Change Integer placesAvailable (in the student branch) → int placesAvailable
  • Change Integer totalBookedint totalBooked
  • Change Integer placesAvailable (in the non-student branch) → int placesAvailable

No other logic or logging needs to change, since log.info handles primitive and boxed numbers transparently via autoboxing/varargs.

Suggested changeset 1
src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java b/src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java
--- a/src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java
+++ b/src/main/java/uk/ac/cam/cl/dtg/isaac/api/managers/EventBookingManager.java
@@ -944,16 +944,16 @@
     // Calculate remaining capacity with a lower bound of zero (no negatives)
     if (isStudentEvent) {
       // For Student events we only limit the number of students, other roles do not count against the capacity
-      Integer studentCount = roleCounts.getOrDefault(Role.STUDENT, 0);
-      Integer placesAvailable = Math.max(0, numberOfPlaces - studentCount);
+      int studentCount = roleCounts.getOrDefault(Role.STUDENT, 0);
+      int placesAvailable = Math.max(0, numberOfPlaces - studentCount);
 
       log.info("Event {} capacity: total={}, studentBooked={}, available={}, includeDeleted={}",
             event.getId(), numberOfPlaces, studentCount, placesAvailable, includeDeletedUsersInCounts);
       return placesAvailable;
     } else {
       // For other events, count all roles
-      Integer totalBooked = roleCounts.values().stream().reduce(0, Integer::sum);
-      Integer placesAvailable = Math.max(0, numberOfPlaces - totalBooked);
+      int totalBooked = roleCounts.values().stream().reduce(0, Integer::sum);
+      int placesAvailable = Math.max(0, numberOfPlaces - totalBooked);
       log.info("Event {} capacity: total={}, totalBooked={}, available={}, includeDeleted={}",
           event.getId(), numberOfPlaces, totalBooked, placesAvailable, includeDeletedUsersInCounts);
 
EOF
@@ -944,16 +944,16 @@
// Calculate remaining capacity with a lower bound of zero (no negatives)
if (isStudentEvent) {
// For Student events we only limit the number of students, other roles do not count against the capacity
Integer studentCount = roleCounts.getOrDefault(Role.STUDENT, 0);
Integer placesAvailable = Math.max(0, numberOfPlaces - studentCount);
int studentCount = roleCounts.getOrDefault(Role.STUDENT, 0);
int placesAvailable = Math.max(0, numberOfPlaces - studentCount);

log.info("Event {} capacity: total={}, studentBooked={}, available={}, includeDeleted={}",
event.getId(), numberOfPlaces, studentCount, placesAvailable, includeDeletedUsersInCounts);
return placesAvailable;
} else {
// For other events, count all roles
Integer totalBooked = roleCounts.values().stream().reduce(0, Integer::sum);
Integer placesAvailable = Math.max(0, numberOfPlaces - totalBooked);
int totalBooked = roleCounts.values().stream().reduce(0, Integer::sum);
int placesAvailable = Math.max(0, numberOfPlaces - totalBooked);
log.info("Event {} capacity: total={}, totalBooked={}, available={}, includeDeleted={}",
event.getId(), numberOfPlaces, totalBooked, placesAvailable, includeDeletedUsersInCounts);

Copilot is powered by AI and may make mistakes. Always verify output.
@sonarqubecloud
Copy link

@github-actions
Copy link

Coverage Report

Overall Project 31.46%
Files changed 96.24%

File Coverage
EventBookingManager.java 65.58% -0.07%
EventBookingPersistenceManager.java 62.87% -1.1%
PgEventBookings.java 54.32%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant