55import com .amadeus .exceptions .ResponseException ;
66import com .amadeus .resources .FlightOfferSearch ;
77import com .amadeus .resources .FlightOrder ;
8+ import com .amadeus .resources .FlightPrice ;
89import com .amadeus .resources .Resource ;
910import com .amadeus .resources .Traveler ;
11+ import com .google .gson .Gson ;
12+ import com .google .gson .GsonBuilder ;
1013import com .google .gson .JsonArray ;
14+ import com .google .gson .JsonElement ;
1115import com .google .gson .JsonObject ;
1216
17+
1318/**
1419 * <p>
1520 * A namespaced client for the
@@ -36,6 +41,41 @@ public FlightOrders(Amadeus client) {
3641 this .client = client ;
3742 }
3843
44+ /**
45+ * Build the JSON from the Travelers array.
46+ *
47+ * @param travelers array of Traveler
48+ * @return
49+ */
50+ private JsonArray buildTravelersJSON (Traveler [] travelers ) {
51+ Gson gson = new GsonBuilder ().create ();
52+ JsonArray travelerArray = new JsonArray ();
53+
54+ for (int i = 0 ; i < travelers .length ; i ++) {
55+ JsonElement traveler = gson .toJsonTree (travelers [i ], Traveler .class );
56+ travelerArray .add (traveler );
57+ }
58+ return travelerArray ;
59+ }
60+
61+ /**
62+ * Build the JSON from the Travelers array.
63+ *
64+ * @param flightOffers Array of FlightOfferSearch
65+ * @return JsonArray of the flightOffers
66+ */
67+ private JsonArray buildFlightOffersJSON (FlightOfferSearch [] flightOffers ) {
68+ Gson gson = new GsonBuilder ().create ();
69+ JsonArray flightOffersArray = new JsonArray ();
70+
71+ for (int i = 0 ; i < flightOffers .length ; i ++) {
72+ JsonElement flightOffer = gson .toJsonTree (flightOffers [i ], FlightOfferSearch .class );
73+ flightOffersArray .add (flightOffer );
74+ }
75+ return flightOffersArray ;
76+ }
77+
78+
3979 /**
4080 * <p>
4181 * The Flight Create Orders API allows you to perform flight booking.
@@ -79,58 +119,82 @@ public FlightOrder post(String body) throws ResponseException {
79119 * amadeus.booking.flightOrders.post(object);</pre>
80120 *
81121 * @param flightOffersSearches List of flight-offers as FlightOfferSearch[]
82- * @param travelers List of travelers as Traveler[]
122+ * @param travelers List of travelers as Traveler[]
83123 * @return an API resource
84124 * @throws ResponseException when an exception occurs
85125 */
86- public FlightOrder post (FlightOfferSearch [] flightOffersSearches ,
87- Traveler [] travelers ) throws ResponseException {
88-
89- JsonObject nameObject = new JsonObject ();
90- nameObject .addProperty ("firstName" , travelers [0 ].getName ().getFirstName ());
91- nameObject .addProperty ("lastName" , travelers [0 ].getName ().getLastName ());
92-
93- JsonObject phoneObject = new JsonObject ();
94- phoneObject .addProperty ("countryCallingCode" ,
95- travelers [0 ].getContact ().getPhones ()[0 ].getCountryCallingCode ());
96- phoneObject .addProperty ("number" , travelers [0 ].getContact ().getPhones ()[0 ].getNumber ());
97- phoneObject .addProperty ("deviceType" , travelers [0 ].getContact ().getPhones ()[0 ].getDeviceType ());
98-
99-
100- JsonArray phonesArray = new JsonArray ();
101- phonesArray .add (phoneObject );
102-
103- JsonObject contactObject = new JsonObject ();
104- contactObject .add ("phones" , phonesArray );
105-
106- JsonObject documentsOject = new JsonObject ();
107- documentsOject .addProperty ("documentType" , travelers [0 ].getDocuments ()[0 ].getDocumentType ());
108- documentsOject .addProperty ("number" , travelers [0 ].getDocuments ()[0 ].getNumber ());
109- documentsOject .addProperty ("expiryDate" , travelers [0 ].getDocuments ()[0 ].getExpiryDate ());
110- documentsOject .addProperty ("nationality" , travelers [0 ].getDocuments ()[0 ].getNationality ());
111- documentsOject .addProperty ("issuanceCountry" ,
112- travelers [0 ].getDocuments ()[0 ].getIssuanceCountry ());
113- documentsOject .addProperty ("holder" , travelers [0 ].getDocuments ()[0 ].isHolder ());
114- JsonArray documentsArray = new JsonArray ();
115- documentsArray .add (documentsOject );
116-
117- JsonObject travelerObject = new JsonObject ();
118- travelerObject .addProperty ("id" , travelers [0 ].getId ());
119- travelerObject .addProperty ("dateOfBirth" , travelers [0 ].getDateOfBirth ());
120- travelerObject .add ("name" , nameObject );
121- travelerObject .add ("contact" , contactObject );
122- travelerObject .add ("documents" , documentsArray );
123- JsonArray travelerArray = new JsonArray ();
124- travelerArray .add (travelerObject );
125-
126+ public FlightOrder post (FlightOfferSearch [] flightOffersSearches ,
127+ Traveler [] travelers ) throws ResponseException {
128+
129+ JsonObject typeObject = new JsonObject ();
130+ typeObject .addProperty ("type" , "flight-order" );
131+
132+ // Prepare the Flight Offers JSON
133+ JsonArray flightOffersArray = buildFlightOffersJSON (flightOffersSearches );
134+ typeObject .add ("flightOffers" , flightOffersArray );
135+
136+ // Prepare the TravelerJSON
137+ JsonArray travelerArray = buildTravelersJSON (travelers );
138+ typeObject .add ("travelers" , travelerArray );
139+
140+ JsonObject jsonObject = new JsonObject ();
141+ jsonObject .add ("data" , typeObject );
142+
143+ Response response = client .post ("/v1/booking/flight-orders" , jsonObject );
144+ return (FlightOrder ) Resource .fromObject (response , FlightOrder .class );
145+ }
146+
147+ /**
148+ * <p>
149+ * The Flight Create Orders API allows you to perform flight booking.
150+ * </p>
151+ *
152+ * <pre>
153+ * amadeus.booking.flightOrders.post(flightOfferSearch, traveler);</pre>
154+ *
155+ * @param flightOffersSearch a flight-offer as FlightOfferSearch
156+ * @param travelers List of travelers as Traveler[]
157+ * @return an API resource
158+ * @throws ResponseException when an exception occurs
159+ */
160+ public FlightOrder post (FlightOfferSearch flightOffersSearch ,
161+ Traveler [] travelers ) throws ResponseException {
162+ FlightOfferSearch [] flightOffersSearchArray = new FlightOfferSearch [1 ];
163+ flightOffersSearchArray [0 ] = flightOffersSearch ;
164+
165+ return post (flightOffersSearchArray , travelers );
166+ }
167+
168+ /**
169+ * <p>
170+ * The Flight Create Orders API allows you to perform flight booking.
171+ * </p>
172+ *
173+ * <pre>
174+ * amadeus.booking.flightOrders.post(flightOfferSearch, traveler);</pre>
175+ *
176+ * @param flightPrice a flight-offers-pricing as FlightPrice
177+ * @param travelers List of travelers as Traveler[]
178+ * @return an API resource
179+ * @throws ResponseException when an exception occurs
180+ */
181+ public FlightOrder post (FlightPrice flightPrice ,
182+ Traveler [] travelers ) throws ResponseException {
183+
126184 JsonObject typeObject = new JsonObject ();
127185 typeObject .addProperty ("type" , "flight-order" );
128- typeObject .add ("flightOffers" , flightOffersSearches [0 ].getResponse ().getData ());
186+
187+ Gson gson = new GsonBuilder ().create ();
188+
189+ JsonArray flightOffersArray = buildFlightOffersJSON (flightPrice .getFlightOffers ());
190+ typeObject .add ("flightOffers" , flightOffersArray );
191+
192+ // Build Traveler JSON
193+ JsonArray travelerArray = buildTravelersJSON (travelers );
129194 typeObject .add ("travelers" , travelerArray );
130195
131196 JsonObject jsonObject = new JsonObject ();
132197 jsonObject .add ("data" , typeObject );
133- System .out .println (jsonObject );
134198
135199 Response response = client .post ("/v1/booking/flight-orders" , jsonObject );
136200 return (FlightOrder ) Resource .fromObject (response , FlightOrder .class );
0 commit comments