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  
21  package org.waarp.openr66.dao.database;
22  
23  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
24  import org.waarp.common.lru.SynchronizedLruCache;
25  import org.waarp.openr66.dao.BusinessDAO;
26  import org.waarp.openr66.pojo.Business;
27  import org.waarp.openr66.pojo.UpdatedInfo;
28  import org.waarp.openr66.protocol.configuration.Configuration;
29  
30  import java.sql.Connection;
31  import java.sql.ResultSet;
32  import java.sql.SQLException;
33  
34  /**
35   * Implementation of BusinessDAO for standard SQL databases
36   */
37  public class DBBusinessDAO extends StatementExecutor<Business>
38      implements BusinessDAO {
39  
40    protected static final String TABLE = "hostconfig";
41  
42    public static final String HOSTID_FIELD = "hostid";
43    public static final String BUSINESS_FIELD = "business";
44    public static final String ROLES_FIELD = "roles";
45    public static final String ALIASES_FIELD = "aliases";
46    public static final String OTHERS_FIELD = "others";
47    public static final String UPDATED_INFO_FIELD = "updatedInfo";
48  
49    protected static final String SQL_DELETE_ALL = "DELETE FROM " + TABLE;
50    protected static final String SQL_DELETE =
51        "DELETE FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
52    protected static final String SQL_GET_ALL = "SELECT * FROM " + TABLE;
53    protected static final String SQL_COUNT_ALL = SQL_COUNT_ALL_PREFIX + TABLE;
54    protected static final String SQL_EXIST =
55        "SELECT 1 FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
56    protected static final String SQL_SELECT =
57        "SELECT * FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
58    protected static final String SQL_INSERT =
59        "INSERT INTO " + TABLE + " (" + HOSTID_FIELD + ", " + BUSINESS_FIELD +
60        ", " + ROLES_FIELD + ", " + ALIASES_FIELD + ", " + OTHERS_FIELD + ", " +
61        UPDATED_INFO_FIELD + ") VALUES (?,?,?,?,?,?)";
62    protected static final String SQL_UPDATE =
63        "UPDATE " + TABLE + " SET " + HOSTID_FIELD + PARAMETER_COMMA +
64        BUSINESS_FIELD + PARAMETER_COMMA + ROLES_FIELD + PARAMETER_COMMA +
65        ALIASES_FIELD + PARAMETER_COMMA + OTHERS_FIELD + PARAMETER_COMMA +
66        UPDATED_INFO_FIELD + " = ? WHERE " + HOSTID_FIELD + PARAMETER;
67  
68    /**
69     * Hashmap for Business
70     */
71    private static final SynchronizedLruCache<String, Business>
72        reentrantConcurrentHashMap =
73        new SynchronizedLruCache<String, Business>(500, 180000);
74  
75    public DBBusinessDAO(final Connection con) {
76      super(con);
77    }
78  
79    @Override
80    protected final boolean isCachedEnable() {
81      return Configuration.configuration.getMultipleMonitors() <= 1;
82    }
83  
84    @Override
85    protected final SynchronizedLruCache<String, Business> getCache() {
86      return reentrantConcurrentHashMap;
87    }
88  
89    @Override
90    protected final String getId(final Business e1) {
91      return e1.getHostid();
92    }
93  
94    @Override
95    protected final String getTable() {
96      return TABLE;
97    }
98  
99    @Override
100   protected final String getSelectRequest() {
101     return SQL_SELECT;
102   }
103 
104   @Override
105   protected final String getGetAllRequest() {
106     return SQL_GET_ALL;
107   }
108 
109   @Override
110   protected final String getCountRequest() {
111     return SQL_COUNT_ALL;
112   }
113 
114   @Override
115   protected final String getExistRequest() {
116     return SQL_EXIST;
117   }
118 
119   @Override
120   protected final Object[] getInsertValues(final Business business)
121       throws WaarpDatabaseSqlException {
122     business.checkValues();
123     return new Object[] {
124         business.getHostid(), business.getBusiness(), business.getRoles(),
125         business.getAliases(), business.getOthers(),
126         business.getUpdatedInfo().ordinal()
127     };
128   }
129 
130   @Override
131   protected final String getInsertRequest() {
132     return SQL_INSERT;
133   }
134 
135   @Override
136   protected final Object[] getUpdateValues(final Business business)
137       throws WaarpDatabaseSqlException {
138     business.checkValues();
139     return new Object[] {
140         business.getHostid(), business.getBusiness(), business.getRoles(),
141         business.getAliases(), business.getOthers(),
142         business.getUpdatedInfo().ordinal(), business.getHostid()
143     };
144   }
145 
146   @Override
147   protected final String getUpdateRequest() {
148     return SQL_UPDATE;
149   }
150 
151   @Override
152   protected final String getDeleteRequest() {
153     return SQL_DELETE;
154   }
155 
156   @Override
157   protected final String getDeleteAllRequest() {
158     return SQL_DELETE_ALL;
159   }
160 
161   @Override
162   public final Business getFromResultSet(final ResultSet set)
163       throws SQLException {
164     try {
165       return new Business(set.getString(HOSTID_FIELD),
166                           set.getString(BUSINESS_FIELD),
167                           set.getString(ROLES_FIELD),
168                           set.getString(ALIASES_FIELD),
169                           set.getString(OTHERS_FIELD),
170                           UpdatedInfo.valueOf(set.getInt(UPDATED_INFO_FIELD)));
171     } catch (final WaarpDatabaseSqlException e) {
172       throw new SQLException(e);
173     }
174   }
175 
176   @Override
177   protected final boolean isDbTransfer() {
178     return false;
179   }
180 }