|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Expedia, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.expediagroup.sdk.xap.examples.scenarios.activity; |
| 16 | + |
| 17 | +import com.expediagroup.sdk.core.model.Response; |
| 18 | +import com.expediagroup.sdk.xap.client.XapClient; |
| 19 | +import com.expediagroup.sdk.xap.examples.scenarios.XapScenario; |
| 20 | +import com.expediagroup.sdk.xap.models.ActivitiesAddress; |
| 21 | +import com.expediagroup.sdk.xap.models.ActivitiesCancellationPolicy; |
| 22 | +import com.expediagroup.sdk.xap.models.ActivitiesGeoLocation; |
| 23 | +import com.expediagroup.sdk.xap.models.ActivitiesLocation; |
| 24 | +import com.expediagroup.sdk.xap.models.Activity; |
| 25 | +import com.expediagroup.sdk.xap.models.ActivityDetailsResponse; |
| 26 | +import com.expediagroup.sdk.xap.models.ActivityListingsResponse; |
| 27 | +import com.expediagroup.sdk.xap.operations.GetActivityDetailsOperation; |
| 28 | +import com.expediagroup.sdk.xap.operations.GetActivityDetailsOperationParams; |
| 29 | +import com.expediagroup.sdk.xap.operations.GetActivityListingsOperation; |
| 30 | +import com.expediagroup.sdk.xap.operations.GetActivityListingsOperationParams; |
| 31 | +import java.time.LocalDate; |
| 32 | +import java.util.Arrays; |
| 33 | +import org.apache.commons.lang3.StringUtils; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +/** |
| 38 | + * This example demonstrates how to get Activity details. |
| 39 | + */ |
| 40 | +public class ActivityDetailsQuickStartScenario implements XapScenario { |
| 41 | + |
| 42 | + private static final Logger LOGGER = |
| 43 | + LoggerFactory.getLogger(ActivityDetailsQuickStartScenario.class); |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + new ActivityDetailsQuickStartScenario().run(); |
| 47 | + System.exit(0); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void run() { |
| 52 | + // This example demonstrates how to obtain the Activity Details Deep Link from the Activity |
| 53 | + // Listings. |
| 54 | + // For information on using activity search, refer to Activity ListingsQuickStartExample. |
| 55 | + LOGGER.info( |
| 56 | + "========================== Running DetailsQuickStartScenario ========================="); |
| 57 | + |
| 58 | + // Execute the operation and get the ActivityListingsRespons |
| 59 | + XapClient xapClient = createClient(); |
| 60 | + |
| 61 | + GetActivityListingsOperationParams getActivityListingsOperationParams = |
| 62 | + GetActivityListingsOperationParams.builder() |
| 63 | + .partnerTransactionId(XapScenario.PARTNER_TRANSACTION_ID).location("seattle") |
| 64 | + .links(Arrays.asList(GetActivityListingsOperationParams.Links.AD)) |
| 65 | + .startDate(LocalDate.now().plusDays(5)).endDate(LocalDate.now().plusDays(8)) |
| 66 | + .locale("en_US").build(); |
| 67 | + |
| 68 | + Response<ActivityListingsResponse> listingsResponse = |
| 69 | + xapClient.execute(new GetActivityListingsOperation(getActivityListingsOperationParams)); |
| 70 | + |
| 71 | + if (listingsResponse == null || listingsResponse.getData() == null |
| 72 | + || listingsResponse.getData().getActivities() == null) { |
| 73 | + throw new IllegalStateException("No activity details found."); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + Activity listingsActivity = listingsResponse.getData().getActivities().get(0); |
| 78 | + |
| 79 | + // Get offerToken from APIDetails link url |
| 80 | + String adLink = listingsActivity.getLinks().get("ApiDetails").getHref(); |
| 81 | + String offerToken = adLink.substring(adLink.lastIndexOf("/") + 1); |
| 82 | + |
| 83 | + // Execute the operation and get the ActivityDetailsResponse |
| 84 | + GetActivityDetailsOperationParams getActivityDetailsOperationParams = |
| 85 | + GetActivityDetailsOperationParams.builder() |
| 86 | + .partnerTransactionId(XapScenario.PARTNER_TRANSACTION_ID).offerToken(offerToken) |
| 87 | + .build(); |
| 88 | + |
| 89 | + Response<ActivityDetailsResponse> detalisResponse = |
| 90 | + xapClient.execute(new GetActivityDetailsOperation(getActivityDetailsOperationParams)); |
| 91 | + |
| 92 | + |
| 93 | + LOGGER.info( |
| 94 | + "======================== GetActivityDetailsOperation Executed ========================"); |
| 95 | + |
| 96 | + if (detalisResponse == null || detalisResponse.getData() == null |
| 97 | + || detalisResponse.getData().getActivityDetails() == null |
| 98 | + || detalisResponse.getData().getActivityDetails() == null) { |
| 99 | + throw new IllegalStateException("No activity details found."); |
| 100 | + } |
| 101 | + |
| 102 | + // The ActivityListingsResponse contains a transaction ID for troubleshooting |
| 103 | + LOGGER.info("Transaction ID: {}", detalisResponse.getData().getTransactionId()); |
| 104 | + |
| 105 | + |
| 106 | + LOGGER.info( |
| 107 | + "============================ Activity details Start ============================"); |
| 108 | + Activity activity = detalisResponse.getData().getActivityDetails(); |
| 109 | + // To get the activity title |
| 110 | + if (StringUtils.isNotEmpty(activity.getTitle())) { |
| 111 | + LOGGER.info("Activity title: {}", activity.getTitle()); |
| 112 | + } |
| 113 | + // To get the activity description |
| 114 | + if (StringUtils.isNotEmpty(activity.getDescription())) { |
| 115 | + LOGGER.info("Activity description: {}", activity.getDescription()); |
| 116 | + } |
| 117 | + |
| 118 | + // To get the activity duration |
| 119 | + if (StringUtils.isNotEmpty(activity.getDuration())) { |
| 120 | + LOGGER.info("Activity duration: {}", activity.getDuration()); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + // To get the activity image |
| 125 | + if (activity.getMedia() != null) { |
| 126 | + LOGGER.info( |
| 127 | + "============================ Activity Images ============================ "); |
| 128 | + activity.getMedia().forEach(image -> { |
| 129 | + LOGGER.info("Image title: {}", image.getTitle()); |
| 130 | + LOGGER.info("Image url: {}", image.getUrl()); |
| 131 | + }); |
| 132 | + LOGGER.info( |
| 133 | + "============================ Activity Images ============================ "); |
| 134 | + } |
| 135 | + |
| 136 | + // To get the activity free cancellation available |
| 137 | + LOGGER.info("Activity free cancellation available: {}", activity.getFreeCancellation()); |
| 138 | + |
| 139 | + // To get the activity price |
| 140 | + if (activity.getPrice() != null) { |
| 141 | + LOGGER.info("Activity price currency: {}", activity.getPrice().getTotalRate().getCurrency()); |
| 142 | + LOGGER.info("Activity price value: {}", activity.getPrice().getTotalRate().getValue()); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + // To get the activity redemption |
| 147 | + if (activity.getRedemption() != null) { |
| 148 | + LOGGER.info( |
| 149 | + "============================ Activity Redemption ============================ "); |
| 150 | + if (StringUtils.isNotEmpty(activity.getDuration())) { |
| 151 | + LOGGER.info("Activity Redemption type: {}", activity.getRedemption().getType()); |
| 152 | + } |
| 153 | + |
| 154 | + if (activity.getRedemption().getRedemptionLocations() != null) { |
| 155 | + activity.getRedemption().getRedemptionLocations().forEach(location -> { |
| 156 | + printLocation(location); |
| 157 | + }); |
| 158 | + } |
| 159 | + LOGGER.info( |
| 160 | + "============================ Activity Redemption ============================ "); |
| 161 | + } |
| 162 | + |
| 163 | + // To get the activity location |
| 164 | + if (activity.getActivityLocations() != null) { |
| 165 | + activity.getActivityLocations().forEach(location -> { |
| 166 | + printLocation(location); |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + // To get the activity cancellation policy |
| 172 | + printCancellationPolicy(activity.getCancellationPolicy()); |
| 173 | + |
| 174 | + // To get activity highlight |
| 175 | + if (activity.getHighlights() != null) { |
| 176 | + LOGGER.info( |
| 177 | + "============================ Activity Highlight ============================ "); |
| 178 | + activity.getHighlights().forEach(highlight -> { |
| 179 | + LOGGER.info(highlight); |
| 180 | + }); |
| 181 | + LOGGER.info( |
| 182 | + "============================ Activity Highlight ============================ "); |
| 183 | + } |
| 184 | + |
| 185 | + // To get activity inclusions |
| 186 | + if (activity.getInclusions() != null) { |
| 187 | + LOGGER.info( |
| 188 | + "============================ Activity Inclusions ============================ "); |
| 189 | + activity.getInclusions().forEach(inclusion -> { |
| 190 | + LOGGER.info(inclusion); |
| 191 | + }); |
| 192 | + LOGGER.info( |
| 193 | + "============================ Activity Inclusions ============================ "); |
| 194 | + } |
| 195 | + |
| 196 | + // To get activity exclusion |
| 197 | + if (activity.getExclusions() != null) { |
| 198 | + LOGGER.info( |
| 199 | + "============================ Activity Exclusions ============================ "); |
| 200 | + activity.getExclusions().forEach(exclusion -> { |
| 201 | + LOGGER.info(exclusion); |
| 202 | + }); |
| 203 | + LOGGER.info( |
| 204 | + "============================ Activity Exclusions ============================ "); |
| 205 | + } |
| 206 | + |
| 207 | + // To get activity knowBeforeYouBook |
| 208 | + if (activity.getKnowBeforeYouBook() != null) { |
| 209 | + LOGGER.info( |
| 210 | + "============================ Activity KnowBeforeYouBook ============================ "); |
| 211 | + activity.getKnowBeforeYouBook().forEach(knowBeforeYouBook -> { |
| 212 | + LOGGER.info(knowBeforeYouBook); |
| 213 | + }); |
| 214 | + LOGGER.info( |
| 215 | + "============================ Activity KnowBeforeYouBook ============================ "); |
| 216 | + } |
| 217 | + |
| 218 | + // To get activity offers |
| 219 | + if (activity.getOffers() != null) { |
| 220 | + LOGGER.info( |
| 221 | + "============================ Activity Offers ============================ "); |
| 222 | + activity.getOffers().forEach(offer -> { |
| 223 | + // To get the Offer title |
| 224 | + if (StringUtils.isNotEmpty(offer.getTitle())) { |
| 225 | + LOGGER.info("Offer title: {}", offer.getTitle()); |
| 226 | + } |
| 227 | + // To get the Offer description |
| 228 | + if (StringUtils.isNotEmpty(offer.getDescription())) { |
| 229 | + LOGGER.info("Offer description: {}", offer.getDescription()); |
| 230 | + } |
| 231 | + |
| 232 | + // To get the Offer duration |
| 233 | + if (StringUtils.isNotEmpty(offer.getDuration())) { |
| 234 | + LOGGER.info("Offer duration: {}", offer.getDuration()); |
| 235 | + } |
| 236 | + |
| 237 | + // To get the offer price |
| 238 | + if (offer.getOfferPrice() != null) { |
| 239 | + LOGGER.info("Offer price currency: {}", |
| 240 | + offer.getOfferPrice().getTotalRate().getCurrency()); |
| 241 | + LOGGER.info("Offer price value: {}", offer.getOfferPrice().getTotalRate().getValue()); |
| 242 | + } |
| 243 | + |
| 244 | + // To get the Offer availableTimeSlots |
| 245 | + if (offer.getAvailableTimeSlots() != null) { |
| 246 | + LOGGER.info( |
| 247 | + "========================= Activity AvailableTimeSlots ========================="); |
| 248 | + |
| 249 | + offer.getAvailableTimeSlots().forEach(availableTimeSlot -> { |
| 250 | + LOGGER.info("DateTime: {}", availableTimeSlot.getDateTime()); |
| 251 | + LOGGER.info("AllDayActivity: {}", availableTimeSlot.getAllDayActivity()); |
| 252 | + printCancellationPolicy(availableTimeSlot.getCancellationPolicy()); |
| 253 | + |
| 254 | + // To get ticket info |
| 255 | + if (availableTimeSlot.getTickets() != null) { |
| 256 | + LOGGER.info( |
| 257 | + "============================ Activity ticket ============================= "); |
| 258 | + availableTimeSlot.getTickets().forEach(ticket -> { |
| 259 | + LOGGER.info("Code: {}", ticket.getCode()); |
| 260 | + LOGGER.info("Ticket price currency: {}", |
| 261 | + ticket.getTicketPrice().getTotalRate().getCurrency()); |
| 262 | + LOGGER.info("Ticket price value: {}", |
| 263 | + ticket.getTicketPrice().getTotalRate().getValue()); |
| 264 | + |
| 265 | + }); |
| 266 | + LOGGER.info( |
| 267 | + "============================ Activity ticket ============================ "); |
| 268 | + } |
| 269 | + |
| 270 | + }); |
| 271 | + |
| 272 | + LOGGER.info( |
| 273 | + "======================== Activity AvailableTimeSlots ==========================="); |
| 274 | + } |
| 275 | + }); |
| 276 | + LOGGER.info( |
| 277 | + "============================ Activity Offers ============================ "); |
| 278 | + } |
| 279 | + |
| 280 | + LOGGER.info( |
| 281 | + "============================= Activity End ============================="); |
| 282 | + |
| 283 | + LOGGER.info( |
| 284 | + "============================ End ListingsQuickStartScenario ==========================="); |
| 285 | + } |
| 286 | + |
| 287 | + private void printCancellationPolicy(ActivitiesCancellationPolicy cancellationPolicy) { |
| 288 | + if (cancellationPolicy != null) { |
| 289 | + LOGGER.info("Activity free cancellation available: {}", |
| 290 | + cancellationPolicy.getFreeCancellation()); |
| 291 | + |
| 292 | + LOGGER.info("FreeCancellationMinHours: {}", cancellationPolicy.getFreeCancellationMinHours()); |
| 293 | + LOGGER.info("FreeCancellationEndDateTime: {}", |
| 294 | + cancellationPolicy.getFreeCancellationEndDateTime()); |
| 295 | + LOGGER.info("CancelPolicyDescription: {}", cancellationPolicy.getCancelPolicyDescription()); |
| 296 | + |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + private void printLocation(ActivitiesLocation location) { |
| 301 | + if (location != null) { |
| 302 | + if (location.getAddress() != null) { |
| 303 | + ActivitiesAddress address = location.getAddress(); |
| 304 | + if (StringUtils.isNotEmpty(address.getCountry())) { |
| 305 | + LOGGER.info("Country: {}", address.getCountry()); |
| 306 | + } |
| 307 | + |
| 308 | + if (StringUtils.isNotEmpty(address.getProvince())) { |
| 309 | + LOGGER.info("Province: {}", address.getProvince()); |
| 310 | + } |
| 311 | + |
| 312 | + if (StringUtils.isNotEmpty(address.getCity())) { |
| 313 | + LOGGER.info("City: {}", address.getCity()); |
| 314 | + } |
| 315 | + |
| 316 | + if (StringUtils.isNotEmpty(address.getAddress1())) { |
| 317 | + LOGGER.info("Address1: {}", address.getAddress1()); |
| 318 | + } |
| 319 | + |
| 320 | + if (StringUtils.isNotEmpty(address.getAddress2())) { |
| 321 | + LOGGER.info("Address2: {}", address.getAddress2()); |
| 322 | + } |
| 323 | + |
| 324 | + } |
| 325 | + |
| 326 | + if (location.getGeoLocation() != null) { |
| 327 | + ActivitiesGeoLocation geoLocation = location.getGeoLocation(); |
| 328 | + if (StringUtils.isNotEmpty(geoLocation.getLatitude())) { |
| 329 | + LOGGER.info("Latitude: {}", geoLocation.getLatitude()); |
| 330 | + } |
| 331 | + |
| 332 | + if (StringUtils.isNotEmpty(geoLocation.getLongitude())) { |
| 333 | + LOGGER.info("Longitude: {}", geoLocation.getLongitude()); |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + } |
| 338 | + |
| 339 | + } |
| 340 | +} |
0 commit comments