// Fetch details of all flights available in the system public FlightDetails[] getAllFlights(boolean isDB) { FlightDetails flightDetails[] = null; if (!isDB) { flightDetails = CacheObject.flight; } else { FlightDetails tempFlightRef; int size = 0; int personId = 0; String query = SQLquery.getAllFlights; Statement stmt = null; try { stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); JourneyDAO journeyDAO = new JourneyDAO(); ReservationDAO reservationDAO = new ReservationDAO(); if (rs != null) { rs.last(); size = rs.getRow(); if (size == 0) { return null; } flightDetails = new FlightDetails[size]; int currRow = 0; rs.beforeFirst(); JourneyDAO jDao = new JourneyDAO(); while (rs.next()) { System.out.println("Caching Flight"); tempFlightRef = new FlightDetails(); tempFlightRef.setFlightNum(rs.getString("FlightNum")); System.out.println("Flight Number:" + rs.getString("FlightNum")); tempFlightRef.setAirlineName(rs.getString("AirlineName")); System.out.println("135"); tempFlightRef.setSrc(rs.getString("SRC")); System.out.println("135"); tempFlightRef.setDest(rs.getString("DEST")); System.out.println("135"); tempFlightRef.setNumOfSeats(Integer.parseInt(rs.getString("NumOfSeats"))); System.out.println("135"); tempFlightRef.setFlightDate(rs.getString("FlightDate")); System.out.println("135"); tempFlightRef.setAvailableSeats((rs.getInt("AVAILABLESEATS"))); System.out.println("135"); tempFlightRef.setJourneyInfo(journeyDAO.getJourneyInfo(tempFlightRef.getFlightNum())); tempFlightRef.setPassengerInfo( reservationDAO.getPassengerInfo(tempFlightRef.getFlightNum())); flightDetails[currRow] = tempFlightRef; currRow++; } } } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } return flightDetails; }
// Returns details of a flight based on search parameter public FlightDetails[] getFlightDetails( String airlineName, String flightNum, String src, String dest, int noOfSeats, String flightDate) { FlightDetails flightDetails[] = null; FlightDetails tempFlightRef; List<FlightDetails> list = new ArrayList<FlightDetails>(); try { conn = DBManager.getConnection(); if (conn != null) { String sqlQuery = SQLquery.searchFlightDetails; sqlQuery = "SELECT * FROM FLIGHTDETAILS "; String addnl = ""; if (!airlineName.equalsIgnoreCase("")) { addnl = "AIRLINENAME LIKE'%" + airlineName + "%' "; } if (flightNum != null && (!flightNum.equalsIgnoreCase(""))) { addnl = "AND FLIGHTNUM LIKE'%" + flightNum + "%' "; } if (!src.equalsIgnoreCase("")) { addnl = "AND SRC LIKE'%" + src + "%' "; } if (!dest.equalsIgnoreCase("")) { addnl = "AND DEST LIKE'%" + dest + "%' "; } if (noOfSeats != 0) { addnl = "AND NUMOFSEATS >= " + noOfSeats + "%' "; } if (!flightDate.equalsIgnoreCase("")) { addnl = "AND FLIGHTDATE = '" + flightDate + "%'"; } System.out.println(addnl); if (addnl.startsWith("AND ")) { addnl = addnl.replaceFirst("AND", " "); } if (!addnl.equalsIgnoreCase("")) { sqlQuery += "WHERE " + addnl; } PreparedStatement statement = conn.prepareStatement(sqlQuery); System.out.println(sqlQuery); ResultSet rs = statement.executeQuery(); JourneyDAO journeyDAO = new JourneyDAO(); ReservationDAO reservationDAO = new ReservationDAO(); if (true) // If records exist { while (rs.next()) { tempFlightRef = new FlightDetails(); tempFlightRef.setFlightNum(rs.getString("FlightNum")); System.out.println("Flight Number:" + rs.getString("FlightNum")); tempFlightRef.setAirlineName(rs.getString("AirlineName")); System.out.println("135"); tempFlightRef.setSrc(rs.getString("SRC")); System.out.println("135"); tempFlightRef.setDest(rs.getString("DEST")); System.out.println("135"); tempFlightRef.setNumOfSeats(Integer.parseInt(rs.getString("NumOfSeats"))); System.out.println("135"); tempFlightRef.setFlightDate(rs.getString("FlightDate")); System.out.println("135"); tempFlightRef.setAvailableSeats((rs.getInt("AVAILABLESEATS"))); System.out.println("135"); tempFlightRef.setJourneyInfo(journeyDAO.getJourneyInfo(tempFlightRef.getFlightNum())); tempFlightRef.setPassengerInfo( reservationDAO.getPassengerInfo(tempFlightRef.getFlightNum())); int index = 0; for (Journey journey : tempFlightRef.getJourneyInfo()) { if ((journey.getBoardingPoint().equalsIgnoreCase(tempFlightRef.getSrc())) && (journey.getDestinationPoint().equalsIgnoreCase(tempFlightRef.getDest()))) { break; } index++; } Journey temp = tempFlightRef.getJourneyInfo()[index]; tempFlightRef.getJourneyInfo()[index] = tempFlightRef.getJourneyInfo()[0]; tempFlightRef.getJourneyInfo()[0] = temp; list.add(tempFlightRef); } } } } catch (Exception e) { e.printStackTrace(); } flightDetails = new FlightDetails[list.size()]; int index = 0; for (FlightDetails fl : list) { flightDetails[index++] = fl; } System.out.println("HERE"); return flightDetails; }