ProductIndexQuery.java

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

  5. import gov.usgs.earthquake.product.ProductId;

  6. import java.math.BigDecimal;
  7. import java.util.Date;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;

  13. /**
  14.  * Criteria for finding events.
  15.  *
  16.  * All properties are inclusive. When a property is null, it means any value.
  17.  *
  18.  * Expected combinations:
  19.  *
  20.  * 1) find events based on event parameters event time event latitude event
  21.  * longitude
  22.  *
  23.  * 2) find previously received update of product product source product type
  24.  * product code
  25.  *
  26.  * 3) find related products/events product ids
  27.  *
  28.  * 4) find related products/events event ids
  29.  */
  30. public class ProductIndexQuery implements Comparable<ProductIndexQuery> {

  31.   /**
  32.    * Event search types determine whether to search only preferred event
  33.    * attributes (faster), or all event attributes from any associated product
  34.    * (more complete).
  35.    */
  36.   private static enum EventSearchTypes {
  37.     /**
  38.      * Search preferred event attributes.
  39.      *
  40.      * NOTE: SEARCH_EVENT_PREFERRED should ONLY be used on event queries. Using this
  41.      * on product queries will more than likely break.
  42.      */
  43.     SEARCH_EVENT_PREFERRED,

  44.     /** Search event product attributes. */
  45.     SEARCH_EVENT_PRODUCTS
  46.   };

  47.   /**
  48.    * Result types determine which products associated to an event are returned.
  49.    */
  50.   private static enum ResultTypes {
  51.     /** Only include current versions of products in the result. */
  52.     RESULT_TYPE_CURRENT,

  53.     /** Only include superseded (old) versions of products in the result. */

  54.     RESULT_TYPE_SUPERSEDED,

  55.     /**
  56.      * Include both current and superseded versions of products in the result.
  57.      */
  58.     RESULT_TYPE_ALL;
  59.   };

  60.   /** EventSearchType for SEARCH_EVENT_PREFERRED */
  61.   public static EventSearchTypes SEARCH_EVENT_PREFERRED = EventSearchTypes.SEARCH_EVENT_PREFERRED;
  62.   /** EventSearchType for SEARCH_EVENT_PRODCUTS */
  63.   public static EventSearchTypes SEARCH_EVENT_PRODUCTS = EventSearchTypes.SEARCH_EVENT_PRODUCTS;

  64.   /** ResultType for RESULT_TYPE_CURRENT */
  65.   public static ResultTypes RESULT_TYPE_CURRENT = ResultTypes.RESULT_TYPE_CURRENT;
  66.   /** ResultType for RESULT_TYPE_SUPERSEDED */
  67.   public static ResultTypes RESULT_TYPE_SUPERSEDED = ResultTypes.RESULT_TYPE_SUPERSEDED;
  68.   /** ResultType for RESULT_TYPE_ALL */
  69.   public static ResultTypes RESULT_TYPE_ALL = ResultTypes.RESULT_TYPE_ALL;

  70.   /** Search preferred or all event attributes? */
  71.   private EventSearchTypes eventSearchType = SEARCH_EVENT_PRODUCTS;

  72.   /** Include previous versions? */
  73.   private ResultTypes resultType = RESULT_TYPE_CURRENT;

  74.   /** Event source */
  75.   private String eventSource;

  76.   /** Event source code */
  77.   private String eventSourceCode;

  78.   /** Minimum event time, inclusive. */
  79.   private Date minEventTime;

  80.   /** Maximum event time, inclusive. */
  81.   private Date maxEventTime;

  82.   /** Minimum event latitude. */
  83.   private BigDecimal minEventLatitude;

  84.   /** Maximum event latitude. */
  85.   private BigDecimal maxEventLatitude;

  86.   /** Minimum event longitude. */
  87.   private BigDecimal minEventLongitude;

  88.   /** Maximum event longitude. */
  89.   private BigDecimal maxEventLongitude;

  90.   /** Minimum event depth. */
  91.   private BigDecimal minEventDepth;

  92.   /** Maximum event depth. */
  93.   private BigDecimal maxEventDepth;

  94.   /** Minimum event magnitude. */
  95.   private BigDecimal minEventMagnitude;

  96.   /** Maximum event magnitude. */
  97.   private BigDecimal maxEventMagnitude;

  98.   /** A list of product ids to search. */
  99.   private List<ProductId> productIds = new LinkedList<ProductId>();

  100.   /** Minimum product update time. */
  101.   private Date minProductUpdateTime;

  102.   /** Maximum product update time. */
  103.   private Date maxProductUpdateTime;

  104.   /** The product source. */
  105.   private String productSource;

  106.   /** The product type. */
  107.   private String productType;

  108.   /** The product code. */
  109.   private String productCode;

  110.   /** The product version */
  111.   private String productVersion;

  112.   /** The product status */
  113.   private String productStatus;

  114.   /** The product index ID; unique per productIndex */
  115.   private Long minProductIndexId;

  116.   /** The max number of results */
  117.   private Integer limit;

  118.   /** List of columns to order by */
  119.   private String orderBy;

  120.   /**
  121.    * Construct a new ProductIndexQuery.
  122.    */
  123.   public ProductIndexQuery() {
  124.   }

  125.   /** @param eventSearchType to set */
  126.   public void setEventSearchType(EventSearchTypes eventSearchType) {
  127.     this.eventSearchType = eventSearchType;
  128.   }

  129.   /** @return eventSearchType */
  130.   public EventSearchTypes getEventSearchType() {
  131.     return eventSearchType;
  132.   }

  133.   /** @param resultType to set */
  134.   public void setResultType(ResultTypes resultType) {
  135.     this.resultType = resultType;
  136.   }

  137.   /** @return resultType */
  138.   public ResultTypes getResultType() {
  139.     return resultType;
  140.   }

  141.   /** @param eventSource to set */
  142.   public void setEventSource(String eventSource) {
  143.     this.eventSource = (eventSource == null ? null : eventSource.toLowerCase());
  144.   }

  145.   /** @return eventSource */
  146.   public String getEventSource() {
  147.     return eventSource;
  148.   }

  149.   /** @param eventSourceCode to set */
  150.   public void setEventSourceCode(String eventSourceCode) {
  151.     this.eventSourceCode = (eventSourceCode == null ? null : eventSourceCode.toLowerCase());
  152.   }

  153.   /** @return eventSourceCode */
  154.   public String getEventSourceCode() {
  155.     return eventSourceCode;
  156.   }

  157.   /** @return minEventTime */
  158.   public Date getMinEventTime() {
  159.     return minEventTime;
  160.   }

  161.   /** @param minEventTime to set */
  162.   public void setMinEventTime(Date minEventTime) {
  163.     this.minEventTime = minEventTime;
  164.   }

  165.   /** @return maxEventTime */
  166.   public Date getMaxEventTime() {
  167.     return maxEventTime;
  168.   }

  169.   /** @param maxEventTime to set */
  170.   public void setMaxEventTime(Date maxEventTime) {
  171.     this.maxEventTime = maxEventTime;
  172.   }

  173.   /** @return minEventLatitude */
  174.   public BigDecimal getMinEventLatitude() {
  175.     return minEventLatitude;
  176.   }

  177.   /** @param minEventLatitude to set */
  178.   public void setMinEventLatitude(BigDecimal minEventLatitude) {
  179.     this.minEventLatitude = minEventLatitude;
  180.   }

  181.   /** @return maxEventLatitude */
  182.   public BigDecimal getMaxEventLatitude() {
  183.     return maxEventLatitude;
  184.   }

  185.   /** @param maxEventLatitude to set */
  186.   public void setMaxEventLatitude(BigDecimal maxEventLatitude) {
  187.     this.maxEventLatitude = maxEventLatitude;
  188.   }

  189.   /** @return minEventLongitude */
  190.   public BigDecimal getMinEventLongitude() {
  191.     return minEventLongitude;
  192.   }

  193.   /** @param minEventLongitude to set */
  194.   public void setMinEventLongitude(BigDecimal minEventLongitude) {
  195.     this.minEventLongitude = minEventLongitude;
  196.   }

  197.   /** @return maxEventLongitude */
  198.   public BigDecimal getMaxEventLongitude() {
  199.     return maxEventLongitude;
  200.   }

  201.   /** @param maxEventLongitude to set */
  202.   public void setMaxEventLongitude(BigDecimal maxEventLongitude) {
  203.     this.maxEventLongitude = maxEventLongitude;
  204.   }

  205.   /** @return minEventDepth */
  206.   public BigDecimal getMinEventDepth() {
  207.     return minEventDepth;
  208.   }

  209.   /** @param minEventDepth to set */
  210.   public void setMinEventDepth(BigDecimal minEventDepth) {
  211.     this.minEventDepth = minEventDepth;
  212.   }

  213.   /** @return maxEventDepth */
  214.   public BigDecimal getMaxEventDepth() {
  215.     return maxEventDepth;
  216.   }

  217.   /** @param maxEventDepth to set */
  218.   public void setMaxEventDepth(BigDecimal maxEventDepth) {
  219.     this.maxEventDepth = maxEventDepth;
  220.   }

  221.   /** @return minEventMagnitude */
  222.   public BigDecimal getMinEventMagnitude() {
  223.     return minEventMagnitude;
  224.   }

  225.   /** @param minEventMagnitude to set */
  226.   public void setMinEventMagnitude(BigDecimal minEventMagnitude) {
  227.     this.minEventMagnitude = minEventMagnitude;
  228.   }

  229.   /** @return maxEventMagnitude */
  230.   public BigDecimal getMaxEventMagnitude() {
  231.     return maxEventMagnitude;
  232.   }

  233.   /** @param maxEventMagnitude to set */
  234.   public void setMaxEventMagnitude(BigDecimal maxEventMagnitude) {
  235.     this.maxEventMagnitude = maxEventMagnitude;
  236.   }

  237.   /** @return list of product Ids */
  238.   public List<ProductId> getProductIds() {
  239.     return productIds;
  240.   }

  241.   /** @param productIds list to set */
  242.   public void setProductIds(List<ProductId> productIds) {
  243.     this.productIds.clear();
  244.     this.productIds.addAll(productIds);
  245.   }

  246.   /** @return minProductUpdateTime */
  247.   public Date getMinProductUpdateTime() {
  248.     return minProductUpdateTime;
  249.   }

  250.   /** @param minProductUpdateTime to set */
  251.   public void setMinProductUpdateTime(Date minProductUpdateTime) {
  252.     this.minProductUpdateTime = minProductUpdateTime;
  253.   }

  254.   /** @return maxProductUpdateTime */
  255.   public Date getMaxProductUpdateTime() {
  256.     return maxProductUpdateTime;
  257.   }

  258.   /** @param maxProductUpdateTime to set */
  259.   public void setMaxProductUpdateTime(Date maxProductUpdateTime) {
  260.     this.maxProductUpdateTime = maxProductUpdateTime;
  261.   }

  262.   /** @return productSource */
  263.   public String getProductSource() {
  264.     return productSource;
  265.   }

  266.   /** @param productSource to set */
  267.   public void setProductSource(String productSource) {
  268.     this.productSource = productSource;
  269.   }

  270.   /** @return productType */
  271.   public String getProductType() {
  272.     return productType;
  273.   }

  274.   /** @param productType to set */
  275.   public void setProductType(String productType) {
  276.     this.productType = productType;
  277.   }

  278.   /** @return productCode */
  279.   public String getProductCode() {
  280.     return productCode;
  281.   }

  282.   /** @param productCode to set */
  283.   public void setProductCode(String productCode) {
  284.     this.productCode = productCode;
  285.   }

  286.   /** @param productVersion to set */
  287.   public void setProductVersion(String productVersion) {
  288.     this.productVersion = productVersion;
  289.   }

  290.   /** @return productVersion */
  291.   public String getProductVersion() {
  292.     return productVersion;
  293.   }

  294.   /** @param productStatus to set */
  295.   public void setProductStatus(String productStatus) {
  296.     this.productStatus = productStatus;
  297.   }

  298.   /** @return productStatus */
  299.   public String getProductStatus() {
  300.     return productStatus;
  301.   }

  302.   /** @param minProductIndexId to set */
  303.   public void setMinProductIndexId(final Long minProductIndexId) {
  304.     this.minProductIndexId = minProductIndexId;
  305.   }

  306.   /** @return minProductIndexId */
  307.   public Long getMinProductIndexId() {
  308.     return this.minProductIndexId;
  309.   }

  310.   /** @param limit to set */
  311.   public void setLimit(final Integer limit) {
  312.     this.limit = limit;
  313.   }

  314.   /** @return limit */
  315.   public Integer getLimit() {
  316.     return this.limit;
  317.   }

  318.   /** @param orderBy to set */
  319.   public void setOrderBy(final String orderBy) {
  320.     this.orderBy = orderBy;
  321.   }

  322.   /** @return orderBy */
  323.   public String getOrderBy() {
  324.     return this.orderBy;
  325.   }

  326.   @Override
  327.   public boolean equals(Object that) {
  328.     return (this.compareTo((ProductIndexQuery) that)) == 0;
  329.   }

  330.   @Override
  331.   public int compareTo(ProductIndexQuery that) {
  332.     int r = 0;

  333.     if ((r = compare(this.eventSource, that.eventSource)) != 0) {
  334.       return r;
  335.     }
  336.     if ((r = compare(this.eventSourceCode, that.eventSourceCode)) != 0) {
  337.       return r;
  338.     }
  339.     if ((r = compare(this.maxEventDepth, that.maxEventDepth)) != 0) {
  340.       return r;
  341.     }
  342.     if ((r = compare(this.maxEventLatitude, that.maxEventLatitude)) != 0) {
  343.       return r;
  344.     }
  345.     if ((r = compare(this.maxEventLongitude, that.maxEventLongitude)) != 0) {
  346.       return r;
  347.     }
  348.     if ((r = compare(this.maxEventMagnitude, that.maxEventMagnitude)) != 0) {
  349.       return r;
  350.     }
  351.     if ((r = compare(this.maxEventTime, that.maxEventTime)) != 0) {
  352.       return r;
  353.     }
  354.     if ((r = compare(this.maxProductUpdateTime, that.maxProductUpdateTime)) != 0) {
  355.       return r;
  356.     }

  357.     if ((r = compare(this.minEventDepth, that.minEventDepth)) != 0) {
  358.       return r;
  359.     }
  360.     if ((r = compare(this.minEventLatitude, that.minEventLatitude)) != 0) {
  361.       return r;
  362.     }
  363.     if ((r = compare(this.minEventLongitude, that.minEventLongitude)) != 0) {
  364.       return r;
  365.     }
  366.     if ((r = compare(this.minEventMagnitude, that.minEventMagnitude)) != 0) {
  367.       return r;
  368.     }
  369.     if ((r = compare(this.minEventTime, that.minEventTime)) != 0) {
  370.       return r;
  371.     }
  372.     if ((r = compare(this.minProductUpdateTime, that.minProductUpdateTime)) != 0) {
  373.       return r;
  374.     }

  375.     if ((r = compare(this.productCode, that.productCode)) != 0) {
  376.       return r;
  377.     }
  378.     if ((r = compare(this.productSource, that.productSource)) != 0) {
  379.       return r;
  380.     }
  381.     if ((r = compare(this.productStatus, that.productStatus)) != 0) {
  382.       return r;
  383.     }
  384.     if ((r = compare(this.productType, that.productType)) != 0) {
  385.       return r;
  386.     }
  387.     if ((r = compare(this.productVersion, that.productVersion)) != 0) {
  388.       return r;
  389.     }

  390.     if ((r = (that.productIds.size() - this.productIds.size())) != 0) {
  391.       // different size lists
  392.       return r;
  393.     } else {
  394.       // lists are same size, check contents
  395.       Iterator<ProductId> thisIter = this.productIds.iterator();
  396.       Iterator<ProductId> thatIter = that.productIds.iterator();
  397.       while (thisIter.hasNext() && thatIter.hasNext()) {
  398.         r = thisIter.next().compareTo(thatIter.next());
  399.         if (r != 0) {
  400.           return r;
  401.         }
  402.       }
  403.     }

  404.     return 0;
  405.   }

  406.   /**
  407.    * Compare function
  408.    *
  409.    * @param <T> Type
  410.    * @param o1  First item to compare
  411.    * @param o2  Second to comoare
  412.    * @return 0 if equal, 1 if o1 is null, -1 if o2 null, or the comparison
  413.    */
  414.   protected <T extends Comparable<T>> int compare(T o1, T o2) {
  415.     if (o1 == null && o2 == null) {
  416.       return 0;
  417.     } else if (o1 == null && o2 != null) {
  418.       return 1;
  419.     } else if (o1 != null && o2 == null) {
  420.       return -1;
  421.     } else {
  422.       return o1.compareTo(o2);
  423.     }
  424.   }

  425.   /**
  426.    * Log function
  427.    *
  428.    * @param logger logger object
  429.    */
  430.   public void log(final Logger logger) {
  431.     if (!logger.isLoggable(Level.FINEST)) {
  432.       return;
  433.     }
  434.     StringBuffer buf = new StringBuffer("Product Index Query");
  435.     buf.append("\neventSearchType=").append(this.eventSearchType);
  436.     buf.append("\nresultType=").append(this.resultType);
  437.     if (this.eventSource != null) {
  438.       buf.append("\neventSource=").append(this.eventSource);
  439.     }
  440.     if (this.eventSourceCode != null) {
  441.       buf.append("\neventSourceCode=").append(this.eventSourceCode);
  442.     }
  443.     if (this.minEventTime != null) {
  444.       buf.append("\nminEventTime=").append(this.minEventTime);
  445.     }
  446.     if (this.maxEventTime != null) {
  447.       buf.append("\nmaxEventTime=").append(this.maxEventTime);
  448.     }
  449.     if (this.minEventLatitude != null) {
  450.       buf.append("\nminEventLatitude=").append(this.minEventLatitude);
  451.     }
  452.     if (this.maxEventLatitude != null) {
  453.       buf.append("\nmaxEventLatitude=").append(this.maxEventLatitude);
  454.     }
  455.     if (this.minEventLongitude != null) {
  456.       buf.append("\nminEventLongitude=").append(this.minEventLongitude);
  457.     }
  458.     if (this.maxEventLongitude != null) {
  459.       buf.append("\nmaxEventLongitude=").append(this.maxEventLongitude);
  460.     }
  461.     if (this.minEventDepth != null) {
  462.       buf.append("\nminEventDepth=").append(this.minEventDepth);
  463.     }
  464.     if (this.maxEventDepth != null) {
  465.       buf.append("\nmaxEventDepth=").append(this.maxEventDepth);
  466.     }
  467.     if (this.minEventMagnitude != null) {
  468.       buf.append("\nminEventMagnitude=").append(this.minEventMagnitude);
  469.     }
  470.     if (this.maxEventMagnitude != null) {
  471.       buf.append("\nmaxEventMagnitude=").append(this.maxEventMagnitude);
  472.     }
  473.     if (this.productIds.size() > 0) {
  474.       buf.append("\nproduct ids=");
  475.       Iterator<ProductId> iter = this.productIds.iterator();
  476.       while (iter.hasNext()) {
  477.         buf.append(iter.next().toString()).append(" ");
  478.       }
  479.     }
  480.     if (this.minProductUpdateTime != null) {
  481.       buf.append("\nminProductUpdateTime=").append(this.minProductUpdateTime);
  482.     }
  483.     if (this.maxProductUpdateTime != null) {
  484.       buf.append("\nmaxProductUpdateTime=").append(this.maxProductUpdateTime);
  485.     }
  486.     if (this.productSource != null) {
  487.       buf.append("\nproductSource=").append(this.productSource);
  488.     }
  489.     if (this.productType != null) {
  490.       buf.append("\nproductType=").append(this.productType);
  491.     }
  492.     if (this.productCode != null) {
  493.       buf.append("\nproductCode=").append(this.productCode);
  494.     }
  495.     if (this.productVersion != null) {
  496.       buf.append("\nproductVersion=").append(this.productVersion);
  497.     }
  498.     if (this.productStatus != null) {
  499.       buf.append("\nproductStatus=").append(this.productStatus);
  500.     }

  501.     logger.finest(buf.toString());
  502.   }
  503. }