View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.database.data;
22  
23  import goldengate.common.database.DbPreparedStatement;
24  import goldengate.common.database.DbSession;
25  import goldengate.common.database.data.AbstractDbData;
26  import goldengate.common.database.data.DbValue;
27  import goldengate.common.database.exception.GoldenGateDatabaseException;
28  import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
29  import goldengate.common.database.exception.GoldenGateDatabaseNoDataException;
30  import goldengate.common.database.exception.GoldenGateDatabaseSqlException;
31  
32  import java.sql.Types;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  import openr66.protocol.configuration.Configuration;
36  
37  /**
38   * Configuration Table object
39   *
40   * @author Frederic Bregier
41   *
42   */
43  public class DbMultipleMonitor extends AbstractDbData {
44      public static enum Columns {
45          COUNTCONFIG,
46          COUNTHOST,
47          COUNTRULE,
48          HOSTID
49      }
50  
51      public static final int[] dbTypes = {
52          Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR };
53  
54      public static final String table = " MULTIPLEMONITOR ";
55  
56      /**
57       * HashTable in case of lack of database
58       */
59      private static final ConcurrentHashMap<String, DbMultipleMonitor> dbR66MMHashMap =
60          new ConcurrentHashMap<String, DbMultipleMonitor>();
61  
62      private String hostid;
63  
64      public int countConfig;
65  
66      public int countHost;
67  
68      public int countRule;
69  
70      // ALL TABLE SHOULD IMPLEMENT THIS
71      public static final int NBPRKEY = 1;
72  
73      protected static final String selectAllFields = Columns.COUNTCONFIG
74              .name() +
75              "," +
76              Columns.COUNTHOST.name() +
77              "," +
78              Columns.COUNTRULE.name() +
79              "," +
80              Columns.HOSTID.name();
81  
82      protected static final String updateAllFields = Columns.COUNTCONFIG
83              .name() +
84              "=?," +
85              Columns.COUNTHOST.name() +
86              "=?," +
87              Columns.COUNTRULE.name() +
88              "=?";
89  
90      protected static final String insertAllValues = " (?,?,?,?) ";
91  
92  
93      /* (non-Javadoc)
94       * @see goldengate.common.database.data.AbstractDbData#initObject()
95       */
96      @Override
97      protected void initObject() {
98          primaryKey = new DbValue[]{new DbValue(hostid, Columns.HOSTID
99                  .name())};
100         otherFields = new DbValue[]{
101                 new DbValue(countConfig, Columns.COUNTCONFIG.name()),
102                 new DbValue(countHost, Columns.COUNTHOST.name()),
103                 new DbValue(countRule, Columns.COUNTRULE.name()) };
104         allFields = new DbValue[]{
105                 otherFields[0], otherFields[1], otherFields[2], primaryKey[0] };
106     }
107 
108     /* (non-Javadoc)
109      * @see goldengate.common.database.data.AbstractDbData#getSelectAllFields()
110      */
111     @Override
112     protected String getSelectAllFields() {
113         return selectAllFields;
114     }
115 
116     /* (non-Javadoc)
117      * @see goldengate.common.database.data.AbstractDbData#getTable()
118      */
119     @Override
120     protected String getTable() {
121         return table;
122     }
123 
124     /* (non-Javadoc)
125      * @see goldengate.common.database.data.AbstractDbData#getInsertAllValues()
126      */
127     @Override
128     protected String getInsertAllValues() {
129         return insertAllValues;
130     }
131 
132     /* (non-Javadoc)
133      * @see goldengate.common.database.data.AbstractDbData#getUpdateAllFields()
134      */
135     @Override
136     protected String getUpdateAllFields() {
137         return updateAllFields;
138     }
139 
140     @Override
141     protected void setToArray() {
142         allFields[Columns.HOSTID.ordinal()].setValue(hostid);
143         allFields[Columns.COUNTCONFIG.ordinal()].setValue(countConfig);
144         allFields[Columns.COUNTHOST.ordinal()]
145                 .setValue(countHost);
146         allFields[Columns.COUNTRULE.ordinal()]
147                 .setValue(countRule);
148     }
149 
150     @Override
151     protected void setFromArray() throws GoldenGateDatabaseSqlException {
152         hostid = (String) allFields[Columns.HOSTID.ordinal()].getValue();
153         countConfig = (Integer) allFields[Columns.COUNTCONFIG.ordinal()]
154                 .getValue();
155         countHost = (Integer) allFields[Columns.COUNTHOST.ordinal()]
156                 .getValue();
157         countRule = (Integer) allFields[Columns.COUNTRULE.ordinal()]
158                 .getValue();
159     }
160 
161     /* (non-Javadoc)
162      * @see goldengate.common.database.data.AbstractDbData#getWherePrimaryKey()
163      */
164     @Override
165     protected String getWherePrimaryKey() {
166         return primaryKey[0].column + " = ? ";
167     }
168 
169     /* (non-Javadoc)
170      * @see goldengate.common.database.data.AbstractDbData#setPrimaryKey()
171      */
172     @Override
173     protected void setPrimaryKey() {
174         primaryKey[0].setValue(hostid);
175     }
176 
177     /**
178      * @param dbSession
179      * @param hostid
180      * @param cc
181      *            count for Config
182      * @param ch
183      *            count for Host
184      * @param cr
185      *            count for Rule
186      */
187     public DbMultipleMonitor(DbSession dbSession, String hostid, int cc, int ch, int cr) {
188         super(dbSession);
189         this.hostid = hostid;
190         countConfig = cc;
191         countHost = ch;
192         countRule = cr;
193         setToArray();
194         isSaved = false;
195     }
196 
197     /**
198      * @param dbSession
199      * @param hostid
200      * @throws GoldenGateDatabaseException
201      */
202     public DbMultipleMonitor(DbSession dbSession, String hostid) throws GoldenGateDatabaseException {
203         super(dbSession);
204         this.hostid = hostid;
205         // load from DB
206         select();
207     }
208 
209     /*
210      * (non-Javadoc)
211      *
212      * @see openr66.databaseold.data.AbstractDbData#delete()
213      */
214     @Override
215     public void delete() throws GoldenGateDatabaseException {
216         if (dbSession == null) {
217             dbR66MMHashMap.remove(this.hostid);
218             isSaved = false;
219             return;
220         }
221         super.delete();
222     }
223 
224     /*
225      * (non-Javadoc)
226      *
227      * @see openr66.databaseold.data.AbstractDbData#insert()
228      */
229     @Override
230     public void insert() throws GoldenGateDatabaseException {
231         if (isSaved) {
232             return;
233         }
234         if (dbSession == null) {
235             dbR66MMHashMap.put(this.hostid, this);
236             isSaved = true;
237             return;
238         }
239         super.insert();
240     }
241 
242     /* (non-Javadoc)
243      * @see openr66.databaseold.data.AbstractDbData#exist()
244      */
245     @Override
246     public boolean exist() throws GoldenGateDatabaseException {
247         if (dbSession == null) {
248             return dbR66MMHashMap.containsKey(hostid);
249         }
250         return super.exist();
251     }
252     /*
253      * (non-Javadoc)
254      *
255      * @see openr66.databaseold.data.AbstractDbData#select()
256      */
257     @Override
258     public void select() throws GoldenGateDatabaseException {
259         if (dbSession == null) {
260             DbMultipleMonitor conf = dbR66MMHashMap.get(this.hostid);
261             if (conf == null) {
262                 throw new GoldenGateDatabaseNoDataException("No row found");
263             } else {
264                 // copy info
265                 for (int i = 0; i < allFields.length; i++){
266                     allFields[i].value = conf.allFields[i].value;
267                 }
268                 setFromArray();
269                 isSaved = true;
270                 return;
271             }
272         }
273         super.select();
274     }
275     /*
276      * (non-Javadoc)
277      *
278      * @see openr66.databaseold.data.AbstractDbData#update()
279      */
280     @Override
281     public void update() throws GoldenGateDatabaseException {
282         if (isSaved) {
283             return;
284         }
285         if (dbSession == null) {
286             dbR66MMHashMap.put(this.hostid, this);
287             isSaved = true;
288             return;
289         }
290         super.update();
291     }
292     /**
293      * Private constructor for Commander only
294      */
295     private DbMultipleMonitor(DbSession session) {
296         super(session);
297     }
298     /**
299      * For instance from Commander when getting updated information
300      * @param preparedStatement
301      * @return the next updated Configuration
302      * @throws GoldenGateDatabaseNoConnectionException
303      * @throws GoldenGateDatabaseSqlException
304      */
305     public static DbMultipleMonitor getFromStatement(DbPreparedStatement preparedStatement) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
306         DbMultipleMonitor dbMm = new DbMultipleMonitor(preparedStatement.getDbSession());
307         dbMm.getValues(preparedStatement, dbMm.allFields);
308         dbMm.setFromArray();
309         dbMm.isSaved = true;
310         return dbMm;
311     }
312     /**
313      *
314      * @return the DbPreparedStatement for getting Updated Object in "FOR UPDATE" mode
315      * @throws GoldenGateDatabaseNoConnectionException
316      * @throws GoldenGateDatabaseSqlException
317      */
318     public static DbPreparedStatement getUpdatedPrepareStament(DbSession session) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
319         String request = "SELECT " +selectAllFields;
320         request += " FROM "+table+ " WHERE "+Columns.HOSTID.name()+" = '"+Configuration.configuration.HOST_ID+"'"+
321             " FOR UPDATE ";
322         DbPreparedStatement prep = new DbPreparedStatement(session, request);
323         return prep;
324     }
325     /**
326      * On Commander side
327      * @return True if this is the last update
328      */
329     public boolean checkUpdateConfig(){
330         if (countConfig <= 0) {
331             countConfig = Configuration.configuration.multipleMonitors;
332             countConfig --;
333             this.isSaved = false;
334         } else {
335             countConfig --;
336             this.isSaved = false;
337         }
338         return this.countConfig <= 0;
339     }
340     /**
341      * On Commander side
342      * @return True if this is the last update
343      */
344     public boolean checkUpdateHost(){
345         if (countHost <= 0) {
346             countHost = Configuration.configuration.multipleMonitors;
347             countHost --;
348             this.isSaved = false;
349         } else {
350             countHost --;
351             this.isSaved = false;
352         }
353         return this.countHost <= 0;
354     }
355     /**
356      * On Commander side
357      * @return True if this is the last update
358      */
359     public boolean checkUpdateRule(){
360         if (countRule <= 0) {
361             countRule = Configuration.configuration.multipleMonitors;
362             countRule --;
363             this.isSaved = false;
364         } else {
365             countRule --;
366             this.isSaved = false;
367         }
368         return this.countRule <= 0;
369     }
370     /*
371      * (non-Javadoc)
372      *
373      * @see openr66.databaseold.data.AbstractDbData#changeUpdatedInfo(UpdatedInfo)
374      */
375     @Override
376     public void changeUpdatedInfo(UpdatedInfo info) {
377     }
378     /**
379      * return the String representation
380      */
381     public String toString() {
382         return "DbMM "+countConfig+":"+countHost+":"+countRule;
383     }
384 }