⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phasetn.java

📁 一个用java写的地震分析软件(无源码)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.trinet.jasi.TN;

import org.trinet.jasi.*;

import java.util.*;
import java.sql.*;

import org.trinet.jdbc.*;
import org.trinet.jdbc.datatypes.*;
import org.trinet.jdbc.table.*;
import org.trinet.util.*;

/**
 * PhaseTN.java
 *
 * Concrete schema-specific implementation
 *
 * Created: Fri Oct 29 12:22:36 1999
 *
 * @author Doug Given
 * @version
 */

/*

  There are two types of phases (distingushed by 'fromDbase' = true/false):
  1) those that were read from the dbase
    - these would never be deleted, updated, or inserted
    - you would never delete or update an AssocArO
    - you may create a new AssocArO associating them with an new origin

  2) those created by the application
    - there's nothing in the dbase to update or delete
          - they must be inserted in the dbase to save them
    - you MUST create a new AssocArO for them if they are to be meaningful
 */
public class PhaseTN extends Phase {


    // Keep references to the original row objects for when we write back
    //    Arrival  arrivalRow = new Arrival();
    //    AssocArO assocRow   = new AssocArO();
    Arrival  arrivalRow;
    AssocArO assocRow;

    /** the orid of association */
    protected DataLong orid   	= new DataLong();
    /** the arid */
    protected DataLong arid   	= 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;

    // The standard start to most SQL queries
    static String sqlPrefix = "Select "+
  Arrival.QUALIFIED_COLUMN_NAMES+","+
  AssocArO.QUALIFIED_COLUMN_NAMES+
  " from Arrival, AssocArO where (Arrival.arid = AssocArO.arid) ";

    boolean debug = false;
    //boolean debug = true;

/**
 * Constructor
 */
public PhaseTN ()
{
}

    /** Copy the phase in the arg's info into this phase. */

    // NOTE: use assignment NOT setValue() because setValue() sets isNull flag false
    // even if the new value is in fact null.
public boolean replace(Phase ph) {

    // use .setValue() to keep DataObject flags right
    setChannelObj( (Channel) ph.getChannelObj().clone() );

    // DataObjects
    this.datetime.setValue(ph.datetime.doubleValue()) ;

    /** Descripton of the phase. @See org.trinet.jasi.PhaseDescription */
    this.description = ph.description;

    this.processingState = ph.processingState;	// rflag

    /** Solution which which this phase is associated. Is null if unassociated */
    this.sol = ph.sol ;

    // Things relating to the associated Solution

    // ***** WARNING *****
    // The setValue() method will set the isNull() flag false
    // even if the value set is in fact null!

    // use .setValue() to keep DataObject flags right
    /*
    this.distance.setValue(ph.distance.doubleValue()) ;
    this.azimuth.setValue(ph.azimuth.doubleValue()) ;
    this.incidence.setValue(ph.incidence.doubleValue()) ;
    this.weightIn.setValue(ph.weightIn.doubleValue()) ;
    this.weightOut.setValue(ph.weightOut.doubleValue()) ;
    this.residual.setValue(ph.residual.doubleValue()) ;
    */

    setDistance(ph.getDistance());
    setHorizontalDistance(ph.getHorizontalDistance());
    this.getChannelObj().azimuth   = ph.getChannelObj().azimuth ;
    this.incidence = ph.incidence;
    this.weightIn  = ph.weightIn ;
    this.weightOut = ph.weightOut ;
    this.residual  = ph.residual ;

    return true;
}

