ProductSummary.java

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

  5. import gov.usgs.earthquake.product.Product;
  6. import gov.usgs.earthquake.product.ProductId;
  7. import gov.usgs.util.XmlUtils;

  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.LinkedList;
  12. import java.util.Map;
  13. import java.math.BigDecimal;
  14. import java.net.URL;
  15. import java.util.List;
  16. import java.net.URI;

  17. /**
  18.  * A ProductSummary is essentially a product without its contents.
  19.  *
  20.  * These are usually created by {@link IndexerModule}s, which may
  21.  * inspect Product Content to add additional summary properties.
  22.  */
  23. public class ProductSummary {

  24.     /** An ID used by the ProductIndex. */
  25.     private Long indexId = null;

  26.     /** The product id. */
  27.     private ProductId id = null;

  28.     /** The product status. */
  29.     private String status = null;

  30.     /** The product tracker url. */
  31.     private URL trackerURL = null;

  32.     /** The product properties. */
  33.     private Map<String, String> properties = new HashMap<String, String>();

  34.     /** The product links. */
  35.     private Map<String, List<URI>> links = new HashMap<String, List<URI>>();

  36.     /** Unique identifier for this event. */
  37.     private String eventSource = null;

  38.     /** Unique identifier from the event network. */
  39.     private String eventSourceCode = null;

  40.     /** When the event occurred. */
  41.     private Date eventTime = null;

  42.     /** Where the event occurred. */
  43.     private BigDecimal eventLatitude = null;

  44.     /** Where the event occurred. */
  45.     private BigDecimal eventLongitude = null;

  46.     /** Where the event occurred. */
  47.     private BigDecimal eventDepth = null;

  48.     /** How big the event was. */
  49.     private BigDecimal eventMagnitude = null;

  50.     /** Product version. */
  51.     private String version = null;

  52.     /** Whether this product is "preferred". */
  53.     private long preferredWeight = 0;

  54.     /**
  55.      * Empty constructor.
  56.      */
  57.     public ProductSummary() {
  58.     }

  59.     /**
  60.      * Copy constructor for ProductSummary.
  61.      *
  62.      * Does a deep copy of properties and links maps. All other attributes are
  63.      * copied by reference.
  64.      *
  65.      * @param copy
  66.      *            product summary to copy.
  67.      */
  68.     public ProductSummary(final ProductSummary copy) {
  69.         this.indexId = copy.getIndexId();
  70.         this.id = copy.getId();
  71.         this.status = copy.getStatus();
  72.         this.trackerURL = copy.getTrackerURL();
  73.         this.properties.putAll(copy.getProperties());

  74.         Map<String, List<URI>> copyLinks = copy.getLinks();
  75.         Iterator<String> iter = copyLinks.keySet().iterator();
  76.         while (iter.hasNext()) {
  77.             String relation = iter.next();
  78.             links.put(relation, new LinkedList<URI>(copyLinks.get(relation)));
  79.         }

  80.         this.setEventSource(copy.getEventSource());
  81.         this.setEventSourceCode(copy.getEventSourceCode());
  82.         this.setEventTime(copy.getEventTime());
  83.         this.setEventLatitude(copy.getEventLatitude());
  84.         this.setEventLongitude(copy.getEventLongitude());
  85.         this.setEventDepth(copy.getEventDepth());
  86.         this.setEventMagnitude(copy.getEventMagnitude());
  87.         this.setVersion(copy.getVersion());
  88.         this.setPreferredWeight(copy.getPreferredWeight());
  89.     }

  90.     /**
  91.      * Create a ProductSummary from a product.
  92.      *
  93.      * All attributes are copied from the product, and preferredWeight is set to
  94.      * 1L.
  95.      *
  96.      * @param product
  97.      *            the product to summarize.
  98.      */
  99.     public ProductSummary(final Product product) {
  100.         this.id = product.getId();
  101.         this.status = product.getStatus();
  102.         this.trackerURL = product.getTrackerURL();
  103.         this.properties.putAll(product.getProperties());

  104.         Map<String, List<URI>> copyLinks = product.getLinks();
  105.         Iterator<String> iter = copyLinks.keySet().iterator();
  106.         while (iter.hasNext()) {
  107.             String relation = iter.next();
  108.             links.put(relation, new LinkedList<URI>(copyLinks.get(relation)));
  109.         }

  110.         this.setEventSource(product.getEventSource());
  111.         this.setEventSourceCode(product.getEventSourceCode());
  112.         this.setEventTime(product.getEventTime());
  113.         this.setEventLatitude(product.getLatitude());
  114.         this.setEventLongitude(product.getLongitude());
  115.         this.setEventDepth(product.getDepth());
  116.         this.setEventMagnitude(product.getMagnitude());
  117.         this.setVersion(product.getVersion());
  118.         this.setPreferredWeight(1L);
  119.     }

  120.     /** @return indexId */
  121.     public Long getIndexId() {
  122.         return indexId;
  123.     }

  124.     /** @param indexId to set */
  125.     public void setIndexId(Long indexId) {
  126.         this.indexId = indexId;
  127.     }

  128.     /** @return product id */
  129.     public ProductId getId() {
  130.         return id;
  131.     }

  132.     /** @param id to set */
  133.     public void setId(ProductId id) {
  134.         this.id = id;
  135.     }

  136.     /** @return status */
  137.     public String getStatus() {
  138.         return status;
  139.     }

  140.     /** @param status to set */
  141.     public void setStatus(String status) {
  142.         this.status = status;
  143.     }

  144.     /** @return if product is deleted */
  145.     public boolean isDeleted() {
  146.         if (Product.STATUS_DELETE.equalsIgnoreCase(this.status)) {
  147.             return true;
  148.         } else {
  149.             return false;
  150.         }
  151.     }

  152.     /** @return trackerURL */
  153.     public URL getTrackerURL() {
  154.         return trackerURL;
  155.     }

  156.     /** @param trackerURL to set */
  157.     public void setTrackerURL(URL trackerURL) {
  158.         this.trackerURL = trackerURL;
  159.     }

  160.     /** @return preferredWeight */
  161.     public long getPreferredWeight() {
  162.         return preferredWeight;
  163.     }

  164.     /** @param weight to set */
  165.     public void setPreferredWeight(long weight) {
  166.         this.preferredWeight = weight;
  167.     }

  168.     /**
  169.      * @return the properties
  170.      */
  171.     public Map<String, String> getProperties() {
  172.         return properties;
  173.     }

  174.     /**
  175.      * @param properties
  176.      *            the properties to set
  177.      */
  178.     public void setProperties(final Map<String, String> properties) {
  179.         this.properties.putAll(properties);
  180.     }

  181.     /**
  182.      * Returns a reference to the links map.
  183.      *
  184.      * @return the links
  185.      */
  186.     public Map<String, List<URI>> getLinks() {
  187.         return links;
  188.     }

  189.     /**
  190.      * Copies entries from provided map.
  191.      *
  192.      * @param links
  193.      *            the links to set
  194.      */
  195.     public void setLinks(final Map<String, List<URI>> links) {
  196.         this.links.clear();
  197.         this.links.putAll(links);
  198.     }

  199.     /**
  200.      * Add a link to a product.
  201.      *
  202.      * @param relation
  203.      *            how link is related to product.
  204.      * @param href
  205.      *            actual link.
  206.      */
  207.     public void addLink(final String relation, final URI href) {
  208.         List<URI> relationLinks = links.get(relation);
  209.         if (relationLinks == null) {
  210.             relationLinks = new LinkedList<URI>();
  211.             links.put(relation, relationLinks);
  212.         }
  213.         relationLinks.add(href);
  214.     }

  215.     /** @return null or eventId */
  216.     public String getEventId() {
  217.         if (eventSource == null || eventSourceCode == null) {
  218.             return null;
  219.         }
  220.         return (eventSource + eventSourceCode).toLowerCase();
  221.     }

  222.     /** @return eventSource */
  223.     public String getEventSource() {
  224.         return eventSource;
  225.     }

  226.     /** @param eventSource to set */
  227.     public void setEventSource(String eventSource) {
  228.         this.eventSource = eventSource;

  229.         // event ids are case insensitive, force lower.
  230.         if (this.eventSource != null) {
  231.             this.eventSource = this.eventSource.toLowerCase();
  232.         }

  233.         if (this.eventSource != null) {
  234.             this.properties.put(Product.EVENTSOURCE_PROPERTY, this.eventSource);
  235.         } else {
  236.             this.properties.remove(Product.EVENTSOURCE_PROPERTY);
  237.         }
  238.     }

  239.     /** @return eventSourceCode */
  240.     public String getEventSourceCode() {
  241.         return eventSourceCode;
  242.     }

  243.     /** @param eventSourceCode to set */
  244.     public void setEventSourceCode(String eventSourceCode) {
  245.         this.eventSourceCode = eventSourceCode;

  246.         // event ids are case insensitive, force lower.
  247.         if (this.eventSourceCode != null) {
  248.             this.eventSourceCode = this.eventSourceCode.toLowerCase();
  249.         }

  250.         if (this.eventSourceCode != null) {
  251.             this.properties.put(Product.EVENTSOURCECODE_PROPERTY,
  252.                     this.eventSourceCode);
  253.         } else {
  254.             this.properties.remove(Product.EVENTSOURCECODE_PROPERTY);
  255.         }
  256.     }

  257.     /** @return eventTime */
  258.     public Date getEventTime() {
  259.         return eventTime;
  260.     }

  261.     /** @param eventTime to set */
  262.     public void setEventTime(Date eventTime) {
  263.         this.eventTime = eventTime;
  264.         if (eventTime != null) {
  265.             this.properties.put(Product.EVENTTIME_PROPERTY,
  266.                     XmlUtils.formatDate(eventTime));
  267.         } else {
  268.             this.properties.remove(Product.EVENTTIME_PROPERTY);
  269.         }

  270.     }

  271.     /** @return eventLatitude */
  272.     public BigDecimal getEventLatitude() {
  273.         return eventLatitude;
  274.     }

  275.     /** @param eventLatitude to set */
  276.     public void setEventLatitude(BigDecimal eventLatitude) {
  277.         this.eventLatitude = eventLatitude;
  278.         if (eventLatitude != null) {
  279.             this.properties.put(Product.LATITUDE_PROPERTY,
  280.                     eventLatitude.toString());
  281.         } else {
  282.             this.properties.remove(Product.LATITUDE_PROPERTY);
  283.         }
  284.     }

  285.     /** @return eventLongitude */
  286.     public BigDecimal getEventLongitude() {
  287.         return eventLongitude;
  288.     }

  289.     /** @param eventLongitude to set */
  290.     public void setEventLongitude(BigDecimal eventLongitude) {
  291.         this.eventLongitude = eventLongitude;
  292.         if (eventLongitude != null) {
  293.             this.properties.put(Product.LONGITUDE_PROPERTY,
  294.                     eventLongitude.toString());
  295.         } else {
  296.             this.properties.remove(Product.LONGITUDE_PROPERTY);
  297.         }
  298.     }

  299.     /** @return eventDepth */
  300.     public BigDecimal getEventDepth() {
  301.         return eventDepth;
  302.     }

  303.     /** @param eventDepth to set */
  304.     public void setEventDepth(BigDecimal eventDepth) {
  305.         this.eventDepth = eventDepth;
  306.         if (eventDepth != null) {
  307.             this.properties.put(Product.DEPTH_PROPERTY, eventDepth.toString());
  308.         } else {
  309.             this.properties.remove(Product.DEPTH_PROPERTY);
  310.         }
  311.     }

  312.     /** @return eventMagnitude */
  313.     public BigDecimal getEventMagnitude() {
  314.         return eventMagnitude;
  315.     }

  316.     /** @param eventMagnitude to set */
  317.     public void setEventMagnitude(BigDecimal eventMagnitude) {
  318.         this.eventMagnitude = eventMagnitude;
  319.         if (eventMagnitude != null) {
  320.             this.properties.put(Product.MAGNITUDE_PROPERTY,
  321.                     eventMagnitude.toString());
  322.         } else {
  323.             this.properties.remove(Product.MAGNITUDE_PROPERTY);
  324.         }
  325.     }

  326.     /** @return version */
  327.     public String getVersion() {
  328.         return version;
  329.     }

  330.     /** @param version to set */
  331.     public void setVersion(String version) {
  332.         this.version = version;
  333.         if (version != null) {
  334.             this.properties.put(Product.VERSION_PROPERTY, version);
  335.         } else {
  336.             this.properties.remove(Product.VERSION_PROPERTY);
  337.         }
  338.     }

  339.     /** @return type */
  340.     public String getType() {
  341.         return getId().getType();
  342.     }

  343.     /** @return source */
  344.     public String getSource() {
  345.         return getId().getSource();
  346.     }

  347.     /** @return code */
  348.     public String getCode() {
  349.         return getId().getCode();
  350.     }

  351.     /** @return updateTime */
  352.     public Date getUpdateTime() {
  353.         return getId().getUpdateTime();
  354.     }

  355.     /**
  356.      * Compares two ProductSummaries to determine if they are equal.
  357.      *
  358.      * This first implementation just considers the ProductId of each summary.
  359.      * This is probably not the best way to check for equality.
  360.      */
  361.     @Override
  362.     public boolean equals(Object o) {
  363.         if (o instanceof ProductSummary) {
  364.             return ((ProductSummary) o).getId().equals(this.getId());
  365.         }
  366.         return false;
  367.     }

  368.     /**
  369.      * Generate hashcode for ProductId using all components.
  370.      *
  371.      * This implementation just uses hashcode from ProductId.
  372.      */
  373.     @Override
  374.     public int hashCode() {
  375.         return getId().hashCode();
  376.     }

  377. }