|
8 | 8 | import java.util.ArrayList; |
9 | 9 | import java.util.List; |
10 | 10 | import java.util.HashMap; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collection; |
11 | 13 |
|
12 | 14 | import org.json.JSONException; |
13 | 15 | import org.json.JSONObject; |
@@ -363,9 +365,105 @@ public JSONArray yellowpagesSearch(HashMap<String, Object> parameters) { |
363 | 365 | return getData(response); |
364 | 366 | } |
365 | 367 |
|
| 368 | + private Integer toPositiveInt(Object value, String fieldName) { |
| 369 | + if (value == null || value == JSONObject.NULL) return null; |
| 370 | + |
| 371 | + int parsed; |
| 372 | + if (value instanceof Number) { |
| 373 | + parsed = ((Number) value).intValue(); |
| 374 | + } else { |
| 375 | + try { |
| 376 | + parsed = Integer.parseInt(String.valueOf(value).trim()); |
| 377 | + } catch (NumberFormatException e) { |
| 378 | + throw new IllegalArgumentException(fieldName + " must be an integer >= 1"); |
| 379 | + } |
| 380 | + } |
| 381 | + |
| 382 | + if (parsed < 1) { |
| 383 | + throw new IllegalArgumentException(fieldName + " must be >= 1"); |
| 384 | + } |
| 385 | + |
| 386 | + return parsed; |
| 387 | + } |
| 388 | + |
| 389 | + private List<String> normalizeEnrichments(Object enrichmentsObj) { |
| 390 | + List<String> enrichments = new ArrayList<>(); |
| 391 | + if (enrichmentsObj == null || enrichmentsObj == JSONObject.NULL) return enrichments; |
| 392 | + |
| 393 | + if (enrichmentsObj instanceof String) { |
| 394 | + String raw = ((String) enrichmentsObj).trim(); |
| 395 | + if (raw.isEmpty()) return enrichments; |
| 396 | + if (raw.contains(",")) { |
| 397 | + Arrays.stream(raw.split(",")) |
| 398 | + .map(String::trim) |
| 399 | + .filter(s -> !s.isEmpty()) |
| 400 | + .forEach(enrichments::add); |
| 401 | + } else { |
| 402 | + enrichments.add(raw); |
| 403 | + } |
| 404 | + return enrichments; |
| 405 | + } |
| 406 | + |
| 407 | + if (enrichmentsObj instanceof JSONArray) { |
| 408 | + JSONArray enrichmentsArray = (JSONArray) enrichmentsObj; |
| 409 | + for (int i = 0; i < enrichmentsArray.length(); i++) { |
| 410 | + Object value = enrichmentsArray.opt(i); |
| 411 | + if (value != null && value != JSONObject.NULL) { |
| 412 | + String normalized = String.valueOf(value).trim(); |
| 413 | + if (!normalized.isEmpty()) enrichments.add(normalized); |
| 414 | + } |
| 415 | + } |
| 416 | + return enrichments; |
| 417 | + } |
| 418 | + |
| 419 | + if (enrichmentsObj instanceof Collection) { |
| 420 | + for (Object value : (Collection<?>) enrichmentsObj) { |
| 421 | + if (value != null) { |
| 422 | + String normalized = String.valueOf(value).trim(); |
| 423 | + if (!normalized.isEmpty()) enrichments.add(normalized); |
| 424 | + } |
| 425 | + } |
| 426 | + return enrichments; |
| 427 | + } |
| 428 | + |
| 429 | + if (enrichmentsObj.getClass().isArray()) { |
| 430 | + Object[] array = (Object[]) enrichmentsObj; |
| 431 | + for (Object value : array) { |
| 432 | + if (value != null) { |
| 433 | + String normalized = String.valueOf(value).trim(); |
| 434 | + if (!normalized.isEmpty()) enrichments.add(normalized); |
| 435 | + } |
| 436 | + } |
| 437 | + return enrichments; |
| 438 | + } |
| 439 | + |
| 440 | + enrichments.add(String.valueOf(enrichmentsObj).trim()); |
| 441 | + enrichments.removeIf(String::isEmpty); |
| 442 | + return enrichments; |
| 443 | + } |
| 444 | + |
| 445 | + private void normalizeBusinessesEnrichmentParams(HashMap<String, Object> parameters) { |
| 446 | + List<String> enrichments = normalizeEnrichments(parameters.get("enrichments")); |
| 447 | + if (!enrichments.isEmpty()) { |
| 448 | + parameters.put("enrichments", enrichments); |
| 449 | + } |
| 450 | + |
| 451 | + Integer contactsPerCompany = toPositiveInt(parameters.get("contacts_per_company"), "contacts_per_company"); |
| 452 | + Integer emailsPerContact = toPositiveInt(parameters.get("emails_per_contact"), "emails_per_contact"); |
| 453 | + |
| 454 | + boolean hasContactsEnrichment = enrichments.contains("contacts_n_leads"); |
| 455 | + if (hasContactsEnrichment) { |
| 456 | + parameters.put("contacts_per_company", contactsPerCompany != null ? contactsPerCompany : 3); |
| 457 | + parameters.put("emails_per_contact", emailsPerContact != null ? emailsPerContact : 1); |
| 458 | + } else if (contactsPerCompany != null || emailsPerContact != null) { |
| 459 | + throw new IllegalArgumentException("contacts_per_company and emails_per_contact require enrichments to include \"contacts_n_leads\""); |
| 460 | + } |
| 461 | + } |
| 462 | + |
366 | 463 | public JSONObject businessesSearch(HashMap<String, Object> parameters) { |
367 | 464 | if (parameters == null) parameters = new HashMap<>(); |
368 | 465 | parameters.putIfAbsent("async", false); |
| 466 | + normalizeBusinessesEnrichmentParams(parameters); |
369 | 467 |
|
370 | 468 | JSONObject response = postAPIRequest("/businesses", parameters); |
371 | 469 | if (response == null) return null; |
|
0 commit comments