diff --git a/src/main/java/seng202/group9/Controller/App.java b/src/main/java/seng202/group9/Controller/App.java index 6b2e3c8..b2b59ef 100644 --- a/src/main/java/seng202/group9/Controller/App.java +++ b/src/main/java/seng202/group9/Controller/App.java @@ -58,6 +58,9 @@ public class App extends Application //testing out dataset try { currentDataset = new Dataset("test's", Dataset.getExisting); + System.out.println(currentDataset.getAirports().get(8078)); + currentDataset.deleteAirport(currentDataset.getAirports().get(8078)); + System.out.println(currentDataset.getAirports().get(8078)); }catch (DataException e){ e.printStackTrace(); @@ -75,14 +78,12 @@ public class App extends Application }catch (DataException e){ e.printStackTrace(); } - //testout single airline adding try { currentDataset.addAirline("Dota2", "Valve", "D2", "DOT", "Defence of the Ancients", "Steam", "Y"); }catch (DataException e){ e.printStackTrace(); } - //testing out airport parser try { System.out.println(currentDataset.importAirport("res/Samples/Airports.txt")); diff --git a/src/main/java/seng202/group9/Controller/Dataset.java b/src/main/java/seng202/group9/Controller/Dataset.java index 795c7bf..1d60ab7 100644 --- a/src/main/java/seng202/group9/Controller/Dataset.java +++ b/src/main/java/seng202/group9/Controller/Dataset.java @@ -236,6 +236,7 @@ public class Dataset { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); System.exit(0); } + createDataLinks(); } /** @@ -1024,7 +1025,136 @@ public class Dataset { String[] tablesToDrop = {"_Airline", "_Airport", "_City", "_Country", "_Routes", "_Flight_Path", "_Flight_Points"}; for (int i = 0; i < tablesToDrop.length; i++){ stmt = c.createStatement(); - stmt.execute(tablesToDrop[i]); + String dropTableStatment = "DROP TABLE `"+this.name+tablesToDrop[i]+"`"; + stmt.execute(dropTableStatment); + stmt.close(); + } + stmt = c.createStatement(); + String deleteDatasetEntry = "DELETE FROM `Datasets` WHERE `Dataset_Name` = \""+this.name+"\""; + stmt.execute(deleteDatasetEntry); + stmt.close(); + c.close(); + } catch ( Exception e ) { + System.err.println( e.getClass().getName() + ": " + e.getMessage() ); + System.exit(0); + } + } + + /** + * deletes an airline from the dataset. + * @param airline + */ + public void deleteAirline(Airline airline){ + //drop the entries + Connection c = null; + Statement stmt = null; + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db"); + String deleteQuery = "DELETE FROM `"+this.name+"_Airline` WHERE `Airline_ID` = " + airline.getID() + ";"; + stmt = c.createStatement(); + stmt.execute(deleteQuery); + stmt.close(); + stmt = c.createStatement(); + //check if number of countries that contain airlines > 0 else delete the country + String countCountry = "SELECT COUNT(*) FROM `"+this.name+"_Airline` JOIN `"+this.name+"_Country` ON" + + " `"+this.name+"_Country`.`Country_Name` = `"+this.name+"_Airline`.`Country`" + + " WHERE `"+this.name+"_Airline`.`Country` = \""+airline.getCountry().getName().replace("\"", "\"\"")+"\""; + ResultSet countCountryRes = stmt.executeQuery(countCountry); + int countryCount = 0; + while (countCountryRes.next()){ + countryCount += countCountryRes.getInt("COUNT(*)"); + } + countCountryRes.close(); + stmt.close(); + //check if number of counties that contain airports > 0 else delete the country + String countCountryA = "SELECT COUNT(*) FROM `"+this.name+"_Airport` JOIN `"+this.name+"_Country` ON" + + " `"+this.name+"_Country`.`Country_Name` = `"+this.name+"_Airport`.`Country`" + + " WHERE `"+this.name+"_Airport`.`Country` = \""+airline.getCountry().getName().replace("\"", "\"\"")+"\""; + countCountryRes = stmt.executeQuery(countCountryA); + while (countCountryRes.next()){ + countryCount += countCountryRes.getInt("COUNT(*)"); + } + countCountryRes.close(); + stmt.close(); + //delete country if there are no matches + if (countryCount == 0){ + stmt = c.createStatement(); + String deleteCountry = "DELETE FROM `"+this.name+"_Country` WHERE `Country_Name` = \""+airline.getCountry().getName()+"\""; + stmt.execute(deleteCountry); + stmt.close(); + } + c.close(); + } catch ( Exception e ) { + System.err.println( e.getClass().getName() + ": " + e.getMessage() ); + System.exit(0); + } + airlines.remove(airline); + } + + public void deleteAirline(int index){ + deleteAirline(airlines.get(index)); + } + + /** + * deletes an airport from the dataset. + * @param airport + */ + public void deleteAirport(Airport airport){ + //drop the entries + Connection c = null; + Statement stmt = null; + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db"); + String deleteQuery = "DELETE FROM `"+this.name+"_Airport` WHERE `Airport_ID` = " + airport.getID() + ";"; + stmt = c.createStatement(); + stmt.execute(deleteQuery); + stmt.close(); + //check if number of countries that contain airports and airlines > 0 else delete the country + String countCountry = "SELECT COUNT(*) FROM `"+this.name+"_Airport` JOIN `"+this.name+"_Country` ON" + + " `"+this.name+"_Country`.`Country_Name` = `"+this.name+"_Airport`.`Country`" + + " WHERE `"+this.name+"_Airport`.`Country` = \""+airport.getCountry().getName().replace("\"", "\"\"")+"\""; + ResultSet countCountryRes = stmt.executeQuery(countCountry); + int countryCount = 0; + while (countCountryRes.next()){ + countryCount = countCountryRes.getInt("COUNT(*)"); + } + countCountryRes.close(); + stmt.close(); + //check if number of countries that contain airlines > 0 else delete the country + String countCountryA = "SELECT COUNT(*) FROM `"+this.name+"_Airline` JOIN `"+this.name+"_Country` ON" + + " `"+this.name+"_Country`.`Country_Name` = `"+this.name+"_Airline`.`Country`" + + " WHERE `"+this.name+"_Airline`.`Country` = \""+airport.getCountry().getName().replace("\"", "\"\"")+"\""; + ResultSet countCountryResA = stmt.executeQuery(countCountry); + while (countCountryResA.next()){ + countryCount += countCountryResA.getInt("COUNT(*)"); + } + countCountryResA.close(); + stmt.close(); + //delete country if no matches + if (countryCount == 0){ + stmt = c.createStatement(); + String deleteCountry = "DELETE FROM `"+this.name+"_Country` WHERE `Country_Name` = \""+airport.getCountry().getName()+"\""; + stmt.execute(deleteCountry); + stmt.close(); + } + //cehck if number cities that contain airports > 0 else delete the city + String countCity = "SELECT COUNT(*) FROM `"+this.name+"_Airport` JOIN `"+this.name+"_City` ON" + + " `"+this.name+"_City`.`City_Name` = `"+this.name+"_Airport`.`City`" + + " WHERE `"+this.name+"_Airport`.`City` = \""+airport.getCityName().replace("\"", "\"\"")+"\""; + ResultSet countCityRes = stmt.executeQuery(countCity); + int cityCount = 0; + while (countCityRes.next()){ + cityCount = countCityRes.getInt("COUNT(*)"); + } + countCountryRes.close(); + stmt.close(); + //delete country if no matches + if (cityCount == 0){ + stmt = c.createStatement(); + String deleteCity = "DELETE FROM `"+this.name+"_City` WHERE `City_Name` = \""+airport.getCityName()+"\""; + stmt.execute(deleteCity); stmt.close(); } c.close(); @@ -1032,6 +1162,90 @@ public class Dataset { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); System.exit(0); } + airports.remove(airport); + } + + public void deleteAirport(int index){ + deleteAirport(airports.get(index)); + } + /** + * deletes an route from the dataset. + * @param route + */ + public void deleteRoute(Route route){ + //drop the entries + Connection c = null; + Statement stmt = null; + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db"); + String deleteQuery = "DELETE FROM `"+this.name+"_Routes` WHERE `Route_ID` = " + route.getID() + ";"; + stmt = c.createStatement(); + stmt.execute(deleteQuery); + c.close(); + } catch ( Exception e ) { + System.err.println( e.getClass().getName() + ": " + e.getMessage() ); + System.exit(0); + } + routes.remove(route); + } + + public void deleteRoute(int index){ + deleteRoute(routes.get(index)); + } + /** + * deletes an airline from the dataset. + * @param flightPath + */ + public void deleteFlightPath(FlightPath flightPath){ + //delete all flight points with the id + while(flightPath.getFlightPoints().size() > 0){ + deleteFlightPoint(flightPath.getFlightPoints().get(0), flightPath); + } + //drop the entries + Connection c = null; + Statement stmt = null; + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db"); + String deleteQuery = "DELETE FROM `"+this.name+"_Flight_Path` WHERE `Path_ID` = " + flightPath.getID() + ";"; + stmt = c.createStatement(); + stmt.execute(deleteQuery); + c.close(); + } catch ( Exception e ) { + System.err.println( e.getClass().getName() + ": " + e.getMessage() ); + System.exit(0); + } + flightPaths.remove(flightPath); + } + + public void deleteFlightPath(int index){ + deleteFlightPath(flightPaths.get(index)); + } + /** + * deletes an airline from the dataset. + * @param flightPoint + */ + public void deleteFlightPoint(FlightPoint flightPoint, FlightPath flightPath){ + //drop the tables + Connection c = null; + Statement stmt = null; + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db"); + String deleteQuery = "DELETE FROM `"+this.name+"_Flight_Points` WHERE `Point_ID` = " + flightPoint.getID() + ";"; + stmt = c.createStatement(); + stmt.execute(deleteQuery); + c.close(); + } catch ( Exception e ) { + System.err.println( e.getClass().getName() + ": " + e.getMessage() ); + System.exit(0); + } + flightPath.getFlightPoints().remove(flightPoint); + } + + public void deleteFlightPoint(int pathIndex, int pointIndex){ + deleteFlightPoint(flightPaths.get(pathIndex).getFlightPoints().get(pointIndex), flightPaths.get(pathIndex)); } public ArrayList getAirlines() { diff --git a/src/main/java/seng202/group9/Core/FlightPoint.java b/src/main/java/seng202/group9/Core/FlightPoint.java index 34c043f..b059626 100644 --- a/src/main/java/seng202/group9/Core/FlightPoint.java +++ b/src/main/java/seng202/group9/Core/FlightPoint.java @@ -18,6 +18,8 @@ public class FlightPoint { public FlightPoint(String type, String name, double altitude, double latitude, double longitude){ //extra calculations will have to be used to find heading, legdistance and total distance. If necessary //Type 1 file the file the lecturers gave us + //indexID = flight path ID + //ID = unique Auto Increment value this.name = name; this.ID = -1; this.indexID = -1;