Files
reservationsystem/src/java/lodge/reservationsystem/DataRepository.java

260 lines
12 KiB
Java
Raw Normal View History

2025-08-30 11:26:49 -04:00
package lodge.reservationsystem;
2025-09-03 08:56:57 -04:00
import java.io.BufferedReader;
import java.io.FileReader;
2025-08-30 11:26:49 -04:00
import java.io.IOException;
2025-09-03 08:56:57 -04:00
import java.nio.charset.StandardCharsets;
2025-08-30 11:26:49 -04:00
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
2025-09-06 19:21:01 -04:00
import java.nio.file.Paths;
2025-08-30 11:26:49 -04:00
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
2025-09-06 19:21:01 -04:00
import java.time.ZonedDateTime;
2025-09-03 08:56:57 -04:00
import com.google.gson.stream.JsonReader;
2025-08-30 11:26:49 -04:00
final class DataRepository {
// SINGLETON CLASS
// hard code data store location for storage of
// account data files on filesystem
private String directoryPath;
private static final DataRepository instance = new DataRepository();
2025-09-04 22:34:26 -04:00
protected final static DataRepository getInstance() {
2025-08-30 11:26:49 -04:00
return instance;
}
public final static void setDataStoreRoot(final String direcoryPath) {
getInstance().directoryPath = direcoryPath;
}
public final static String getPath() {
return getInstance().directoryPath;
}
2025-09-05 09:16:57 -04:00
public static void WalkFileSystemTree(final AccomodationManager manager, final Path rootDir) throws IOException {
2025-08-30 11:26:49 -04:00
Files.walkFileTree(rootDir, new SimpleFileVisitor<Path>() {
@Override
2025-09-05 09:16:57 -04:00
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) {
2025-08-30 11:26:49 -04:00
System.out.println("File: " + file.toAbsolutePath());
// load account number, and content
if (attrs.isRegularFile()) {
2025-09-05 09:16:57 -04:00
final String namestring = file.getName(file.getNameCount() - 1).toString();
2025-08-30 11:26:49 -04:00
if (namestring.endsWith("json")) {
if (namestring.startsWith("acc")) { // * load Account *//
try {
manager.load(file);
2025-09-05 09:16:57 -04:00
} catch (final Exception e) {
2025-08-30 11:26:49 -04:00
e.printStackTrace();
}
}
}
}
// load reservation
return FileVisitResult.CONTINUE;
}
@Override
2025-09-05 09:16:57 -04:00
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) {
2025-08-30 11:26:49 -04:00
System.out.println("Directory: " + dir.toAbsolutePath());
// prepare to load account number
return FileVisitResult.CONTINUE;
}
});
2025-09-03 08:56:57 -04:00
}
2025-09-10 15:44:29 -04:00
2025-09-05 09:16:57 -04:00
public final static Account LoadAccount(final Path file) throws IOException {
2025-09-04 22:34:26 -04:00
/** @TODO finish loading Account */
2025-09-05 09:16:57 -04:00
try (BufferedReader in = new BufferedReader(new FileReader(file.toFile(), StandardCharsets.UTF_8));
JsonReader jsonReader = new JsonReader(in)) {
2025-09-03 08:56:57 -04:00
jsonReader.beginObject();
2025-09-05 09:16:57 -04:00
Account ac = new Account();
2025-09-03 08:56:57 -04:00
while (jsonReader.hasNext()) {
2025-09-05 09:16:57 -04:00
final String name = jsonReader.nextName();
2025-09-03 08:56:57 -04:00
switch (name) {
case "Account":
jsonReader.beginObject();
break;
case "account_number":
ac.setAccount_number(jsonReader.nextString());
break;
case "phone_number":
ac.setPhone_number(jsonReader.nextString());
break;
case "mailing_address":
jsonReader.beginObject();
break;
case "Address":
jsonReader.beginObject();
2025-09-05 09:16:57 -04:00
Address ad = new Address();
2025-09-03 10:12:45 -04:00
jsonReader.nextName();
2025-09-03 08:56:57 -04:00
ad.setStreet(jsonReader.nextString());
2025-09-03 10:12:45 -04:00
jsonReader.nextName();
2025-09-03 08:56:57 -04:00
ad.setCity(jsonReader.nextString());
2025-09-03 10:12:45 -04:00
jsonReader.nextName();
2025-09-03 08:56:57 -04:00
ad.setState(jsonReader.nextString());
2025-09-03 10:12:45 -04:00
jsonReader.nextName();
2025-09-03 08:56:57 -04:00
ad.setZip(jsonReader.nextString());
jsonReader.endObject();
jsonReader.endObject();
ac.setMailing_address(ad);
break;
case "email_address":
jsonReader.beginObject();
break;
case "EmailAddress":
jsonReader.beginObject();
break;
case "email":
2025-09-05 09:16:57 -04:00
String s = jsonReader.nextString();
s = s != null ? s : "";
ac.setEmail_address(new EmailAddress(s));
2025-09-03 08:56:57 -04:00
jsonReader.endObject();
jsonReader.endObject();
break;
case "reservation_list":
2025-09-06 19:21:01 -04:00
loadReservationRefList(jsonReader, ac);
2025-09-03 08:56:57 -04:00
break;
default:
System.out.println(name);
}
}
jsonReader.close();
2025-09-10 16:08:11 -04:00
return ac.getAccount_number().length() > 8 ? ac : null;
2025-09-03 08:56:57 -04:00
}
2025-09-04 22:34:26 -04:00
}
2025-09-06 19:21:01 -04:00
static void loadReservationRefList(JsonReader rdr, Account ac) throws IOException {
2025-09-04 22:34:26 -04:00
rdr.beginArray();
while (rdr.hasNext()) {
rdr.beginObject();
2025-09-06 19:21:01 -04:00
String reservationType = rdr.nextName();
2025-09-04 22:34:26 -04:00
rdr.beginObject();
2025-09-06 19:21:01 -04:00
rdr.nextName();
String reservationNumber = rdr.nextString();
2025-09-10 16:08:11 -04:00
loadReservation(ac, reservationType, reservationNumber);
2025-09-04 22:34:26 -04:00
rdr.endObject();
rdr.endObject();
}
rdr.endArray();
}
2025-09-06 19:21:01 -04:00
2025-09-10 16:08:11 -04:00
private static void loadReservation(Account ac, String reservationType,
2025-09-06 19:21:01 -04:00
String reservationNumber) throws IOException {
String filename = String.format("rsv-%s.json", reservationNumber);
Path basePath = Paths.get(getPath());
Path resolvedPath = basePath.resolve(filename);
try (BufferedReader in = new BufferedReader(new FileReader(resolvedPath.toFile(), StandardCharsets.UTF_8))) {
try (JsonReader jsonReader = new JsonReader(in)) {
jsonReader.beginObject();
Reservation rsrv = null;
2025-09-10 15:44:29 -04:00
try {
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
switch (name) {
case "HotelReservation":
jsonReader.beginObject();
rsrv = new HotelReservation();
break;
case "HouseReservation":
jsonReader.beginObject();
rsrv = new HouseReservation();
break;
case "CabinReservation":
jsonReader.beginObject();
rsrv = new CabinReservation();
break;
case "physical_address":
jsonReader.beginObject();
jsonReader.nextName();
jsonReader.beginObject();
jsonReader.nextName();
Address adP = new Address();
adP.setStreet(jsonReader.nextString());
jsonReader.nextName();
adP.setCity(jsonReader.nextString());
jsonReader.nextName();
adP.setState(jsonReader.nextString());
jsonReader.nextName();
adP.setZip(jsonReader.nextString());
jsonReader.endObject();
jsonReader.endObject();
rsrv.setPhysical_address(adP);
break;
case "mailing_address":
jsonReader.beginObject();
jsonReader.nextName();
jsonReader.beginObject();
jsonReader.nextName();
Address adM = new Address();
adM.setStreet(jsonReader.nextString());
jsonReader.nextName();
adM.setCity(jsonReader.nextString());
jsonReader.nextName();
adM.setState(jsonReader.nextString());
jsonReader.nextName();
adM.setZip(jsonReader.nextString());
jsonReader.endObject();
jsonReader.endObject();
rsrv.setMailing_address(adM);
break;
case "reservation_type":
jsonReader.nextString();
break;
case "reservation_number":
rsrv.setReservation_number(jsonReader.nextString());
break;
case "reservation_status":
rsrv.setReservation_status(ReservationStatusEnum.valueOf(jsonReader.nextString()));
break;
case "kitchen":
rsrv.setKitchen(KitchenEnum.valueOf(jsonReader.nextString()));
break;
case "numberOfBeds":
rsrv.setNumberOfBeds(Integer.valueOf(jsonReader.nextString()));
break;
case "numberOfBedRooms":
rsrv.setNumberOfBedRooms(Integer.valueOf(jsonReader.nextString()));
break;
case "numberOfBathRooms":
rsrv.setNumberOfBathRooms(Integer.valueOf(jsonReader.nextString()));
break;
case "numberOfFloors":
rsrv.setNumberOfFloors(Integer.valueOf(jsonReader.nextString()));
break;
case "squareFeet":
rsrv.setSquareFeet(Integer.valueOf(jsonReader.nextString()));
break;
case "price":
rsrv.setPrice(Float.valueOf(jsonReader.nextString()));
break;
case "reservation_start_date":
rsrv.setReservation_start_date(ZonedDateTime.parse(jsonReader.nextString()));
break;
case "reservation_end_date":
rsrv.setReservation_end_date(ZonedDateTime.parse(jsonReader.nextString()));
break;
2025-09-13 20:33:29 -04:00
case "account_number":
rsrv.setAccountNumber(jsonReader.nextString());
break;
2025-09-06 19:21:01 -04:00
2025-09-10 15:44:29 -04:00
default:
System.out.println(name);
}
2025-09-06 19:21:01 -04:00
}
2025-09-10 16:08:11 -04:00
ac.add(rsrv);
2025-09-06 19:21:01 -04:00
} catch (Exception e) {
2025-09-10 15:44:29 -04:00
System.out.println(e.toString());
2025-09-06 19:21:01 -04:00
}
}
}
}
2025-08-30 11:26:49 -04:00
}