/******************************************************************************* * Copyright (c) 2011 IRIS/DMC. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * IRIS/DMC- initial API and implementation * * ACKNOWLEDGEMENT * This software was developed as part of a project supported by * Cooperative Agreement Number G10AC00533 from the United States * Geological Survey. Its contents are solely the responsibility of * the authors and the USGS is not responsible for the efficacy, * safety, or suitability of this software. ******************************************************************************/ import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import org.apache.commons.codec.digest.DigestUtils; import org.joda.time.DateTime; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; public class Query { public static final int NETWORK_LEVEL = 0; public static final int STATION_LEVEL = 1; public static final int CHANNEL_LEVEL = 2; public static final int RESPONSE_LEVEL = 3; private String[] networks; private String[] stations; private String[] channels; private String[] locations; private String country; private String countryCode; private String region; private int regionCode = -1; private Double minLat; private Double maxLat; private Double minLon; private Double maxLon; private Double lat; private Double lon; private Double minRadius = 0D; private Double maxRadius; private DateTime startAfter; private DateTime startBefore; private DateTime endAfter; private DateTime endBefore; private DateTime updatedAfter; private Boolean Availability = false; private DateTime[] timeWindow; private int level = 1; private int limit = -1; public Query() { } public String[] getNetworks() { return networks; } public void setNetworks(String[] networks) { List vNetworks = null; List rNetworks = null; if (networks != null && networks.length > 0) { for (int i = 0; i < networks.length; i++) { if (networks[i].startsWith("_")) { if (vNetworks == null) { vNetworks = new ArrayList(); } vNetworks.add(networks[i]); } else { if (rNetworks == null) { rNetworks = new ArrayList(); } rNetworks.add(networks[i]); } } } if (rNetworks != null && !rNetworks.isEmpty()) { Object[] o = rNetworks.toArray(); this.networks = Arrays.copyOf(o, o.length, String[].class); Arrays.sort(this.networks); } } public String[] getStations() { return stations; } public void setStations(String[] stations) { this.stations = stations; Arrays.sort(this.stations); } public String[] getChannels() { return channels; } public void setLocations(String[] locations) { this.locations = locations; Arrays.sort(this.locations); } public String[] getLocations() { return locations; } public void setChannels(String[] channels) { this.channels = channels; Arrays.sort(this.channels); } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; } public String getRegion() { return region; } public void setRegion(String region) { this.region = region; } public int getRegionCode() { return regionCode; } public void setRegionCode(int regionCode) { this.regionCode = regionCode; } public DateTime getStartAfter() { return startAfter; } public void setStartAfter(DateTime startAfter) { this.startAfter = startAfter; } public DateTime getStartBefore() { return startBefore; } public void setStartBefore(DateTime startBefore) { this.startBefore = startBefore; } public DateTime getEndAfter() { return endAfter; } public void setEndAfter(DateTime endAfter) { this.endAfter = endAfter; } public DateTime getEndBefore() { return endBefore; } public void setEndBefore(DateTime endBefore) { this.endBefore = endBefore; } public DateTime[] getTimeWindow() { return timeWindow; } public void setTimeWindow(DateTime[] timeWindow) { this.timeWindow = timeWindow; } public DateTime getUpdatedAfter() { return updatedAfter; } public void setUpdatedAfter(DateTime updatedAfter) { this.updatedAfter = updatedAfter; } public Boolean getAvailability() { return Availability; } public void setAvailability(Boolean avail) { this.Availability = avail; } public Double getMinLat() { return minLat; } public void setMinLat(Double minLat) { this.minLat = minLat; } public Double getMaxLat() { return maxLat; } public void setMaxLat(Double maxLat) { this.maxLat = maxLat; } public Double getMinLon() { return minLon; } public void setMinLon(Double minLon) { this.minLon = minLon; } public Double getMaxLon() { return maxLon; } public void setMaxLon(Double maxLon) { this.maxLon = maxLon; } public Double getLat() { return lat; } public void setLat(Double lat) { this.lat = lat; } public Double getLon() { return lon; } public void setLon(Double lon) { this.lon = lon; } public Double getMinRadius() { return minRadius; } public void setMinRadius(Double minRadius) { this.minRadius = minRadius; } public Double getMaxRadius() { return maxRadius; } public void setMaxRadius(Double maxRadius) { this.maxRadius = maxRadius; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public int getLimit() { return limit; } public void setLimit(int limit) { if (limit < 10) { return; } this.limit = limit; } public String toHashString() { return DigestUtils.md5Hex(toString()); } public String toString() { // http://service.ncedc.org/fdsnws/station/1/query?net=BK&sta=CMB&loc=00&cha=BHE&starttime=2013-05-30T01:00:00&endtime=2013-05-30T01:00:00&level=response StringBuilder sBuilder = new StringBuilder(); sBuilder.append("http://service.ncedc.org/fdsnws/station/1/query?"); if ((networks != null && networks.length > 0)) { Arrays.sort(networks); sBuilder.append("net="); for (int i = 0; i < networks.length; i++) { if (i == 0) { sBuilder.append(networks[i].toUpperCase()); } else { sBuilder.append(" " + networks[i].toUpperCase()); } } sBuilder.append("&"); } if (stations != null && stations.length > 0) { Arrays.sort(stations); sBuilder.append("sta="); for (int i = 0; i < stations.length; i++) { if (i == 0) { sBuilder.append(stations[i].toUpperCase()); } else { sBuilder.append(" " + stations[i].toUpperCase()); } } sBuilder.append("&"); } if (channels != null && channels.length > 0) { Arrays.sort(channels); sBuilder.append("cha="); for (int i = 0; i < channels.length; i++) { if (i == 0) { sBuilder.append(channels[i].toUpperCase()); } else { sBuilder.append(" " + channels[i].toUpperCase()); } } sBuilder.append("&"); if (locations != null && locations.length > 0) { Arrays.sort(locations); sBuilder.append("loc="); for (int i = 0; i < locations.length; i++) { if (i == 0) { sBuilder.append(locations[i].toUpperCase()); } else { sBuilder.append(" " + locations[i].toUpperCase()); } } sBuilder.append("&"); } } if (startAfter != null) { sBuilder.append("startafter=" + startAfter.toString() + "&"); } if (startBefore != null) { sBuilder.append("startbefore=" + startBefore.toString() + "&"); } if (endAfter != null) { sBuilder.append("endafter=" + endAfter.toString() + "&"); } if (endBefore != null) { sBuilder.append("endbefore=" + endBefore.toString() + "&"); } if (timeWindow != null && timeWindow.length == 2) { sBuilder.append("start=" + timeWindow[0].toString() + "&end=" + timeWindow[1].toString() + "&"); } if (minLat != null && minLat >= -90 && minLat <= 90) { sBuilder.append("minlat=" + minLat + "&"); } if (maxLat != null && maxLat >= -90 && maxLat <= 90) { sBuilder.append("maxlat=" + maxLat + "&"); } if (minLon != null && minLon >= -180 && minLon <= 180) { sBuilder.append("minlon=" + minLon + "&"); } if (maxLon != null && maxLon >= -180 && maxLon <= 180) { sBuilder.append("maxlon=" + maxLon + "&"); } if(maxRadius!=null){ sBuilder.append("lat="+lat+"&lon="+lon+"&minradius="+minRadius+"&maxradius="+maxRadius+"&"); } if(updatedAfter!=null){ sBuilder.append("updatedafter=" + updatedAfter + "&"); } String l = ""; if (level == 0) { l = "net"; } else if (level == 1) { l = "sta"; } else if (level == 2) { l = "chan"; } else if (level == 3) { l = "resp"; } else { l = "[UNKOWN]"; } sBuilder.append("level=" + l); return sBuilder.toString(); } }