Class FacadeSingleton

java.lang.Object
com.prog3.ipt.Model.FacadeClasses.FacadeSingleton

public class FacadeSingleton extends Object
Thread-safe Singleton class. The instance is lazily initialized and thus needs synchronization mechanism.
  • Field Details

    • instance

      private static volatile FacadeSingleton instance
    • databaseConnection

      private static DatabaseConnectionSingleton databaseConnection
    • connection

      private static Connection connection
    • statement

      private static Statement statement
    • preparedStatement

      private static PreparedStatement preparedStatement
    • queryOutput

      private static ResultSet queryOutput
    • queryOutputDML

      private static int queryOutputDML
    • noticeObservableList

      private static javafx.collections.ObservableList<Notice> noticeObservableList
    • lineRideObservableList

      private static javafx.collections.ObservableList<RideLineFX> lineRideObservableList
    • travelDocumentObservableList

      private static javafx.collections.ObservableList<TravelDocumentFX> travelDocumentObservableList
    • RANDOM

      private static final Random RANDOM
    • ALPHABET

      private static final String ALPHABET
      See Also:
    • ITERATIONS

      private static final int ITERATIONS
      See Also:
    • KEY_LENGTH

      private static final int KEY_LENGTH
      See Also:
    • salt

      private static String salt
  • Constructor Details

    • FacadeSingleton

      private FacadeSingleton()
      FacadeSingleton constructor
  • Method Details

    • getInstance

      public static FacadeSingleton getInstance()
      Recovers the salt key from the Salt table of IPT database and returns the FacadeSingleton instance. The instance doesn't get created until the method is called for the first time.
      Returns:
      A reference to a FacadeSingleton object.
      See Also:
    • connect

      private static void connect()
      Set up a connection to IPT MySQL Database using a DatabaseConnectionSingleton object.
    • closeConnection

      public static void closeConnection()
      Close a connection previously opened
      See Also:
    • executeQuery

      private static boolean executeQuery(String queryString)
      Wrapper method that allow to connect to IPT database, initialize a Statement object and get the result set as output of the Statement object. The returning ResultSet object is put in a static ResultSet object, such as queryOutput variable.
      Parameters:
      queryString - an SQL statement to be sent to the database, typically a static SQL SELECT statement
      Returns:
      True if the given query is syntactically correct, otherwise false.
      See Also:
    • updatePreparedStatement

      private static boolean updatePreparedStatement(String queryTemplate)
      Wrapper method that allow to connect to IPT database, initialize and set a PreparedStatement object
      Parameters:
      queryTemplate - an SQL statement to be sent to the database, typically a static SQL INSERT statement
      Returns:
      True if the given query is syntactically correct, otherwise false
      See Also:
    • updateTicketStatement

      private static boolean updateTicketStatement(String queryTemplate, SingleTicket singleTicket)
      Allows to make a DML operation to the Ticket table in IPT database, SQL INSERT in this case, and get the result of the given SQL INSERT operation
      Parameters:
      queryTemplate - an SQL statement to be sent to the database, typically a static SQL INSERT statement
      singleTicket - A reference to a SingleTicket object that provides its fields as parameters to a PreparedStatement object
      Returns:
      True if the result of given query is syntactically correct and is not zero, otherwise false
      See Also:
    • updateMembershipStatement

      private static boolean updateMembershipStatement(String queryTemplate, Membership membership)
      Allows to make a DML operation to the Membership table in IPT database, SQL INSERT in this case, and get the result of the given SQL INSERT operation
      Parameters:
      queryTemplate - an SQL statement to be sent to the database, typically a static SQL INSERT statement
      membership - A reference to a Membership object that provides its fields as parameters to a PreparedStatement object
      Returns:
      True if the result of given query is syntactically correct and is not zero, otherwise false
      See Also:
    • deleteSingleTicketStatement

      private static boolean deleteSingleTicketStatement(String queryTemplate)
      Allows to make a DML operation to the Ticket table in IPT database, SQL DELETE in this case, and get the result of the given SQL DELETE operation The returning integer value is put in a static int object, such as queryOutputDML variable.
      Parameters:
      queryTemplate - An SQL statement to be sent to the database, typically a static SQL DELETE statement
      Returns:
      True if the result of given query is syntactically correct and is not zero, otherwise false
      See Also:
    • deleteMembershipStatement

      private static boolean deleteMembershipStatement(String queryTemplate)
      Allows to make a DML operation to the Membership table in IPT database, SQL DELETE in this case, and get the result of the given SQL DELETE operation The returning integer value is put in a static int object, such as queryOutputDML variable.
      Parameters:
      queryTemplate - An SQL statement to be sent to the database, typically a static SQL DELETE statement
      Returns:
      True if the result of given query is syntactically correct and is not zero, otherwise false
      See Also:
    • updateTransactionStatement

      private static boolean updateTransactionStatement(String queryTemplate, Order localOrder)
      Allows to make a DML operation to the Transaction table in IPT database, SQL INSERT in this case, and get the result of the given SQL INSERT operation The returning integer value is put in a static int object, such as queryOutputDML variable.
      Parameters:
      queryTemplate - An SQL statement to be sent to the database, typically a static SQL INSERT statement
      localOrder - A reference to a Order object that provides its fields as parameters to a PreparedStatement object
      Returns:
      True if the result of given query is syntactically correct and is not zero, otherwise false
      See Also:
    • validateGeneratedTransactionID

      private static boolean validateGeneratedTransactionID(String transactionCode)
      Validates a 5-chars transaction code generated using randomUUID method and converted to a String object. Checks if the transaction code string is not already used by a record in the Transaction table of IPT database
      Parameters:
      transactionCode - A 5-chars transaction code generated using randomUUID method and converted to a String object.
      Returns:
      True if the transaction code is not already used, so it is a valid transaction code, otherwise false
      See Also:
    • insertTransaction

      public static boolean insertTransaction()
      Inserts an order made by the logged citizen as a transaction record to the Transaction table of IPT database. This operation obviously involves the insertion of the travel documents, bought by the logged citizen, in the Ticket and Membership tables of IPT database.
      Returns:
      True on success, otherwise false
      See Also:
    • checkTicketIssueDateValidity

      public static boolean checkTicketIssueDateValidity(String ticketLineID, LocalDate ticketIssueDate)
      Validates an issue date for a SingleTicket object. Tickets cannot be bought for an inactive line.
      Parameters:
      ticketLineID - A string that represents a unique identifier of a line
      ticketIssueDate - An issue date for the ticket
      Returns:
      True if the issue date of the ticket is valid, otherwise false
      See Also:
    • validateRide

      public static boolean validateRide(String ticketLineID, String ticketRideID)
      Checks if the ticket that a citizen is going to buy is a ticket for a valid line and ride.
      Parameters:
      ticketLineID - A unique identifier of the line to validate
      ticketRideID - A unique identifier of the ride to validate
      Returns:
      True if the line and the ride are valid, otherwise false
      See Also:
    • getMyTicketsViewContent

      public static javafx.collections.ObservableList<TravelDocumentFX> getMyTicketsViewContent()
      Returns the tickets bought by the logged citizen that are displayed in the MyTickets view
      Returns:
      An ObservableList used for displaying items in a TableView JavaFX object
      See Also:
    • deleteMySingleTicket

      public static boolean deleteMySingleTicket(TravelDocumentFX localSingleTicket)
      Deletes a ticket bought by the logged citizen from the Ticket table of IPT database
      Parameters:
      localSingleTicket - A reference to a TravelDocumentFX object, representing a ticket
      Returns:
      True on success, otherwise false
      See Also:
    • getMyMembershipsViewContent

      public static javafx.collections.ObservableList<TravelDocumentFX> getMyMembershipsViewContent()
      Returns the memberships bought by the logged citizen that are displayed in the MyMemberships view
      Returns:
      An ObservableList used for displaying items in a TableView JavaFX object
      See Also:
    • deleteMyMembership

      public static boolean deleteMyMembership(TravelDocumentFX localMembership)
      Deletes a membership bought by the logged citizen from the Ticket table of IPT database
      Parameters:
      localMembership - A reference to a TravelDocumentFX object, representing a membership
      Returns:
      True on success, otherwise false
      See Also:
    • getNoticesViewContent

      public static javafx.collections.ObservableList<Notice> getNoticesViewContent()
      Returns the notices issued by IPT that are displayed in the Notices view
      Returns:
      An ObservableList used for displaying items in a TableView JavaFX object
      See Also:
    • getCorsaLineaViewContent

      public static javafx.collections.ObservableList<RideLineFX> getCorsaLineaViewContent()
      Returns information about the IPT lines and rides that are displayed in Info view
      Returns:
      An ObservableList used for displaying items in a TableView JavaFX object
      See Also:
    • insertCitizenQuerySender

      private static boolean insertCitizenQuerySender(String queryTemplate, Citizen newCitizen)
      Returns the result of an SQL INSERT operation in the Citizen table of IPT database
      Parameters:
      queryTemplate - An SQL statement to be sent to the database, typically a static SQL INSERT statement
      newCitizen - A reference to a Citizen object that provides its fields to a PreparedStatement object
      Returns:
      True if the given query is syntactically correct and the result is not zero, otherwise false
      See Also:
    • updateCitizenQuerySender

      private static boolean updateCitizenQuerySender(String queryTemplate, Citizen newCitizen)
      Updates logged citizen's data in the Citizen table of IPT database
      Parameters:
      queryTemplate - An SQL statement to be sent to the database, typically a static SQL UPDATE statement
      newCitizen - A reference to a Citizen object, typically an IPT user
      Returns:
      See Also:
    • retrieveCitizen

      public static Citizen retrieveCitizen(String citizenUsername, String citizenPassword)
      Retrieves data about citizen signed in with a given username and a given password
      Parameters:
      citizenUsername - A string that represents the username of a Citizen
      citizenPassword - A string that represents the password of a Citizen
      Returns:
      A reference to a Citizen object if the given username and the given password are valid, otherwise null
      See Also:
    • insertCitizen

      public static boolean insertCitizen(Citizen localCitizen)
      Inserts a Citizen record in the Citizen table of IPT database
      Parameters:
      localCitizen - A reference to a Citizen object, typically a new user
      Returns:
      True on success, otherwise false
      See Also:
    • validateGeneratedCitizenID

      public static boolean validateGeneratedCitizenID(Citizen localCitizen)
      Validates a 5-chars citizen unique identifier generated using randomUUID method and converted to a String object. Checks if the citizen unique identifier string is not already used by a record in the Citizen table of IPT database
      Parameters:
      localCitizen - A 5-chars citizen unique identifier generated using randomUUID method and converted to a String object.
      Returns:
      True if the citizen unique identifier is not already used, so it is a valid citizen unique identifier, otherwise false
      See Also:
    • updateCitizenData

      public static boolean updateCitizenData(Citizen localCitizen)
      Updates logged citizen's data in the Citizen table of IPT database
      Returns:
      True on success, otherwise false
      See Also:
    • validateGeneratedSingleTicketID

      public static boolean validateGeneratedSingleTicketID(String singleTicketID)
      Validates a 5-chars ticket unique identifier generated using randomUUID method and converted to a String object. Checks if the ticket unique identifier string is not already used by a record in the Ticket table of IPT database
      Parameters:
      singleTicketID - A 5-chars ticket unique identifier generated using randomUUID method and converted to a String object.
      Returns:
      True if the ticket unique identifier is not already used, so it is a valid ticket unique identifier, otherwise false
      See Also:
    • validateGeneratedMembershipID

      public static boolean validateGeneratedMembershipID(String membershipID)
      Validates a 5-chars membership unique identifier generated using randomUUID method and converted to a String object. Checks if the membership unique identifier string is not already used by a record in the Ticket table of IPT database
      Parameters:
      membershipID - A 5-chars membership unique identifier generated using randomUUID method and converted to a String object.
      Returns:
      True if the membership unique identifier is not already used, so it is a valid membership unique identifier, otherwise false
      See Also:
    • getSaltFromDB

      private static boolean getSaltFromDB()
      Return the salt key stored into the salt field in the Salt table of IPT database.
      Returns:
      True on success, otherwise false
      See Also:
    • getSalt

      private static String getSalt(int length)
      Builds the salt key as string
      Parameters:
      length - The length of the salt key as integer value
      Returns:
      A string that represents a new salt key string
    • hash

      private static byte[] hash(char[] password, byte[] salt)
      Hash function to generete a secret key
      Parameters:
      password - An array of characters representing a password string
      salt - An array of bytes representing the salt key
      Returns:
      A secret key as an array of bytes
      See Also:
    • generateSecurePassword

      private static String generateSecurePassword(String password)
      Generates a secure password stored in the password field in the Citizen table of IPT database
      Parameters:
      password - A string that represents a user password
      Returns:
      A coded string for the given password
      See Also:
    • verifyUserPassword

      private static boolean verifyUserPassword(String providedPassword, String securedPassword)
      Verifies the given user password is valid
      Parameters:
      providedPassword - A string that represents a user password
      securedPassword - A string that represents a secured password stored in the password filed of a Citizen record in the Citizen table of IPT database
      Returns:
      True if the given user password is valid, otherwise false
      See Also: