Class JDBCProductIndex

All Implemented Interfaces:
ProductIndex, Configurable, AutoCloseable
Direct Known Subclasses:
ExtentIndex

public class JDBCProductIndex extends JDBCConnection implements ProductIndex
JDBC Implementation of ProductIndex.
  • Field Details

    • JDBC_DEFAULT_FILE

      public static final String JDBC_DEFAULT_FILE
      Default index file. Created by copying JDBC_DEFAULT_INDEX out of Jar if doesn't already exist in file system
      See Also:
    • SUMMARY_PRODUCT_INDEX_ID

      public static final String SUMMARY_PRODUCT_INDEX_ID
      Public var for summary product index IDs
      See Also:
  • Constructor Details

    • JDBCProductIndex

      public JDBCProductIndex() throws Exception
      Constructor. Sets index_file to the default value JDBC_DEFAULT_FILE
      Throws:
      Exception - if error occurs
    • JDBCProductIndex

      public JDBCProductIndex(String sqliteFileName) throws Exception
      Constructor. Uses custom index_file
      Parameters:
      sqliteFileName - String for sqlite file name
      Throws:
      Exception - if error occurs
  • Method Details

    • configure

      public void configure(Config config) throws Exception
      Grab values from the Config object and put them into private variables.
      Specified by:
      configure in interface Configurable
      Overrides:
      configure in class JDBCConnection
      Parameters:
      config - Configuration for the product index
      Throws:
      Exception - Exception
    • connect

      public Connection connect() throws Exception
      Return a connection to the database.
      Overrides:
      connect in class JDBCConnection
      Returns:
      Connection object
      Throws:
      Exception - if error occurs
    • getEvents

      public List<Event> getEvents(ProductIndexQuery query) throws Exception
      Return all events from the database that meet the parameters specified in the ProductIndexQuery object.
      Specified by:
      getEvents in interface ProductIndex
      Parameters:
      query - A description of which events to retrieve.
      Returns:
      List of Event objects
      Throws:
      Exception - if error occurs
    • addEvent

      public Event addEvent(Event event) throws Exception
      Add an event to the database
      Specified by:
      addEvent in interface ProductIndex
      Parameters:
      event - Event to store
      Returns:
      Event object with eventId set to the database id
      Throws:
      Exception - if error occurs
    • removeEvent

      public List<ProductId> removeEvent(Event event) throws Exception
      Delete an event from the database.
      Specified by:
      removeEvent in interface ProductIndex
      Parameters:
      event - Event to remove
      Returns:
      List containing all the ProductIds that were deleted by the method call
      Throws:
      Exception - if the event is associated to products.
    • getUnassociatedProducts

      public List<ProductSummary> getUnassociatedProducts(ProductIndexQuery query) throws Exception
      Return all products that aren't associated with an event.
      Specified by:
      getUnassociatedProducts in interface ProductIndex
      Parameters:
      query - ProductIndexQuery used to further limit the results
      Returns:
      List of unassociated Products
      Throws:
      IllegalArgumentException - when query event search type is SEARCH_EVENT_PREFERRED.
      Exception - if error occurs
    • getProducts

      public List<ProductSummary> getProducts(ProductIndexQuery query) throws Exception
      Return all products that meet the parameters specified in the ProductIndexQuery object.
      Specified by:
      getProducts in interface ProductIndex
      Parameters:
      query - A description of which products to retrieve.
      Returns:
      List of ProductSummary objects
      Throws:
      IllegalArgumentException - when query event search type is SEARCH_EVENT_PREFERRED.
      Exception - if error occurs
    • getProducts

      public List<ProductSummary> getProducts(ProductIndexQuery query, boolean loadDetails) throws Exception
      Load product summaries.
      Parameters:
      query - product query
      loadDetails - whether to call loadProductSummaries(List), which loads links and properties with additional queries.
      Returns:
      A list of loaded product summaries
      Throws:
      Exception - if error occurs
    • hasProduct

      public boolean hasProduct(ProductId id) throws Exception
      Check whether product summary is in index.
      Specified by:
      hasProduct in interface ProductIndex
      Parameters:
      id - product to search.
      Returns:
      boolean if index has product
      Throws:
      Exception - if error occurs
    • addProductSummary

      public ProductSummary addProductSummary(ProductSummary summary) throws Exception
      Add a product summary to the database
      Specified by:
      addProductSummary in interface ProductIndex
      Parameters:
      summary - ProductSummary object to store. Must not be null.
      Returns:
      Copy of the product summary object with the indexId set to the newly inserted id.
      Throws:
      Exception - if error occurs
    • removeProductSummary

      public ProductId removeProductSummary(ProductSummary summary) throws Exception
      Delete a product summary from the database If the summary doesn't have an indexId value set, throw an exception
      Specified by:
      removeProductSummary in interface ProductIndex
      Parameters:
      summary - ProductSummary object to delete
      Returns:
      id of removed summary.
      Throws:
      Exception - if error occurs
    • addAssociation

      public Event addAssociation(Event event, ProductSummary summary) throws Exception
      Create an association between the given event and product summary. This assumes that both the event and the product are already stored in their respective tables.
      Specified by:
      addAssociation in interface ProductIndex
      Parameters:
      event - Event to add association to
      summary - ProductSummary to add association to
      Returns:
      Copy of event with summary added to the event's products list
      Throws:
      Exception - if error occurs
    • removeAssociation

      public Event removeAssociation(Event event, ProductSummary summary) throws Exception
      Delete the association, if it exists, between the given event and product summary. NOTE: this removes the association between the event and ALL versions of the product summary.
      Specified by:
      removeAssociation in interface ProductIndex
      Parameters:
      event - An event to remove an association with
      summary - A ProductSummary to remove an association with
      Returns:
      Copy of event with summary removed from the products list
      Throws:
      Exception - if error occurs
    • buildProductClauses

      protected List<String> buildProductClauses(ProductIndexQuery query)
      Build a list of all the pieces of the WHERE clause relevant to the productSummary table. If the query doesn't set any properties, this method will return an empty list. It is up to the calling methods to check if the clause list is empty when they build their WHERE clause.
      Parameters:
      query - ProductIndexQuery
      Returns:
      list containing clauses in the form: column="value"
    • buildProductQuery

      protected String buildProductQuery(ProductIndexQuery query)
      Create the full SELECT query for the products table using the default clauseList.
      Parameters:
      query - Query to build.
      Returns:
      String containing the full SELECT query
      See Also:
    • buildProductQuery

      protected String buildProductQuery(ProductIndexQuery query, List<String> clauseList)
      Create the full SELECT query for the products table using a custom clauseList.
      Parameters:
      query - Query to build.
      clauseList - List of clauses for WHERE
      Returns:
      String containing the full SELECT query
    • loadProductSummaries

      protected void loadProductSummaries(List<ProductSummary> summaries) throws Exception
      Populate links and properties for provided product summaries.
      Parameters:
      summaries - List of ProductSummaries
      Throws:
      Exception - if error occurs
    • parseProductSummary

      protected ProductSummary parseProductSummary(ResultSet results) throws Exception
      Parse ProductSummary without loading links or properties.
      Parameters:
      results - ResultSet to parse
      Returns:
      ProductSummary object without links or properties.
      Throws:
      Exception - if error occurs
    • removeProductSummaries

      public List<ProductId> removeProductSummaries(List<ProductSummary> summaries) throws Exception
      Parameters:
      summaries - List of product summaries to remove
      Returns:
      List of ProductIds that were removed
      Throws:
      Exception - if error occurs
    • addProductProperties

      protected void addProductProperties(long productId, Map<String,String> properties) throws SQLException
      Save the properties in the database and associate them to the given productId
      Parameters:
      productId - long product ID to associate to
      properties - Map of properties to save
      Throws:
      SQLException - if SQL error occurs
    • addProductLinks

      protected void addProductLinks(long productId, Map<String,List<URI>> links) throws SQLException
      Save the links in the database and associate them to the given productId
      Parameters:
      productId - Index id of the product to select
      links - Map of relations to URIs
      Throws:
      SQLException - if sql error occurs
    • normalizeLongitude

      public static double normalizeLongitude(double lon)
      Convert the given longitude to be between -180 and 180. If the given value is already in the range, this method just returns the value.
      Parameters:
      lon - Double longitude
      Returns:
      double normalized between -180 and 180
    • normalizeLongitude

      public static BigDecimal normalizeLongitude(BigDecimal lon)
      Wrapper to normalize BigDecimal longitudes
      Parameters:
      lon - BigDecimal Longitude
      Returns:
      Normalized BigDecimal latitude
    • eventsUpdated

      public void eventsUpdated(List<Event> events) throws Exception
      Called when the indexer is done updating events after a product is processed. Stores the preferred attributes for each event in the list
      Specified by:
      eventsUpdated in interface ProductIndex
      Parameters:
      events - the events that have been updated.
      Throws:
      Exception - if error occurs