📄 phasetn.java
字号:
// Debug
// System.out.println ("SQL: "+sql);
try {
if ( conn.isClosed() ) // check that valid connection exists
{
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) return null; // nothing found
while ( rsdb.getResultSet().next() ) {
phList.add(PhaseTN.parseResultSet(rsdb));
}
sm.close();
}
catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
}
return (Collection) phList;
}
/**
* Parse a resultset row that contains a concatinated Event/Origin/NetMag
*/
protected static Phase parseResultSet (ResultSetDb rsdb) {
Arrival arr = new Arrival(); // jdbc object
AssocArO assoc = new AssocArO();
int offset = 0;
// get jdbc objects from the result set
arr = (Arrival) arr.parseOneRow(rsdb, offset);
offset += Arrival.MAX_FIELDS;
assoc = (AssocArO) assoc.parseOneRow(rsdb, offset);
// allow changes to this DataTableRow. Need to set this because we are
// not using our own rather than org.trinet.jdbc parsing methods
if (DataSource.isWriteBackEnabled()) {
arr.setMutable(true);
assoc.setMutable(true);
}
// build up the Phase from the jdbc objects
PhaseTN ph = new PhaseTN();
ph.parseArrival(arr);
ph.parseAssocArO(assoc);
// remember that we got this from the dbase
ph.fromDbase = true;
return (Phase) ph;
}
/**
* Copy DataObjects out of a jdbc.Arrival object into a Phase.
* This method can be used to build up a complete
* phase by parsing rows from different tables (like AssocArO).
*/
protected void parseArrival(Arrival arr) {
// defaults
String phaseType = " ";
String ie = " ";
double decimalWt = 4;
String fm = " ";
// use setChannel to synch with master channel list
setChannelObj(ChannelTN.parseNameFromDataTableRow(arr));
this.arid = (DataLong) arr.getDataObject(Arrival.ARID);
this.datetime = (DataDouble) arr.getDataObject(Arrival.DATETIME);
this.processingState = (DataString) arr.getDataObject(Arrival.RFLAG);
this.incidence = (DataDouble) arr.getDataObject(Arrival.EMA);
this.authority = (DataString) arr.getDataObject(Arrival.AUTH);
this.source = (DataString) arr.getDataObject(Arrival.SUBSOURCE);
// Create phaseDescription based on stuff in dbase
DataString dataStr = (DataString) arr.getDataObject(Arrival.IPHASE);
if (!dataStr.isNull()) phaseType = dataStr.toString();
dataStr = (DataString) arr.getDataObject(Arrival.QUAL);
if (!dataStr.isNull()) ie = dataStr.toString();
dataStr = (DataString) arr.getDataObject(Arrival.FM);
if (!dataStr.isNull()) fm = dataStr.toString();
DataDouble quality = (DataDouble) arr.getDataObject(Arrival.QUALITY);
if (!quality.isNull()) decimalWt = quality.doubleValue();
this.description = new PhaseDescription (phaseType, ie, fm, decimalWt);
// Save the arrivalRow for future use (commit)
arrivalRow = arr;
return;
}
/**
* Stuff contents from this Phase object into a new Arrival (TableRow) object. Gets a
* new 'arid' from "arseq" in the dbase.
* @See: org.trinet.jdbc.Arrival().
*/
protected Arrival toArrivalRow () {
// Always make a new row, get unique key #
// arid.setValue(SeqIds.getNextSeq("arseq"));
// Arrival arrivalRow = new Arrival(arid.longValue());
// if (arid.isNull()) {
long newId = SeqIds.getNextSeq("arseq");
arid.setValue(newId); // set ID for this.Phase
// }
Arrival arrivalRow = new Arrival(arid.longValue()); // set ID for arrival row
if (debug) System.out.println ("toArrivalRow new id: " + arrivalRow.toString());
// Stuff contents of channel name.
stuffChannel(arrivalRow, getChannelObj());
// set flag to enable processing
arrivalRow.setUpdate(true);
arrivalRow.setValue(Arrival.DATETIME, datetime);
// the following can be null
if (!authority.isNull()) arrivalRow.setValue(Arrival.AUTH, authority);
if (!processingState.isNull())
arrivalRow.setValue(Arrival.RFLAG, processingState);
if (!source.isNull()) arrivalRow.setValue(Arrival.SUBSOURCE, source);
// phase description.
// if (description.hasChanged()) {
arrivalRow.setValue(Arrival.IPHASE, description.iphase);
arrivalRow.setValue(Arrival.QUAL, description.ei);
arrivalRow.setValue(Arrival.QUALITY, description.getQuality());
arrivalRow.setValue(Arrival.FM, description.fm);
// }
// all the other fields are not used by us
if (debug) {
System.out.println ("phase : "+this.toString());
System.out.println ("arrivalRow: "+arrivalRow.toString());
}
return arrivalRow;
}
/** Put the channel name attributes in the Arrival DataTableRow */
protected void stuffChannel (Arrival arrivalRow, Channel cn) {
// this one can't be null
arrivalRow.setValue(Arrival.STA, cn.getSta());
// by default all fields are "" rather then null strings: see ChannelName
if (cn.getNet().length() > 0) arrivalRow.setValue(Arrival.NET, cn.getNet());
if (cn.getAuth().length() > 0) arrivalRow.setValue(Arrival.AUTH, cn.getAuth());
if (cn.getSubsource().length() > 0) arrivalRow.setValue(Arrival.SUBSOURCE, cn.getSubsource());
if (cn.getChannel().length() > 0) arrivalRow.setValue(Arrival.CHANNEL, cn.getChannel());
if (cn.getChannelsrc().length() > 0) arrivalRow.setValue(Arrival.CHANNELSRC, cn.getChannelsrc());
if (cn.getSeedchan().length() > 0) arrivalRow.setValue(Arrival.SEEDCHAN, cn.getSeedchan());
if (cn.getLocation().length() > 0) arrivalRow.setValue(Arrival.LOCATION, cn.getLocation());
}
/**
* Copy DataObjects out of a jdbc.AssocArO object into a Phase.
* This method can be used to build up a complete
* phase by parsing rows from different tables (like Arrival).
*/
protected void parseAssocArO(AssocArO assoc)
{
this.orid = (DataLong) assoc.getDataObject(AssocArO.ORID);
chan.dist = (DataDouble) assoc.getDataObject(AssocArO.DELTA);
// setDistance( ((DataDouble) assoc.getDataObject(AssocArO.DELTA)).doubleValue());
this.getChannelObj().azimuth = (DataDouble) assoc.getDataObject(AssocArO.SEAZ);
// this.incidence = (DataDouble) assoc.getDataObject(AssocArO.EMA);
this.weightIn = (DataDouble) assoc.getDataObject(AssocArO.IN_WGT);
this.weightOut = (DataDouble) assoc.getDataObject(AssocArO.WGT);
this.residual = (DataDouble) assoc.getDataObject(AssocArO.TIMERES);
// Save the assocRow for future writeBack
assocRow = assoc;
return;
}
/**
* Stuff contents of this Phase/association into an AssocArO (TableRow) object.
*/
protected AssocArO toAssocArORow () {
// always create a new one
assocRow = new AssocArO();
// if no association...
if (!isAssociated() ) return null;
long assOrid = ((SolutionTN)sol).getOrid();
if (assOrid == 0) return null;
if (arid.isNull()) return null; // no arid!
// set flag to enable processing
assocRow.setUpdate(true);
// (KEY #1) assumes ARID has been set for this phase
assocRow.setValue(AssocArO.ARID, arid);
// (KEY #2) set ORID to orid of associated Solution
// if (debug) System.out.println ("assoc Sol: "+sol.toString()+ " orid="+assOrid);
//mutable?
assocRow.setValue(AssocArO.ORID, assOrid);
// the following can be null
if (!authority.isNull()) assocRow.setValue(AssocArO.AUTH, authority);
if (!source.isNull()) assocRow.setValue(AssocArO.SUBSOURCE, source);
// set flag to allow changes
// assocRow.setMutable(true);
// if (orid.isNull()) assocRow.setValue(AssocArO.ORID, orid);
if (!chan.dist.isNull()) assocRow.setValue(AssocArO.DELTA, getDistance());
if (!getChannelObj().azimuth.isNull()) assocRow.setValue(AssocArO.SEAZ, getChannelObj().azimuth);
if (!weightIn.isNull()) assocRow.setValue(AssocArO.IN_WGT, weightIn);
if (!weightOut.isNull()) assocRow.setValue(AssocArO.WGT, weightOut);
if (!residual.isNull()) assocRow.setValue(AssocArO.TIMERES, residual);
// phase description. Note: this is redundant with Arrival.IPHASE
assocRow.setValue(AssocArO.IPHASE, description.iphase);
if (!processingState.isNull())
assocRow.setValue(AssocArO.RFLAG, processingState);
if (debug) {
System.out.println ("phase : "+this.toString());
System.out.println ("assocRow: "+assocRow.toString());
}
return assocRow;
}
public static void doWriteBackTest (Solution sol, Phase phx) {
// test phase descp
System.out.println ("ExecuteSQL.isSelectForUpdate = "+
ExecuteSQL.isSelectForUpdate());
boolean status = true;
long evid = 9691944;
Solution solx = Solution.create().getById(evid);
// NOTE: you would NEVER unassociate phases from an origin in the dbase;
// that would alter the origin.
// You would only NOT associate them with the new origin.
System.out.println ("\n----- New associate test ----- old id = "+
sol.id.toString()+" new evid= "+evid);
phx.associate(solx);
status = phx.commit();
if (status) {
long arid = ((PhaseTN) phx).getArid();
System.out.println ("<><><><> commit (insert) status = "+status+
"(arid = "+arid+" )");
// readback check
System.out.println ("Readback from dbase...");
Phase cph = (Phase) PhaseTN.getByArid(arid);
System.out.println (cph.toString());
} else {
System.out.println ("<><><><> commit FAILED (insert) status = "+status);
}
System.out.println ("\n----- Insert new phase test -----");
// Make an new phase
Phase newPh = Phase.create();
java.util.Calendar cal = java.util.Calendar.getInstance();
java.util.Date date = cal.getTime(); // current epoch millisec
long now = date.getTime()/1000; // current epoch sec (millisecs -> seconds)
newPh.datetime.setValue(now);
newPh.authority.setValue(EnvironmentInfo.getNetworkCode());
// newPh.source.setValue("RT1");
newPh.source.setValue("Test");
// need to associate with a solution
newPh.associate(sol);
newPh.description = new PhaseDescription("P", "i", "c.", 0);
newPh.setChannelObj( Channel.create().setChannelName("CI", "XYZ", "BHZ"));
System.out.println (newPh.toString());
status = newPh.commit();
if (status) {
long arid = ((PhaseTN) newPh).getArid();
System.out.println ("<><><><> commit (insert) status = "+status+
"(arid = "+arid+" )");
// readback check
System.out.println ("Readback from dbase...");
Phase cph = (Phase) PhaseTN.getByArid(arid);
System.out.println (cph.toString());
} else {
System.out.println ("<><><><> commit (insert) status = "+status);
}
System.out.println ("\n----- No Change test -----");
// committ again with no changes made, nothing should happen
status = newPh.commit();
if (status) {
long arid = ((PhaseTN) newPh).getArid();
System.out.println ("<><><><> commit (insert) status = "+status+
"(arid = "+arid+" )");
// readback check
System.out.println ("Readback from dbase...");
Phase cph = (Phase) PhaseTN.getByArid(arid);
System.out.println (cph.toString());
} else {
System.out.println ("<><><><> commit (insert) status = "+status);
}
// DataSource.commit();
}
// ///////////////////////////////////////////////////////
public static void main (String args[])
{
System.err.println ("Making DataSource connection...");
new TestDataSource("k2"); // make connection
System.err.println ("Setting ExecuteSQL.setSelectForUpdate = true");
System.out.println ("DataSource.isReadonly = "+
DataSource.isReadOnly());
System.out.println ("DataSource.isWriteBackEnabled = "+
DataSource.isWriteBackEnabled());
System.out.println ("ExecuteSQL.isSelectForUpdate()= "+
ExecuteSQL.isSelectForUpdate());
// long evid = 9526705;
// long evid = 9691940;
long evid = 9629677;
System.out.println (" Getting phases for evid = "+evid);
Solution sol = Solution.create().getById(evid);
PhaseList ph = new PhaseList (Phase.create().getBySolution(sol));
System.out.println ("Phase count = " + ph.size());
System.out.println (ph.toNeatString());
//
// doWriteBackTest(sol, (Phase) ph.get(0));
DataSource.close();
}
} // PhaseTN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -