SearchQuery.java

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

  5. /**
  6.  * A search to execute against a ProductIndex, or ProductStorage.
  7.  */
  8. public abstract class SearchQuery implements Comparable<SearchQuery> {

  9.     /** The type of response that is expected. */
  10.     private final SearchMethod type;

  11.     /** The search parameters. */
  12.     private final ProductIndexQuery query;

  13.     /** Contains an error returned in a SearchResult if one occurred **/
  14.     private String error;

  15.     /**
  16.      * Construct a new SearchQuery object.
  17.      *
  18.      * @param type
  19.      *            the type of search.
  20.      * @param query
  21.      *            the query parameters.
  22.      */
  23.     protected SearchQuery(final SearchMethod type, final ProductIndexQuery query) {
  24.         this.type = type;
  25.         this.query = query;
  26.         this.error = null;
  27.     }

  28.     /** @return type */
  29.     public SearchMethod getType() {
  30.         return this.type;
  31.     }

  32.     /** @return ProductIndexQuery */
  33.     public ProductIndexQuery getProductIndexQuery() {
  34.         return this.query;
  35.     }

  36.     /**
  37.      * Get the result associated with a specific query type.
  38.      *
  39.      * @return the result, or null if the search has not yet executed.
  40.      */
  41.     public abstract Object getResult();

  42.     /**
  43.      * Create a SearchQuery object based on a SearchType.
  44.      *
  45.      * @param type
  46.      *            the search type to create
  47.      * @param query
  48.      *            the associated query
  49.      * @return a SearchQuery, or null if type is unknown.
  50.      */
  51.     public static SearchQuery getSearchQuery(final SearchMethod type,
  52.             final ProductIndexQuery query) {
  53.         if (type == null) {
  54.             return null;
  55.         } else if (type.equals(SearchMethod.EVENTS_SUMMARY)) {
  56.             return new EventsSummaryQuery(query);
  57.         } else if (type.equals(SearchMethod.EVENT_DETAIL)) {
  58.             return new EventDetailQuery(query);
  59.         } else if (type.equals(SearchMethod.PRODUCTS_SUMMARY)) {
  60.             return new ProductsSummaryQuery(query);
  61.         } else if (type.equals(SearchMethod.PRODUCT_DETAIL)) {
  62.             return new ProductDetailQuery(query);
  63.         } else {
  64.             return null;
  65.         }
  66.     }

  67.     @Override
  68.     public boolean equals(Object that) {
  69.         return (this.compareTo((SearchQuery) that)) == 0;
  70.     }

  71.     @Override
  72.     public int compareTo(SearchQuery that) {
  73.         int r;

  74.         if ((r = this.type.compareTo(that.type)) != 0) {
  75.             return r;
  76.         }

  77.         if ((r = this.query.compareTo(that.query)) != 0) {
  78.             return r;
  79.         }

  80.         //both have results
  81.         Object thatResult = that.getResult();
  82.         Object thisResult = this.getResult();
  83.         if (thisResult == null && thatResult == null) {
  84.             return 0;
  85.         } else if (thisResult != null && thatResult == null) {
  86.             return -1;
  87.         } else if (thisResult == null && thatResult != null) {
  88.             return 1;
  89.         }

  90.         return 0;
  91.     }

  92.     /**
  93.      * @param error the error to set
  94.      */
  95.     public void setError(String error) {
  96.         this.error = error;
  97.     }

  98.     /**
  99.      * @return the error or null if no error occurred.
  100.      */
  101.     public String getError() {
  102.         return error;
  103.     }

  104. }