View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, 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 Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.common.database.data;
21  
22  import org.waarp.common.database.DbPreparedStatement;
23  import org.waarp.common.database.DbSession;
24  import org.waarp.common.database.exception.WaarpDatabaseException;
25  import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
26  import org.waarp.common.database.exception.WaarpDatabaseNoDataException;
27  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
28  
29  import java.sql.Types;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  /**
33   * Example of Table object
34   */
35  public class DbDataModel extends AbstractDbData {
36    private static final String NO_ROW_FOUND = "No row found";
37  
38    private static final String WHERE = " WHERE ";
39  
40    private static final String SELECT = "SELECT ";
41  
42    private static final String FROM = " FROM ";
43  
44    public enum Columns {
45      READGLOBALLIMIT, WRITEGLOBALLIMIT, READSESSIONLIMIT, WRITESESSIONLIMIT,
46      DELAYLIMIT, UPDATEDINFO, HOSTID
47    }
48  
49    public static final int[] dbTypes = {
50        Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.BIGINT,
51        Types.INTEGER, Types.NVARCHAR
52    };
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,
58        Columns.WRITEGLOBALLIMIT, Columns.WRITESESSIONLIMIT, Columns.HOSTID
59    };
60  
61    /**
62     * HashTable in case of lack of database
63     */
64    private static final ConcurrentHashMap<String, DbDataModel>
65        dbR66ConfigurationHashMap = 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 =
85        Columns.READGLOBALLIMIT.name() + ',' + Columns.WRITEGLOBALLIMIT.name() +
86        ',' + Columns.READSESSIONLIMIT.name() + ',' +
87        Columns.WRITESESSIONLIMIT.name() + ',' + Columns.DELAYLIMIT.name() + ',' +
88        Columns.UPDATEDINFO.name() + ',' + Columns.HOSTID.name();
89  
90    protected static final String updateAllFields =
91        Columns.READGLOBALLIMIT.name() + "=?," + Columns.WRITEGLOBALLIMIT.name() +
92        "=?," + Columns.READSESSIONLIMIT.name() + "=?," +
93        Columns.WRITESESSIONLIMIT.name() + "=?," + Columns.DELAYLIMIT.name() +
94        "=?," + Columns.UPDATEDINFO.name() + "=?";
95  
96    protected static final String insertAllValues = " (?,?,?,?,?,?,?) ";
97  
98    @Override
99    protected void initObject() {
100     primaryKey = new DbValue[] { new DbValue(hostid, Columns.HOSTID.name()) };
101     otherFields = new DbValue[] {
102         new DbValue(readgloballimit, Columns.READGLOBALLIMIT.name()),
103         new DbValue(writegloballimit, Columns.WRITEGLOBALLIMIT.name()),
104         new DbValue(readsessionlimit, Columns.READSESSIONLIMIT.name()),
105         new DbValue(writesessionlimit, Columns.WRITESESSIONLIMIT.name()),
106         new DbValue(delayllimit, Columns.DELAYLIMIT.name()),
107         new DbValue(updatedInfo, Columns.UPDATEDINFO.name())
108     };
109     allFields = new DbValue[] {
110         otherFields[0], otherFields[1], otherFields[2], otherFields[3],
111         otherFields[4], otherFields[5], primaryKey[0]
112     };
113   }
114 
115   @Override
116   protected void setToArray() throws WaarpDatabaseSqlException {
117     validateLength(Types.NVARCHAR, hostid);
118     allFields[Columns.HOSTID.ordinal()].setValue(hostid);
119     allFields[Columns.READGLOBALLIMIT.ordinal()].setValue(readgloballimit);
120     allFields[Columns.WRITEGLOBALLIMIT.ordinal()].setValue(writegloballimit);
121     allFields[Columns.READSESSIONLIMIT.ordinal()].setValue(readsessionlimit);
122     allFields[Columns.WRITESESSIONLIMIT.ordinal()].setValue(writesessionlimit);
123     allFields[Columns.DELAYLIMIT.ordinal()].setValue(delayllimit);
124     allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
125   }
126 
127   @Override
128   protected void setFromArray() {
129     hostid = (String) allFields[Columns.HOSTID.ordinal()].getValue();
130     readgloballimit =
131         (Long) allFields[Columns.READGLOBALLIMIT.ordinal()].getValue();
132     writegloballimit =
133         (Long) allFields[Columns.WRITEGLOBALLIMIT.ordinal()].getValue();
134     readsessionlimit =
135         (Long) allFields[Columns.READSESSIONLIMIT.ordinal()].getValue();
136     writesessionlimit =
137         (Long) allFields[Columns.WRITESESSIONLIMIT.ordinal()].getValue();
138     delayllimit = (Long) allFields[Columns.DELAYLIMIT.ordinal()].getValue();
139     updatedInfo = (Integer) allFields[Columns.UPDATEDINFO.ordinal()].getValue();
140   }
141 
142   @Override
143   protected String getSelectAllFields() {
144     return selectAllFields;
145   }
146 
147   @Override
148   protected String getTable() {
149     return table;
150   }
151 
152   @Override
153   protected String getInsertAllValues() {
154     return insertAllValues;
155   }
156 
157   @Override
158   protected String getUpdateAllFields() {
159     return updateAllFields;
160   }
161 
162   @Override
163   protected String getWherePrimaryKey() {
164     return primaryKey[0].getColumn() + " = ? ";
165   }
166 
167   /**
168    * Set the primary Key as current value
169    */
170   @Override
171   protected void setPrimaryKey() {
172     primaryKey[0].setValue(hostid);
173   }
174 
175   /**
176    * @param dbSession
177    * @param hostid
178    * @param rg Read Global Limit
179    * @param wg Write Global Limit
180    * @param rs Read Session Limit
181    * @param ws Write Session Limit
182    * @param del Delay Limit
183    */
184   public DbDataModel(final DbSession dbSession, final String hostid,
185                      final long rg, final long wg, final long rs, final long ws,
186                      final long del) throws WaarpDatabaseSqlException {
187     super(dbSession);
188     this.hostid = hostid;
189     readgloballimit = rg;
190     writegloballimit = wg;
191     readsessionlimit = rs;
192     writesessionlimit = ws;
193     delayllimit = del;
194     setToArray();
195     isSaved = false;
196   }
197 
198   /**
199    * @param dbSession
200    * @param hostid
201    *
202    * @throws WaarpDatabaseException
203    */
204   public DbDataModel(final DbSession dbSession, final String hostid)
205       throws WaarpDatabaseException {
206     super(dbSession);
207     this.hostid = hostid;
208     // load from DB
209     select();
210   }
211 
212   @Override
213   public void delete() throws WaarpDatabaseException {
214     if (dbSession == null) {
215       dbR66ConfigurationHashMap.remove(hostid);
216       isSaved = false;
217       return;
218     }
219     final DbPreparedStatement preparedStatement =
220         new DbPreparedStatement(dbSession);
221     try {
222       preparedStatement.createPrepareStatement(
223           "DELETE FROM " + table + WHERE + getWherePrimaryKey());
224       setPrimaryKey();
225       setValues(preparedStatement, primaryKey);
226       final int count = preparedStatement.executeUpdate();
227       if (count <= 0) {
228         throw new WaarpDatabaseNoDataException(NO_ROW_FOUND);
229       }
230       isSaved = false;
231     } finally {
232       preparedStatement.realClose();
233     }
234   }
235 
236   @Override
237   public void insert() throws WaarpDatabaseException {
238     if (isSaved) {
239       return;
240     }
241     if (dbSession == null) {
242       dbR66ConfigurationHashMap.put(hostid, this);
243       isSaved = true;
244       return;
245     }
246     final DbPreparedStatement preparedStatement =
247         new DbPreparedStatement(dbSession);
248     try {
249       preparedStatement.createPrepareStatement(
250           "INSERT INTO " + table + " (" + selectAllFields + ") VALUES " +
251           insertAllValues);
252       setValues(preparedStatement, allFields);
253       final int count = preparedStatement.executeUpdate();
254       if (count <= 0) {
255         throw new WaarpDatabaseNoDataException(NO_ROW_FOUND);
256       }
257       isSaved = true;
258     } finally {
259       preparedStatement.realClose();
260     }
261   }
262 
263   @Override
264   public final boolean exist() throws WaarpDatabaseException {
265     if (dbSession == null) {
266       return dbR66ConfigurationHashMap.containsKey(hostid);
267     }
268     final DbPreparedStatement preparedStatement =
269         new DbPreparedStatement(dbSession);
270     try {
271       preparedStatement.createPrepareStatement(
272           SELECT + primaryKey[0].getColumn() + FROM + table + WHERE +
273           getWherePrimaryKey());
274       setPrimaryKey();
275       setValues(preparedStatement, primaryKey);
276       preparedStatement.executeQuery();
277       return preparedStatement.getNext();
278     } finally {
279       preparedStatement.realClose();
280     }
281   }
282 
283   @Override
284   public final void select() throws WaarpDatabaseException {
285     if (dbSession == null) {
286       final DbDataModel conf = dbR66ConfigurationHashMap.get(hostid);
287       if (conf == null) {
288         throw new WaarpDatabaseNoDataException(NO_ROW_FOUND);
289       } else {
290         // copy info
291         for (int i = 0; i < allFields.length; i++) {
292           allFields[i].setValue(conf.allFields[i].getValue());
293         }
294         setFromArray();
295         isSaved = true;
296         return;
297       }
298     }
299     final DbPreparedStatement preparedStatement =
300         new DbPreparedStatement(dbSession);
301     try {
302       preparedStatement.createPrepareStatement(
303           SELECT + selectAllFields + FROM + table + WHERE +
304           getWherePrimaryKey());
305       setPrimaryKey();
306       setValues(preparedStatement, primaryKey);
307       preparedStatement.executeQuery();
308       if (preparedStatement.getNext()) {
309         getValues(preparedStatement, allFields);
310         setFromArray();
311         isSaved = true;
312       } else {
313         throw new WaarpDatabaseNoDataException(NO_ROW_FOUND);
314       }
315     } finally {
316       preparedStatement.realClose();
317     }
318   }
319 
320   @Override
321   public final void update() throws WaarpDatabaseException {
322     if (isSaved) {
323       return;
324     }
325     if (dbSession == null) {
326       dbR66ConfigurationHashMap.put(hostid, this);
327       isSaved = true;
328       return;
329     }
330     final DbPreparedStatement preparedStatement =
331         new DbPreparedStatement(dbSession);
332     try {
333       preparedStatement.createPrepareStatement(
334           "UPDATE " + table + " SET " + updateAllFields + WHERE +
335           getWherePrimaryKey());
336       setValues(preparedStatement, allFields);
337       final int count = preparedStatement.executeUpdate();
338       if (count <= 0) {
339         throw new WaarpDatabaseNoDataException(NO_ROW_FOUND);
340       }
341       isSaved = true;
342     } finally {
343       preparedStatement.realClose();
344     }
345   }
346 
347   /**
348    * @return the DbPreparedStatement for getting Updated Object
349    *
350    * @throws WaarpDatabaseNoConnectionException
351    * @throws WaarpDatabaseSqlException
352    */
353   public static DbPreparedStatement getUpdatedPrepareStament(
354       final DbSession session)
355       throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
356     String request = SELECT + selectAllFields;
357     request += FROM + table + WHERE + Columns.UPDATEDINFO.name() + " = " +
358                AbstractDbData.UpdatedInfo.TOSUBMIT.ordinal();
359     final DbPreparedStatement prep = new DbPreparedStatement(session, request);
360     session.addLongTermPreparedStatement(prep);
361     return prep;
362   }
363 
364   @Override
365   public void changeUpdatedInfo(final UpdatedInfo info) {
366     if (updatedInfo != info.ordinal()) {
367       updatedInfo = info.ordinal();
368       allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
369       isSaved = false;
370     }
371   }
372 
373 }