📄 solutiontn.java
字号:
package org.trinet.jasi.TN;
import org.trinet.jasi.*;
import java.text.*;
import java.util.*;
import org.trinet.jdbc.*;
import org.trinet.util.DateTime;
import org.trinet.util.RangeDouble;
//import org.trinet.util.LatLonZ;
import org.trinet.util.gazetteer.LatLonZ;
import org.trinet.util.Format; // CoreJava printf-like Format class
import org.trinet.util.TimeSpan;
import org.trinet.jdbc.table.*;
import org.trinet.jdbc.table.SeqIds;
import org.trinet.jdbc.datatypes.*;
import java.sql.*;
import org.trinet.jasi.*;
import org.trinet.jasi.coda.*;
/**
* Component of the Seismic Abstraction Layer. This implementation interfaces
* to the Berkeley/Caltech/USGS schema in Oracle. <p>
*
* All communication with the data source is via the mechanism specified in
* DataSource. Therefore, DataSource must be used before any JASI components
* can read/write to data sources. <p>
*
* All data members are DataObjects rather than primative types so they can
* handle the concepts of "nullness", "mutability", and "change-state".<p>
*
* NULLNESS = a field can be null, i.e. unknown or no value. The traditional
* approach has been to insert zero or some bogus value. In ASCII
* representations sometimes a field was just left blank. For example: RMS
* 'distance' might simply be unknown. To give it a value of 0.0km could foul up
* other calculations. When you write data out to a database you want to leave
* null fields null and not write a bunch of zeros that were used as an
* expedient inside of an application.<p>
*
* MUTABLITY = some fields should NEVER be explicity manipulated by an
* application. These are usually "key" fields that would break the database or
* cause constraint errors. They would be set immutable. Think of these as
* "read-only".<p>
*
* CHANGE-STATE = this is primarily an efficiency issue. If you read in 1000
* records, change 10 of them, then save your work, you only want to write
* ('update') the changed records to the database.<p>
*
* The database can contain null values for
* certain fields. Also applications may not want to write a "bogus" value like 0
* into the database when when the real value is not known. Data primatives can't
* be null and are initiallize to zero. */
/* NCDC Schema map
[EVENT]--[Origin]-+------------[[[AssocAmO]]]--+
| +--[[[Amp]]
+--[NetMag]--[[[AssocAmM]]]--+
|
+------------[[[AssocArO]]]-----[[[Arrival]]]
*/
public class SolutionTN extends Solution {
/*
Here are protected data members used to support this particular
implimentation of the abstract layer */
// Schema classes that map to tables. Keep these for update, delete, etc.
protected Event eventRow;
protected Origin originRow;
// foreign keys
/*
The fields prefmag, prefmec & commid exist in BOTH the Event and
Origin tables. Which to use?? Made decision to use those in Origin. */
protected DataLong prefor = new DataLong();
protected DataLong prefmag = new DataLong();
protected DataLong prefmec = new DataLong();
protected DataLong commid = new DataLong();
// flags
protected DataLong bogusFlag = new DataLong();
protected DataLong selectFlag = new DataLong();
/** True if this Solution was orginally read from the dbase. Used to know if
a commit requires an 'insert' or an 'update' */
boolean fromDbase = false;
// Dbase connection objects
/** Database connection to be used by an instance of Solution */
// NOTE: static calls must pass connection as arg OR use default DataSource connection
// protected static Connection conn;
protected static PreparedStatement psCount = null; // Waveform counter prepared statement
// The basic event/origin/netmag join
// protected static final String sqlPrefix =
// "Select event.*, origin.*, netmag.* from event, origin, netmag ";
protected static final String sqlPrefix =
"Select "+
Event.QUALIFIED_COLUMN_NAMES+","+
Origin.QUALIFIED_COLUMN_NAMES+","+
NetMag.QUALIFIED_COLUMN_NAMES+
" from event, origin, netmag ";
/* Join: note that we must do an "outer" join by using (+). Otherwise, an
event that lacks an 'prefor' or 'prefmag' will NOT be seen. (see: Oracel8
Bible, McCullough-Dieter, pg. 217) */
protected static final String sqlJoin = sqlPrefix +
" WHERE (event.prefor = origin.orid(+)) and (event.prefmag = netmag.magid(+)) ";
// debug output control flag
boolean debug = true;
//boolean debug = false;
/** Collection containing alternate (non-preferred) solutions */
// ArrayList altSols = new ArrayList();
/** Collection containing alternate (non-preferred) magnitudes */
// ArrayList altMags = new ArrayList();
/**
* Create a Solution object.
* A new solution must have a unique id at the time it is created.
*/
public SolutionTN () {
// setConnection(DataSource.conn); // use default connection
}
/**
* Create a Solution object. DataSource must be stablished first.
*/
/*
public SolutionTN (Connection conn) {
setConnection(conn);
}
*/
/**
* Explicitely set or change the dbase connection to be used by a solution object.
*/
/* public static void setConnection (Connection connection) {
conn = connection;
prepCountStatement(conn);
}
*/
/** Return the connection object. If none was explicitly set returns the
* DataSource default connection. If none return null. */
public static Connection getConnection() {
// if (conn == null) setConnection(DataSource.getConnection());
return DataSource.getConnection();
// return conn;
}
/**
* Make a prepared statement for counting waveforms for a solution.
* This is for efficiency. Use the default DataSource connection.
*/
protected static void prepCountStatement() {
prepCountStatement(getConnection());
}
/**
* Make a prepared statement for counting waveforms for a solution.
* This is for efficiency.
*/
protected static void prepCountStatement(Connection conn) {
if (conn == null) return;
try {
psCount =
conn.prepareStatement("Select COUNT(wfid) from AssocWaE where evid = ?" );
} catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
}
}
/**
* Get the next valid solution (event) sequence number (evseq).
* Returns the unique value. Returns 0 if there is an error.
*/
public long setUniqueId() {
long newId = SeqIds.getNextSeq("evseq");
id.setValue(newId);
return newId;
}
/**
* Set the internal copy of the Event table object for later use.
*/
protected void setEventRow (Event evt) {
eventRow = evt;
}
/**
* Set the internal copy of the Origin table object for later use.
*/
protected void setOriginRow (Origin org) {
originRow = org;
}
/**
* Set value of 'waveRecords' for this Solution by examining the datasource,
* if available.
*/
public int countWaveforms(){
// We can't do this at instatiation time because the Connection is not created yet.
if (psCount == null) prepCountStatement(getConnection());
// make the query
try
{
psCount.setLong(1, this.id.longValue());
ResultSet rset = psCount.executeQuery();
if (!rset.next()) return 0; // no resultset
int wcount = rset.getInt(1);
waveRecords.setValue(wcount);
rset.close();
return wcount;
}
catch (SQLException ex) {
System.err.println("countWaveforms SQLException: " + ex.getMessage());
System.err.println(ex);
return 0;
}
}
/** Set the event type to a valid jasi eventType. If the type passed in 'type'
* is not valid 'false' is returned and the eventType is not changed.
* @see org.trinet.jasi.EventTypeMap */
public boolean setEventType (String type) {
return setEventType (EventTypeMap.getIntOfJasiCode(type));
}
/** Set the event type. If the type passed in 'type'
* is not valid 'false' is returned and the eventType is not changed.
* @see org.trinet.jasi.EventTypeMap */
public boolean setEventType (int type) {
if (!EventTypeMap.isValid(type)) return false;
eventType.setValue(EventTypeMap.get(type));
return true;
}
/** Return the local implentation's event type string.
* @see org.trinet.jasi.EventTypeMap */
public String getEventTypeString () {
return eventType.toString();
}
/**
* Returns array of Solutions based on the results of the SQL query to an
* NCDCv1.5 data base. It must be of the form:<p> "Select
* event., origin., netmag. from event, origin, netmag " <p>
* because you must do a join of Event/Origin/NetMag to get all the
* info we need. Returns null if no event is found.<p> * Uses
* default connection created by DataSource. */
public static SolutionTN[] getBySQL(String sql)
{
if (getConnection() == null) {
System.err.println ("* No DataSource is open.");
return null;
}
return getBySQL(getConnection(), sql);
}
/**
* Returns array of Solutions based on the results of the SQL query to an
* NCDCv1.5 * data base. It must be of the form:<p> "Select
* event.*, origin.*, netmag.* from event, origin, netmag " <p> *
* because you must do a join of Event/Origin/NetMag to get all the
* info we need. * Returns null if no event is found. */
protected static SolutionTN[] getBySQL(Connection connection, String sql) {
Vector vec = new Vector();
// Debug
//System.out.println ("getBYSQL(Connection, String) SQL\n: "+sql);
try {
if ( getConnection() == null || getConnection().isClosed() ) // check that valid connection exists
{
System.err.println ("* DataSource connection is closed");
return null;
}
// ** Removed: don't use Oracle locking
// do "select for update" if writeBack is enabled
// ExecuteSQL.setSelectForUpdate(DataSource.isWriteBackEnabled());
Statement sm = getConnection().createStatement();
ResultSetDb rsdb = new ResultSetDb(ExecuteSQL.rowQuery(sm, sql));
if (rsdb == null) {
// System.out.println("getBySQL(Connection, String) rsdb is null");
return null; // nothing found
}
while ( rsdb.getResultSet().next() ) {
vec.add(SolutionTN.parseResultSet(rsdb));
}
sm.close();
}
catch (SQLException ex) {
System.out.println ("getBYSQL(Connection, String) SQL=\n"+sql);
System.err.println(ex);
ex.printStackTrace();
}
// convert vector to array
if (vec.size() == 0) return null;
SolutionTN sol[] = new SolutionTN[vec.size()];
vec.copyInto(sol);
dbFetchComments(sol);
return sol;
}
/**
* Returns the Solution with this ID number from the data source.
* Uses default DataSource.
* Returns null if no event is found.
* For the NCDC v1.5 schema ID is the 'evid' not the 'orid'.
*/
public Solution getById(long id){
return getById(getConnection(), id);
}
/**
* Returns the Solution with this ID number from the data source.
* Returns null if no event is found.
* For the NCDC v1.5 schema ID is the 'evid' not the 'orid'.
*/
public Solution getById(Connection conn, long id){
// must do a join of Event/Origin/NetMag to get all the info we need
String sql = sqlJoin + " and (event.evid = " + id +")" ;
Solution sol[] = SolutionTN.getBySQL(conn, sql);
if (sol == null) return null;
return sol[0];
}
/**
* Returns the VALID Solution with this ID number from the data source.
* Uses default DataSource.
* Returns null if no event is found.
* For the NCDC v1.5 schema ID is the 'evid' not the 'orid'.
*/
public Solution getValidById(long id){
return getValidById(getConnection(), id);
}
/**
* Returns the VALID Solution with this ID number from the data source.
* Returns null if no event is found.
* For the NCDC v1.5 schema ID is the 'evid' not the 'orid'.
*/
public Solution getValidById(Connection conn, long id) {
// must do a join of Event/Origin/NetMag to get all the info we need
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -