📄 solutiontn.java
字号:
* Enforce schema length and notnull constraints. Truncate if necessary. If it is null
* will default to EnvironmentInfo.getApplicationName()*/
String getSource() {
String str = source.toString();
if (source.isNull()) str = EnvironmentInfo.getApplicationName();
int end = Math.min(str.length(), MaxSubsourceSize) ;
return str.substring(0, end);
}
/** Get EVENT.auth.
* Enforce schema length and notnull constraints. Truncate if necessary.
* AUTH is a 'NOT NULL' attribute so if the internal value is null
* default to "??". That shouldn't happen because the Solution constructor
* always sets it to "?". */
String getEventAuthority() {
String str = eventAuthority.toString();
if (eventAuthority.isNull()) str = "??";
int end = Math.min(str.length(), MaxAuthSize) ;
return str.substring(0, end);
}
/** Get EVENT.subsource.
* Enforce schema length and notnull constraints. Truncate if necessary. If it is null
* will default to EnvironmentInfo.getApplicationName()*/
String getEventSource() {
String str = eventSource.toString();
if (eventSource.isNull()) str = EnvironmentInfo.getApplicationName();
int end = Math.min(str.length(), MaxSubsourceSize) ;
return str.substring(0, end);
}
// TODO: must translate from model string to model int
private int getCrustalModelId(String crustModel) {
return 0;
}
// TODO: must translate from model string to model int
private int getVelocityModelId(String velModel) {
return 0;
}
// TODO: must translate from model string to model int
private String getCrustalModelString(int crustModelId) {
return "";
}
// TODO: must translate from model string to model int
private String getVelocityModelString(int velModelId) {
return "";
}
/*
STATES:
o The class has a boolean deleteFlag.
o The fromDbase boolean tells us if we will 'insert' or 'update' on commit.
o If the individual DataObject.isUpdate() = true, an update is necessary.
Commit action matrix:
fromDbase = true false
deleteFlag=true dbaseDelete noop
isUpdate()=true update insert
isUpdate()=false noop insert
*/
/** Commit changes or delete to underlying DataSource. To write to the
DataSource, the flag DataSource.isWriteBackEnabled() must be true. This
must be set with DataSource.setWriteBackEnabled(true) BEFORE data is
read. <p>
Behavior:<p>
1) If event was not originally read from the dbase, new EVENT, ORIGIN and NETMAG rows are inserted.<p>
2) If the event was read from the dbase and:<br>
a) no changes were made, nothing is done to the dbase.<br>
b) changes were made, new ORIGIN and NETMAG rows are inserted, and the original EVENT row's <it>prefor</it> and <it> prefmag </it> entries are pointed at the new rows.<br> (Note that a new NETMAG row is written event if it has not changed. if the ORIGIN changes the NETMAG row <b>should</b> change. This rule would need to be enforced by the application.
If there is a problem with this commit it will do a rollback. This means that any
transactions done before this one can be lost if DataSource.commit() is not called
first.
*/
public boolean commit() throws JasiCommitException {
boolean status = false;
commitStatus = "";
long evid = id.longValue();
// you can't write to the dbase
if (!DataSource.isWriteBackEnabled()) {
commitStatus = "Database is NOT write enabled.";
if (debug) System.out.println(commitStatus);
throw new JasiCommitException(commitStatus);
//return false;
}
if (debug) {
System.out.println ("SolutionTN.commit() : fromDbase= "+ fromDbase);
System.out.println (" isDeleted() = "+ isDeleted());
System.out.println (" hasChanged()= "+ hasChanged());
}
// ////////////////////////////////////////////////////////////////////////
// Event/Origin DELETE
// ////////////////////////////////////////////////////////////////////////
// <delete from dbase>
try {
if (isDeleted()) {
return commitDelete();
// ////////////////////////////////////////////////////////////////////////
// Event/Origin UPDATE/INSERT
// ////////////////////////////////////////////////////////////////////////
} else if (hasChanged()) {
// insert comment early so commid gets written with Event row
dbaseInsertComment();
// update if Solution was originally from the dbase
if (fromDbase) {
if (debug) System.out.println ("Update existing event...");
status = dbaseUpdate();
// insert if this is a new Solution
} else {
if (debug) System.out.println ("Insert new event...");
status = dbaseInsert();
}
}
} catch (Exception ex) { // SQL or JasiCommitException
System.err.println(ex);
ex.printStackTrace();
rollback();
commitStatus = "Event/Origin insert/update failed for event "+evid;
throw new JasiCommitException(commitStatus);
}
// Do intermediate commit of summary info, rollback and bail if problems
if (status) { // something got written :. commit
try {
if (debug) System.out.println ("DataSource.commit event >>> ");
getConnection().commit();
} catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
rollback();
commitStatus += "\nEvent/Origin commit failed.";
throw new JasiCommitException(commitStatus);
}
commitStatus += "\nEvent/Origin commit succeeded." +evid;
} else {
commitStatus = "No change to Event/Origin.";
if (debug) System.out.println (commitStatus);
}
// ////////////////////////////////////////////////////////////////////////
// Magnitude
// ////////////////////////////////////////////////////////////////////////
// MAGNITUDE: It may have changed even if Solution didn't.
// Magnitude.commit() will do more checks and do the right thing
if (magnitude != null) {
try {
status = magnitude.commit();
} catch (Exception ex) {
System.err.println(ex);
ex.printStackTrace();
rollback();
status = false;
commitStatus += "\nWrite of magnitude failed.";
// NON-FATAL
// throw new JasiCommitException("Magnitude database write failed.");
}
if (status) { // Do intermediate commit of mag info
commitStatus += "\nWrite of magnitude succeeded.";
try {
if (debug) System.out.println ("DataSource.commit mag >>> ");
getConnection().commit();
commitStatus += "\nCommit of magnitude succeeded.";
} catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
commitStatus += "\nCommit of magnitude failed.";
// NON-FATAL return false;
} // end of try/catch
} else {
commitStatus += "\nNo change to Magnitude.";
if (debug) System.out.println ("No change to magnitude.");
}
} // end of "if (mag..."
// ////////////////////////////////////////////////////////////////////////
// Alternate Magnitudes
// ////////////////////////////////////////////////////////////////////////
//
// Removed this because it is nasty.
//
// 1) Old mags become bogus once a new solution is created.
//
/*
if (commitAltMagList()) {
try {
if (debug) System.out.println ("DataSource.commit alt mags >>> ");
getConnection().commit();
commitStatus += "\nCommit of alternate magnitudes succeeded.";
} catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
commitStatus += "\nWrite of alternate magnitudes failed.";
}
} else {
rollback();
}
*/
// Now that all mags are saved, let the dbase logic decide which mag to use
// This does a dbase commit!
//MagnitudeTN.dbaseAutoSetPrefMag(getConnection(), id.longValue());
// ////////////////////////////////////////////////////////////////////////
// WAVEFORMS <AssocWaE> write waveform associations
// ////////////////////////////////////////////////////////////////////////
if (commitWaveformList()) {
try {
if (debug) System.out.println ("DataSource.commit waveforms >>> ");
getConnection().commit();
commitStatus += "\nCommit of wavforms succeeded.";
} catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
commitStatus += "\nWrite of waveform list failed.";
}
} else {
rollback();
}
setNeedsCommit(false);
return status;
} // end of commit()
/** Rollback the last set of dbase transactions. Catch exceptions and print them.
* Returns false on exception. */
protected boolean rollback () {
try {
getConnection().rollback();
} catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
return false;
}
return true;
}
/** Do everything necessary to delete event from dbase. */
protected boolean commitDelete() throws JasiCommitException {
long evid = getId().longValue();
boolean status;
if (debug) System.out.println ("Deleting "+evid );
// No reason to continue with mag, etc. if its a delete
// Note: commit is done in dbaseDelete()
try {
commitStatus = "Event/Origin delete failed for event "+evid;
status = dbaseDelete();
if (status) commitStatus = "Event/Origin delete succeeded for event "+evid;
} catch (Exception ex) { // SQL or JasiCommitException
System.err.println(ex);
ex.printStackTrace();
rollback();
commitStatus = "Event/Origin delete failed for event "+evid;
throw new JasiCommitException(commitStatus);
}
setNeedsCommit(false);
return status;
}
/** Commit any additions, changes or deletions to the data source plus
* take any site-specific actions for a final solution. */
public boolean finalCommit() throws JasiCommitException {
boolean status = true;
if (getNeedsCommit()) status = commit();
// commit succeeded or unnecessary, finalize
//if (status) status = dbaseFinalize();
// finalize if no exception was thrown
status = dbaseFinalize();
return status;
}
/**
* Delete event from the database. This calls a stored procedure, so the
* actual meaning of "delete" is defined there. (This is a virtual delete that sets
* selectflag = 0). The change is commited in this method.
*/
protected boolean dbaseDelete () throws JasiCommitException {
if (fromDbase) {
// Call stored procedure that knows rules for deleting an event
// The procedure does a commit so you don't have to.
String sql = "call jasi.Delete_Event("+id.longValue()+")";
return doSql(sql);
} else { // not from dbase :. nothing to delete
// if (debug) System.out.println ("Nothing to delete");
return true;
}
}
/**
* Finalize event in the database. This calls a stored procedure, so the
* actual meaning of "finalize" is defined there.
*/
protected boolean dbaseFinalize () throws JasiCommitException {
// Call stored procedure that knows rules for finalizing an event
String sql = "call jasi.Finalize_Event("+id.longValue()+")";
System.out.println (sql);
return doSql(sql);
}
/** Execute the sql string, return false on error */
protected boolean doSql (String sql) throws JasiCommitException {
/// make sure there's a connection
if (getConnection() == null) {
throw new JasiCommitException("No connection to data source.");
//return false;
}
try {
Statement sm = getConnection().createStatement();
sm.execute(sql);
} catch (SQLException ex) {
System.out.println ("SQL: "+sql);
System.err.println(ex);
ex.printStackTrace();
// rollback on error
try {
getConnection().rollback();
} catch (SQLException ex2) { // rollback failed
System.err.println("Rollback failed:\n"+ex2);
ex2.printStackTrace();
return false;
} // end of try/catch
return false;
}
return true;
}
/**
* Insert a new row in the dbase
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -