ExtentIndex.java

  1. /**
  2.  * Extent Index
  3.  */
  4. package gov.usgs.earthquake.indexer;

  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.Types;
  8. import java.sql.SQLException;

  9. /**
  10.  * ExtentIndex is a type of JDBCProductIndex that can also send updates to the
  11.  * extentSummary table.
  12.  */
  13. public class ExtentIndex extends JDBCProductIndex {

  14.   /** Table for extentSummary */
  15.   public static final String EXTENT_TABLE = "extentSummary";
  16.   /** Extent index id - productSummaryIndexId */
  17.   public static final String EXTENT_INDEX_ID = "productSummaryIndexId";
  18.   /** Extent start time */
  19.   public static final String EXTENT_START_TIME = "starttime";
  20.   /** Extent end time */
  21.   public static final String EXTENT_END_TIME = "endtime";
  22.   /** Extent max latitude */
  23.   public static final String EXTENT_MAX_LAT = "maximum_latitude";
  24.   /** Extent minimum latitude */
  25.   public static final String EXTENT_MIN_LAT = "minimum_latitude";
  26.   /** Extent max longitude */
  27.   public static final String EXTENT_MAX_LONG = "maximum_longitude";
  28.   /** Extent min longitude */
  29.   public static final String EXTENT_MIN_LONG = "minimum_longitude";

  30.   /**
  31.    * Default constructor
  32.    * @throws Exception if error occurs
  33.    */
  34.   public ExtentIndex() throws Exception {
  35.     super();
  36.   }

  37.   /**
  38.    * Queries extentSummary table for the largest index id.
  39.    *
  40.    * @return long last extent index id
  41.    * @throws Exception if something goes wrong with database transaction
  42.    */
  43.   public long getLastExtentIndexId() throws Exception {
  44.     long lastIndex;

  45.     //Prepare statement
  46.     String sql = "SELECT MAX("
  47.                  + EXTENT_INDEX_ID
  48.                  + ") AS "
  49.                  + EXTENT_INDEX_ID
  50.                  + " FROM "
  51.                  + EXTENT_TABLE;
  52.     beginTransaction();
  53.     try (PreparedStatement getLastIndex = getConnection().prepareStatement(sql)) {
  54.       //Parse Results
  55.       ResultSet results = getLastIndex.executeQuery();
  56.       if (results.next()) {
  57.         lastIndex = results.getLong(EXTENT_INDEX_ID);
  58.       } else {
  59.         //No index in extentSummary table
  60.         lastIndex = 0;
  61.       }
  62.       commitTransaction();
  63.     } catch (SQLException e) {
  64.       try {
  65.         rollbackTransaction();
  66.       } catch (Exception e2) {}
  67.       //Throws exception with SQL for debugging
  68.       throw new SQLException(e.getMessage() + ". SQL query was: " + sql, e);
  69.     }
  70.     return lastIndex;
  71.   }

  72.   /**
  73.    * Inserts valid ExtentSummary products into extentSummary table
  74.    *
  75.    * @param product the product to be added
  76.    *
  77.    * @throws Exception if something goes wrong with the database transaction
  78.    */
  79.   public void addExtentSummary(ExtentSummary product) throws Exception {
  80.     //Prepare statement
  81.     String sql = "INSERT INTO " + EXTENT_TABLE +
  82.         "(" +
  83.           EXTENT_INDEX_ID + "," +
  84.           EXTENT_START_TIME + "," +
  85.           EXTENT_END_TIME + "," +
  86.           EXTENT_MIN_LAT + "," +
  87.           EXTENT_MAX_LAT + "," +
  88.           EXTENT_MIN_LONG + "," +
  89.           EXTENT_MAX_LONG +
  90.         ") VALUES (?, ?, ?, ?, ?, ?, ?)";

  91.     beginTransaction();
  92.     try (PreparedStatement addProduct = getConnection().prepareStatement(sql)) {
  93.       //Add values
  94.       addProduct.setLong(1, product.getIndexId());
  95.       if (product.getStartTime() != null) {
  96.         addProduct.setLong(2, product.getStartTime().getTime());
  97.       } else {
  98.         addProduct.setNull(2, Types.BIGINT);
  99.       }
  100.       if (product.getEndTime() != null) {
  101.         addProduct.setLong(3, product.getEndTime().getTime());
  102.       } else {
  103.         addProduct.setNull(3, Types.BIGINT);
  104.       }
  105.       if (product.getMinLatitude() != null) {
  106.         addProduct.setBigDecimal(4, product.getMinLatitude());
  107.       } else {
  108.         addProduct.setNull(4, Types.DECIMAL);
  109.       }
  110.       if (product.getMaxLatitude() != null) {
  111.         addProduct.setBigDecimal(5, product.getMaxLatitude());
  112.       } else {
  113.         addProduct.setNull(5, Types.DECIMAL);
  114.       }
  115.       if (product.getMinLongitude() != null) {
  116.         addProduct.setBigDecimal(6, product.getMinLongitude());
  117.       } else {
  118.         addProduct.setNull(6, Types.DECIMAL);
  119.       }
  120.       if (product.getMaxLongitude() != null) {
  121.         addProduct.setBigDecimal(7, product.getMaxLongitude());
  122.       } else {
  123.         addProduct.setNull(7, Types.DECIMAL);
  124.       }

  125.       //Add to extentSummary table
  126.       addProduct.executeUpdate();
  127.       addProduct.clearParameters();
  128.       commitTransaction();
  129.     } catch (Exception e) {
  130.       try {
  131.         rollbackTransaction();
  132.       } catch (Exception e2) {}
  133.       throw e;
  134.     }
  135.   }

  136. }