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.
  41.          * Using this 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
  49.      * returned.
  50.      */
  51.     private static enum ResultTypes {
  52.         /** Only include current versions of products in the result. */
  53.         RESULT_TYPE_CURRENT,

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

  55.         RESULT_TYPE_SUPERSEDED,

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

  62.     /** EventSearchType for SEARCH_EVENT_PREFERRED */
  63.     public static EventSearchTypes SEARCH_EVENT_PREFERRED = EventSearchTypes.SEARCH_EVENT_PREFERRED;
  64.     /** EventSearchType for SEARCH_EVENT_PRODCUTS */
  65.     public static EventSearchTypes SEARCH_EVENT_PRODUCTS = EventSearchTypes.SEARCH_EVENT_PRODUCTS;

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

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

  74.     /** Include previous versions? */
  75.     private ResultTypes resultType = RESULT_TYPE_CURRENT;

  76.     /** Event source */
  77.     private String eventSource;

  78.     /** Event source code */
  79.     private String eventSourceCode;

  80.     /** Minimum event time, inclusive. */
  81.     private Date minEventTime;

  82.     /** Maximum event time, inclusive. */
  83.     private Date maxEventTime;

  84.     /** Minimum event latitude. */
  85.     private BigDecimal minEventLatitude;

  86.     /** Maximum event latitude. */
  87.     private BigDecimal maxEventLatitude;

  88.     /** Minimum event longitude. */
  89.     private BigDecimal minEventLongitude;

  90.     /** Maximum event longitude. */
  91.     private BigDecimal maxEventLongitude;

  92.     /** Minimum event depth. */
  93.     private BigDecimal minEventDepth;

  94.     /** Maximum event depth. */
  95.     private BigDecimal maxEventDepth;

  96.     /** Minimum event magnitude. */
  97.     private BigDecimal minEventMagnitude;

  98.     /** Maximum event magnitude. */
  99.     private BigDecimal maxEventMagnitude;

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

  102.     /** Minimum product update time. */
  103.     private Date minProductUpdateTime;

  104.     /** Maximum product update time. */
  105.     private Date maxProductUpdateTime;

  106.     /** The product source. */
  107.     private String productSource;

  108.     /** The product type. */
  109.     private String productType;

  110.     /** The product code. */
  111.     private String productCode;

  112.     /** The product version */
  113.     private String productVersion;

  114.     /** The product status */
  115.     private String productStatus;

  116.     /** The product index ID; unique per productIndex */
  117.     private Long minProductIndexId;

  118.     /** The max number of results */
  119.     private Integer limit;

  120.     /** List of columns to order by */
  121.     private String orderBy;

  122.     /**
  123.      * Construct a new ProductIndexQuery.
  124.      */
  125.     public ProductIndexQuery() {
  126.     }

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

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

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

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

  143.     /** @param eventSource to set */
  144.     public void setEventSource(String eventSource) {
  145.         this.eventSource = (eventSource == null ? null : eventSource
  146.                 .toLowerCase());
  147.     }
  148.     /** @return eventSource */
  149.     public String getEventSource() {
  150.         return eventSource;
  151.     }

  152.     /** @param eventSourceCode to set */
  153.     public void setEventSourceCode(String eventSourceCode) {
  154.         this.eventSourceCode = (eventSourceCode == null ? null
  155.                 : eventSourceCode.toLowerCase());
  156.     }

  157.     /** @return eventSourceCode */
  158.     public String getEventSourceCode() {
  159.         return eventSourceCode;
  160.     }

  161.     /** @return minEventTime */
  162.     public Date getMinEventTime() {
  163.         return minEventTime;
  164.     }

  165.     /** @param minEventTime to set */
  166.     public void setMinEventTime(Date minEventTime) {
  167.         this.minEventTime = minEventTime;
  168.     }

  169.     /** @return maxEventTime */
  170.     public Date getMaxEventTime() {
  171.         return maxEventTime;
  172.     }

  173.     /** @param maxEventTime to set */
  174.     public void setMaxEventTime(Date maxEventTime) {
  175.         this.maxEventTime = maxEventTime;
  176.     }

  177.     /** @return minEventLatitude */
  178.     public BigDecimal getMinEventLatitude() {
  179.         return minEventLatitude;
  180.     }

  181.     /** @param minEventLatitude to set */
  182.     public void setMinEventLatitude(BigDecimal minEventLatitude) {
  183.         this.minEventLatitude = minEventLatitude;
  184.     }

  185.     /** @return maxEventLatitude */
  186.     public BigDecimal getMaxEventLatitude() {
  187.         return maxEventLatitude;
  188.     }

  189.     /** @param maxEventLatitude to set */
  190.     public void setMaxEventLatitude(BigDecimal maxEventLatitude) {
  191.         this.maxEventLatitude = maxEventLatitude;
  192.     }

  193.     /** @return minEventLongitude */
  194.     public BigDecimal getMinEventLongitude() {
  195.         return minEventLongitude;
  196.     }

  197.     /** @param minEventLongitude to set */
  198.     public void setMinEventLongitude(BigDecimal minEventLongitude) {
  199.         this.minEventLongitude = minEventLongitude;
  200.     }

  201.     /** @return maxEventLongitude */
  202.     public BigDecimal getMaxEventLongitude() {
  203.         return maxEventLongitude;
  204.     }

  205.     /** @param maxEventLongitude to set */
  206.     public void setMaxEventLongitude(BigDecimal maxEventLongitude) {
  207.         this.maxEventLongitude = maxEventLongitude;
  208.     }

  209.     /** @return minEventDepth */
  210.     public BigDecimal getMinEventDepth() {
  211.         return minEventDepth;
  212.     }

  213.     /** @param minEventDepth to set */
  214.     public void setMinEventDepth(BigDecimal minEventDepth) {
  215.         this.minEventDepth = minEventDepth;
  216.     }

  217.     /** @return maxEventDepth */
  218.     public BigDecimal getMaxEventDepth() {
  219.         return maxEventDepth;
  220.     }

  221.     /** @param maxEventDepth to set */
  222.     public void setMaxEventDepth(BigDecimal maxEventDepth) {
  223.         this.maxEventDepth = maxEventDepth;
  224.     }

  225.     /** @return minEventMagnitude */
  226.     public BigDecimal getMinEventMagnitude() {
  227.         return minEventMagnitude;
  228.     }

  229.     /** @param minEventMagnitude to set */
  230.     public void setMinEventMagnitude(BigDecimal minEventMagnitude) {
  231.         this.minEventMagnitude = minEventMagnitude;
  232.     }

  233.     /** @return maxEventMagnitude */
  234.     public BigDecimal getMaxEventMagnitude() {
  235.         return maxEventMagnitude;
  236.     }

  237.     /** @param maxEventMagnitude to set */
  238.     public void setMaxEventMagnitude(BigDecimal maxEventMagnitude) {
  239.         this.maxEventMagnitude = maxEventMagnitude;
  240.     }

  241.     /** @return list of product Ids */
  242.     public List<ProductId> getProductIds() {
  243.         return productIds;
  244.     }

  245.     /** @param productIds list to set */
  246.     public void setProductIds(List<ProductId> productIds) {
  247.         this.productIds.clear();
  248.         this.productIds.addAll(productIds);
  249.     }

  250.     /** @return minProductUpdateTime */
  251.     public Date getMinProductUpdateTime() {
  252.         return minProductUpdateTime;
  253.     }

  254.     /** @param minProductUpdateTime to set */
  255.     public void setMinProductUpdateTime(Date minProductUpdateTime) {
  256.         this.minProductUpdateTime = minProductUpdateTime;
  257.     }

  258.     /** @return maxProductUpdateTime */
  259.     public Date getMaxProductUpdateTime() {
  260.         return maxProductUpdateTime;
  261.     }

  262.     /** @param maxProductUpdateTime to set */
  263.     public void setMaxProductUpdateTime(Date maxProductUpdateTime) {
  264.         this.maxProductUpdateTime = maxProductUpdateTime;
  265.     }

  266.     /** @return productSource */
  267.     public String getProductSource() {
  268.         return productSource;
  269.     }

  270.     /** @param productSource to set */
  271.     public void setProductSource(String productSource) {
  272.         this.productSource = productSource;
  273.     }

  274.     /** @return productType */
  275.     public String getProductType() {
  276.         return productType;
  277.     }

  278.     /** @param productType to set */
  279.     public void setProductType(String productType) {
  280.         this.productType = productType;
  281.     }

  282.     /** @return productCode */
  283.     public String getProductCode() {
  284.         return productCode;
  285.     }

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

  290.     /** @param productVersion to set */
  291.     public void setProductVersion(String productVersion) {
  292.         this.productVersion = productVersion;
  293.     }

  294.     /** @return productVersion */
  295.     public String getProductVersion() {
  296.         return productVersion;
  297.     }

  298.     /** @param productStatus to set */
  299.     public void setProductStatus(String productStatus) {
  300.         this.productStatus = productStatus;
  301.     }

  302.     /** @return productStatus */
  303.     public String getProductStatus() {
  304.         return productStatus;
  305.     }

  306.     /** @param minProductIndexId to set */
  307.     public void setMinProductIndexId(final Long minProductIndexId) {
  308.         this.minProductIndexId = minProductIndexId;
  309.     }

  310.     /** @return minProductIndexId */
  311.     public Long getMinProductIndexId() {
  312.         return this.minProductIndexId;
  313.     }

  314.     /** @param limit to set */
  315.     public void setLimit(final Integer limit) {
  316.         this.limit = limit;
  317.     }

  318.     /** @return limit */
  319.     public Integer getLimit() {
  320.         return this.limit;
  321.     }

  322.     /** @param orderBy to set */
  323.     public void setOrderBy(final String orderBy) {
  324.         this.orderBy = orderBy;
  325.     }

  326.     /** @return orderBy */
  327.     public String getOrderBy() {
  328.         return this.orderBy;
  329.     }

  330.     @Override
  331.     public boolean equals(Object that) {
  332.         return (this.compareTo((ProductIndexQuery) that)) == 0;
  333.     }

  334.     @Override
  335.     public int compareTo(ProductIndexQuery that) {
  336.         int r = 0;

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

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

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

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

  408.         return 0;
  409.     }

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

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

  505.         logger.finest(buf.toString());
  506.     }
  507. }