001: /*_############################################################################
002: _##
003: _## SNMP4J-Agent - MOTableRelation.java
004: _##
005: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.agent.mo;
022:
023: import org.snmp4j.smi.OID;
024:
025: /**
026: * The <code>MOTableRelation</code> class models table relations like sparse
027: * table relationship and augmentation. This class implements the augmentation
028: * relationship. In order to implement a sparse table relationship, sub-classing
029: * <code>MOTableRelation</code> is needed and the methods
030: * {@link #hasDependentRow} and {@link #getDependentIndexes} must be overwritten
031: * then.
032: *
033: * @author Frank Fock
034: * @version 1.0
035: */
036: public class MOTableRelation {
037:
038: private MOTable baseTable;
039: private MOTable dependentTable;
040:
041: /**
042: * Creates a table relation from a base table and the dependent table.
043: * To actually set up the relationship between those tables
044: * {@link #createRelationShip()} needs to be called.
045: *
046: * @param baseTable
047: * the base table.
048: * @param dependentTable
049: * the dependent (augmenting) table.
050: */
051: public MOTableRelation(MOTable baseTable, MOTable dependentTable) {
052: this .baseTable = baseTable;
053: this .dependentTable = dependentTable;
054: }
055:
056: /**
057: * Actually sets up the relationship between base and dependent table by
058: * adding this instance as row listener to the base table.
059: */
060: public void createRelationShip() {
061: this .baseTable
062: .addMOTableRowListener(createRelationShipListener());
063: }
064:
065: protected MOTableRowListener createRelationShipListener() {
066: return new RelationShipListener();
067: }
068:
069: /**
070: * Indicates whether the specified baseTableRow has any dependent rows.
071: * By default this method returns <code>true</code> because the default
072: * implementation represents an augmentation relationship.
073: * Overwrite this method in a sub-class to implement a sparse table
074: * relationship.
075: * @param baseTableRow
076: * a row of the base table.
077: * @return
078: * <code>true</code> if the row has dependent rows.
079: */
080: public boolean hasDependentRow(MOTableRow baseTableRow) {
081: // by default this is an augmentation
082: // overwrite in a subclass to implement sparse relationship
083: return true;
084: }
085:
086: /**
087: * Returns the dependent indexes for the specified base row. By default, this
088: * method returns the base rows index in a one element array, because
089: * the default implementation represents an augmentation relationship.
090: * Overwrite this method in a sub-class to implement a sparse table
091: * relationship.
092: * @param baseRow
093: * a row of the base table.
094: * @return
095: * an array of row index values of the dependent rows.
096: */
097: public OID[] getDependentIndexes(MOTableRow baseRow) {
098: // by default this is an augmentation
099: // overwrite in a subclass to implement sparse relationship
100: return new OID[] { baseRow.getIndex() };
101: }
102:
103: /**
104: * Adds all dependent rows for the specified base table row to the dependent
105: * table. This method is automatically called if {@link #createRelationShip()}
106: * has been called.
107: *
108: * @param baseTableRow
109: * a row of the base table.
110: */
111: protected void addDependentRows(MOTableRow baseTableRow) {
112: OID[] indexes = getDependentIndexes(baseTableRow);
113: for (int i = 0; i < indexes.length; i++) {
114: MOTableRow depRow = dependentTable.createRow(indexes[i],
115: dependentTable.getDefaultValues());
116: depRow.setBaseRow(baseTableRow);
117: dependentTable.addRow(depRow);
118: }
119: }
120:
121: /**
122: * Removes all dependent rows for the specified base table row
123: * from the dependent table. This method is automatically called if
124: * {@link #createRelationShip()} has been called.
125: *
126: * @param baseTableRow
127: * a row of the base table.
128: * @return
129: * an array of the removed rows.
130: */
131: protected MOTableRow[] removeDependentRows(MOTableRow baseTableRow) {
132: OID[] indexes = getDependentIndexes(baseTableRow);
133: MOTableRow[] removedRows = new MOTableRow[indexes.length];
134: for (int i = 0; i < indexes.length; i++) {
135: removedRows[i] = dependentTable.removeRow(indexes[i]);
136: }
137: return removedRows;
138: }
139:
140: protected class RelationShipListener implements MOTableRowListener {
141:
142: public void rowChanged(MOTableRowEvent event) {
143: switch (event.getType()) {
144: case MOTableRowEvent.ADD: {
145: if (hasDependentRow(event.getRow())) {
146: addDependentRows(event.getRow());
147: }
148: break;
149: }
150: case MOTableRowEvent.DELETE: {
151: removeDependentRows(event.getRow());
152: }
153: }
154: }
155:
156: }
157: }
|