1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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 }