📄 codatn.java
字号:
return (! fromDbase || isUpdate() || hasChangedAttributes());
}
/**
* Set the isUpdate() flag for all data dbase members the given boolean value. */
protected void setUpdate (boolean tf) {
fromDbase = !tf;
tau.setUpdate(tf);
datetime.setUpdate(tf);
descriptor.setUpdate(tf);
aFix.setUpdate(tf);
aFree.setUpdate(tf);
qFix.setUpdate(tf);
qFree.setUpdate(tf);
}
/**
* Returns value of update flag. Setting update == true after instance initialization from a database
* forces an new row to be inserted into the database upon commit().
*/
public boolean isUpdate() { return update;}
/** Returns true if certain derived data member values were modified after initialization. */
protected boolean hasChangedAttributes() {
return (datetime.isUpdate() || tau.isUpdate() || descriptor.hasChanged()) ||
(aFix.isUpdate() || aFree.isUpdate() || qFix.isUpdate() || qFree.isUpdate());
}
/**
* Derives from the DataSource a Collection of Coda associated with input Magnitude identifier (magid).
* Input magnitude must have a non-null associated Solution.
* Retrieves the AssocCoM table data corresponding to the input magid joined with
* their associated Coda table data. Returns null if no codas.
*/
public Collection getByMagnitude(Magnitude mag) {
String sql = sqlPrefixByMag + " and (AssocCoM.magid = " + mag.magid.toString()+")";
joinType = Type_AssocCoM;
ArrayList codaList = (ArrayList) getBySQL(sql);
if (codaList == null) return null;
int size = codaList.size();
System.out.println ("getByMagnitude: codas found ="+size);
// associate all found codas with this magnitude
for (int i = 0; i<size; i++) {
org.trinet.jasi.Coda coda = ((org.trinet.jasi.Coda)codaList.get(i));
coda.associate(mag.getAssociatedSolution());
coda.associateMag(mag);
}
return codaList;
}
/**
* Derives from DataSource a Collection of Coda associated with the preferred Magnitude of the Solution identified by the input id.
* Retrieves the AssocCoM table data corresponding to the input magid joined with their associated Coda table data.
* Returns null if no Solution in the database corresponds to the input id.
* @see #getByMagnitude(Magnitude)
*/
public Collection getByMagnitude(long id) {
// must first retreive the Solution, then look up prefor for this id
Solution tsol = Solution.create().getById(id);
return (tsol == null) ? null : getByMagnitude(tsol.magnitude);
}
/**
* Derives from DataSource a Collection of Coda associated with this Solution.
* Retrieves the AssocCoO table data corresponding to the Solution's preferred orid joined with their associated Coda table data.
*/
public Collection getBySolution(Solution aSol) {
String sql = sqlPrefixByOrigin + " and (AssocCoO.orid = " + ((SolutionTN)aSol).getOrid()+")";
joinType = Type_AssocCoO;
ArrayList codaList = (ArrayList) getBySQL(sql);
int size = codaList.size();
for (int i = 0; i<size; i++) {
((org.trinet.jasi.Coda)codaList.get(i)).associate(aSol); // set associated solution to the one in the arg
}
return codaList;
}
/**
* Derives from DataSource a Collection of Coda associated with this Solution.
* Retrieves the AssocCoO table data corresponding to the Solution's preferred orid joined with their associated Coda table data.
* Returns null if no Solution in the database corresponds to the input id.
* @see #getBySolution(Solution)
*/
public Collection getBySolution(long id) {
Solution tmpSol = Solution.create().getById(id); // must create Solution, then get prefor for evid
return (tmpSol == null) ? null : getBySolution(tmpSol);
}
/**
* Derives from DataSource the Collection of Coda that begin within the input time window.
* Coda in the collection are then associated with the Solutions in the input SolutionList;
* if a Coda is associated with a Solution not in the input list that Solution will be added to the list.
*/
public static Collection getByTime(double start, double end, SolutionList sl) {
ArrayList codaList = (ArrayList) org.trinet.jasi.Coda.create().getByTime(start, end);
int size = codaList.size();
if (size != 0) { // Match up with associated solutions.
for (int i = 0; i < size; i++) {
CodaTN coda = (CodaTN) codaList.get(i);
Solution tmpSol = sl.getByOrid(coda.orid.longValue());
if (tmpSol != null) { // a match
coda.associate(tmpSol);
} else { // no match, look in database for Solution
tmpSol = SolutionTN.getByOrid(coda.orid.longValue());
if (tmpSol != null){
coda.associate(tmpSol);
sl.addByTime(tmpSol); // add to the Solution list
}
}
}
}
return codaList;
}
/**
* Derives from the DataSource a Collection of Coda that begin within this time window.
* Input times are seconds in UNIX epoch time. Returns null if no codas are found.<br>
* NOTE: in TriNet this gets codas from ALL sources (RT1, RT2)
*/
public Collection getByTime(double start, double end) {
String sql = sqlPrefix + " where CODA.DATETIME BETWEEN " +
StringSQL.valueOf(start) + " AND " + StringSQL.valueOf(end) +
" order by Datetime";
return getBySQL(sql); // This is quite SLOW, need dbase indexing?
}
/**
* Convenience wrapper method.
* @see #getByTime(double,double,SolutionList)
*/
public static Collection getByTime(TimeSpan ts, SolutionList sl) {
return getByTime(ts.getStart(), ts.getEnd(), sl);
}
/**
* Derives from the DataSource a Coda instance corresponding to the input coda id ( coid in the NCDC schema).
* Retrieves only parent Coda table data, no AssocCoO or AssocCoM table data are retrieved from DataSource database.
* Returns null if no data is found for the specified id.
*/
protected static org.trinet.jasi.Coda getByCoid (long coid) {
String sql = sqlPrefix + " and coid = "+ coid;
ArrayList codaList = (ArrayList) getBySQL(sql);
return (codaList.size() > 0) ? (org.trinet.jasi.Coda) codaList.get(0) : null; // only returns one Coda
}
/**
* Derives from the DataSource a Coda instance corresponding to the input coda id ( coid in the NCDC schema).
* Retrieves Coda, AssocCoO and AssocCoM data from DataSource database.
* Returns null if no data is found for the specified id.
*/
protected static org.trinet.jasi.Coda getByCoidAssoc (long coid) {
String sql = sqlPrefixByAssoc;
joinType = Type_Assoc;
ArrayList codaList = (ArrayList) getBySQL(sql);
return (codaList.size() > 0) ? (org.trinet.jasi.Coda) codaList.get(0) : null; // only returns one Coda
}
/**
* Derives from the DataSource a Collection of Coda corresponding to the input SQL query.
* Uses the default connection created by DataSource.
* Query must begin SELECT .... from Coda.
* Returns null if no data is found.
*/
protected static Collection getBySQL(String sql) {
if (DataSource.getConnection() == null) {
System.err.println ("* No DataSource is open.");
return null;
}
return getBySQL(DataSource.getConnection(), sql);
}
/**
* Derives from the input Connection a Collection of Coda corresponding to the input SQL query.
* Query must begin SELECT .... from Coda.
* Returns null if no data is found.
*/
protected static Collection getBySQL(Connection conn, String sql) {
// System.out.println ("SQL: "+sql);
ArrayList codaList = new ArrayList();
try {
if ( conn.isClosed() ) {
System.err.println ("* DataSource connection is closed");
return null;
}
Statement sm = conn.createStatement();
// NOTE: if ExecuteSQL.setSelectForUpdate(true) was set in DataSource
// this will lock the selected rows until commit() or close() are called
ResultSetDb rsdb = new ResultSetDb(ExecuteSQL.rowQuery(sm, sql));
if (rsdb == null || rsdb.getResultSet() == null) return null; // no data found
while ( rsdb.getResultSet().next() ) {
codaList.add(parseResultSet(rsdb));
}
sm.close();
}
catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
}
return (Collection) codaList;
}
/**
* Parses a ResultSet table row that possibly contains the ordered concatenation of Coda, AssocCoO and AssocCoM table columns.
*/
protected static org.trinet.jasi.Coda parseResultSet(ResultSetDb rsdb) {
org.trinet.jdbc.table.Coda coda = new org.trinet.jdbc.table.Coda(); // create new jdbc object
int offset = 0;
coda = (org.trinet.jdbc.table.Coda) coda.parseOneRow(rsdb, offset);
offset += org.trinet.jdbc.table.Coda.MAX_FIELDS;
// invoking coda.setMutable(true) allow changes to CodaTN fields aliased to this DataTableRow.
if (DataSource.isWriteBackEnabled()) coda.setMutable(true);
CodaTN newCodaTN = new CodaTN();
newCodaTN.parseCoda(coda);
// Parse depends on whether you did joint with AssocCoO or AssocCoM table.
if (joinType == Type_AssocCoO || joinType == Type_Assoc) {
AssocCoO assoc = new AssocCoO();
assoc = (AssocCoO) assoc.parseOneRow(rsdb, offset);
newCodaTN.parseAssocCoO(assoc);
if (DataSource.isWriteBackEnabled()) assoc.setMutable(true);
offset += AssocCoO.MAX_FIELDS;
}
if (joinType == Type_AssocCoM || joinType == Type_Assoc) {
AssocCoM assoc = new AssocCoM();
assoc = (AssocCoM) assoc.parseOneRow(rsdb, offset);
newCodaTN.parseAssocCoM(assoc);
if (DataSource.isWriteBackEnabled()) assoc.setMutable(true);
offset += AssocCoM.MAX_FIELDS;
}
newCodaTN.fromDbase = true; // remember that we got this from the dbase
return newCodaTN;
}
/**
* Parse data member values from input org.trinet.jdbc.table.Coda object data.
*/
protected void parseCoda(org.trinet.jdbc.table.Coda coda) {
this.coid = (DataLong) coda.getDataObject(org.trinet.jdbc.table.Coda.COID);
// use setChannel to synch with master channel list
setChannelObj(((ChannelTN)getChannelObj()).parseNameFromDataTableRow(coda));
this.authority = (DataString) coda.getDataObject(org.trinet.jdbc.table.Coda.AUTH);
this.datetime = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.DATETIME);
// Create codaDescriptor
//String codaType = ((DataString) coda.getDataObject(org.trinet.jdbc.table.Coda.CODATYPE)).toString();
String durType = ((DataString) coda.getDataObject(org.trinet.jdbc.table.Coda.DURTYPE)).toString();
String iphase = ((DataString) coda.getDataObject(org.trinet.jdbc.table.Coda.IPHASE)).toString();
this.descriptor = new CodaPhaseDescriptorTN (CodaType.getCodaType(durType), iphase);
this.source = (DataString) coda.getDataObject(org.trinet.jdbc.table.Coda.SUBSOURCE);
this.tau = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.TAU);
this.aFix = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.AFIX);
this.aFree = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.AFREE);
this.qFix = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.QFIX);
this.qFree = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.QFREE);
this.algorithm = (DataString) coda.getDataObject(org.trinet.jdbc.table.Coda.ALGORITHM);
this.uncertainty = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.ERAMP);
this.ampUnits = (DataString) coda.getDataObject(org.trinet.jdbc.table.Coda.UNITS);
this.windowSize = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.WINSIZE);
this.windowCount = (DataLong) coda.getDataObject(org.trinet.jdbc.table.Coda.NSAMPLE);
this.weightIn = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.QUALITY);
this.residual = (DataDouble) coda.getDataObject(org.trinet.jdbc.table.Coda.RMS);
this.processingState = (DataString) coda.getDataObject(org.trinet.jdbc.table.Coda.RFLAG);
parseTimeAmps(coda);
}
// Would be better if time amps fields in schema coda table row changed from integer to double
protected void parseTimeAmps(org.trinet.jdbc.table.Coda coda) {
DataObject time = coda.getDataObject(org.trinet.jdbc.table.Coda.TIME1);
DataObject amp = coda.getDataObject(org.trinet.jdbc.table.Coda.AMP1);
if (time.isNull() || amp.isNull()) {
// System.out.println("CodaTN Warning - no non-null timeamps pairs.");
return;
}
this.windowTimeAmpPairs.add( new TimeAmp(time.doubleValue(), amp.doubleValue()));
time = coda.getDataObject(org.trinet.jdbc.table.Coda.TIME2);
if( ! time.isNull()) {
if (windowCount.longValue() < 2)
System.out.println("CodaTN Warning - window count less than non-null timeamps pair count.");
amp = coda.getDataObject(org.trinet.jdbc.table.Coda.AMP2);
this.windowTimeAmpPairs.add( new TimeAmp(time.doubleValue(), amp.doubleValue()));
// Extra debug check to find more non-null values in coda row
time = coda.getDataObject(org.trinet.jdbc.table.Coda.TIME3);
if (! time.isNull())
System.out.println("CodaTN Warning - more than two non-null timeamps pair values found in coda row.");
}
}
/**
* Assign contents of this Coda object into a new Coda (TableRow) object.
* Gets a * new 'coid' from "coseq" in the database.
* @See: org.trinet.jdbc.Coda().
*/
protected org.trinet.jdbc.table.Coda toCodaRow () {
long newId = SeqIds.getNextSeq("coseq");
coid.setValue(newId);
org.trinet.jdbc.table.Coda codaRow = new org.trinet.jdbc.table.Coda(newId);
codaRow.setUpdate(true); // sets flag to enable processing
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -