org.iata.ndc.builder.AirShoppingRQBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ndc-client Show documentation
Show all versions of ndc-client Show documentation
NDC Client for calling services implmenting IATA NDC standard
package org.iata.ndc.builder;
import java.math.BigInteger;
import java.util.*;
import javax.xml.datatype.*;
import org.iata.ndc.ClientException;
import org.iata.ndc.schema.*;
import org.iata.ndc.schema.AirShopReqAttributeQueryTypeOriginDestination.CalendarDates;
import org.iata.ndc.schema.FarePreferencesType.Type;
import org.iata.ndc.schema.FlightDepartureType.AirportCode;
import org.iata.ndc.schema.MsgPartiesType.Sender;
import org.iata.ndc.schema.TravelerCoreType.PTC;
public class AirShoppingRQBuilder {
public enum Traveler {
/** Adult */
ADT,
/** Child */
CNN,
/** Child */
CHD,
/** Infant */
INF,
/** Government */
GVT,
/** State government */
GST
};
private AirShoppingRQ request;
private static final ObjectFactory factory = new ObjectFactory();
private Map anonymousTravelers;
private Sender sender;
private Set airlines;
private Set fares;
private Set cabins;
public AirShoppingRQBuilder() {
anonymousTravelers = new HashMap();
airlines = new LinkedHashSet();
fares = new LinkedHashSet();
cabins = new LinkedHashSet();
request = Initializer.getObject(AirShoppingRQ.class);
sender = null;
}
public AirShoppingRQBuilder addTravelAgencySender(String name, String iataNumber, String agencyId) {
sender = factory.createMsgPartiesTypeSender();
TravelAgencySenderType agency = factory.createTravelAgencySenderType();
agency.setName(name);
agency.setIATANumber(iataNumber);
AgencyIDType agencyIDType = factory.createAgencyIDType();
agencyIDType.setValue(agencyId);
agency.setAgencyID(agencyIDType);
sender.setTravelAgencySender(agency);
return this;
}
public AirShoppingRQBuilder addAnonymousTraveler(Traveler traveler) {
return addAnonymousTravelers(traveler, 1);
}
public AirShoppingRQBuilder addAnonymousTravelers(Traveler traveler, int count) {
if (!anonymousTravelers.containsKey(traveler)) {
anonymousTravelers.put(traveler, count);
return this;
}
Integer total = anonymousTravelers.get(traveler) + count;
anonymousTravelers.put(traveler, total);
return this;
}
public AirShoppingRQBuilder addOriginDestination(AirShopReqAttributeQueryTypeOriginDestination originDestination) {
request.getCoreQuery().getOriginDestinations().add(originDestination);
return this;
}
public AirShoppingRQBuilder addOriginDestination(String origin, String destination, Date date) {
return addOriginDestination(origin, destination, date, 0, 0);
}
public AirShoppingRQBuilder addOriginDestination(String origin, String destination, Date date, int daysBefore, int daysAfter) {
AirShopReqAttributeQueryTypeOriginDestination originDestination = Initializer.getObject(AirShopReqAttributeQueryTypeOriginDestination.class);
AirportCode airportCode = factory.createFlightDepartureTypeAirportCode();
originDestination.getDeparture().setAirportCode(airportCode);
originDestination.getDeparture().getAirportCode().setValue(origin);
originDestination.getArrival().getAirportCode().setValue(destination);
originDestination.getDeparture().setDate(getDate(date));
if( daysBefore != 0 || daysAfter != 0) {
CalendarDates dates = factory.createAirShopReqAttributeQueryTypeOriginDestinationCalendarDates();
dates.setDaysBefore(daysBefore);
dates.setDaysAfter(daysAfter);
originDestination.setCalendarDates(dates);
}
return addOriginDestination(originDestination);
}
public AirShoppingRQBuilder addAirlinePreference(String airlineId) {
airlines.add(airlineId);
return this;
}
public AirShoppingRQBuilder addFarePreference(String fareCode) {
fares.add(fareCode);
return this;
}
public AirShoppingRQBuilder addCabinPreference(String cabinCode) {
cabins.add(cabinCode);
return this;
}
public AirShoppingRQ build() {
setDefaults();
addRequestAttributes();
addDocumentNode();
addPartyNode();
addTravelers();
addAirlinePreferences();
addFarePreferences();
addCabinPreferences();
return request;
}
private void addCabinPreferences() {
if (cabins.size() == 0) {
return;
}
CabinPreferencesType cabinPreferencesType = factory.createCabinPreferencesType();
for (String code: cabins) {
CabinType cabin = factory.createCabinType();
cabin.setCode(code);
cabinPreferencesType.getCabinType().add(cabin);
}
org.iata.ndc.schema.AirShoppingRQ.Preference preferenceElement = factory.createAirShoppingRQPreference();
preferenceElement.setCabinPreferences(cabinPreferencesType);
request.getPreferences().add(preferenceElement);
}
private void addFarePreferences() {
if (fares.size() == 0) {
return;
}
FarePreferencesType farePreferences = factory.createFarePreferencesType();
for (String code : fares) {
Type type = factory.createFarePreferencesTypeType();
type.setCode(code);
farePreferences.getTypes().add(type);
}
org.iata.ndc.schema.AirShoppingRQ.Preference preferenceElement = factory.createAirShoppingRQPreference();
preferenceElement.setFarePreferences(farePreferences);
request.getPreferences().add(preferenceElement);
}
private void addAirlinePreferences() {
if (airlines.size() == 0) {
return;
}
AirlinePreferencesType airlinePreferences = factory.createAirlinePreferencesType();
for (String code : airlines) {
AirlinePreferencesType.Airline airline = factory.createAirlinePreferencesTypeAirline();
AirlineID airlineID = factory.createAirlineID();
airlineID.setValue(code);
airline.setAirlineID(airlineID);
airlinePreferences.getAirline().add(airline);
}
org.iata.ndc.schema.AirShoppingRQ.Preference preferenceElement = factory.createAirShoppingRQPreference();
preferenceElement.setAirlinePreferences(airlinePreferences);
request.getPreferences().add(preferenceElement);
}
private void setDefaults() {
if (anonymousTravelers.size() == 0) {
addAnonymousTraveler(Traveler.ADT);
}
}
private void addPartyNode() {
MsgPartiesType party = factory.createMsgPartiesType();
party.setSender(sender);
request.setParty(party);
}
private void addTravelers() {
for (Traveler t: anonymousTravelers.keySet()) {
org.iata.ndc.schema.TravelersTraveler traveler = factory.createTravelersTraveler();
traveler.setAnonymousTraveler(factory.createAnonymousTravelerType());
PTC ptc = factory.createTravelerCoreTypePTC();
ptc.setValue(t.name());
ptc.setQuantity(BigInteger.valueOf(anonymousTravelers.get(t)));
traveler.getAnonymousTraveler().setPTC(ptc);
request.getTravelers().add(traveler);
}
}
private void addDocumentNode() {
MsgDocumentType document = factory.createMsgDocumentType();
document.setName("NDC AirShoppingRQ Message");
document.setReferenceVersion("1.0");
request.setDocument(document);
}
private void addRequestAttributes() {
request.setVersion("1.1.5");
}
private static XMLGregorianCalendar getDate(Date date) {
XMLGregorianCalendar xmlgc = null;
DatatypeFactory df = null;
try {
df = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
throw new ClientException(e);
}
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
xmlgc = df.newXMLGregorianCalendar(gc);
xmlgc.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
xmlgc.setSecond(DatatypeConstants.FIELD_UNDEFINED);
xmlgc.setMinute(DatatypeConstants.FIELD_UNDEFINED);
xmlgc.setHour(DatatypeConstants.FIELD_UNDEFINED);
xmlgc.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
return xmlgc;
}
}