1313import com .google .gson .JsonParser ;
1414import java .util .ArrayList ;
1515
16- /** Class to handles CRUD related to the Trip */
16+ /** Class to handle CRU functions related to the Trip */
1717public class TripCrud {
1818 private static final DatastoreService datastore = DatastoreServiceFactory .getDatastoreService ();
1919
@@ -25,10 +25,23 @@ public class TripCrud {
2525 * @return tripEntity a trip entity
2626 */
2727 public static Entity createTrip (String email , String tripData ) {
28- Entity tripEntity = toEntity (tripData , "" , null );
28+ Entity tripEntity = toEntity (tripData , null , null );
29+ datastore .put (tripEntity );
30+ Long id = tripEntity .getKey ().getId ();
31+ UserCrud .addTripId (email , id );
32+ TripCrud .addTripId (id );
33+ return TripCrud .readTrip (id );
34+ }
35+
36+ /**
37+ * Add tripId property to Trip Entity
38+ *
39+ * @param id the trip id
40+ */
41+ private static void addTripId (Long id ) {
42+ Entity tripEntity = TripCrud .readTrip (id );
43+ tripEntity .setProperty ("tripId" , id );
2944 datastore .put (tripEntity );
30- UserCrud .addTripId (email , tripEntity .getKey ().getId ());
31- return tripEntity ;
3245 }
3346
3447 /**
@@ -39,8 +52,8 @@ public static Entity createTrip(String email, String tripData) {
3952 * @param tripKey trip key, can be null
4053 * @return tripEntity from tripData
4154 */
42- public static Entity toEntity (String tripData , String tripId , Key tripKey ) {
43- Entity tripEntity = tripId == "" ? new Entity ("Trip" ) : new Entity ("Trip" , tripId , tripKey );
55+ public static Entity toEntity (String tripData , Long tripId , Key tripKey ) {
56+ Entity tripEntity = tripId == null ? new Entity ("Trip" ) : new Entity ("Trip" , tripId , tripKey );
4457 setProperties (tripEntity , tripData );
4558 return tripEntity ;
4659 }
@@ -55,22 +68,24 @@ private static void setProperties(Entity tripEntity, String tripData) {
5568 JsonParser parser = new JsonParser ();
5669 JsonElement jsonElement = parser .parse (tripData );
5770 JsonObject jsonObject = jsonElement .getAsJsonObject ();
58-
5971 tripEntity .setProperty ("isOptimized" , jsonObject .get ("isOptimized" ).getAsBoolean ());
6072 tripEntity .setProperty ("searchText" , jsonObject .get ("searchText" ).getAsString ());
6173 tripEntity .setProperty ("tripName" , jsonObject .get ("tripName" ).getAsString ());
62- ArrayList <EmbeddedEntity > attractions = new ArrayList <EmbeddedEntity >();
6374
75+ Integer centerLat = jsonObject .get ("centerLat" ).getAsInt ();
76+ Integer centerLng = jsonObject .get ("centerLng" ).getAsInt ();
77+ tripEntity .setProperty ("centerLng" , centerLng );
78+ tripEntity .setProperty ("centerLat" , centerLat );
79+
80+ ArrayList <EmbeddedEntity > attractions = new ArrayList <EmbeddedEntity >();
6481 for (JsonElement attractionElement : jsonObject .getAsJsonArray ("attractions" )) {
65- JsonObject attraction =
66- attractionElement .getAsJsonObject (); // needed? might only need JsonElement
82+ JsonObject attraction = attractionElement .getAsJsonObject ();
6783 EmbeddedEntity embeddedAttraction = new EmbeddedEntity ();
68- embeddedAttraction .setProperty (
69- "attractionName" , attraction .get ("attractionName" ).getAsString ());
70- embeddedAttraction .setProperty (
71- "photoReference" , attraction .get ("photoReference" ).getAsString ());
84+ embeddedAttraction .setProperty ("name" , attraction .get ("name" ).getAsString ());
85+ embeddedAttraction .setProperty ("photoUrl" , attraction .get ("photoUrl" ).getAsString ());
7286 embeddedAttraction .setProperty ("routeIndex" , attraction .get ("routeIndex" ).getAsInt ());
73- embeddedAttraction .setProperty ("coordinates" , attraction .get ("coordinates" ).getAsString ());
87+ embeddedAttraction .setProperty ("lat" , attraction .get ("lat" ).getAsString ());
88+ embeddedAttraction .setProperty ("lng" , attraction .get ("lng" ).getAsString ());
7489 attractions .add (embeddedAttraction );
7590 }
7691 tripEntity .setProperty ("attractions" , attractions );
@@ -80,12 +95,17 @@ private static void setProperties(Entity tripEntity, String tripData) {
8095 * Finds a trip entity by id
8196 *
8297 * @param tripId id of the trip to find
83- * @throws EntityNotFoundException if trip entity is not found
84- * @return Trip entity
98+ * @return Trip entity if found, null if EntityNotFoundException is caught
8599 */
86- public static Entity readTrip (String tripId ) throws EntityNotFoundException {
87- Key entityKey = KeyFactory .createKey ("Trip" , Long .parseLong (tripId ));
88- return datastore .get (entityKey );
100+ public static Entity readTrip (Long tripId ) {
101+ Key entityKey = KeyFactory .createKey ("Trip" , tripId );
102+ Entity tripEntity ;
103+ try {
104+ tripEntity = datastore .get (entityKey );
105+ } catch (EntityNotFoundException e ) {
106+ return null ;
107+ }
108+ return tripEntity ;
89109 }
90110
91111 /**
@@ -101,16 +121,17 @@ public static JsonObject toJson(Entity tripEntity) {
101121 jsonTrip .addProperty ("searchText" , (String ) tripEntity .getProperty ("searchText" ));
102122 jsonTrip .addProperty ("tripName" , (String ) tripEntity .getProperty ("tripName" ));
103123
124+ jsonTrip .addProperty ("centerLng" , (Long ) tripEntity .getProperty ("centerLng" ));
125+ jsonTrip .addProperty ("centerLat" , (Long ) tripEntity .getProperty ("centerLat" ));
104126 JsonArray attractions = new JsonArray ();
105127 for (EmbeddedEntity attraction :
106128 (ArrayList <EmbeddedEntity >) tripEntity .getProperty ("attractions" )) {
107129 JsonObject attractionJson = new JsonObject ();
108- attractionJson .addProperty (
109- "attractionName" , (String ) attraction .getProperty ("attractionName" ));
110- attractionJson .addProperty (
111- "photoReference" , (String ) attraction .getProperty ("photoReference" ));
112- attractionJson .addProperty ("routeIndex" , (Integer ) attraction .getProperty ("routeIndex" ));
113- attractionJson .addProperty ("coordinates" , (String ) attraction .getProperty ("coordinates" ));
130+ attractionJson .addProperty ("name" , (String ) attraction .getProperty ("name" ));
131+ attractionJson .addProperty ("photoUrl" , (String ) attraction .getProperty ("photoUrl" ));
132+ attractionJson .addProperty ("routeIndex" , (Long ) attraction .getProperty ("routeIndex" ));
133+ attractionJson .addProperty ("lat" , (String ) attraction .getProperty ("lat" ));
134+ attractionJson .addProperty ("lng" , (String ) attraction .getProperty ("lng" ));
114135 attractions .add (attractionJson );
115136 }
116137 jsonTrip .add ("attractions" , attractions );
@@ -123,11 +144,9 @@ public static JsonObject toJson(Entity tripEntity) {
123144 * @param tripId id of the trip to find
124145 * @param tripData string representation of tripData json
125146 */
126- public static void updateTrip (String tripId , String tripData ) {
127- Entity tripEntity ;
128- try {
129- tripEntity = TripCrud .readTrip (tripId );
130- } catch (EntityNotFoundException e ) {
147+ public static void updateTrip (Long tripId , String tripData ) {
148+ Entity tripEntity = TripCrud .readTrip (tripId );
149+ if (tripEntity == null ) {
131150 return ;
132151 }
133152 setProperties (tripEntity , tripData );
0 commit comments