-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookingService.java
More file actions
200 lines (186 loc) · 9.12 KB
/
Copy pathBookingService.java
File metadata and controls
200 lines (186 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package Service;
import jakarta.servlet.ServletContext;
import model.Booking;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class BookingService {
private final String filePath;
private final String queueFilePath;
public BookingService(ServletContext context) {
System.out.println("**************** BookingService initialized");
String tempPath = context.getRealPath("/WEB-INF/bookingData/bookings.txt");
this.filePath = tempPath;
this.queueFilePath = context.getRealPath("/WEB-INF/bookingData/queue.txt");
System.out.println("BookingService filePath: " + tempPath);
System.out.println("BookingService queueFilePath: " + queueFilePath);
if (tempPath == null) {
File tempDir = new File(System.getProperty("java.io.tmpdir"), "bookingData");
tempDir.mkdirs();
tempPath = new File(tempDir, "bookings.txt").getAbsolutePath();
System.out.println("Falling back to temp path: " + tempPath);
}
File file = new File(this.filePath);
file.getParentFile().mkdirs();
if (!file.exists()) {
try {
file.createNewFile();
System.out.println("Created bookings.txt at: " + file.getAbsolutePath());
} catch (IOException e) {
System.err.println("Failed to create bookings.txt: " + e.getMessage());
throw new RuntimeException("Failed to create bookings.txt", e);
}
}
File queueFile = new File(this.queueFilePath);
if (!queueFile.exists()) {
try {
queueFile.createNewFile();
System.out.println("Created queue.txt at: " + queueFile.getAbsolutePath());
} catch (IOException e) {
System.err.println("Failed to create queue.txt: " + e.getMessage());
throw new RuntimeException("Failed to create queue.txt", e);
}
}
}
public String getFilePath() {
return filePath;
}
public String getQueueFilePath() {
return queueFilePath;
}
public boolean saveBooking(Booking booking) throws IOException {
List<Booking> bookings = readBookings();
int newId = bookings.isEmpty() ? 1 : bookings.get(bookings.size() - 1).getId() + 1;
booking.setId(newId);
System.out.println("Writing booking to file: " + filePath);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
String bookingLine = String.format(
"%d|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%d|%s|%s|%s|%s|%s",
booking.getId(),
booking.getName() != null ? booking.getName() : "",
booking.getPhone() != null ? booking.getPhone() : "",
booking.getEmail() != null ? booking.getEmail() : "",
booking.getAddress() != null ? booking.getAddress() : "",
booking.getCity() != null ? booking.getCity() : "",
booking.getState() != null ? booking.getState() : "",
booking.getPostal() != null ? booking.getPostal() : "",
booking.getSessionType() != null ? booking.getSessionType() : "",
booking.getSessionDate() != null ? booking.getSessionDate() : "",
booking.getStartTime() != null ? booking.getStartTime() : "",
booking.getEndTime() != null ? booking.getEndTime() : "",
booking.getNumPeople(),
booking.getAges() != null ? booking.getAges() : "",
booking.getNotes() != null ? booking.getNotes() : "",
booking.getExtras() != null ? booking.getExtras() : "",
booking.getStatus() != null ? booking.getStatus() : "Pending",
booking.getUsername() != null ? booking.getUsername() : ""
);
writer.write(bookingLine);
writer.newLine();
System.out.println("Successfully wrote booking: " + bookingLine);
return true;
} catch (IOException e) {
System.err.println("Failed to write booking: " + e.getMessage());
throw e;
}
}
public List<Booking> readBookings() throws IOException {
List<Booking> bookings = new ArrayList<>();
File file = new File(filePath);
if (!file.exists()) {
System.out.println("Bookings file does not exist: " + filePath);
return bookings;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty()) {
System.out.println("Skipping empty line in bookings file");
continue;
}
String[] parts = line.split("\\|");
if (parts.length == 16 || parts.length == 17 || parts.length == 18) {
try {
int id = Integer.parseInt(parts[0]);
int numPeople = Integer.parseInt(parts[12]);
String username = (parts.length == 18 && !parts[17].isEmpty()) ? parts[17] : null;
Booking booking = new Booking(
id,
parts[1].isEmpty() ? null : parts[1],
parts[2].isEmpty() ? null : parts[2],
parts[3].isEmpty() ? null : parts[3],
parts[4].isEmpty() ? null : parts[4],
parts[5].isEmpty() ? null : parts[5],
parts[6].isEmpty() ? null : parts[6],
parts[7].isEmpty() ? null : parts[7],
parts[8].isEmpty() ? null : parts[8],
parts[9].isEmpty() ? null : parts[9],
parts[10].isEmpty() ? null : parts[10],
parts[11].isEmpty() ? null : parts[11],
numPeople,
parts[13].isEmpty() ? null : parts[13],
parts[14].isEmpty() ? null : parts[14],
parts[15].isEmpty() ? null : parts[15],
username
);
booking.setStatus(parts.length >= 17 && !parts[16].isEmpty() ? parts[16] : "Pending");
bookings.add(booking);
System.out.println("Successfully read booking ID: " + id);
} catch (NumberFormatException e) {
System.err.println("Invalid booking data in file (skipping line): " + line + " - " + e.getMessage());
}
} else {
System.err.println("Malformed booking line (skipping): " + line);
}
}
}
return bookings;
}
public int[] readQueue() throws IOException {
List<Integer> tempList = new ArrayList<>();
File file = new File(queueFilePath);
if (!file.exists()) {
System.out.println("Queue file does not exist: " + queueFilePath);
return new int[0];
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty()) {
continue;
}
try {
int bookingId = Integer.parseInt(line.trim());
tempList.add(bookingId);
System.out.println("Read queue booking ID: " + bookingId);
} catch (NumberFormatException e) {
System.err.println("Invalid queue entry (skipping): " + line);
}
}
}
int[] queue = new int[tempList.size()];
for (int i = 0; i < tempList.size(); i++) {
queue[i] = tempList.get(i);
}
return queue;
}
public void writeQueue(int[] queue) throws IOException {
File file = new File(queueFilePath);
File backupFile = new File(queueFilePath + ".bak");
if (file.exists() && !file.renameTo(backupFile)) {
System.err.println("Failed to create backup file for queue.txt");
throw new IOException("Failed to create backup file");
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
for (int bookingId : queue) {
writer.write(String.valueOf(bookingId));
writer.newLine();
}
System.out.println("Successfully wrote queue to: " + queueFilePath);
backupFile.delete();
} catch (IOException e) {
backupFile.renameTo(file);
throw e;
}
}
}