Advertisement
kitlolz012

nawala ung error sa edithotel

Nov 23rd, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. private void update(String hotelName, String hotelAddress, String hotelDescription, List<Uri> imageUris, String hotelContact,
  2. String hotelEmail, List<String> roomTypes, String cancellationPolicy, String hotelId, String amenitiesHotel,
  3. String gcashNumber, String gcashName, String reservationFee, double latitude, double longitude) {
  4.  
  5. DatabaseReference hotelsRef = databaseReference.child("hotels");
  6. Query query = hotelsRef.orderByChild("user_id").equalTo(userId);
  7. query.addListenerForSingleValueEvent(new ValueEventListener() {
  8. @Override
  9. public void onDataChange(@NonNull com.google.firebase.database.DataSnapshot dataSnapshot) {
  10. if (dataSnapshot.exists()) {
  11. for (com.google.firebase.database.DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
  12. String hotelId = childSnapshot.getKey();
  13.  
  14. DatabaseReference hotelRef = hotelsRef.child(hotelId);
  15. hotelRef.child("name").setValue(hotelName);
  16. hotelRef.child("address").setValue(hotelAddress);
  17. hotelRef.child("description").setValue(hotelDescription);
  18. hotelRef.child("contact").setValue(hotelContact);
  19. hotelRef.child("email").setValue(hotelEmail);
  20. hotelRef.child("cancellationPolicy").setValue(cancellationPolicy);
  21. hotelRef.child("amenities").setValue(amenitiesHotel);
  22. hotelRef.child("gcashNumber").setValue(gcashNumber);
  23. hotelRef.child("gcashName").setValue(gcashName);
  24. hotelRef.child("reservationFee").setValue(reservationFee);
  25.  
  26. hotelRef.child("latitude").setValue(latitude);
  27. hotelRef.child("longitude").setValue(longitude);
  28.  
  29. saveRoomTypes(hotelId, roomTypes);
  30.  
  31. if (!imageUris.isEmpty()) {
  32. List<String> imageUrls = new ArrayList<>();
  33. AtomicInteger uploadCount = new AtomicInteger(0);
  34.  
  35. // Use a CountDownLatch to wait for all uploads to complete
  36. CountDownLatch latch = new CountDownLatch(imageUris.size());
  37.  
  38. for (int i = 0; i < imageUris.size(); i++) {
  39. Uri imageUri = imageUris.get(i);
  40.  
  41. if (imageUri != null) {
  42. // Check if the file exists before attempting to upload
  43. if (fileExists(imageUri)) {
  44. StorageReference imageRef = storageReference.child("hotel_images/" + hotelId + "/image" + (i + 1));
  45.  
  46. UploadTask uploadTask = imageRef.putFile(imageUri);
  47.  
  48. uploadTask.addOnSuccessListener(taskSnapshot -> {
  49. imageRef.getDownloadUrl().addOnSuccessListener(downloadUri -> {
  50. String imageUrl = downloadUri.toString();
  51. imageUrls.add(imageUrl);
  52.  
  53. if (uploadCount.incrementAndGet() == imageUris.size()) {
  54. // All images uploaded, update the database
  55. save(hotelId, imageUrls, hotelName, hotelAddress, hotelDescription, hotelContact,
  56. hotelEmail, roomTypes, cancellationPolicy, amenitiesHotel, gcashNumber, gcashName, reservationFee, latitude, longitude);
  57.  
  58. // Release the latch when all uploads are complete
  59. latch.countDown();
  60. }
  61. }).addOnFailureListener(e -> {
  62. Log.e("image upload", "Firebase Storage Error: " + e.getMessage());
  63. latch.countDown(); // Release the latch even if there's an error
  64. });
  65. }).addOnFailureListener(e -> {
  66. Log.e("image upload", "Error during upload: " + e.getMessage());
  67. latch.countDown(); // Release the latch even if there's an error
  68. });
  69. } else {
  70. Log.e(TAG, "Image file does not exist at Uri: " + imageUri.toString());
  71. latch.countDown(); // Release the latch in case of file not found
  72. }
  73. } else {
  74. Log.e(TAG, "ImageUri is null. Handle this case accordingly.");
  75. latch.countDown(); // Release the latch in case of null Uri
  76. }
  77. }
  78.  
  79. try {
  80. // Wait for all uploads to complete before proceeding
  81. latch.await();
  82. } catch (InterruptedException e) {
  83. e.printStackTrace();
  84. }
  85. } else {
  86. // If no images, update the database directly
  87. save(hotelId, null, hotelName, hotelAddress, hotelDescription, hotelContact,
  88. hotelEmail, roomTypes, cancellationPolicy, amenitiesHotel, gcashNumber, gcashName, reservationFee, latitude, longitude);
  89. }
  90. }
  91. }
  92. }
  93.  
  94. @Override
  95. public void onCancelled(@NonNull DatabaseError databaseError) {
  96. Log.e("update", "error" + databaseError.getMessage());
  97. }
  98. });
  99. }
  100.  
  101.  
  102. // Function to check if a file exists at the given Uri
  103. private boolean fileExists(Uri uri) {
  104. if (uri != null) {
  105. File file = new File(uri.getPath());
  106. return file.exists();
  107. }
  108. return false;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement