You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gcc.communityHall.configuration;
  2. import java.util.Map;
  3. import java.util.UUID;
  4. import java.util.concurrent.ConcurrentHashMap;
  5. /**
  6. * Utility class for handling UUID-based ref_id for booking verification
  7. * This provides a secure and URL-friendly approach for booking reference IDs
  8. */
  9. public class UuidBookingUtil {
  10. // In-memory cache to store UUID to booking ID mapping
  11. // In production, this should be replaced with a database table or Redis
  12. private static final Map<String, Integer> uuidToBookingIdMap = new ConcurrentHashMap<>();
  13. private static final Map<Integer, String> bookingIdToUuidMap = new ConcurrentHashMap<>();
  14. /**
  15. * Generate a UUID ref_id for a booking ID
  16. * @param bookingId The original booking ID
  17. * @return UUID string that can be used as ref_id
  18. */
  19. public static String generateRefIdForBooking(int bookingId) {
  20. String uuid = UUID.randomUUID().toString();
  21. // Store the mapping
  22. uuidToBookingIdMap.put(uuid, bookingId);
  23. bookingIdToUuidMap.put(bookingId, uuid);
  24. return uuid;
  25. }
  26. /**
  27. * Get booking ID from ref_id UUID
  28. * @param refId The UUID string from URL
  29. * @return Original booking ID, or null if not found
  30. */
  31. public static Integer getBookingIdFromRefId(String refId) {
  32. return uuidToBookingIdMap.get(refId);
  33. }
  34. /**
  35. * Get ref_id UUID for existing booking ID (if already generated)
  36. * @param bookingId The booking ID
  37. * @return UUID string, or null if not found
  38. */
  39. public static String getRefIdForBooking(int bookingId) {
  40. return bookingIdToUuidMap.get(bookingId);
  41. }
  42. /**
  43. * Remove mapping when booking is cancelled or expired
  44. * @param bookingId The booking ID to remove
  45. */
  46. public static void removeBookingMapping(int bookingId) {
  47. String uuid = bookingIdToUuidMap.remove(bookingId);
  48. if (uuid != null) {
  49. uuidToBookingIdMap.remove(uuid);
  50. }
  51. }
  52. /**
  53. * Check if ref_id UUID is valid
  54. * @param refId The UUID string to validate
  55. * @return true if UUID exists in mapping
  56. */
  57. public static boolean isValidRefId(String refId) {
  58. return uuidToBookingIdMap.containsKey(refId);
  59. }
  60. /**
  61. * Get total number of active mappings (for monitoring)
  62. * @return Number of active UUID mappings
  63. */
  64. public static int getActiveMappingCount() {
  65. return uuidToBookingIdMap.size();
  66. }
  67. }