Serialising photos works now
This commit is contained in:
@@ -2,9 +2,13 @@ package controllers;
|
||||
|
||||
import javafx.embed.swing.SwingFXUtils;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.stage.FileChooser;
|
||||
import model.SceneCode;
|
||||
import model.Session;
|
||||
import model.User;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
@@ -55,7 +59,27 @@ public class UploadPhotoController extends Controller {
|
||||
}
|
||||
|
||||
public void next(){
|
||||
|
||||
User.AccountType ac = User.AccountType.getValueOf(accountType);
|
||||
if (ac == User.AccountType.PASSENGER){
|
||||
User user = new User(studentNumber, email, password, address,homephone, mobilephone, photoStored);
|
||||
if (Session.session.getDataManager().addUser(user)){
|
||||
popUp(Alert.AlertType.CONFIRMATION, "Success!", "Your Account has been made.", "You can now log in and will be redirected the the title screen after.");
|
||||
try {
|
||||
changeScene(SceneCode.MAIN);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else{
|
||||
popUp(Alert.AlertType.WARNING, "Error!", "There is already an passenger with this id.", "You will be redirected to the homescreen to log in.");
|
||||
try {
|
||||
changeScene(SceneCode.MAIN);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}else{
|
||||
//changeScene()
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,7 +6,9 @@ import javafx.collections.ObservableList;
|
||||
import javafx.collections.ObservableSet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Gondr on 4/04/2017.
|
||||
@@ -17,17 +19,21 @@ public class DataManager {
|
||||
private ObservableList<Route> routes;
|
||||
private ObservableList<Trip> trips;
|
||||
private ObservableList<SharedTrip> sharedTrips;
|
||||
private Map<String, User> users;
|
||||
private Map<String, Driver> drivers;//seperated from users as they are two different types of accounts
|
||||
|
||||
public DataManager(List rides, List stops, List routes, List trips, List sharedTrips){
|
||||
public DataManager(List rides, List stops, List routes, List trips, List sharedTrips, Map users, Map drivers){
|
||||
this.rides = FXCollections.observableArrayList(rides);
|
||||
this.stops = FXCollections.observableArrayList(stops);
|
||||
this.routes = FXCollections.observableArrayList(routes);
|
||||
this.trips = FXCollections.observableArrayList(trips);
|
||||
this.sharedTrips = FXCollections.observableArrayList(sharedTrips);
|
||||
this.users = users;
|
||||
this.drivers = drivers;
|
||||
}
|
||||
|
||||
public DataManager(){
|
||||
this(FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList());
|
||||
this(FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>());
|
||||
}
|
||||
|
||||
public ObservableList<Ride> getRides() {
|
||||
@@ -102,4 +108,19 @@ public class DataManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean addUser(User user){
|
||||
if (!users.containsKey(String.valueOf(user.studentNumber))) {
|
||||
users.put(String.valueOf(user.studentNumber), user);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Map<String, User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public Map<String, Driver> getDrivers() {
|
||||
return drivers;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package model;
|
||||
|
||||
import javafx.embed.swing.SwingFXUtils;
|
||||
import javafx.scene.image.Image;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Created by Gondr on 1/06/2017.
|
||||
@@ -15,7 +18,8 @@ public class User {
|
||||
protected String homephone;
|
||||
protected String mobilephone;
|
||||
protected AccountType accountType;
|
||||
protected Image photo;
|
||||
transient protected Image photo;
|
||||
private byte[] image;
|
||||
|
||||
public enum AccountType{
|
||||
PASSENGER("Passenger"), DRIVER("Driver");
|
||||
@@ -23,6 +27,16 @@ public class User {
|
||||
AccountType(String name){
|
||||
this.name = name;
|
||||
}
|
||||
public static AccountType getValueOf(String s){
|
||||
switch(s){
|
||||
case "Passenger":
|
||||
return PASSENGER;
|
||||
case "Driver":
|
||||
return DRIVER;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public User(int studentNumber, String email, String password, String address, String homephone, String mobilephone,
|
||||
@@ -35,5 +49,31 @@ public class User {
|
||||
this.mobilephone = mobilephone;
|
||||
this.photo = photo;
|
||||
this.accountType = AccountType.PASSENGER;
|
||||
|
||||
}
|
||||
|
||||
public void serialise(){
|
||||
try {
|
||||
BufferedImage bi = SwingFXUtils.fromFXImage(photo, null);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ImageIO.write(bi, "png", out);
|
||||
out.flush();
|
||||
image = out.toByteArray();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void deserialise(){
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(image);
|
||||
BufferedImage bi = null;
|
||||
try {
|
||||
bi = ImageIO.read(in);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
photo = SwingFXUtils.toFXImage(bi, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import model.*;
|
||||
|
||||
@@ -7,9 +9,7 @@ import java.io.*;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by Gondr on 19/05/2017.
|
||||
@@ -36,7 +36,9 @@ public class DataManagerSerialiser extends Serialiser {
|
||||
boolean serialRoutesSuccess = serialiseRoutes(routes, "serialisation/routes.json");
|
||||
boolean serialTripsSuccess = serialiseTrips(trips, "serialisation/trips.json");
|
||||
boolean serialSharedTripsSuccess = serialiseSharedTrips(sharedTrips, "serialisation/sharedtrips.json");
|
||||
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess && serialTripsSuccess && serialSharedTripsSuccess);
|
||||
boolean serialUsersSuccess = serialiseUsers(dataManager.getUsers(), "serialisation/users.json");
|
||||
boolean serialDriversSuccess = serialiseDrivers(dataManager.getDrivers(), "serialisation/drivers.json");
|
||||
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess && serialTripsSuccess && serialSharedTripsSuccess && serialUsersSuccess && serialDriversSuccess);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,6 +108,37 @@ public class DataManagerSerialiser extends Serialiser {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean serialiseUsers(Map<String,User> users, String filepath){
|
||||
for (String key: users.keySet()){
|
||||
users.get(key).serialise();
|
||||
}
|
||||
try{
|
||||
String path = getClass().getClassLoader().getResource(filepath).getPath();
|
||||
Writer writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
|
||||
gson.toJson(users, writer);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean serialiseDrivers(Map<String,Driver> drivers, String filepath){
|
||||
for (String key: drivers.keySet()){
|
||||
drivers.get(key).serialise();
|
||||
}
|
||||
try{
|
||||
String path = getClass().getClassLoader().getResource(filepath).getPath();
|
||||
Writer writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
|
||||
gson.toJson(drivers, 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
|
||||
@@ -148,7 +181,21 @@ public class DataManagerSerialiser extends Serialiser {
|
||||
trip.deserialise();
|
||||
}
|
||||
|
||||
return new DataManager(rides, stops, routes, trips, sharedTrips);
|
||||
Reader reader6 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/users.json"), "UTF-8");
|
||||
HashMap<String, User> users = gson.fromJson(reader6, new TypeToken<HashMap<String, User>>(){}.getType());
|
||||
reader6.close();
|
||||
for (String userkey: users.keySet()){
|
||||
users.get(userkey).deserialise();
|
||||
}
|
||||
|
||||
Reader reader7 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/drivers.json"), "UTF-8");
|
||||
HashMap<String, Driver> drivers = gson.fromJson(reader7, new TypeToken<HashMap<String, Driver>>(){}.getType());
|
||||
reader7.close();
|
||||
for (String driverkey: drivers.keySet()){
|
||||
users.get(driverkey).deserialise();
|
||||
}
|
||||
|
||||
return new DataManager(rides, stops, routes, trips, sharedTrips, users, drivers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
|
||||
{}
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user