/******************************************************************************* * 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.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.TimeZone; import org.joda.time.DateTime; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.ResultSetExtractor; public class CacheHandler implements ResultSetExtractor { @Override public Store extractData(ResultSet rs) throws SQLException, DataAccessException { Store store = new Store(); Station station = null; StationEpoch stationEpoch = null; Channel channel = null; ChannelEpoch channelEpoch = null; boolean newStation = false; boolean newStationEpoch = false; Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); boolean advanceCursor = true; while (true) { if(advanceCursor){ if(!rs.next()){ break; } } String netCode = rs.getString("NETWORK"); String staCode = rs.getString("STATION"); if (station == null || isNew(station, netCode, staCode)) { station = new Station(store.intern(netCode, Store.NETWORK), store.intern(staCode, Store.STATION)); store.add(station); newStation = true; } DateTime start = new DateTime(rs.getTimestamp("STATION_START_TIME", calendar).getTime()); DateTime end = new DateTime(rs.getTimestamp("STATION_END_TIME", calendar).getTime()); if (newStation || stationEpoch == null || isNew(stationEpoch, start, end)) { stationEpoch = new StationEpoch(start, end); stationEpoch.setElevation(rs.getDouble("STATION_ELEVATION")); stationEpoch.setLatitude(rs.getDouble("STATION_LATITUDE")); stationEpoch.setLongitude(rs.getDouble("STATION_LONGITUDE")); stationEpoch.setLastModified(new DateTime(rs.getTimestamp("ARCHIVEDT", calendar).getTime())); if (rs.getInt("WORD") == 10) { stationEpoch.setSwapOrder16(store.intern("BIG_ENDIAN", Store.STRING)); } else { stationEpoch.setSwapOrder16(store.intern("LITTLE_ENDIAN", Store.STRING)); } if (rs.getInt("LONG_WORD") == 3210) { stationEpoch.setSwapOrder32(store.intern("BIG_ENDIAN", Store.STRING)); } else { stationEpoch.setSwapOrder32(store.intern("LITTLE_ENDIAN", Store.STRING)); } stationEpoch.setSite(store.intern(rs.getString("SITE"), Store.STRING)); station.add(stationEpoch); newStationEpoch = true; } String chan = rs.getString("CHANNEL"); if (chan == null) { continue; } if (newStationEpoch || channel == null || isNew(channel, chan, rs.getString("LOCATION"))) { channel = new Channel(store.intern(chan,Store.CHANNEL), store.intern(rs.getString("LOCATION"),Store.LOCATION)); channel.setCreated(start); stationEpoch.add(channel); } channelEpoch = new ChannelEpoch(new DateTime( rs.getTimestamp("CHANNEL_START_TIME")), new DateTime( rs.getTimestamp("CHANNEL_END_TIME"))); channelEpoch.setLatitude(rs.getDouble("CHANNEL_LATITUDE")); channelEpoch.setLongitude(rs.getDouble("CHANNEL_LONGITUDE")); channelEpoch.setAzimuth(rs.getDouble("AZIMUTH")); channelEpoch.setDepth(rs.getDouble("CHANNEL_DEPTH")); channelEpoch.setDip(rs.getDouble("CHANNEL_DIP")); channelEpoch.setSampleRate(rs.getDouble("SAMPLE_RATE")); channelEpoch.setClockDrift(rs.getDouble("CLOCK_DRIFT")); channelEpoch.setFlags(rs.getString("FLAGS")); channelEpoch.setElevation(rs.getDouble("CHANNEL_ELEVATION")); channelEpoch.setInstrument(store.intern(rs.getString("channel_instrument"),Store.STRING)); channel.add(channelEpoch); int stage = rs.getInt("STAGE"); if (rs.wasNull()) { //do not do anything advanceCursor = true; } else { B58 b58 = null; if (stage == 0) { b58 = new B58(rs.getDouble("SENS_GAIN"), rs.getDouble("FREQUENCY")); // if (!rs.next()) { // break; // } stage = rs.getInt("STAGE"); if (stage == 0) { b58.setDescription(store.intern(rs.getString("unit_description"),Store.STRING)); b58.setUnits(store.intern(rs.getString("response_units_in_name"),Store.STRING)); advanceCursor = true; } else { advanceCursor = false; } } else if(stage == 1){ b58 = new B58(); b58.setDescription(store.intern(rs.getString("unit_description"),Store.STRING)); b58.setUnits(store.intern(rs.getString("response_units_in_name"),Store.STRING)); advanceCursor = true; }else{ advanceCursor = true; } channelEpoch.setB58(b58); } newStation = false; newStationEpoch = false; } return store; } private boolean isNew(Station station, String net, String sta) { if (!station.getNetCode().equalsIgnoreCase(net)) { return true; } if (!station.getStaCode().equalsIgnoreCase(sta)) { return true; } return false; } private boolean isNew(StationEpoch station, DateTime start, DateTime end) { if (!station.getStarted().equals(start)) { return true; } if (!station.getEnded().equals(end)) { return true; } return false; } private boolean isNew(Channel channel, String code, String location) { if (!channel.getCode().equalsIgnoreCase(code)) { return true; } if (!channel.getLocation().equalsIgnoreCase(location)) { return true; } return false; } }