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 goldengate.common.database.data;
22  
23  import java.sql.Types;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  import goldengate.common.database.DbPreparedStatement;
27  import goldengate.common.database.DbSession;
28  import goldengate.common.database.exception.GoldenGateDatabaseException;
29  import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
30  import goldengate.common.database.exception.GoldenGateDatabaseNoDataException;
31  import goldengate.common.database.exception.GoldenGateDatabaseSqlException;
32  
33  /**
34   * Example of Table object
35   *
36   * @author Frederic Bregier
37   *
38   */
39  public class DbDataModel extends AbstractDbData {
40      public static enum Columns {
41          READGLOBALLIMIT,
42          WRITEGLOBALLIMIT,
43          READSESSIONLIMIT,
44          WRITESESSIONLIMIT,
45          DELAYLIMIT,
46          UPDATEDINFO,
47          HOSTID
48      }
49  
50      public static final int[] dbTypes = {
51              Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.BIGINT,
52              Types.BIGINT, Types.INTEGER, Types.VARCHAR };
53  
54      public static final String table = " CONFIGURATION ";
55      public static final String fieldseq = "RUNSEQ";
56      public static final Columns [] indexes = {
57          Columns.READGLOBALLIMIT, Columns.READSESSIONLIMIT, Columns.WRITEGLOBALLIMIT, 
58          Columns.WRITESESSIONLIMIT, Columns.HOSTID
59      };
60  
61      /**
62       * HashTable in case of lack of database
63       */
64      private static final ConcurrentHashMap<String, DbDataModel> dbR66ConfigurationHashMap =
65          new ConcurrentHashMap<String, DbDataModel>();
66  
67      private String hostid;
68  
69      private long readgloballimit;
70  
71      private long writegloballimit;
72  
73      private long readsessionlimit;
74  
75      private long writesessionlimit;
76  
77      private long delayllimit;
78  
79      private int updatedInfo = UpdatedInfo.UNKNOWN.ordinal();
80  
81      // ALL TABLE SHOULD IMPLEMENT THIS
82      public static final int NBPRKEY = 1;
83  
84      protected static final String selectAllFields = Columns.READGLOBALLIMIT
85              .name() +
86              "," +
87              Columns.WRITEGLOBALLIMIT.name() +
88              "," +
89              Columns.READSESSIONLIMIT.name() +
90              "," +
91              Columns.WRITESESSIONLIMIT.name() +
92              "," +
93              Columns.DELAYLIMIT.name() +
94              "," + Columns.UPDATEDINFO.name() + "," + Columns.HOSTID.name();
95  
96      protected static final String updateAllFields = Columns.READGLOBALLIMIT
97              .name() +
98              "=?," +
99              Columns.WRITEGLOBALLIMIT.name() +
100             "=?," +
101             Columns.READSESSIONLIMIT.name() +
102             "=?," +
103             Columns.WRITESESSIONLIMIT.name() +
104             "=?," +
105             Columns.DELAYLIMIT.name() +
106             "=?," +
107             Columns.UPDATEDINFO.name() +
108             "=?";
109 
110     protected static final String insertAllValues = " (?,?,?,?,?,?,?) ";
111 
112     /* (non-Javadoc)
113      * @see goldengate.common.database.data.AbstractDbData#initObject()
114      */
115     @Override
116     protected void initObject() {
117         primaryKey = new DbValue[]{new DbValue(hostid, Columns.HOSTID
118                 .name())};
119         otherFields = new DbValue[]{
120                 new DbValue(readgloballimit, Columns.READGLOBALLIMIT.name()),
121                 new DbValue(writegloballimit, Columns.WRITEGLOBALLIMIT.name()),
122                 new DbValue(readsessionlimit, Columns.READSESSIONLIMIT.name()),
123                 new DbValue(writesessionlimit, Columns.WRITESESSIONLIMIT.name()),
124                 new DbValue(delayllimit, Columns.DELAYLIMIT.name()),
125                 new DbValue(updatedInfo, Columns.UPDATEDINFO.name()) };
126         allFields = new DbValue[]{
127                 otherFields[0], otherFields[1], otherFields[2], otherFields[3],
128                 otherFields[4], otherFields[5], primaryKey[0] };
129     }
130 
131     @Override
132     protected void setToArray() {
133         allFields[Columns.HOSTID.ordinal()].setValue(hostid);
134         allFields[Columns.READGLOBALLIMIT.ordinal()].setValue(readgloballimit);
135         allFields[Columns.WRITEGLOBALLIMIT.ordinal()]
136                 .setValue(writegloballimit);
137         allFields[Columns.READSESSIONLIMIT.ordinal()]
138                 .setValue(readsessionlimit);
139         allFields[Columns.WRITESESSIONLIMIT.ordinal()]
140                 .setValue(writesessionlimit);
141         allFields[Columns.DELAYLIMIT.ordinal()].setValue(delayllimit);
142         allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
143     }
144 
145     @Override
146     protected void setFromArray() throws GoldenGateDatabaseSqlException {
147         hostid = (String) allFields[Columns.HOSTID.ordinal()].getValue();
148         readgloballimit = (Long) allFields[Columns.READGLOBALLIMIT.ordinal()]
149                 .getValue();
150         writegloballimit = (Long) allFields[Columns.WRITEGLOBALLIMIT.ordinal()]
151                 .getValue();
152         readsessionlimit = (Long) allFields[Columns.READSESSIONLIMIT.ordinal()]
153                 .getValue();
154         writesessionlimit = (Long) allFields[Columns.WRITESESSIONLIMIT
155                 .ordinal()].getValue();
156         delayllimit = (Long) allFields[Columns.DELAYLIMIT.ordinal()].getValue();
157         updatedInfo = (Integer) allFields[Columns.UPDATEDINFO.ordinal()]
158                 .getValue();
159     }
160 
161     /* (non-Javadoc)
162      * @see goldengate.common.database.data.AbstractDbData#getSelectAllFields()
163      */
164     @Override
165     protected String getSelectAllFields() {
166         return selectAllFields;
167     }
168 
169     /* (non-Javadoc)
170      * @see goldengate.common.database.data.AbstractDbData#getTable()
171      */
172     @Override
173     protected String getTable() {
174         return table;
175     }
176 
177     /* (non-Javadoc)
178      * @see goldengate.common.database.data.AbstractDbData#getInsertAllValues()
179      */
180     @Override
181     protected String getInsertAllValues() {
182         return insertAllValues;
183     }
184 
185     /* (non-Javadoc)
186      * @see goldengate.common.database.data.AbstractDbData#getUpdateAllFields()
187      */
188     @Override
189     protected String getUpdateAllFields() {
190         return updateAllFields;
191     }
192 
193     /* (non-Javadoc)
194      * @see goldengate.common.database.data.AbstractDbData#getWherePrimaryKey()
195      */
196     @Override
197     protected String getWherePrimaryKey() {
198         return primaryKey[0].column + " = ? ";
199     }
200     /**
201      * Set the primary Key as current value
202      */
203     protected void setPrimaryKey() {
204         primaryKey[0].setValue(hostid);
205     }
206 
207     /**
208      * @param dbSession
209      * @param hostid
210      * @param rg
211      *            Read Global Limit
212      * @param wg
213      *            Write Global Limit
214      * @param rs
215      *            Read Session Limit
216      * @param ws
217      *            Write Session Limit
218      * @param del
219      *            Delay Limit
220      */
221     public DbDataModel(DbSession dbSession, String hostid, long rg, long wg, long rs,
222             long ws, long del) {
223         super(dbSession);
224         this.hostid = hostid;
225         readgloballimit = rg;
226         writegloballimit = wg;
227         readsessionlimit = rs;
228         writesessionlimit = ws;
229         delayllimit = del;
230         setToArray();
231         isSaved = false;
232     }
233 
234     /**
235      * @param dbSession
236      * @param hostid
237      * @throws GoldenGateDatabaseException
238      */
239     public DbDataModel(DbSession dbSession, String hostid) throws GoldenGateDatabaseException {
240         super(dbSession);
241         this.hostid = hostid;
242         // load from DB
243         select();
244     }
245 
246     /*
247      * (non-Javadoc)
248      *
249      * @see openr66.database.data.AbstractDbData#delete()
250      */
251     @Override
252     public void delete() throws GoldenGateDatabaseException {
253         if (dbSession == null) {
254             dbR66ConfigurationHashMap.remove(this.hostid);
255             isSaved = false;
256             return;
257         }
258         DbPreparedStatement preparedStatement = new DbPreparedStatement(
259                 dbSession);
260         try {
261             preparedStatement.createPrepareStatement("DELETE FROM " + table +
262                     " WHERE " + getWherePrimaryKey());
263             setPrimaryKey();
264             setValues(preparedStatement, primaryKey);
265             int count = preparedStatement.executeUpdate();
266             if (count <= 0) {
267                 throw new GoldenGateDatabaseNoDataException("No row found");
268             }
269             isSaved = false;
270         } finally {
271             preparedStatement.realClose();
272         }
273     }
274 
275     /*
276      * (non-Javadoc)
277      *
278      * @see openr66.database.data.AbstractDbData#insert()
279      */
280     @Override
281     public void insert() throws GoldenGateDatabaseException {
282         if (isSaved) {
283             return;
284         }
285         if (dbSession == null) {
286             dbR66ConfigurationHashMap.put(this.hostid, this);
287             isSaved = true;
288             return;
289         }
290         DbPreparedStatement preparedStatement = new DbPreparedStatement(
291                 dbSession);
292         try {
293             preparedStatement.createPrepareStatement("INSERT INTO " + table +
294                     " (" + selectAllFields + ") VALUES " + insertAllValues);
295             setValues(preparedStatement, allFields);
296             int count = preparedStatement.executeUpdate();
297             if (count <= 0) {
298                 throw new GoldenGateDatabaseNoDataException("No row found");
299             }
300             isSaved = true;
301         } finally {
302             preparedStatement.realClose();
303         }
304     }
305 
306     /* (non-Javadoc)
307      * @see openr66.database.data.AbstractDbData#exist()
308      */
309     @Override
310     public boolean exist() throws GoldenGateDatabaseException {
311         if (dbSession == null) {
312             return dbR66ConfigurationHashMap.containsKey(hostid);
313         }
314         DbPreparedStatement preparedStatement = new DbPreparedStatement(
315                 dbSession);
316         try {
317             preparedStatement.createPrepareStatement("SELECT " +
318                     primaryKey[0].column + " FROM " + table + " WHERE " +
319                     getWherePrimaryKey());
320             setPrimaryKey();
321             setValues(preparedStatement, primaryKey);
322             preparedStatement.executeQuery();
323             return preparedStatement.getNext();
324         } finally {
325             preparedStatement.realClose();
326         }
327     }
328     /*
329      * (non-Javadoc)
330      *
331      * @see openr66.database.data.AbstractDbData#select()
332      */
333     @Override
334     public void select() throws GoldenGateDatabaseException {
335         if (dbSession == null) {
336             DbDataModel conf = dbR66ConfigurationHashMap.get(this.hostid);
337             if (conf == null) {
338                 throw new GoldenGateDatabaseNoDataException("No row found");
339             } else {
340                 // copy info
341                 for (int i = 0; i < allFields.length; i++){
342                     allFields[i].value = conf.allFields[i].value;
343                 }
344                 setFromArray();
345                 isSaved = true;
346                 return;
347             }
348         }
349         DbPreparedStatement preparedStatement = new DbPreparedStatement(
350                 dbSession);
351         try {
352             preparedStatement.createPrepareStatement("SELECT " +
353                     selectAllFields + " FROM " + table + " WHERE " +
354                     getWherePrimaryKey());
355             setPrimaryKey();
356             setValues(preparedStatement, primaryKey);
357             preparedStatement.executeQuery();
358             if (preparedStatement.getNext()) {
359                 getValues(preparedStatement, allFields);
360                 setFromArray();
361                 isSaved = true;
362             } else {
363                 throw new GoldenGateDatabaseNoDataException("No row found");
364             }
365         } finally {
366             preparedStatement.realClose();
367         }
368     }
369     /*
370      * (non-Javadoc)
371      *
372      * @see openr66.database.data.AbstractDbData#update()
373      */
374     @Override
375     public void update() throws GoldenGateDatabaseException {
376         if (isSaved) {
377             return;
378         }
379         if (dbSession == null) {
380             dbR66ConfigurationHashMap.put(this.hostid, this);
381             isSaved = true;
382             return;
383         }
384         DbPreparedStatement preparedStatement = new DbPreparedStatement(
385                 dbSession);
386         try {
387             preparedStatement.createPrepareStatement("UPDATE " + table +
388                     " SET " + updateAllFields + " WHERE " +
389                     getWherePrimaryKey());
390             setValues(preparedStatement, allFields);
391             int count = preparedStatement.executeUpdate();
392             if (count <= 0) {
393                 throw new GoldenGateDatabaseNoDataException("No row found");
394             }
395             isSaved = true;
396         } finally {
397             preparedStatement.realClose();
398         }
399     }
400     /**
401      * Private constructor for Commander only
402      */
403     private DbDataModel(DbSession session) {
404         super(session);
405     }
406     /**
407      * For instance from Commander when getting updated information
408      * @param preparedStatement
409      * @return the next updated Configuration
410      * @throws GoldenGateDatabaseNoConnectionException
411      * @throws GoldenGateDatabaseSqlException
412      */
413     public static DbDataModel getFromStatement(DbPreparedStatement preparedStatement) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
414         DbDataModel dbDataModel = new DbDataModel(preparedStatement.getDbSession());
415         dbDataModel.getValues(preparedStatement, dbDataModel.allFields);
416         dbDataModel.setFromArray();
417         dbDataModel.isSaved = true;
418         return dbDataModel;
419     }
420     /**
421      *
422      * @return the DbPreparedStatement for getting Updated Object
423      * @throws GoldenGateDatabaseNoConnectionException
424      * @throws GoldenGateDatabaseSqlException
425      */
426     public static DbPreparedStatement getUpdatedPrepareStament(DbSession session) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
427         String request = "SELECT " +selectAllFields;
428         request += " FROM "+table+
429             " WHERE "+Columns.UPDATEDINFO.name()+" = "+
430             AbstractDbData.UpdatedInfo.TOSUBMIT.ordinal();
431         DbPreparedStatement prep = new DbPreparedStatement(session, request);
432         session.addLongTermPreparedStatement(prep);
433         return prep;
434     }
435     /*
436      * (non-Javadoc)
437      *
438      * @see openr66.database.data.AbstractDbData#changeUpdatedInfo(UpdatedInfo)
439      */
440     @Override
441     public void changeUpdatedInfo(UpdatedInfo info) {
442         if (updatedInfo != info.ordinal()) {
443             updatedInfo = info.ordinal();
444             allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
445             isSaved = false;
446         }
447     }
448     
449 }