-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityModel.java
413 lines (334 loc) · 15.1 KB
/
EntityModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package mx.nic.rdap.sql.model;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import mx.nic.rdap.core.catalog.Role;
import mx.nic.rdap.core.catalog.Status;
import mx.nic.rdap.core.db.Autnum;
import mx.nic.rdap.core.db.Entity;
import mx.nic.rdap.core.db.Event;
import mx.nic.rdap.core.db.IpNetwork;
import mx.nic.rdap.core.db.PublicId;
import mx.nic.rdap.core.db.UserConsent;
import mx.nic.rdap.core.db.UserConsentGlobal;
import mx.nic.rdap.core.db.UserConsentByAttribute;
import mx.nic.rdap.core.db.VCard;
import mx.nic.rdap.db.exception.http.NotImplementedException;
import mx.nic.rdap.db.struct.SearchResultStruct;
import mx.nic.rdap.sql.QueryGroup;
import mx.nic.rdap.sql.SQLProviderConfiguration;
import mx.nic.rdap.sql.objects.EntityDbObj;
/**
* Model for the {@link Entity} Object
*
*/
public class EntityModel {
private final static Logger logger = Logger.getLogger(EntityModel.class.getName());
private final static String QUERY_GROUP = "Entity";
private static QueryGroup queryGroup = null;
private final static String GET_BY_HANDLE_QUERY = "getByHandle";
private final static String SEARCH_BY_PARTIAL_HANDLE_QUERY = "searchByPartialHandle";
private final static String SEARCH_BY_HANDLE_QUERY = "searchByHandle";
private final static String SEARCH_BY_PARTIAL_NAME_QUERY = "searchByPartialName";
private final static String SEARCH_BY_NAME_QUERY = "searchByName";
private final static String GET_ENTITY_ENTITY_QUERY = "getEntitysEntitiesQuery";
private final static String GET_DOMAIN_ENTITY_QUERY = "getDomainsEntitiesQuery";
private final static String GET_NS_ENTITY_QUERY = "getNameserversEntitiesQuery";
private final static String GET_AUTNUM_ENTITY_QUERY = "getAutnumEntitiesQuery";
private final static String GET_IP_NETWORK_ENTITY_QUERY = "getIpNetworkEntitiesQuery";
private final static String SEARCH_BY_HANDLE_REGEX_QUERY = "searchByRegexHandle";
private final static String SEARCH_BY_NAME_REGEX_QUERY = "searchByRegexName";
private final static String GET_USER_GLOBAL_CONSENT = "userGlobalConsent";
private final static String GET_USER_CONSENT_BY_ATTR = "userConsentByAttribute";
private static String GET_CONSENT;
public static void loadQueryGroup(String schema) {
try {
QueryGroup qG = new QueryGroup(QUERY_GROUP, schema);
setQueryGroup(qG);
} catch (IOException e) {
throw new RuntimeException("Error loading query group");
}
switch (SQLProviderConfiguration.getUserConsentType()) {
case GLOBAL:
GET_CONSENT = GET_USER_GLOBAL_CONSENT;
break;
case ATTRIBUTES:
GET_CONSENT = GET_USER_CONSENT_BY_ATTR;
break;
case NONE:
default:
GET_CONSENT = null;
// nothing to do.
break;
}
}
private static void setQueryGroup(QueryGroup qG) {
queryGroup = qG;
}
private static QueryGroup getQueryGroup() {
return queryGroup;
}
public static EntityDbObj getByHandle(String entityHandle, Connection connection)
throws SQLException, NotImplementedException {
EntityDbObj entResult = null;
String query = getQueryGroup().getQuery(GET_BY_HANDLE_QUERY);
QueryGroup.userImplemented(query);
try (PreparedStatement statement = connection.prepareStatement(query);) {
statement.setString(1, entityHandle);
logger.log(Level.INFO, "Executing QUERY:" + statement.toString());
ResultSet resultSet = statement.executeQuery();
entResult = processResultSet(resultSet, connection);
}
if (entResult == null) {
return null;
}
loadNestedObjects(entResult, connection);
return entResult;
}
private static void loadNestedObjects(Entity entity, Connection connection) throws SQLException {
Long entityId = entity.getId();
entity.getVCardList().addAll(VCardModel.getByEntityId(entityId, connection));
// Retrieve the status
entity.getStatus().addAll(StatusModel.getByEntityId(entityId, connection));
// Retrieve the links
entity.getLinks().addAll(LinkModel.getByEntityId(entityId, connection));
// Retrive the remarks
entity.getRemarks().addAll(RemarkModel.getByEntityId(entityId, connection));
// Retrieve the events
entity.getEvents().addAll(EventModel.getByEntityId(entityId, connection));
// Retrieve the public ids
entity.getPublicIds().addAll(PublicIdModel.getByEntity(entityId, connection));
// Retrieve the entities
List<Entity> entitiesByEntityId = getEntitiesByEntityId(entityId, connection);
entity.getEntities().addAll(entitiesByEntityId);
// retrieve the networks
List<IpNetwork> networks = IpNetworkModel.getByEntityId(entityId, connection);
entity.getIpNetworks().addAll(networks);
// retrieve the autnums
List<Autnum> autnums = AutnumModel.getByEntityId(entityId, connection);
entity.getAutnums().addAll(autnums);
// retrieve the entity roles
List<Role> mainEntityRole = RoleModel.getMainEntityRole(entityId, connection);
entity.getRoles().addAll(mainEntityRole);
// retrieve the consent for this object;
entity.setConsent(getConsentById(entityId, connection));
}
private static UserConsent getConsentById(long entityId, Connection connection) throws SQLException {
if (GET_CONSENT == null) {
return null;
}
String query = getQueryGroup().getQuery(GET_CONSENT);
if (SQLProviderConfiguration.isUserSQL() && query == null) {
return null;
}
UserConsent result = null;
try (PreparedStatement statement = connection.prepareStatement(query);) {
statement.setLong(1, entityId);
logger.log(Level.INFO, "Executing QUERY: " + statement.toString());
ResultSet rs = statement.executeQuery();
if (!rs.next()) {
return null;
}
// TODO handle result;
//result
switch (SQLProviderConfiguration.getUserConsentType()) {
case GLOBAL:
UserConsentGlobal userConsent = new UserConsentGlobal();
userConsent.setGlobalConsent(rs.getBoolean("ugc_consent"));
result = userConsent;
break;
case ATTRIBUTES:
UserConsentByAttribute consentByAttr = new UserConsentByAttribute();
consentByAttr.setHandle(rs.getBoolean("uca_handle"));
consentByAttr.setName(rs.getBoolean("uca_name"));
consentByAttr.setCompanyName(rs.getBoolean("uca_companyName"));
consentByAttr.setCompanyURL(rs.getBoolean("uca_companyURL"));
consentByAttr.setEmail(rs.getBoolean("uca_email"));
consentByAttr.setVoice(rs.getBoolean("uca_voice"));
consentByAttr.setCellphone(rs.getBoolean("uca_cellphone"));
consentByAttr.setFax(rs.getBoolean("uca_fax"));
consentByAttr.setJobTitle(rs.getBoolean("uca_jobTitle"));
consentByAttr.setContactUri(rs.getBoolean("uca_contactUri"));
consentByAttr.setType(rs.getBoolean("uca_type"));
consentByAttr.setCountry(rs.getBoolean("uca_country"));
consentByAttr.setCountryCode(rs.getBoolean("uca_countryCode"));
consentByAttr.setCity(rs.getBoolean("uca_city"));
consentByAttr.setState(rs.getBoolean("uca_state"));
consentByAttr.setStreet1(rs.getBoolean("uca_street1"));
consentByAttr.setStreet2(rs.getBoolean("uca_street2"));
consentByAttr.setStreet3(rs.getBoolean("uca_street3"));
consentByAttr.setPostalCode(rs.getBoolean("uca_postalCode"));
result = consentByAttr;
break;
default:
// Maybe Programming error?
break;
}
}
return result;
}
private static EntityDbObj processResultSet(ResultSet resultSet, Connection connection) throws SQLException {
if (!resultSet.next()) {
return null;
}
return new EntityDbObj(resultSet);
}
private static List<Entity> getEntitiesByEntityId(Long entityId, Connection connection) throws SQLException {
return getEntitiesByEntityId(entityId, connection, true);
}
private static List<Entity> getEntitiesByEntityId(Long entityId, Connection connection, boolean isFirstNested) throws SQLException {
List<Entity> entitiesById = getEntitiesById(entityId, connection, GET_ENTITY_ENTITY_QUERY, isFirstNested);
for (Entity ent : entitiesById) {
List<Role> entityEntityRole = RoleModel.getEntityEntityRole(entityId, ent.getId(), connection);
ent.getRoles().addAll(entityEntityRole);
}
return entitiesById;
}
public static List<Entity> getEntitiesByDomainId(Long domainId, Connection connection) throws SQLException {
List<Entity> entitiesById = getEntitiesById(domainId, connection, GET_DOMAIN_ENTITY_QUERY);
for (Entity ent : entitiesById) {
List<Role> entityEntityRole = RoleModel.getDomainEntityRole(domainId, ent.getId(), connection);
ent.getRoles().addAll(entityEntityRole);
}
return entitiesById;
}
public static List<Entity> getEntitiesByNameserverId(Long nameserverId, Connection connection) throws SQLException {
List<Entity> entitiesById = getEntitiesById(nameserverId, connection, GET_NS_ENTITY_QUERY);
for (Entity ent : entitiesById) {
List<Role> entityEntityRole = RoleModel.getNameserverEntityRole(nameserverId, ent.getId(), connection);
ent.getRoles().addAll(entityEntityRole);
}
return entitiesById;
}
public static List<Entity> getEntitiesByAutnumId(Long autnumId, Connection connection) throws SQLException {
List<Entity> entitiesById = getEntitiesById(autnumId, connection, GET_AUTNUM_ENTITY_QUERY);
for (Entity ent : entitiesById) {
List<Role> entityEntityRole = RoleModel.getAutnumEntityRole(autnumId, ent.getId(), connection);
ent.getRoles().addAll(entityEntityRole);
}
return entitiesById;
}
public static List<Entity> getEntitiesByIpNetworkId(Long ipNetworkId, Connection connection) throws SQLException {
List<Entity> entitiesById = getEntitiesById(ipNetworkId, connection, GET_IP_NETWORK_ENTITY_QUERY);
for (Entity ent : entitiesById) {
List<Role> entityEntityRole = RoleModel.getIpNetworkEntityRole(ipNetworkId, ent.getId(), connection);
ent.getRoles().addAll(entityEntityRole);
}
return entitiesById;
}
private static List<Entity> getEntitiesById(Long id, Connection connection, String getQueryId) throws SQLException {
return getEntitiesById(id, connection, getQueryId, true);
}
private static List<Entity> getEntitiesById(Long id, Connection connection, String getQueryId, boolean isFirstNested) throws SQLException {
String query = getQueryGroup().getQuery(getQueryId);
if (SQLProviderConfiguration.isUserSQL() && query == null) {
return Collections.emptyList();
}
List<Entity> result = null;
try (PreparedStatement statement = connection.prepareStatement(query);) {
statement.setLong(1, id);
logger.log(Level.INFO, "Executing QUERY: " + statement.toString());
ResultSet rs = statement.executeQuery();
if (!rs.next()) {
return Collections.emptyList();
}
result = new ArrayList<>();
do {
result.add(new EntityDbObj(rs));
} while (rs.next());
}
setNestedSimpleObjects(result, connection, isFirstNested);
return result;
}
private static void setNestedSimpleObjects(List<Entity> entities, Connection connection, boolean isFirstNested) throws SQLException {
for (Entity entity : entities) {
Long entityId = entity.getId();
List<VCard> vCardList = VCardModel.getByEntityId(entityId, connection);
entity.getVCardList().addAll(vCardList);
List<Status> statusList = StatusModel.getByEntityId(entityId, connection);
entity.getStatus().addAll(statusList);
List<PublicId> pidList = PublicIdModel.getByEntity(entityId, connection);
entity.getPublicIds().addAll(pidList);
List<Event> eventList = EventModel.getByEntityId(entityId, connection);
entity.getEvents().addAll(eventList);
// retrieve the consent for this object;
entity.setConsent(getConsentById(entityId, connection));
if (isFirstNested) {
List<Entity> nestedEntities = getEntitiesByEntityId(entityId, connection, false);
entity.getEntities().addAll(nestedEntities);
}
}
return;
}
public static SearchResultStruct<Entity> searchByHandle(String handle, int resultLimit, Connection connection)
throws SQLException, NotImplementedException {
String query = null;
if (handle.contains("*")) {
query = getQueryGroup().getQuery(SEARCH_BY_PARTIAL_HANDLE_QUERY);
// Escape special chars for the "LIKE" sentence and consecutive wildcards are treated as one
handle = handle.replaceAll("(\\%|\\_)", "\\\\$1").replaceAll("(\\*)+", "\\*").replace("*", "%");
} else {
query = getQueryGroup().getQuery(SEARCH_BY_HANDLE_QUERY);
}
return searchBy(handle, resultLimit, connection, query);
}
public static SearchResultStruct<Entity> searchByVCardName(String vcardName, int resultLimit, Connection connection)
throws SQLException, NotImplementedException {
String query = null;
if (vcardName.contains("*")) {
// Escape special chars for the "LIKE" sentence and consecutive wildcards are treated as one
vcardName = vcardName.replaceAll("(\\%|\\_)", "\\\\$1").replaceAll("(\\*)+", "\\*").replace("*", "%");
query = getQueryGroup().getQuery(SEARCH_BY_PARTIAL_NAME_QUERY);
} else {
query = getQueryGroup().getQuery(SEARCH_BY_NAME_QUERY);
}
return searchBy(vcardName, resultLimit, connection, query);
}
public static SearchResultStruct<Entity> searchByRegexHandle(String regexHandle, int resultLimit,
Connection connection) throws SQLException, NotImplementedException {
return searchBy(regexHandle, resultLimit, connection, getQueryGroup().getQuery(SEARCH_BY_HANDLE_REGEX_QUERY));
}
public static SearchResultStruct<Entity> searchByRegexName(String regexName, int resultLimit, Connection connection)
throws SQLException, NotImplementedException {
return searchBy(regexName, resultLimit, connection, getQueryGroup().getQuery(SEARCH_BY_NAME_REGEX_QUERY));
}
private static SearchResultStruct<Entity> searchBy(String criteria, int resultLimit, Connection connection,
String query) throws SQLException, NotImplementedException {
QueryGroup.userImplemented(query);
SearchResultStruct<Entity> result = new SearchResultStruct<Entity>();
// Hack to know is there is more domains that the limit, used for
// notices
resultLimit = resultLimit + 1;
List<EntityDbObj> entities = new ArrayList<EntityDbObj>();
try (PreparedStatement statement = connection.prepareStatement(query);) {
statement.setString(1, criteria);
statement.setInt(2, resultLimit);
logger.log(Level.INFO, "Executing QUERY:" + statement.toString());
ResultSet rs = statement.executeQuery();
if (!rs.next()) {
return null;
}
do {
entities.add(new EntityDbObj(rs));
} while (rs.next());
resultLimit = resultLimit - 1;// Back to the original limit
if (entities.size() > resultLimit) {
result.setResultSetWasLimitedByUserConfiguration(true);
entities.remove(entities.size() - 1);
}
for (EntityDbObj ent : entities) {
loadNestedObjects(ent, connection);
}
result.setSearchResultsLimitForUser(resultLimit);
result.getResults().addAll(entities);
return result;
}
}
}