    /** Commit changes or delete to underlying DataSource. The action taken will
        depend on the state of the phase. It may or may not be originally
        from the data source, it may be deleted, unassociated, modified, or
        unchanged.  To write to the DataSource, the flag
        DataSource.isWriteBackEnabled() must be true. This must be set with
        DataSource.setWriteBackEnabled(true) BEFORE data is read.*/

public boolean commit() {

    // you can't write to the dbase
    if (!DataSource.isWriteBackEnabled()) return false;

    // set the 'rflag' value to "H" for human checked
    //!! This might not be right here! What if this class is used by an auto program?
    //    processingState.setValue("H");

    // debug
    if (debug) System.out.println ("PhaseTN.commit() : fromDbase= "+ fromDbase+
           "  isDeleted()= "+isDeleted());

    // <DELETE>
    // Phase is deleted or not associated! So don't write or associate it.
    if (isDeleted() || !isAssociated()) return true;

    // Check that 'orid' of associated event has been set
    long orid = ((SolutionTN)sol).getOrid();
    if ( orid == 0 ) {
  if (debug) System.out.println ("Associated Solution has no orid.");
  return false;
    }

    // "existing" phase has changed insert a new table row (with new number)
    // and a new association
    if (fromDbase) {

  if (hasChanged()) {

      if (debug) System.out.println ("(changed phase) INSERT and assoc");
      return dbaseInsert();		// insert & associate
  } else {
      if (debug) System.out.println ("(phase unchanged) assoc only");
      // if no changes to arrival row, don't change just make new assoc
      if (dbaseAssociate()) {
            setUpdate(false);    // now there's a dbase row
            return true;
         } else {
            return false;
         }
  }

    // <INSERT>
    } else {	// not from dbase

  if (debug) System.out.println ("(new phase) INSERT and assoc");

  return dbaseInsert();	// insert & associate
    }

}

/**
 * NEVER delete an arrival. Never remove association with another origin.  If
you don't like it just don't assoc it with your new origin.  */
protected boolean dbaseDelete () {

    if (fromDbase) {

  return true;

    } else {		// not from dbase :. nothing to delete

  return true;
    }
}


/**
 * Make AssocArO row for this Arrival
 */
protected boolean dbaseAssociate () {

    if ( !isAssociated() ) return false;

    AssocArO assocRow = toAssocArORow();
    assocRow.setProcessing(DataTableRowStates.INSERT);
    return (assocRow.insertRow(DataSource.getConnection()) > 0);

}

/**
 * Insert a new row in the dbase
 */
protected boolean dbaseInsert () {

    boolean status = true;

    if (debug) System.out.println ("dbaseInsert: "+this.toString());

    Arrival arrivalRow = toArrivalRow();
    arrivalRow.setProcessing(DataTableRowStates.INSERT);

    // write arrival row
    status = (arrivalRow.insertRow(DataSource.getConnection()) > 0);

    if (status) {		// success
     setUpdate(false);             // set flag to indicate there's a dbase row
  status = dbaseAssociate();	// write association row
    }

    return status;
}

/**
 * Return the 'arid' of the row (if it came from the database). If it did not
 * come from the data base 0 is returned.
 */
protected long getArid () {

    if (arid.isNull()) return 0;
    return arid.longValue();
}

/**
 * Return true if the phasedescription or time has changed from what was
 * read in from the dbase. Returns true if phase was not originally from the
 * dbase.  */
protected boolean hasChanged () {

    if (!fromDbase ||
  description.hasChanged() ||
  //	     chanName.isUpdate() ||
  datetime.isUpdate()
  ) return true;

    return false;
}
/**
 * Set the isUpdate() flag for all data dbase members the given boolean value.  */
protected void setUpdate (boolean tf) {

          fromDbase = !tf;
          description.remember(description);
          datetime.setUpdate(tf);
}
/**
 * Extract from DataSource all phases associated with this Solution.
 */

// Don't call getBySolutionId() because it has the expensive step of looking up
// the prefor which is already known to the Solution.
public Collection getBySolution (Solution tsol) {

    String sql = sqlPrefix + " and (AssocArO.orid = "+((SolutionTN)tsol).prefor.toString()+")";

    ArrayList ph = (ArrayList) PhaseTN.getBySQL(sql);

    // set associated solution to the one in the arg
    for (int i = 0; i<ph.size(); i++) {
  ((Phase)ph.get(i)).associate(tsol);
    }

    return ph;
}
/**
 * Extract from DataSource all phases associated with this Solution ID.
 */
public Collection getBySolution (long id) {

    // must first  retreive the Solution, then look up prefor for this id
    Solution tsol = Solution.create().getById(id);

    if (tsol == null) return null;	// no such solution in DataSource

    return getBySolution(tsol);
}

/**
 * Extract from DataSource all phases for this time window. The SolutionList
 * is used to match associated phases with solutions in the list. If a
 * Phase is associated with a Solution not in the list that Solution will be
 * added to the list.<p>
 * If you pass in an empty SolutionList, it will be populated with the Solutions
 * that match the Phases found in the time window.
 */
/*
  There is a problem here. Know the ORID they're associated with but don't
  know have a Solution object.
  Don't want to look them up but rather draw from an extant Sol list.
*/
public static Collection getByTime (double start, double end, SolutionList sl) {

    ArrayList phList = (ArrayList) Phase.create().getByTime(start, end);
    PhaseTN ph;

    if (phList.size() != 0) {

    // Match up with associated solutions. Get from dbase is necessary.

  Solution tsol;
  for (int i = 0; i < phList.size(); i++) {

      ph = (PhaseTN) phList.get(i);		//pull phase from collection

      // is it in the solution list?
      tsol = sl.getByOrid(ph.orid.longValue());	// lookup sol by ORID of phase

      if (tsol != null) {		// a match
    ph.associate(tsol);
      } else {			// no match, look in dbase for Solution

    tsol = SolutionTN.getByOrid(ph.orid.longValue());

    if (tsol != null){
        ph.associate(tsol);
        sl.addByTime(tsol);	// add to the Solution list
    }
      }

  }
    }

    return phList;
}


public Collection getByTime (double start, double end) {

    String sql = sqlPrefix + " and Arrival.datetime between "+
     start+" and "+ end;

    ArrayList phList = (ArrayList) PhaseTN.getBySQL(sql);
    Phase ph;

    return phList;
}

/**
 * Extract from DataSource all phases associated with this Solution ID.
 */
public static Collection getByTime (TimeSpan ts, SolutionList sl) {

    return getByTime (ts.getStart(),  ts.getEnd(), sl);

}

/**
 * Return a phase for the given Arid in the NCDC schema. Does not get assoc info.
 */
protected static Phase getByArid (long arid) {

    String sql = sqlPrefix + " and AssocArO.arid = "+ arid;

    ArrayList phList = (ArrayList) PhaseTN.getBySQL(sql);
    Phase ph;

    // only returns one Phase
    if (phList.size() > 0) return (Phase) phList.get(0);
    return null;
}

    /**
     * Returns array of Phases based on the results of the SQL query to an
     * NCDCv1.5 data base. It must be of the form:<p> * Select Arrival.*,
     * AssocArO.* from Arrival, AssocArO * where (Arrival.arid = AssocArO.arid)
     * <p> because you must do a join of Arrival and AssocArO to get all the
     * info we need.  Returns null if no data is found.  Uses default connection
     * created by DataSource.  */
    protected static Collection getBySQL(String sql)
    {
  if (DataSource.getConnection() == null)
      {
    System.err.println ("* No DataSource is open.");
    return null;
      }
  return getBySQL(DataSource.getConnection(), sql);
    }

    /**
     * Returns array of Phases based on the results of the SQL query to an
     *	      NCDCv1.5 * data base. It must be of the form:<p> * Select
     *	      Arrival.*, AssocArO.* from Arrival, AssocArO where (Arrival.arid =
     *	      AssocArO.arid) <p> * because you must do a join of Arrival and
     *	      AssocArO to get all the info we need.  * Returns null if no data
     *	      is found.  */
    protected static Collection getBySQL(Connection conn, String sql)
    {
  //	Vector vec = new Vector();

  ArrayList phList = new ArrayList();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -