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.openr66.database.data;
21  
22  import com.fasterxml.jackson.databind.JsonNode;
23  import org.waarp.common.database.DbConstant;
24  import org.waarp.common.database.DbPreparedStatement;
25  import org.waarp.common.database.DbSession;
26  import org.waarp.common.database.exception.WaarpDatabaseException;
27  import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
28  import org.waarp.common.database.exception.WaarpDatabaseNoDataException;
29  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
30  import org.waarp.openr66.dao.AbstractDAO;
31  import org.waarp.openr66.dao.DAOFactory;
32  import org.waarp.openr66.dao.MultipleMonitorDAO;
33  import org.waarp.openr66.dao.database.DBDAOFactory;
34  import org.waarp.openr66.dao.database.StatementExecutor;
35  import org.waarp.openr66.dao.exception.DAOConnectionException;
36  import org.waarp.openr66.dao.exception.DAONoDataException;
37  import org.waarp.openr66.pojo.MultipleMonitor;
38  import org.waarp.openr66.protocol.configuration.Configuration;
39  
40  import java.sql.SQLException;
41  import java.sql.Types;
42  
43  /**
44   * Configuration Table object
45   */
46  public class DbMultipleMonitor extends AbstractDbDataDao<MultipleMonitor> {
47    public enum Columns {
48      COUNTCONFIG, COUNTHOST, COUNTRULE, HOSTID
49    }
50  
51    public static final int[] dbTypes =
52        { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.NVARCHAR };
53  
54    public static final String table = " MULTIPLEMONITOR ";
55  
56    public static final String selectAllFields =
57        Columns.COUNTCONFIG.name() + ',' + Columns.COUNTHOST.name() + ',' +
58        Columns.COUNTRULE.name() + ',' + Columns.HOSTID.name();
59  
60    @Override
61    protected final void initObject() {
62      // Nothing
63    }
64  
65    @Override
66    protected final String getTable() {
67      return table;
68    }
69  
70    @Override
71    protected final AbstractDAO<MultipleMonitor> getDao(final boolean isCacheable)
72        throws DAOConnectionException {
73      return DAOFactory.getInstance().getMultipleMonitorDAO(isCacheable);
74    }
75  
76    @Override
77    protected final String getPrimaryKey() {
78      if (pojo != null) {
79        return pojo.getHostid();
80      }
81      throw new IllegalArgumentException("pojo is null");
82    }
83  
84    @Override
85    protected final String getPrimaryField() {
86      return Columns.HOSTID.name();
87    }
88  
89    /**
90     * @param hostid
91     * @param cc count for Config
92     * @param ch count for Host
93     * @param cr count for Rule
94     */
95    public DbMultipleMonitor(final String hostid, final int cc, final int ch,
96                             final int cr) throws WaarpDatabaseSqlException {
97      pojo = new MultipleMonitor(hostid, cc, ch, cr);
98    }
99  
100   /**
101    * @param hostid
102    *
103    * @throws WaarpDatabaseException
104    */
105   public DbMultipleMonitor(final String hostid) throws WaarpDatabaseException {
106     MultipleMonitorDAO monitorAccess = null;
107     try {
108       monitorAccess = DAOFactory.getInstance().getMultipleMonitorDAO(true);
109       pojo = monitorAccess.select(hostid);
110     } catch (final DAOConnectionException e) {
111       throw new WaarpDatabaseException(e);
112     } catch (final DAONoDataException e) {
113       throw new WaarpDatabaseNoDataException("Cannot find " + "MultipleMonitor",
114                                              e);
115     } finally {
116       DBDAOFactory.closeDAO(monitorAccess);
117     }
118   }
119 
120   @Override
121   protected final void checkValues() throws WaarpDatabaseSqlException {
122     pojo.checkValues();
123   }
124 
125   @Override
126   protected final void setFromJson(final String field, final JsonNode value) {
127     if (value == null) {
128       return;
129     }
130     for (final Columns column : Columns.values()) {
131       if (column.name().equalsIgnoreCase(field)) {
132         switch (column) {
133           case COUNTCONFIG:
134             pojo.setCountConfig(value.asInt());
135             break;
136           case COUNTHOST:
137             pojo.setCountHost(value.asInt());
138             break;
139           case COUNTRULE:
140             pojo.setCountRule(value.asInt());
141             break;
142           case HOSTID:
143             pojo.setHostid(value.asText());
144             break;
145         }
146       }
147     }
148   }
149 
150   /**
151    * Private constructor for Commander only
152    */
153   private DbMultipleMonitor() {
154     pojo = new MultipleMonitor();
155   }
156 
157   /**
158    * For instance from Commander when getting updated information
159    *
160    * @param preparedStatement
161    *
162    * @return the next updated Configuration
163    *
164    * @throws WaarpDatabaseNoConnectionException
165    * @throws WaarpDatabaseSqlException
166    */
167   public static DbMultipleMonitor getFromStatement(
168       final DbPreparedStatement preparedStatement)
169       throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
170     final DbMultipleMonitor dbMm = new DbMultipleMonitor();
171     AbstractDAO<MultipleMonitor> multipleDAO = null;
172     try {
173       multipleDAO = dbMm.getDao(false);
174       dbMm.pojo =
175           ((StatementExecutor<MultipleMonitor>) multipleDAO).getFromResultSet(
176               preparedStatement.getResultSet());
177       return dbMm;
178     } catch (final SQLException e) {
179       DbConstant.error(e);
180       throw new WaarpDatabaseSqlException("Getting values in error", e);
181     } catch (final DAOConnectionException e) {
182       throw new WaarpDatabaseSqlException("Getting values in error", e);
183     } finally {
184       DAOFactory.closeDAO(multipleDAO);
185     }
186   }
187 
188   /**
189    * @return the DbPreparedStatement for getting Updated Object in "FOR
190    *     UPDATE" mode
191    *
192    * @throws WaarpDatabaseNoConnectionException
193    * @throws WaarpDatabaseSqlException
194    */
195   public static DbPreparedStatement getUpdatedPrepareStament(
196       final DbSession session)
197       throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
198     final DbMultipleMonitor multipleMonitor =
199         new DbMultipleMonitor(Configuration.configuration.getHostId(), 0, 0, 0);
200     try {
201       if (!multipleMonitor.exist()) {
202         multipleMonitor.insert();
203         session.commit();
204       }
205     } catch (final WaarpDatabaseException ignored) {
206       // ignore
207     }
208     String request = "SELECT " + selectAllFields;
209     request += " FROM " + table + " WHERE " + Columns.HOSTID.name() + " = '" +
210                Configuration.configuration.getHostId() + '\'' + " FOR UPDATE ";
211     return new DbPreparedStatement(session, request);
212   }
213 
214   /**
215    * On Commander side
216    *
217    * @return True if this is the last update
218    */
219   public final boolean checkUpdateConfig() {
220     if (getCountConfig() <= 0) {
221       setCountConfig(Configuration.configuration.getMultipleMonitors());
222       setCountConfig(getCountConfig() - 1);
223       isSaved = false;
224     } else {
225       setCountConfig(getCountConfig() - 1);
226       isSaved = false;
227     }
228     return getCountConfig() <= 0;
229   }
230 
231   /**
232    * On Commander side
233    *
234    * @return True if this is the last update
235    */
236   public final boolean checkUpdateHost() {
237     if (getCountHost() <= 0) {
238       setCountHost(Configuration.configuration.getMultipleMonitors());
239       setCountHost(getCountHost() - 1);
240       isSaved = false;
241     } else {
242       setCountHost(getCountHost() - 1);
243       isSaved = false;
244     }
245     return getCountHost() <= 0;
246   }
247 
248   /**
249    * On Commander side
250    *
251    * @return True if this is the last update
252    */
253   public final boolean checkUpdateRule() {
254     if (getCountRule() <= 0) {
255       setCountRule(Configuration.configuration.getMultipleMonitors());
256       setCountRule(getCountRule() - 1);
257       isSaved = false;
258     } else {
259       setCountRule(getCountRule() - 1);
260       isSaved = false;
261     }
262     return getCountRule() <= 0;
263   }
264 
265   @Override
266   public final void changeUpdatedInfo(final UpdatedInfo info) {
267     // ignore
268   }
269 
270   /**
271    * return the String representation
272    */
273   @Override
274   public final String toString() {
275     return "DbMM " + getCountConfig() + ':' + getCountHost() + ':' +
276            getCountRule();
277   }
278 
279   /**
280    * @return the countConfig
281    */
282   public final int getCountConfig() {
283     return pojo.getCountConfig();
284   }
285 
286   /**
287    * @param countConfig the countConfig to set
288    */
289   private void setCountConfig(final int countConfig) {
290     isSaved = false;
291     pojo.setCountConfig(countConfig);
292   }
293 
294   /**
295    * @return the countHost
296    */
297   public final int getCountHost() {
298     return pojo.getCountHost();
299   }
300 
301   /**
302    * @param countHost the countHost to set
303    */
304   private void setCountHost(final int countHost) {
305     isSaved = false;
306     pojo.setCountHost(countHost);
307   }
308 
309   /**
310    * @return the countRule
311    */
312   public final int getCountRule() {
313     return pojo.getCountRule();
314   }
315 
316   /**
317    * @param countRule the countRule to set
318    */
319   private void setCountRule(final int countRule) {
320     isSaved = false;
321     pojo.setCountRule(countRule);
322   }
323 }