Skip to content
Snippets Groups Projects
Commit 1d14c494 authored by Théo PIRKL's avatar Théo PIRKL
Browse files

Merge branch 'open_transport_stuff' into 'gui-transport'

OTS integration

See merge request !13
parents 5b931922 cc7aba18
No related branches found
No related tags found
3 merge requests!21*poof* final version,!16Gui transport,!13OTS integration
...@@ -11,7 +11,7 @@ import org.json.JSONArray; ...@@ -11,7 +11,7 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
public final class Connection { public class Connection {
private Stop from; private Stop from;
private Stop to; private Stop to;
private Time duration; private Time duration;
...@@ -21,6 +21,26 @@ public final class Connection { ...@@ -21,6 +21,26 @@ public final class Connection {
private int capacity2nd; private int capacity2nd;
private List<Section> sections; private List<Section> sections;
/**
* Empty Connection
*/
private final static class EmptyConnection extends Connection{
/**
* Constructor of EmptyConnection
* All values are default ones
* An Empty Connection go from Geneva to Geneva
* @throws ParseException
*/
private EmptyConnection() throws ParseException {
super(new Stop.StopBuilder(new JSONObject()).build(),
new Stop.StopBuilder(new JSONObject()).build(),
new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()),
new Service.ServiceBuilder(new JSONObject()).build(),
new ArrayList<>(),
0,0, new ArrayList<>());
}
}
/** /**
* Constructor of Connection Object * Constructor of Connection Object
* @param from stop object * @param from stop object
...@@ -51,10 +71,11 @@ public final class Connection { ...@@ -51,10 +71,11 @@ public final class Connection {
return new ArrayList<>(sections); return new ArrayList<>(sections);
} }
/** /**
* Builder of Connection object * Builder of Connection object
*/ */
public static class ConnectionBuilder{ public final static class ConnectionBuilder{
private JSONObject datas; private JSONObject datas;
/** /**
...@@ -72,6 +93,11 @@ public final class Connection { ...@@ -72,6 +93,11 @@ public final class Connection {
* @throws ParseException * @throws ParseException
*/ */
public Connection build() throws ParseException { public Connection build() throws ParseException {
//if the datas retrieved contain nothing, we have to prevent errors
if(datas.isEmpty()){
return new EmptyConnection();
}
JSONObject fromJSON = new JSONObject(datas.get("from")); JSONObject fromJSON = new JSONObject(datas.get("from"));
Stop.StopBuilder from = new Stop.StopBuilder(fromJSON); Stop.StopBuilder from = new Stop.StopBuilder(fromJSON);
...@@ -79,7 +105,7 @@ public final class Connection { ...@@ -79,7 +105,7 @@ public final class Connection {
Stop.StopBuilder to = new Stop.StopBuilder(toJSON); Stop.StopBuilder to = new Stop.StopBuilder(toJSON);
DateFormat formatter = new SimpleDateFormat("dd:kk:mm:ss"); DateFormat formatter = new SimpleDateFormat("dd:kk:mm:ss");
String date = datas.getString("duration").replace('d',':'); String date = datas.isNull("duration") ? "00:00:00:00" : datas.getString("duration").replace('d',':');
Time duration = new Time(formatter.parse(date).getTime()); Time duration = new Time(formatter.parse(date).getTime());
JSONObject serviceJSON = new JSONObject(datas.get("service")); JSONObject serviceJSON = new JSONObject(datas.get("service"));
......
...@@ -2,11 +2,24 @@ package ch.hepia.api.transport; ...@@ -2,11 +2,24 @@ package ch.hepia.api.transport;
import org.json.JSONObject; import org.json.JSONObject;
public final class Coordinates { public class Coordinates {
private Type type; private String type;
private double x; private double x;
private double y; private double y;
/**
* Default coordinates containing default values (Geneva is the default value)
* WGS84 is the default type value
*/
private final static class DefaultCoordinates extends Coordinates{
/**
* Constructor of DefaultCoordinates, located in Geneva
*/
private DefaultCoordinates(){
super("WGS84", 46.210237, 6.142422);
}
}
/** /**
* Constructor of the class Coordinates * Constructor of the class Coordinates
* A coordinates is represented by a type of place and a point (x, y) * A coordinates is represented by a type of place and a point (x, y)
...@@ -14,7 +27,7 @@ public final class Coordinates { ...@@ -14,7 +27,7 @@ public final class Coordinates {
* @param x coordinate in x * @param x coordinate in x
* @param y coordinate in y * @param y coordinate in y
*/ */
private Coordinates(Type type, double x, double y){ private Coordinates(String type, double x, double y){
this.type = type; this.type = type;
this.x = x; this.x = x;
this.y = y; this.y = y;
...@@ -23,7 +36,7 @@ public final class Coordinates { ...@@ -23,7 +36,7 @@ public final class Coordinates {
@Override @Override
public String toString(){ public String toString(){
StringBuilder string = new StringBuilder(); StringBuilder string = new StringBuilder();
return string.append(type.toString()).append("au point (").append(x).append(", ").append(y).append(")").toString(); return string.append("Coordonnées du type ").append(type).append(" au point (").append(x).append(", ").append(y).append(")").toString();
} }
/** /**
...@@ -45,7 +58,7 @@ public final class Coordinates { ...@@ -45,7 +58,7 @@ public final class Coordinates {
/** /**
* Builder of Coordinates object * Builder of Coordinates object
*/ */
public static class CoordinatesBuilder{ public final static class CoordinatesBuilder{
private JSONObject datas; private JSONObject datas;
/** /**
...@@ -62,7 +75,11 @@ public final class Coordinates { ...@@ -62,7 +75,11 @@ public final class Coordinates {
* @return a new Coordinates object * @return a new Coordinates object
*/ */
public Coordinates build(){ public Coordinates build(){
Type type = Type.valueOf(datas.getString("type")); if(datas.isEmpty()){
return new DefaultCoordinates();
}
String type = datas.getString("type");
double x = datas.getDouble("x"); double x = datas.getDouble("x");
double y = datas.getDouble("y"); double y = datas.getDouble("y");
......
...@@ -7,7 +7,7 @@ import java.text.ParseException; ...@@ -7,7 +7,7 @@ import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public final class Journey { public class Journey {
private String name; private String name;
private String category; private String category;
private int categoryCode; private int categoryCode;
...@@ -18,6 +18,21 @@ public final class Journey { ...@@ -18,6 +18,21 @@ public final class Journey {
private int capacity1st; private int capacity1st;
private int capacity2nd; private int capacity2nd;
/**
* Empty Journey
*/
private final static class EmptyJourney extends Journey{
/**
* Constructor of EmptyJourney
* No Information and no Stops in the Journey
* @throws ParseException
*/
private EmptyJourney() throws ParseException {
super("", "", 0, 0, "", "",
new ArrayList<>(), 0, 0);
}
}
/** /**
* Constructor of the Journey Object * Constructor of the Journey Object
* @param name * @param name
...@@ -85,7 +100,7 @@ public final class Journey { ...@@ -85,7 +100,7 @@ public final class Journey {
/** /**
* Builder of Journey object * Builder of Journey object
*/ */
public static class JourneyBuilder{ public final static class JourneyBuilder{
private JSONObject datas; private JSONObject datas;
/** /**
...@@ -103,6 +118,10 @@ public final class Journey { ...@@ -103,6 +118,10 @@ public final class Journey {
* @throws ParseException * @throws ParseException
*/ */
public Journey build() throws ParseException { public Journey build() throws ParseException {
if(datas.isEmpty()){
return new EmptyJourney();
}
String name = datas.getString("name"); String name = datas.getString("name");
String category = datas.getString("category"); String category = datas.getString("category");
int categoryCode = datas.getInt("categoryCode"); int categoryCode = datas.getInt("categoryCode");
......
...@@ -2,7 +2,7 @@ package ch.hepia.api.transport; ...@@ -2,7 +2,7 @@ package ch.hepia.api.transport;
import org.json.JSONObject; import org.json.JSONObject;
public final class Location { public class Location {
private int id; private int id;
private Type type; private Type type;
private String name; private String name;
...@@ -10,6 +10,20 @@ public final class Location { ...@@ -10,6 +10,20 @@ public final class Location {
private Coordinates where; private Coordinates where;
private double distance; private double distance;
/**
* Default Location
*/
private final static class DefaultLocation extends Location{
/**
* Constructor of DefaultLocation, centered in Geneva
* Type is REFINE, because it's a default choice
*/
private DefaultLocation(){
//here building new empty coordinates will return a default coordinates corresponding to Geneva
super(8501008, Type.REFINE, "Genève", 0, new Coordinates.CoordinatesBuilder(new JSONObject()).build(), 0);
}
}
/** /**
* Constructor of the class Location * Constructor of the class Location
* @param id unique to the Location * @param id unique to the Location
...@@ -39,7 +53,7 @@ public final class Location { ...@@ -39,7 +53,7 @@ public final class Location {
/** /**
* Builder of Location objects * Builder of Location objects
*/ */
public static class LocationBuilder{ public final static class LocationBuilder{
private JSONObject datas; private JSONObject datas;
private String type; private String type;
...@@ -58,15 +72,19 @@ public final class Location { ...@@ -58,15 +72,19 @@ public final class Location {
* @return a new Location object * @return a new Location object
*/ */
public Location build(){ public Location build(){
int id = datas.getInt("id"); if(datas.isEmpty()){
Type type = Type.valueOf(this.type); return new DefaultLocation();
String name = datas.getString("name"); }
double score = datas.getDouble("score");
int id = datas.isNull("id") ? 0 : datas.getInt("id");
Type type = Type.valueOf(this.type.toUpperCase());
String name = datas.isNull("name") ? "" : datas.getString("name");
double score = datas.isNull("score") ? 0 : datas.getDouble("score");
JSONObject coordinatesJSON = new JSONObject(datas.get("coordinates")); JSONObject coordinatesJSON = datas.getJSONObject("coordinate");
Coordinates.CoordinatesBuilder coordinates = new Coordinates.CoordinatesBuilder(coordinatesJSON); Coordinates.CoordinatesBuilder coordinates = new Coordinates.CoordinatesBuilder(coordinatesJSON);
double distance = datas.getDouble("distance"); double distance = datas.isNull("distance") ? 0 : datas.getDouble("distance");
return new Location(id, type, name, score, coordinates.build(), distance); return new Location(id, type, name, score, coordinates.build(), distance);
} }
......
...@@ -7,13 +7,31 @@ import java.text.DateFormat; ...@@ -7,13 +7,31 @@ import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
public final class Prognosis { public class Prognosis {
private int plateform; private int plateform;
private Time departure; private Time departure;
private Time arrival; private Time arrival;
private int capacity1st; private int capacity1st;
private int capacity2nd; private int capacity2nd;
/**
* Empty Prognosis
*/
private final static class EmptyPrognosis extends Prognosis{
/**
* Constructor of EmptyPrognosis
* No departure and no Arrival Time
* No information on the capacity of 1st and 2nd class nor on the plateform
* @throws ParseException
*/
private EmptyPrognosis() throws ParseException {
super(0,
new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()),
new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()),
0, 0);
}
}
/** /**
* Constructor of the Prognosis class * Constructor of the Prognosis class
* A Prognosis contains information on the status of a Stop Object * A Prognosis contains information on the status of a Stop Object
...@@ -34,7 +52,7 @@ public final class Prognosis { ...@@ -34,7 +52,7 @@ public final class Prognosis {
/** /**
* Builder of Prognosis Objects * Builder of Prognosis Objects
*/ */
public static class PrognosisBuilder{ public final static class PrognosisBuilder{
private JSONObject datas; private JSONObject datas;
/** /**
...@@ -52,17 +70,21 @@ public final class Prognosis { ...@@ -52,17 +70,21 @@ public final class Prognosis {
* @throws ParseException * @throws ParseException
*/ */
public Prognosis build() throws ParseException { public Prognosis build() throws ParseException {
int plateform = datas.getInt("plateform"); if(datas.isEmpty()){
return new EmptyPrognosis();
}
int plateform = datas.isNull("plateform") ? 0 : datas.getInt("plateform");
DateFormat formatter = new SimpleDateFormat("dd:kk:mm:ss"); DateFormat formatter = new SimpleDateFormat("dd:kk:mm:ss");
String depatureString = datas.getString("departure").replace('d',':'); String depatureString = datas.isNull("departure") ? "00:00:00:00" : datas.getString("departure").replace('d',':');
Time departure = new Time(formatter.parse(depatureString).getTime()); Time departure = new Time(formatter.parse(depatureString).getTime());
String arrivalString = datas.getString("arrival").replace('d', ':'); String arrivalString = datas.isNull("arrival") ? "00:00:00:00" : datas.getString("arrival").replace('d', ':');
Time arrival = new Time(formatter.parse(arrivalString).getTime()); Time arrival = new Time(formatter.parse(arrivalString).getTime());
int capacity1st = datas.getInt("capacity1st"); int capacity1st = datas.isNull("capacity1st") ? 0 : datas.getInt("capacity1st");
int capacity2nd = datas.getInt("capacity2nd"); int capacity2nd = datas.isNull("capacity2nd") ? 0 : datas.getInt("capacity2nd");
return new Prognosis(plateform, departure, arrival, capacity1st, capacity2nd); return new Prognosis(plateform, departure, arrival, capacity1st, capacity2nd);
} }
......
...@@ -4,12 +4,29 @@ import org.json.JSONObject; ...@@ -4,12 +4,29 @@ import org.json.JSONObject;
import java.text.ParseException; import java.text.ParseException;
public final class Section { public class Section {
private Journey journey; private Journey journey;
private double walktime; private double walktime;
private Stop departure; private Stop departure;
private Stop arrival; private Stop arrival;
/**
* Empty Section
*/
private final static class EmptySection extends Section{
/**
* Constructor of EmptySection
* An empty Section is a Journey From Geneva to Geneva
* @throws ParseException
*/
private EmptySection() throws ParseException {
super(new Journey.JourneyBuilder(new JSONObject()).build(), 0,
new Stop.StopBuilder(new JSONObject()).build(),
new Stop.StopBuilder(new JSONObject()).build());
}
}
/** /**
* Constructor of the class Section * Constructor of the class Section
* @param journey what Journey this section is part of * @param journey what Journey this section is part of
...@@ -51,7 +68,7 @@ public final class Section { ...@@ -51,7 +68,7 @@ public final class Section {
/** /**
* Builder of Section objects * Builder of Section objects
*/ */
public static class SectionBuilder{ public final static class SectionBuilder{
private JSONObject datas; private JSONObject datas;
/** /**
...@@ -69,15 +86,19 @@ public final class Section { ...@@ -69,15 +86,19 @@ public final class Section {
* @throws ParseException * @throws ParseException
*/ */
public Section build() throws ParseException { public Section build() throws ParseException {
JSONObject journeyJSON = new JSONObject(datas.get("journey")); if(datas.isEmpty()){
return new EmptySection();
}
JSONObject journeyJSON = datas.getJSONObject("journey");
Journey.JourneyBuilder journey = new Journey.JourneyBuilder(journeyJSON); Journey.JourneyBuilder journey = new Journey.JourneyBuilder(journeyJSON);
double walktime = datas.isNull("walktime") ? 0 : datas.getDouble("walktime"); double walktime = datas.isNull("walktime") ? 0 : datas.getDouble("walktime");
JSONObject departureJSON = new JSONObject(datas.get("departure")); JSONObject departureJSON = datas.getJSONObject("departure");
Stop.StopBuilder departure = new Stop.StopBuilder(departureJSON); Stop.StopBuilder departure = new Stop.StopBuilder(departureJSON);
JSONObject arrivalJSON = new JSONObject(datas.get("arrival")); JSONObject arrivalJSON = datas.getJSONObject("arrival");
Stop.StopBuilder arrival = new Stop.StopBuilder(arrivalJSON); Stop.StopBuilder arrival = new Stop.StopBuilder(arrivalJSON);
return new Section(journey.build(), walktime, departure.build(), arrival.build()); return new Section(journey.build(), walktime, departure.build(), arrival.build());
......
...@@ -3,10 +3,23 @@ package ch.hepia.api.transport; ...@@ -3,10 +3,23 @@ package ch.hepia.api.transport;
import org.json.JSONObject; import org.json.JSONObject;
public final class Service { public class Service {
private String regular; private String regular;
private String irregular; private String irregular;
/**
* Empty Service
*/
private final static class EmptyService extends Service{
/**
* Constructor of EmptyService
* No informations given
*/
private EmptyService(){
super("", "");
}
}
/** /**
* Constructor of Service class * Constructor of Service class
* @param regular is the service regular ? * @param regular is the service regular ?
...@@ -20,7 +33,7 @@ public final class Service { ...@@ -20,7 +33,7 @@ public final class Service {
/** /**
* Builder of Service objects * Builder of Service objects
*/ */
public static class ServiceBuilder{ public final static class ServiceBuilder{
private JSONObject datas; private JSONObject datas;
/** /**
...@@ -37,8 +50,12 @@ public final class Service { ...@@ -37,8 +50,12 @@ public final class Service {
* @return a new Service object * @return a new Service object
*/ */
public Service build(){ public Service build(){
String regular = datas.getString("regular"); if(datas.isEmpty()){
String irregular = datas.getString("irregular"); return new EmptyService();
}
String regular = datas.isNull("regular") ? "" : datas.getString("regular");
String irregular = datas.isNull("irregular") ? "" : datas.getString("irregular");
return new Service(regular, irregular); return new Service(regular, irregular);
} }
......
...@@ -7,7 +7,7 @@ import java.text.DateFormat; ...@@ -7,7 +7,7 @@ import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
public final class Stop { public class Stop {
private Location station; private Location station;
private Time arrival; private Time arrival;
private Time departure; private Time departure;
...@@ -15,6 +15,25 @@ public final class Stop { ...@@ -15,6 +15,25 @@ public final class Stop {
private int plateform; private int plateform;
private Prognosis prognosis; private Prognosis prognosis;
/**
* Default Stop in Geneva
*/
private final static class DefaultStop extends Stop{
/**
* Constructor of DefaultStop
* DefaultStop is located in Geneva
* No Time of departure nor Arrival, no Prognosis, no plateform and no delay
* @throws ParseException
*/
private DefaultStop() throws ParseException {
super(new Location.LocationBuilder(new JSONObject(), "refine").build(),
new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()),
new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()),
0, 0,
new Prognosis.PrognosisBuilder(new JSONObject()).build());
}
}
/** /**
* Constructor of the Stop class * Constructor of the Stop class
* @param station Location where the Stop is * @param station Location where the Stop is
...@@ -60,7 +79,7 @@ public final class Stop { ...@@ -60,7 +79,7 @@ public final class Stop {
/** /**
* Builder of Stop objects * Builder of Stop objects
*/ */
public static class StopBuilder{ public final static class StopBuilder{
private JSONObject datas; private JSONObject datas;
/** /**
...@@ -78,21 +97,25 @@ public final class Stop { ...@@ -78,21 +97,25 @@ public final class Stop {
* @throws ParseException * @throws ParseException
*/ */
public Stop build() throws ParseException { public Stop build() throws ParseException {
if(datas.isEmpty()){
return new DefaultStop();
}
//getting the first key to know the type of the Location //getting the first key to know the type of the Location
String[] keys = (String[]) datas.keySet().toArray(); String[] keys = (String[]) datas.keySet().toArray();
String type = keys[0]; String type = keys[0];
JSONObject locationJSON = new JSONObject(datas.get(type)); JSONObject locationJSON = datas.getJSONObject(type);
Location.LocationBuilder location = new Location.LocationBuilder(locationJSON, type); Location.LocationBuilder location = new Location.LocationBuilder(locationJSON, type);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dddkk:mm:ss"); DateFormat formatter = new SimpleDateFormat("yyyy-MM-dddkk:mm:ss");
String arrivalString = datas.getString("arrival"); String arrivalString = datas.isNull("arrival") ? "0000-00-00000:00:00" : datas.getString("arrival");
Time arrival = new Time(formatter.parse(arrivalString).getTime()); Time arrival = new Time(formatter.parse(arrivalString).getTime());
String depatureString = datas.getString("departure"); String depatureString = datas.isNull("departure") ? "0000-00-00000:00:00" : datas.getString("departure");
Time departure = new Time(formatter.parse(depatureString).getTime()); Time departure = new Time(formatter.parse(depatureString).getTime());
int delay = datas.getInt("delay"); int delay = datas.isNull("delay") ? 0 : datas.getInt("delay");
int plateform = datas.getInt("plateform"); int plateform = datas.isNull("plateform") ? 0 : datas.getInt("plateform");
JSONObject prognosisJSON = new JSONObject(datas.get("prognosis")); JSONObject prognosisJSON = new JSONObject(datas.get("prognosis"));
Prognosis.PrognosisBuilder prognosis = new Prognosis.PrognosisBuilder(prognosisJSON); Prognosis.PrognosisBuilder prognosis = new Prognosis.PrognosisBuilder(prognosisJSON);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment