|
|
|
@ -1,10 +1,7 @@
|
|
|
|
package utils;
|
|
|
|
package utils;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import model.DataManager;
|
|
|
|
import model.*;
|
|
|
|
import model.Ride;
|
|
|
|
|
|
|
|
import model.Route;
|
|
|
|
|
|
|
|
import model.Stop;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.io.*;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
@ -23,17 +20,30 @@ public class DataManagerSerialiser extends Serialiser {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* serialise DataManger
|
|
|
|
|
|
|
|
* @param dataManager
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
public boolean serialise(DataManager dataManager){
|
|
|
|
public boolean serialise(DataManager dataManager){
|
|
|
|
ArrayList<Ride> rides = new ArrayList<>(dataManager.getRides());
|
|
|
|
ArrayList<Ride> rides = new ArrayList<>(dataManager.getRides());
|
|
|
|
ArrayList<Stop> stops = new ArrayList<>(dataManager.getStops());
|
|
|
|
ArrayList<Stop> stops = new ArrayList<>(dataManager.getStops());
|
|
|
|
ArrayList<Route> routes = new ArrayList<>(dataManager.getRoutes());
|
|
|
|
ArrayList<Route> routes = new ArrayList<>(dataManager.getRoutes());
|
|
|
|
|
|
|
|
ArrayList<Trip> trips = new ArrayList<>(dataManager.getTrips());
|
|
|
|
boolean serialRidesSuccess = serialise(rides, "serialisation/rides.json");
|
|
|
|
boolean serialRidesSuccess = serialise(rides, "serialisation/rides.json");
|
|
|
|
boolean serialStopsSuccess = serialise(stops, "serialisation/stops.json");
|
|
|
|
boolean serialStopsSuccess = serialise(stops, "serialisation/stops.json");
|
|
|
|
boolean serialRoutesSuccess = serialise(routes, "serialisation/routes.json");
|
|
|
|
boolean serialRoutesSuccess = serialiseRoutes(routes, "serialisation/routes.json");
|
|
|
|
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess);
|
|
|
|
boolean serialTripsSuccess = serialiseTrips(trips, "serialisation/trips.json");
|
|
|
|
|
|
|
|
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess && serialTripsSuccess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean serialise(ArrayList<Route> routes, String filepath){
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Serialises routes
|
|
|
|
|
|
|
|
* @param routes Routes to be serialised
|
|
|
|
|
|
|
|
* @param filepath File path to serialise it to.
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public boolean serialiseRoutes(ArrayList<Route> routes, String filepath){
|
|
|
|
for (Route route: routes){
|
|
|
|
for (Route route: routes){
|
|
|
|
route.serialise();
|
|
|
|
route.serialise();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -49,6 +59,37 @@ public class DataManagerSerialiser extends Serialiser {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Serialises trips
|
|
|
|
|
|
|
|
* @param trips Trips to be serialised
|
|
|
|
|
|
|
|
* @param filepath File path to serialise it to.
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public boolean serialiseTrips(ArrayList<Trip> trips, String filepath){
|
|
|
|
|
|
|
|
for (Trip trip: trips){
|
|
|
|
|
|
|
|
trip.serialise();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
String path = getClass().getClassLoader().getResource(filepath).getPath();
|
|
|
|
|
|
|
|
Writer writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
|
|
|
|
|
|
|
|
gson.toJson(trips, writer);
|
|
|
|
|
|
|
|
writer.close();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* loads the data manager from file
|
|
|
|
|
|
|
|
* @return whether the data was loaded properly or not
|
|
|
|
|
|
|
|
* @throws IOException No File
|
|
|
|
|
|
|
|
* @throws NoSuchMethodException No Method to Write
|
|
|
|
|
|
|
|
* @throws IllegalAccessException No access allowed to file
|
|
|
|
|
|
|
|
* @throws InvocationTargetException Thread error
|
|
|
|
|
|
|
|
* @throws InstantiationException Nothing set
|
|
|
|
|
|
|
|
*/
|
|
|
|
public DataManager load() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
|
|
|
public DataManager load() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
|
|
|
Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/rides.json"), "UTF-8");
|
|
|
|
Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/rides.json"), "UTF-8");
|
|
|
|
ArrayList<Ride> rides = gson.fromJson(reader, new TypeToken<ArrayList<Ride>>(){}.getType());
|
|
|
|
ArrayList<Ride> rides = gson.fromJson(reader, new TypeToken<ArrayList<Ride>>(){}.getType());
|
|
|
|
@ -66,12 +107,15 @@ public class DataManagerSerialiser extends Serialiser {
|
|
|
|
route.deserialise();
|
|
|
|
route.deserialise();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Constructor c = DataManager.class.getDeclaredConstructor(List.class, List.class, List.class);
|
|
|
|
Reader reader4 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/trips.json"), "UTF-8");
|
|
|
|
// DataManager dataManager = (DataManager) c.newInstance(rides, stops, routes);
|
|
|
|
ArrayList<Trip> trips = gson.fromJson(reader4, new TypeToken<ArrayList<Trip>>(){}.getType());
|
|
|
|
// return dataManager;
|
|
|
|
reader4.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (Trip trip: trips){
|
|
|
|
|
|
|
|
trip.deserialise();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new DataManager(rides, stops, routes);
|
|
|
|
return new DataManager(rides, stops, routes, trips);
|
|
|
|
// return new DataManager();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|