KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > MOTableRelation


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - MOTableRelation.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22 package org.snmp4j.agent.mo;
23
24 import org.snmp4j.smi.OID;
25
26 /**
27  * The <code>MOTableRelation</code> class models table relations like sparse
28  * table relationship and augmentation. This class implements the augmentation
29  * relationship. In order to implement a sparse table relationship, sub-classing
30  * <code>MOTableRelation</code> is needed and the methods
31  * {@link #hasDependentRow} and {@link #getDependentIndexes} must be overwritten
32  * then.
33  *
34  * @author Frank Fock
35  * @version 1.0
36  */

37 public class MOTableRelation {
38
39   private MOTable baseTable;
40   private MOTable dependentTable;
41
42   /**
43    * Creates a table relation from a base table and the dependent table.
44    * To actually set up the relationship between those tables
45    * {@link #createRelationShip()} needs to be called.
46    *
47    * @param baseTable
48    * the base table.
49    * @param dependentTable
50    * the dependent (augmenting) table.
51    */

52   public MOTableRelation(MOTable baseTable, MOTable dependentTable) {
53     this.baseTable = baseTable;
54     this.dependentTable = dependentTable;
55   }
56
57   /**
58    * Actually sets up the relationship between base and dependent table by
59    * adding this instance as row listener to the base table.
60    */

61   public void createRelationShip() {
62     this.baseTable.addMOTableRowListener(createRelationShipListener());
63   }
64
65   protected MOTableRowListener createRelationShipListener() {
66     return new RelationShipListener();
67   }
68
69   /**
70    * Indicates whether the specified baseTableRow has any dependent rows.
71    * By default this method returns <code>true</code> because the default
72    * implementation represents an augmentation relationship.
73    * Overwrite this method in a sub-class to implement a sparse table
74    * relationship.
75    * @param baseTableRow
76    * a row of the base table.
77    * @return
78    * <code>true</code> if the row has dependent rows.
79    */

80   public boolean hasDependentRow(MOTableRow baseTableRow) {
81     // by default this is an augmentation
82
// overwrite in a subclass to implement sparse relationship
83
return true;
84   }
85
86   /**
87    * Returns the dependent indexes for the specified base row. By default, this
88    * method returns the base rows index in a one element array, because
89    * the default implementation represents an augmentation relationship.
90    * Overwrite this method in a sub-class to implement a sparse table
91    * relationship.
92    * @param baseRow
93    * a row of the base table.
94    * @return
95    * an array of row index values of the dependent rows.
96    */

97   public OID[] getDependentIndexes(MOTableRow baseRow) {
98     // by default this is an augmentation
99
// 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 =
115           dependentTable.createRow(indexes[i],
116                                    dependentTable.getDefaultValues());
117       depRow.setBaseRow(baseTableRow);
118       dependentTable.addRow(depRow);
119     }
120   }
121
122   /**
123    * Removes all dependent rows for the specified base table row
124    * from the dependent table. This method is automatically called if
125    * {@link #createRelationShip()} has been called.
126    *
127    * @param baseTableRow
128    * a row of the base table.
129    * @return
130    * an array of the removed rows.
131    */

132   protected MOTableRow[] removeDependentRows(MOTableRow baseTableRow) {
133     OID[] indexes = getDependentIndexes(baseTableRow);
134     MOTableRow[] removedRows = new MOTableRow[indexes.length];
135     for (int i = 0; i < indexes.length; i++) {
136       removedRows[i] = dependentTable.removeRow(indexes[i]);
137     }
138     return removedRows;
139   }
140
141   protected class RelationShipListener implements MOTableRowListener {
142
143     public void rowChanged(MOTableRowEvent event) {
144       switch (event.getType()) {
145         case MOTableRowEvent.ADD: {
146           if (hasDependentRow(event.getRow())) {
147             addDependentRows(event.getRow());
148           }
149           break;
150         }
151         case MOTableRowEvent.DELETE: {
152           removeDependentRows(event.getRow());
153         }
154       }
155     }
156
157   }
158 }
159
Popular Tags