Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/google/sps/Trip.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private static LocalDate getLocalDate(String inDate) {
* @param startDate The LocalDate representation of start date.
* @param endDate The LocalDate representation of end date.
*/
private static int calcNumDays(LocalDate startDate, LocalDate endDate) {
public static int calcNumDays(LocalDate startDate, LocalDate endDate) {
int numDays = ((int) ChronoUnit.DAYS.between(startDate, endDate)) + 1;
return numDays;
}
Expand Down
60 changes: 52 additions & 8 deletions src/main/java/com/google/sps/servlets/TripServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,70 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
Entity tripEntity = storeTripEntity(response, this.tripName, this.destinationName,
this.tripDayOfTravel, this.tripEndDate, this.photoSrc, datastore);

//do post for maps
// Calculate optimal ordering for all POIs.
DirectionsApiRequest dirRequest = generateDirectionsRequest(this.tripDestination, this.tripDestination, poiStrings, this.context);
DirectionsResult dirResult = getDirectionsResult(dirRequest);
List<Integer> travelTimes = getTravelTimes(dirResult);
List<String> orderedLocationStrings = getOrderedWaypoints(dirResult, poiStrings);

// put TripDay entity into datastore
Entity tripDayEntity = putTripDayInDatastore(this.tripDestination, datastore, LocalDate.parse(tripDayOfTravel), tripEntity.getKey());
// Calculate numDays in trip.
int numDays = Trip.calcNumDays(LocalDate.parse(this.tripDayOfTravel), LocalDate.parse(this.tripEndDate));

List<Entity> locationEntities = TripDay.locationsToEntities(orderedLocationStrings, tripDayEntity.getKey());
TripDay.storeLocationsInDatastore(locationEntities, this.datastore);
// put Event entities in datastore
putEventsInDatastore(tripDayEntity, LocalDate.parse(tripDayOfTravel), datastore, orderedLocationStrings, travelTimes);
// Split ordered POIs across multiple days.
String[][] poiSplit = multiDaySplitPois(orderedLocationStrings, numDays);

// Store information for each of these TripDays in Datastore.
multiDayStorage(poiSplit, this.tripDestination, this.datastore, this.tripDayOfTravel, tripEntity.getKey(), this.context);

// Redirect to the Maps page of this trip to show the trip that was added.
String tripKeyString = KeyFactory.keyToString(tripEntity.getKey());
response.sendRedirect("/maps.html?tripKey=" + tripKeyString);
}

public static String[][] multiDaySplitPois(List<String> orderedLocationStrings, int numDays) {
int numPois = orderedLocationStrings.size();
int poisPerDay = numPois / numDays;
int startIndex = 0;
int dayIndex = 0;
String[] orderedLocationArray = new String[numPois];
orderedLocationArray = orderedLocationStrings.toArray(orderedLocationArray);
String[][] poiSplit = new String[numDays][];
while (dayIndex < numDays - 1) {
poiSplit[dayIndex] = new String[poisPerDay];
poiSplit[dayIndex] = Arrays.copyOfRange(orderedLocationArray, startIndex, startIndex + poisPerDay);
startIndex += poisPerDay;
dayIndex++;
}
poiSplit[dayIndex] = Arrays.copyOfRange(orderedLocationArray, startIndex, numPois);
return poiSplit;
}

public void multiDayStorage(String[][] poiSplit, String tripDestination, DatastoreService datastore,
String tripDayOfTravel, Key tripEntityKey, GeoApiContext context) throws IOException {

LocalDate currentDate = LocalDate.parse(tripDayOfTravel);
for (String[] dailyLocations : poiSplit) {

List<String> dailyLocationsList = Arrays.asList(dailyLocations);

DirectionsApiRequest dirRequest = generateDirectionsRequest(tripDestination, tripDestination, dailyLocations, context);
DirectionsResult dirResult = getDirectionsResult(dirRequest);
List<Integer> travelTimes = getTravelTimes(dirResult);

// put TripDay entity into datastore
Entity tripDayEntity = putTripDayInDatastore(tripDestination, datastore, currentDate, tripEntityKey);

List<Entity> locationEntities = TripDay.locationsToEntities(dailyLocationsList, tripDayEntity.getKey());
TripDay.storeLocationsInDatastore(locationEntities, datastore);

// put Event entities in datastore
putEventsInDatastore(tripDayEntity, currentDate, datastore, dailyLocationsList, travelTimes);

// increment date by 1
currentDate = currentDate.plusDays(1);
}
}

/**
* Get the place ID of the text search. Return null if no place ID matches
* the search.
Expand Down