📄 waveformtn.java
字号:
package org.trinet.jasi.TN;
import org.trinet.jasi.*;
import java.lang.*;
import java.io.File;
import java.util.*;
import java.sql.*;
import org.trinet.jdbc.ResultSetDb;
import org.trinet.jdbc.datatypes.*;
import org.trinet.jasi.seed.*;
import org.trinet.util.TimeSpan;
import org.trinet.util.EpochTime;
import org.trinet.util.WaveClient;
/**
* Represents the waveform time series for one channel for a time window (TimeSpan).
* Because a time window may contain gaps or no data, the TimeSeries may contain
* from zero to 'n' WFSegments. Each WFSegment is a CONTIGUOUS time series.<p>
*
* Fires ChangeEvents when the time-series is loaded so that time-series can be
* loaded asynchronously (in background thread) then notify a graphics object when
* loading is complete.<p>
*
* Note that a waveform may be "empty", that is it contain no time series. This may be
* true before the loader thread has retreived the time-series. Check this
* condition using the boolean method hasTimeSeries().<p>
*
* This class also knows how to read the waveforms from various data sources.
* @see: loadTimeSeries().
* <p>
*/
/*
1/15/02 Integrated AWW's DbBlobReader
*/
public class WaveformTN extends Waveform {
static final String SelectJoinString =
"SELECT "+
org.trinet.jdbc.table.Waveform.QUALIFIED_COLUMN_NAMES+","+
" AssocWaE.evid, "+
" WavefilePath(Waveform.wfid), WavefileDfile(Waveform.wfid) "+
" FROM Waveform, AssocWaE WHERE Waveform.wfid = AssocWaE.wfid ";
// //////////////////////////////////////////////////////////////////
/**
* Empty constructor
*/
public WaveformTN() {}
/**
* Get an array of waveform headers given an SQL query.
*/
protected static Collection getBySQL(String sql)
{
return getBySQL(DataSource.getConnection(), sql);
}
/**
* Get an array of waveform headers given an SQL query.
*/
protected static Collection getBySQL(Connection conn, String sql) {
ArrayList wfList = new ArrayList();
// Debug
// System.out.println ("SQL: "+sql);
try {
if ( conn.isClosed() ) // check that valid connection exists
{
System.err.println ("* Connection is closed");
return null;
}
Statement sm = conn.createStatement();
// ResultSetDb rsdb =
// new ResultSetDb(org.trinet.jdbc.table.ExecuteSQL.rowQuery(sm, sql));
ResultSet rs = org.trinet.jdbc.table.ExecuteSQL.rowQuery(sm, sql);
if (rs == null) return null; // nothing found
// parse and add rows one-at-a-time
while ( rs.next() ) {
wfList.add(parseResultSet(rs));
}
sm.close();
}
catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
}
// convert vector to array
// if (wfList.size() == 0) return null;
return (Collection) wfList;
}
/**
* Parse a resultset row that contains the results of a join + some
* stored procedures
*/
// static Waveform parseResultSet (ResultSetDb rsdb)
static Waveform parseResultSet (ResultSet rs) {
// a jdbc.table Waveform
org.trinet.jdbc.table.Waveform wfrow = new org.trinet.jdbc.table.Waveform();
// a jsai Waveform
WaveformTN wf = new WaveformTN();
int offset = 0;
// parse the first part of the return that is just a Waveform table row
ResultSetDb rsdb = new ResultSetDb(rs);
wfrow = (org.trinet.jdbc.table.Waveform) wfrow.parseOneRow(rsdb, offset);
// transfer row contents to this Waveform
wf.parseWaveformRow(wfrow);
// Now, pick up the "extra" columns specified by the SQL query.
offset += wfrow.MAX_FIELDS;
try {
// <AssocWaE.evid>
// event ID for which this event was saved
int oid = rs.getInt(++offset); // get as int
String str = "" + oid;
wf.savingEvent.setValue(str); // save as string
// <WavefilePath(AssocWaE.evid)>
// time-series data file PATH from stored procedure
wf.path = rs.getString(++offset);
// <WavefileDfile(Waveform.wfid)>
// time-series data filename from stored procedure
wf.filename = rs.getString(++offset);
}
catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
}
// keep a copy of the row
// wf.setWaveformRow(wfrow);
return wf;
}
/**
* Transfer the needed DataObject's from the Waveform row to this Waveform.
*/
// Note that this gets DataObjects not values.
// Note the use of 'wfr.EVID' for static constants rather then
// 'org.trinet.jdbc.table.Waveform.EVID' for brevity.
private Waveform parseWaveformRow(org.trinet.jdbc.table.Waveform wfr) {
int i = 0;
this.id = (DataLong) wfr.getDataObject(wfr.WFID);
// suck out the Channel description
// this.chan.parseNameFromDataTableRow(wfr);
setChannelObj(ChannelTN.parseNameFromDataTableRow(wfr));
// Should we do DateTime.trimToNanos(timeValue) here?
this.timeStart = (DataDouble) wfr.getDataObject(wfr.DATETIME_ON);
this.timeEnd = (DataDouble) wfr.getDataObject(wfr.DATETIME_OFF);
this.samplesPerSecond=(DataDouble) wfr.getDataObject(wfr.SAMPRATE);
this.dataMode = (DataString) wfr.getDataObject(wfr.WAVETYPE);
// sample count is unknown
this.format = (DataLong) wfr.getDataObject(wfr.WAVE_FMT);
this.encoding = (DataLong) wfr.getDataObject(wfr.FORMAT_ID);
this.savingEvent= (DataString) wfr.getDataObject(wfr.LOCEVID);
// file access stuff
this.fileOffset = (int) wfr.getDataObject(wfr.FOFF).longValue();
this.byteCount = (int) wfr.getDataObject(wfr.NBYTES).longValue();
// this.fileOffset = wfr.getLong(wfr.FOFF);
// this.byteCount = wfr.getLong(wfr.NBYTES);
return this;
}
/**
* Get one waveform from the DataSource by Waveform record ID #
* Returns empty Waveform object if no match is found.
*/
public Waveform getByWaveformId (long wfid) {
String sql = SelectJoinString +
" and AssocWaE.wfid = " + wfid;
ArrayList wf = (ArrayList) getBySQL(sql);
if (wf.size() == 0) {
return Waveform.create();
} else {
return (Waveform) wf.get(0); // only one
}
}
/**
* Get an array of waveforms from the DataSource by solution ID #
*/
public Collection getBySolutionId (long id) {
String sql = SelectJoinString +
" and AssocWaE.evid = " + id;
return getBySQL(sql);
}
/**
* Get an array of waveforms from the DataSource by solution ID #
*/
public Collection getBySolution (Solution sol) {
String sql = SelectJoinString +
" and AssocWaE.evid = " + sol.id.toString();
return getBySQL(sql);
}
/**
* Create AssocWaE record for existing WaveForm and pointing at this Solution.
* The underlying stored procedure commits, so you don't have to.
* */
public boolean commit (Solution sol) {
String sql = "call jasi.AssociateWaveform("+
id.longValue() + ", "+ sol.id.longValue()+ ")";
return doSql(sql);
}
/** Execute the sql string, return false on error */
protected boolean doSql (String sql) {
try {
Statement sm = DataSource.getConnection().createStatement();
sm.execute(sql);
} catch (SQLException ex) {
System.out.println ("SQL: "+sql);
System.err.println(ex);
ex.printStackTrace();
return false;
}
return true;
}
/**
* Gets generic waveform file root from the database. Needs a
* JDBConn open connection. Note that this is an over simplification because
* there may be multiple wavefile roots.
*/
protected String getFileRoot() {
String root = "";
// get the data directory root from the data base
String sql = "select wavefileroot() from dual";
try {
Statement sm = DataSource.getConnection().createStatement();
ResultSet rs = sm.executeQuery(sql);
rs.next();
root = rs.getString(1);
sm.close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -