-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3426504
commit 4ce9157
Showing
6 changed files
with
145 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ain/java/org/gbif/occurrence/search/configuration/NameUsageMatchServiceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.gbif.occurrence.search.configuration; | ||
|
||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@Configuration | ||
@ConfigurationProperties(prefix = "name-services") | ||
public class NameUsageMatchServiceConfiguration { | ||
|
||
List<NameUsageMatchServiceConfig> services; | ||
|
||
@Data | ||
public static class NameUsageMatchServiceConfig { | ||
private String name; | ||
private String datasetKey; | ||
private Ws ws; | ||
private String prefix; | ||
|
||
@Data | ||
public static class Ws { | ||
private Api api; | ||
|
||
@Data | ||
public static class Api { | ||
private String wsUrl; | ||
} | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...h/src/main/java/org/gbif/occurrence/search/configuration/NameUsageMatchServiceTriage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.gbif.occurrence.search.configuration; | ||
|
||
import org.gbif.kvs.species.NameUsageMatchRequest; | ||
import org.gbif.rest.client.species.NameUsageMatchResponse; | ||
import org.gbif.rest.client.species.NameUsageMatchingService; | ||
import org.gbif.ws.client.ClientBuilder; | ||
import org.gbif.ws.json.JacksonJsonObjectMapperProvider; | ||
import org.springframework.stereotype.Service; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Name usage match service triage. | ||
* This class is responsible for routing requests to the appropriate name usage matching service. | ||
*/ | ||
@Service | ||
public class NameUsageMatchServiceTriage { | ||
|
||
Map<String, NameUsageMatchingService> serviceByPrefix = new LinkedHashMap<>(); | ||
Map<String, NameUsageMatchingService> serviceByChecklistKey = new LinkedHashMap<>(); | ||
|
||
public NameUsageMatchServiceTriage(NameUsageMatchServiceConfiguration nameUsageMatchServiceConfiguration) { | ||
|
||
for (NameUsageMatchServiceConfiguration.NameUsageMatchServiceConfig config : nameUsageMatchServiceConfiguration.getServices()) { | ||
NameUsageMatchingService service = new ClientBuilder() | ||
.withUrl(config.getWs().getApi().getWsUrl()) | ||
.withObjectMapper(JacksonJsonObjectMapperProvider.getObjectMapperWithBuilderSupport()) | ||
.withFormEncoder() | ||
.build(NameUsageMatchingService.class); | ||
serviceByPrefix.put(config.getPrefix(), service); | ||
serviceByChecklistKey.put(config.getDatasetKey(), service); | ||
} | ||
} | ||
|
||
/** | ||
* Match a name usage against a service identified by its prefix. | ||
* @param checklistKey the checklist key | ||
* @param matchRequest the match request | ||
* @return the match response | ||
* @throws IllegalArgumentException if no service is found for the checklistKey | ||
*/ | ||
public NameUsageMatchResponse match(String checklistKey, NameUsageMatchRequest matchRequest) { | ||
if (checklistKey == null) { | ||
return match(matchRequest); | ||
} | ||
return Optional.ofNullable(serviceByChecklistKey.get(checklistKey)) | ||
.map(service -> service.match(matchRequest)) | ||
.orElseThrow(() -> new IllegalArgumentException("No service for checklist key " + checklistKey)); | ||
} | ||
|
||
/** | ||
* Match a name usage against the default service | ||
* @param matchRequest the match request | ||
* @return the match response | ||
* @throws IllegalArgumentException if no service is found for the checklistKey | ||
*/ | ||
NameUsageMatchResponse match(NameUsageMatchRequest matchRequest) { | ||
Optional<NameUsageMatchingService> n = serviceByChecklistKey.values().stream().findFirst(); | ||
if (n.isPresent()){ | ||
return n.get().match(matchRequest); | ||
} else { | ||
throw new IllegalArgumentException("No configured service"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters