home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / RelationViewPlus.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  12.2 KB  |  387 lines

  1. package symantec.itools.db.pro;
  2.  
  3. import symantec.itools.db.beans.binding.*;
  4. import java.sql.*;
  5. import java.util.Vector;
  6. import java.math.BigDecimal;
  7. import java.io.*;
  8.  
  9. public class RelationViewPlus extends RelationView
  10.     implements BasicDataSource
  11. {
  12.     private MediatorDS      _mediatorDS = null;
  13.     protected Vector        triggerListeners = null;
  14.     private final String    RowNumber = "RowNumber";
  15.     private final String    RowState = "RowState";
  16.     private final String    CurrentDataSource = "CurrentDataSource";
  17.     String[]                SpecialFeatures = { RowNumber, RowState, CurrentDataSource };
  18.     public final int        numberOfSpecialFeatures = 3;
  19.     RelationViewMetaData    m_MetaData = null;
  20.  
  21.     public RelationViewPlus(Request request) throws SQLException
  22.     {
  23.         super(request);
  24.     }
  25.  
  26.     public RelationViewPlus(AutoDetail detail) throws SQLException
  27.     {
  28.         super(detail);
  29.     }
  30.  
  31.     public void setName(String name) throws SQLException
  32.     {
  33.         super.setName(name);
  34.         if (java.beans.Beans.isDesignTime()) {
  35.             return;
  36.         }
  37.         init();
  38.     }
  39.  
  40.     void  goToInitialRecPos() throws SQLException
  41.     {
  42.         super.goToInitialRecPos();
  43.         triggerUI();
  44.     }
  45.  
  46.     void notifyDataChange(int projID,Record rec)
  47.     {
  48.         super.notifyDataChange(projID, rec);
  49.         //This notify only for current record
  50.         //If record not current record, do nothing
  51.         // if(rec == _rec) {
  52.             triggerUI();
  53.         // }
  54.     }
  55.  
  56.     void notifyRowStateChange() throws SQLException
  57.     {
  58.         super.notifyRowStateChange();
  59.         if (isBindingNotifyEnabled()) {
  60.             triggerUI();
  61.         }
  62.     }
  63.  
  64.     void notifyRowChange() throws SQLException
  65.     {
  66.         super.notifyRowChange();
  67.         if (isBindingNotifyEnabled()) {
  68.             triggerUI();
  69.         }
  70.     }
  71.  
  72.     void notifyRecordSetChange() throws SQLException
  73.     {
  74.         super.notifyRecordSetChange();
  75.         if (isBindingNotifyEnabled()) {
  76.             triggerUI();
  77.         }
  78.     }
  79.  
  80.     private void init() throws SQLException
  81.     {
  82.         triggerListeners = new Vector();
  83.  
  84.         _mediatorDS = new MediatorDS();
  85.         _mediatorDS.setOutput(this);
  86.  
  87.         String[] getm={"getValue(Row,Col)"};
  88.         String[] setm={"setValue(Value,Row,Col)"};
  89.  
  90.         _mediatorDS.setSetMethods(setm);
  91.         _mediatorDS.setGetMethods(getm);
  92.  
  93.         String name = getDataName(Name.ColumnSeparator);
  94.         String Special = "";
  95.  
  96.         for(int i = 0; i < SpecialFeatures.length; i++) {
  97.            // Special=Special+SpecialFeatures[i]+Name.ColumnSeparator;
  98.            Special=Special+Name.ColumnSeparator+SpecialFeatures[i];
  99.         }
  100.         Name iName = new Name(getName(),name.substring(name.indexOf(Name.TableSeparator)+1,name.length())+Special);
  101.         _mediatorDS.setDataBinding(iName.getFullName());
  102.  
  103.         m_MetaData = getMetaData();
  104.     }
  105.  
  106.     public String getDataName(String pattern) throws SQLException
  107.     {
  108.         String strName = getName() + Name.TableSeparator;
  109.         int i;
  110.         RelationViewMetaData metadata = getMetaData();
  111.         int colCount = metadata.getColumnCount();
  112.         for(i = 1; i <= colCount - 1; i++) {
  113.             strName += metadata.getColumnName(i) + pattern;
  114.         }
  115.         strName += metadata.getColumnName(i);
  116.         return strName;
  117.     }
  118.  
  119.     /**
  120.      * Implementing the basicDataSource Interface
  121.      */
  122.     public Object getValue(int row, int column)
  123.     {
  124.         Object value = "";
  125.         try {
  126.             RelationViewMetaData metadata = getMetaData();
  127.             int colCount = metadata.getColumnCount();
  128.             if (column < colCount) {
  129.                 if (getCurrentRecordState() != Record.RECSTATE_INVALID) {
  130.                     value = getColumnValue(column + 1); // 1 based
  131.                 }
  132.             }
  133.             else {
  134.                 value = getSpecialFeature(column - colCount, row);
  135.             }
  136.         }
  137.         catch (IOException e) {
  138.         }
  139.         catch (SQLException e) {
  140.         }
  141.         return value;
  142.     }
  143.  
  144.     public void setValue(Object value, int row, int column)
  145.     {
  146.         try {
  147.             RelationViewMetaData metadata=getMetaData();
  148.  
  149.             int colCount = metadata.getColumnCount();
  150.             if (column < colCount) {
  151.                 if (getCurrentRecordState() != Record.RECSTATE_INVALID) {
  152.                     if (value == null) {
  153.                         setNull(column + 1, getColumnType(column + 1)); // 1 based
  154.                     }
  155.                     else {
  156.                         setColumnValue(column + 1, value);  // 1 based
  157.                     }
  158.                 }
  159.             }
  160.             else {
  161.                 setSpecialFeature(column - colCount, row, value);
  162.             }
  163.         }
  164.         catch (IOException e) {
  165.         }
  166.         catch (SQLException e) {
  167.         }
  168.     }
  169.  
  170.     public synchronized void addTriggerUIListener(TriggerUIListener mds)
  171.     {
  172.         triggerListeners.addElement(mds);
  173.         triggerUI();
  174.     }
  175.  
  176.     public synchronized void removeTriggerUIListener(TriggerUIListener mds)
  177.     {
  178.         triggerListeners.removeElement(mds);
  179.     }
  180.  
  181.     protected void triggerUI()
  182.     {
  183.         synchronized (this) {
  184.             if (triggerListeners == null) {
  185.                 return;
  186.             }
  187.         }
  188.         Vector l;
  189.         TriggerUIEvent tuie = new TriggerUIEvent(this);
  190.         synchronized(this) {
  191.             l = (Vector)triggerListeners.clone();
  192.         }
  193.         for(int i = 0; i < l.size(); i++) {
  194.             ((TriggerUIListener)l.elementAt(i)).commitUI(tuie);
  195.         }
  196.     }
  197.  
  198.     /**
  199.      * Returns a special value for the getvalue Method. These special features are defined
  200.      * in an array of strings.
  201.      * @param column. the special feature column ( < 0 )
  202.      * @param offset. the row for the special feature
  203.      */
  204.     protected Object getSpecialFeature(int column,int offset)
  205.     {
  206.         if (SpecialFeatures[column]==RowNumber) {
  207.             if (getCurrentRecordState() != Record.RECSTATE_INVALID) {
  208.                 return getCurrentRecordNumberString();
  209.             }
  210.         }
  211.         else if (SpecialFeatures[column]==RowState) {
  212.             if (getCurrentRecordState() != Record.RECSTATE_INVALID) {
  213.                 return getStateString(getCurrentRecordState());
  214.             }
  215.         }
  216.         else if (SpecialFeatures[column]==CurrentDataSource) {
  217.             return this;
  218.         }
  219.         return "";
  220.     }
  221.  
  222.     protected void setSpecialFeature(int column,int offset,Object value)
  223.     {
  224.         try {
  225.             if (SpecialFeatures[column]==RowNumber) {
  226.                 goTo(new Integer((String)value).intValue());
  227.             }
  228.         }
  229.         catch (Exception ex) {
  230.         }
  231.     }
  232.  
  233.     /**
  234.      *   This method is used to retrieve the current state of this Record as a String
  235.      *
  236.      * @return The current state of this Record as a String. The state can be one
  237.      *         of the following values:
  238.      *              "Existing"              The record exists in database
  239.      *              "Modified"              The record has been modified
  240.      *              "New"                   The record has no data and doesn't exist in database
  241.      *              "Marked for Deletion"   The record is marked for deletion
  242.      *              "Deleted"               The record has been deleted
  243.      *              "Invalid"               The record is in undefined state.
  244.      *              "New Modified"          The record is new with data
  245.      */
  246.     public String getStateString (byte state)
  247.     {
  248.         switch (state)
  249.         {
  250.             case Record.RECSTATE_EXISTING:
  251.                 return new String("Existing");
  252.             case Record.RECSTATE_MODIFIED:
  253.                 return new String("Modified");
  254.             case Record.RECSTATE_NEW:
  255.                 return new String("New");
  256.             case Record.RECSTATE_DELETED:
  257.                 return new String("Marked for Deletion");
  258.             case Record.RECSTATE_DB_DELETED :
  259.                 return new String("Deleted");
  260.             case Record.RECSTATE_NEW_MODIFIED :
  261.                 return new String("New Modified");
  262.             default :
  263.                 return new String("Invalid");
  264.         }
  265.     }
  266.  
  267.     public int getTotalNumberOfRows()
  268.     {
  269.         return 1;
  270.     }
  271.  
  272.     public int getCurrentRowNumber()
  273.     {
  274.         return getCurrentRecordNumber();
  275.     }
  276.  
  277.     protected int getColumnType(int column) throws SQLException
  278.     {
  279.         return m_MetaData.getColumnType(column);
  280.     }
  281.  
  282.     protected int getScale(int column) throws SQLException
  283.     {
  284.         return m_MetaData.getScale(column);
  285.     }
  286.  
  287.     protected Object getColumnValue(int column) throws IOException, SQLException
  288.     {
  289.         Object value = null;
  290.  
  291.         switch (getColumnType(column)) {
  292.             case Types.BIT:
  293.                 value = new Boolean(getBoolean(column));
  294.                 break;
  295.             case Types.BINARY:
  296.             case Types.VARBINARY:
  297.             case Types.LONGVARBINARY:
  298.             case Types.OTHER:
  299.                 value = getBinaryStream(column);
  300.                 break;
  301.             case Types.DATE:
  302.                 value = getDate(column);
  303.                 break;
  304.             case Types.TIME:
  305.                 value = getTime(column);
  306.                 break;
  307.             case Types.TIMESTAMP:
  308.                 value = getTimestamp(column);
  309.                 break;
  310.             case Types.NULL:
  311.                 value = null;
  312.                 break;
  313.             default:
  314.                 value = getStringValue(column);
  315.                 break;
  316.         }
  317.         return value;
  318.     }
  319.  
  320.     protected void setColumnValue(int column, Object value) throws IOException, SQLException
  321.     {
  322.         if (value instanceof Date) {
  323.             setDate(column, (Date)value);
  324.         }
  325.         else if (value instanceof Time) {
  326.             setTime(column, (Time)value);
  327.         }
  328.         else if (value instanceof Timestamp) {
  329.             setTimestamp(column, (Timestamp)value);
  330.         }
  331.         else {
  332.             switch (getColumnType(column)) {
  333.             case Types.LONGVARCHAR:
  334.                 if (value instanceof InputStream) {
  335.                     InputStream stream = (InputStream)value;
  336.                     setAsciiStream(column, stream, stream.available());
  337.                 }
  338.                 else if (value instanceof byte[]) {
  339.                     InputStream stream = new ByteArrayInputStream((byte[])value);
  340.                     setAsciiStream(column, stream, stream.available());
  341.                 }
  342.                 else if (value instanceof ByteArrayOutputStream) {
  343.                     ByteArrayOutputStream outStream = (ByteArrayOutputStream)value;
  344.                     InputStream stream = new ByteArrayInputStream(outStream.toByteArray());
  345.                     setAsciiStream(column, stream, stream.available());
  346.                 }
  347.                 else if (value instanceof String) {
  348.                     setValueFromString(column, value.toString());
  349.                 }
  350.                 break;
  351.             case Types.BINARY:
  352.             case Types.VARBINARY:
  353.             case Types.LONGVARBINARY:
  354.                 if (value instanceof InputStream) {
  355.                     InputStream stream = (InputStream)value;
  356.                     setBinaryStream(column, stream, stream.available());
  357.                 }
  358.                 else if (value instanceof byte[]) {
  359.                     InputStream stream = new ByteArrayInputStream((byte[])value);
  360.                     setBinaryStream(column, stream, stream.available());
  361.                 }
  362.                 else if (value instanceof ByteArrayOutputStream) {
  363.                     ByteArrayOutputStream outStream = (ByteArrayOutputStream)value;
  364.                     InputStream stream = new ByteArrayInputStream(outStream.toByteArray());
  365.                     setBinaryStream(column, stream, stream.available());
  366.                 }
  367.                 break;
  368.             case Types.NULL:
  369.                 setNull(column, Types.NULL);
  370.                 break;
  371.             default:
  372.                 setValueFromString(column, value.toString());
  373.                 break;
  374.             }
  375.         }
  376.     }
  377.     public void close()//throws Throwable
  378.     {
  379.         try{
  380.         _mediatorDS.killAll();
  381.         }
  382.         catch(Throwable e){
  383.             System.out.println("Could not destroy MediatorDS");
  384.         }
  385.        
  386.     }
  387. }