home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 12.2 KB | 387 lines |
- package symantec.itools.db.pro;
-
- import symantec.itools.db.beans.binding.*;
- import java.sql.*;
- import java.util.Vector;
- import java.math.BigDecimal;
- import java.io.*;
-
- public class RelationViewPlus extends RelationView
- implements BasicDataSource
- {
- private MediatorDS _mediatorDS = null;
- protected Vector triggerListeners = null;
- private final String RowNumber = "RowNumber";
- private final String RowState = "RowState";
- private final String CurrentDataSource = "CurrentDataSource";
- String[] SpecialFeatures = { RowNumber, RowState, CurrentDataSource };
- public final int numberOfSpecialFeatures = 3;
- RelationViewMetaData m_MetaData = null;
-
- public RelationViewPlus(Request request) throws SQLException
- {
- super(request);
- }
-
- public RelationViewPlus(AutoDetail detail) throws SQLException
- {
- super(detail);
- }
-
- public void setName(String name) throws SQLException
- {
- super.setName(name);
- if (java.beans.Beans.isDesignTime()) {
- return;
- }
- init();
- }
-
- void goToInitialRecPos() throws SQLException
- {
- super.goToInitialRecPos();
- triggerUI();
- }
-
- void notifyDataChange(int projID,Record rec)
- {
- super.notifyDataChange(projID, rec);
- //This notify only for current record
- //If record not current record, do nothing
- // if(rec == _rec) {
- triggerUI();
- // }
- }
-
- void notifyRowStateChange() throws SQLException
- {
- super.notifyRowStateChange();
- if (isBindingNotifyEnabled()) {
- triggerUI();
- }
- }
-
- void notifyRowChange() throws SQLException
- {
- super.notifyRowChange();
- if (isBindingNotifyEnabled()) {
- triggerUI();
- }
- }
-
- void notifyRecordSetChange() throws SQLException
- {
- super.notifyRecordSetChange();
- if (isBindingNotifyEnabled()) {
- triggerUI();
- }
- }
-
- private void init() throws SQLException
- {
- triggerListeners = new Vector();
-
- _mediatorDS = new MediatorDS();
- _mediatorDS.setOutput(this);
-
- String[] getm={"getValue(Row,Col)"};
- String[] setm={"setValue(Value,Row,Col)"};
-
- _mediatorDS.setSetMethods(setm);
- _mediatorDS.setGetMethods(getm);
-
- String name = getDataName(Name.ColumnSeparator);
- String Special = "";
-
- for(int i = 0; i < SpecialFeatures.length; i++) {
- // Special=Special+SpecialFeatures[i]+Name.ColumnSeparator;
- Special=Special+Name.ColumnSeparator+SpecialFeatures[i];
- }
- Name iName = new Name(getName(),name.substring(name.indexOf(Name.TableSeparator)+1,name.length())+Special);
- _mediatorDS.setDataBinding(iName.getFullName());
-
- m_MetaData = getMetaData();
- }
-
- public String getDataName(String pattern) throws SQLException
- {
- String strName = getName() + Name.TableSeparator;
- int i;
- RelationViewMetaData metadata = getMetaData();
- int colCount = metadata.getColumnCount();
- for(i = 1; i <= colCount - 1; i++) {
- strName += metadata.getColumnName(i) + pattern;
- }
- strName += metadata.getColumnName(i);
- return strName;
- }
-
- /**
- * Implementing the basicDataSource Interface
- */
- public Object getValue(int row, int column)
- {
- Object value = "";
- try {
- RelationViewMetaData metadata = getMetaData();
- int colCount = metadata.getColumnCount();
- if (column < colCount) {
- if (getCurrentRecordState() != Record.RECSTATE_INVALID) {
- value = getColumnValue(column + 1); // 1 based
- }
- }
- else {
- value = getSpecialFeature(column - colCount, row);
- }
- }
- catch (IOException e) {
- }
- catch (SQLException e) {
- }
- return value;
- }
-
- public void setValue(Object value, int row, int column)
- {
- try {
- RelationViewMetaData metadata=getMetaData();
-
- int colCount = metadata.getColumnCount();
- if (column < colCount) {
- if (getCurrentRecordState() != Record.RECSTATE_INVALID) {
- if (value == null) {
- setNull(column + 1, getColumnType(column + 1)); // 1 based
- }
- else {
- setColumnValue(column + 1, value); // 1 based
- }
- }
- }
- else {
- setSpecialFeature(column - colCount, row, value);
- }
- }
- catch (IOException e) {
- }
- catch (SQLException e) {
- }
- }
-
- public synchronized void addTriggerUIListener(TriggerUIListener mds)
- {
- triggerListeners.addElement(mds);
- triggerUI();
- }
-
- public synchronized void removeTriggerUIListener(TriggerUIListener mds)
- {
- triggerListeners.removeElement(mds);
- }
-
- protected void triggerUI()
- {
- synchronized (this) {
- if (triggerListeners == null) {
- return;
- }
- }
- Vector l;
- TriggerUIEvent tuie = new TriggerUIEvent(this);
- synchronized(this) {
- l = (Vector)triggerListeners.clone();
- }
- for(int i = 0; i < l.size(); i++) {
- ((TriggerUIListener)l.elementAt(i)).commitUI(tuie);
- }
- }
-
- /**
- * Returns a special value for the getvalue Method. These special features are defined
- * in an array of strings.
- * @param column. the special feature column ( < 0 )
- * @param offset. the row for the special feature
- */
- protected Object getSpecialFeature(int column,int offset)
- {
- if (SpecialFeatures[column]==RowNumber) {
- if (getCurrentRecordState() != Record.RECSTATE_INVALID) {
- return getCurrentRecordNumberString();
- }
- }
- else if (SpecialFeatures[column]==RowState) {
- if (getCurrentRecordState() != Record.RECSTATE_INVALID) {
- return getStateString(getCurrentRecordState());
- }
- }
- else if (SpecialFeatures[column]==CurrentDataSource) {
- return this;
- }
- return "";
- }
-
- protected void setSpecialFeature(int column,int offset,Object value)
- {
- try {
- if (SpecialFeatures[column]==RowNumber) {
- goTo(new Integer((String)value).intValue());
- }
- }
- catch (Exception ex) {
- }
- }
-
- /**
- * This method is used to retrieve the current state of this Record as a String
- *
- * @return The current state of this Record as a String. The state can be one
- * of the following values:
- * "Existing" The record exists in database
- * "Modified" The record has been modified
- * "New" The record has no data and doesn't exist in database
- * "Marked for Deletion" The record is marked for deletion
- * "Deleted" The record has been deleted
- * "Invalid" The record is in undefined state.
- * "New Modified" The record is new with data
- */
- public String getStateString (byte state)
- {
- switch (state)
- {
- case Record.RECSTATE_EXISTING:
- return new String("Existing");
- case Record.RECSTATE_MODIFIED:
- return new String("Modified");
- case Record.RECSTATE_NEW:
- return new String("New");
- case Record.RECSTATE_DELETED:
- return new String("Marked for Deletion");
- case Record.RECSTATE_DB_DELETED :
- return new String("Deleted");
- case Record.RECSTATE_NEW_MODIFIED :
- return new String("New Modified");
- default :
- return new String("Invalid");
- }
- }
-
- public int getTotalNumberOfRows()
- {
- return 1;
- }
-
- public int getCurrentRowNumber()
- {
- return getCurrentRecordNumber();
- }
-
- protected int getColumnType(int column) throws SQLException
- {
- return m_MetaData.getColumnType(column);
- }
-
- protected int getScale(int column) throws SQLException
- {
- return m_MetaData.getScale(column);
- }
-
- protected Object getColumnValue(int column) throws IOException, SQLException
- {
- Object value = null;
-
- switch (getColumnType(column)) {
- case Types.BIT:
- value = new Boolean(getBoolean(column));
- break;
- case Types.BINARY:
- case Types.VARBINARY:
- case Types.LONGVARBINARY:
- case Types.OTHER:
- value = getBinaryStream(column);
- break;
- case Types.DATE:
- value = getDate(column);
- break;
- case Types.TIME:
- value = getTime(column);
- break;
- case Types.TIMESTAMP:
- value = getTimestamp(column);
- break;
- case Types.NULL:
- value = null;
- break;
- default:
- value = getStringValue(column);
- break;
- }
- return value;
- }
-
- protected void setColumnValue(int column, Object value) throws IOException, SQLException
- {
- if (value instanceof Date) {
- setDate(column, (Date)value);
- }
- else if (value instanceof Time) {
- setTime(column, (Time)value);
- }
- else if (value instanceof Timestamp) {
- setTimestamp(column, (Timestamp)value);
- }
- else {
- switch (getColumnType(column)) {
- case Types.LONGVARCHAR:
- if (value instanceof InputStream) {
- InputStream stream = (InputStream)value;
- setAsciiStream(column, stream, stream.available());
- }
- else if (value instanceof byte[]) {
- InputStream stream = new ByteArrayInputStream((byte[])value);
- setAsciiStream(column, stream, stream.available());
- }
- else if (value instanceof ByteArrayOutputStream) {
- ByteArrayOutputStream outStream = (ByteArrayOutputStream)value;
- InputStream stream = new ByteArrayInputStream(outStream.toByteArray());
- setAsciiStream(column, stream, stream.available());
- }
- else if (value instanceof String) {
- setValueFromString(column, value.toString());
- }
- break;
- case Types.BINARY:
- case Types.VARBINARY:
- case Types.LONGVARBINARY:
- if (value instanceof InputStream) {
- InputStream stream = (InputStream)value;
- setBinaryStream(column, stream, stream.available());
- }
- else if (value instanceof byte[]) {
- InputStream stream = new ByteArrayInputStream((byte[])value);
- setBinaryStream(column, stream, stream.available());
- }
- else if (value instanceof ByteArrayOutputStream) {
- ByteArrayOutputStream outStream = (ByteArrayOutputStream)value;
- InputStream stream = new ByteArrayInputStream(outStream.toByteArray());
- setBinaryStream(column, stream, stream.available());
- }
- break;
- case Types.NULL:
- setNull(column, Types.NULL);
- break;
- default:
- setValueFromString(column, value.toString());
- break;
- }
- }
- }
- public void close()//throws Throwable
- {
- try{
- _mediatorDS.killAll();
- }
- catch(Throwable e){
- System.out.println("Could not destroy MediatorDS");
- }
-
- }
- }