Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match species lists webservices #44

Merged
merged 4 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# sds [![Build Status](https://travis-ci.com/AtlasOfLivingAustralia/sds.svg?branch#master)](https://travis-ci.com/AtlasOfLivingAustralia/sds)
# sds [![Build Status](https://travis-ci.com/AtlasOfLivingAustralia/sds.svg?branch=master)](https://app.travis-ci.com/github/AtlasOfLivingAustralia/sds)


The sensitive data service manages sensitivity concerns in the conservation and biosecurity areas.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>au.org.ala</groupId>
<artifactId>sds</artifactId>
<version>1.4.7-SNAPSHOT</version>
<version>1.4.7</version>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/AtlasOfLivingAustralia/sds/issues</url>
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/au/org/ala/sds/model/SDSSpeciesListItemDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package au.org.ala.sds.model;

import com.google.common.collect.Lists;
import org.codehaus.jackson.annotate.JsonIgnore;

import java.util.List;
import java.util.Map;
Expand All @@ -31,6 +32,7 @@ public class SDSSpeciesListItemDTO {
private String dataResourceUid;
private List<Map<String, String>> kvpValues;
public static final List<String> commonNameLabels= Lists.newArrayList("commonname","vernacularname");
private String commonName;

public String getGuid() {
return guid;
Expand All @@ -40,6 +42,19 @@ public void setGuid(String guid) {
this.guid = guid;
}

public String getCommonName() {
return commonName;
}

public void setCommonName(String commonName) {
this.commonName = commonName;
}

// Added for the change data structure returned by lists
public void setLsid(String lsid) {
this.guid = lsid;
}

public String getName() {
return name;
}
Expand Down Expand Up @@ -70,28 +85,35 @@ public void setFamily(String family) {

public void setKvpValues(List<Map<String, String>> kvpValues) {
this.kvpValues = kvpValues;

// family has moved to kvpValues
for(Map<String, String> pair: kvpValues){
if("family".equalsIgnoreCase(pair.get("key"))){
setFamily(pair.get("value"));
}
}
}
public String getKVPValue(String key){
for(Map<String, String> pair: kvpValues){
if(key.equals(pair.get("key"))){
if(key.equalsIgnoreCase(pair.get("key").replaceAll("[^a-zA-Z]", ""))){
return pair.get("value");
}
}
return null;
}
public String getKVPValue(List<String> keys){
public String getKVPValueCommonName(){
for(Map<String, String> pair: kvpValues){
if(keys.contains(pair.get("key").toLowerCase().replaceAll(" ", ""))){
if(commonNameLabels.contains(pair.get("key").toLowerCase().replaceAll(" ", ""))){
return pair.get("value");
}
}
return null;
return commonName;
}

@Override
public String toString() {
return "SDSSpeciesListItemDTO{" +
"guid='" + guid + '\'' +
"lsid='" + guid + '\'' +
", name='" + name + '\'' +
", dataResourceUid='" + dataResourceUid + '\'' +
", kvpValues=" + kvpValues +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static boolean generateFromWebservices(OutputStream out, Date lastGenerat
}

//Step 1: get all of the items that have a guid
List<SDSSpeciesListItemDTO> guidItems = SpeciesListUtil.getSDSListItems(true);
List<SDSSpeciesListItemDTO> guidItems = SpeciesListUtil.getSDSListItems(sdsLists.keySet(), true);
if(sdsLists.isEmpty() || guidItems == null || guidItems.isEmpty()) {
return false;
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public static boolean generateFromWebservices(OutputStream out, Date lastGenerat
}
sensitiveSpecies.setAttribute("guid", item.getGuid());
sensitiveSpecies.setAttribute("rank", rank);
String commonName = item.getKVPValue(item.commonNameLabels);
String commonName = item.getKVPValueCommonName();
sensitiveSpecies.setAttribute("commonName", commonName != null ? commonName : "");
doc.getRootElement().addContent(sensitiveSpecies);
currentGuid = item.getGuid();
Expand All @@ -121,7 +121,7 @@ public static boolean generateFromWebservices(OutputStream out, Date lastGenerat
}

//Step 2: get all the items that could NOT be matched to the current species list
List<SDSSpeciesListItemDTO> unmatchedItems = SpeciesListUtil.getSDSListItems(false);
List<SDSSpeciesListItemDTO> unmatchedItems = SpeciesListUtil.getSDSListItems(sdsLists.keySet(), false);
String currentName = "";
sensitiveSpecies = null;
instances = null;
Expand All @@ -143,7 +143,7 @@ public static boolean generateFromWebservices(OutputStream out, Date lastGenerat
logger.error("Unable to get rank for " + item.getName(), e);
}
sensitiveSpecies.setAttribute("rank", rank);
String commonName = item.getKVPValue(item.commonNameLabels);
String commonName = item.getKVPValueCommonName();
sensitiveSpecies.setAttribute("commonName", commonName != null ? commonName : "");
//sensitiveSpecies.setAttribute("commonName", st.getCommonName() != null ? st.getCommonName() : "");
doc.getRootElement().addContent(sensitiveSpecies);
Expand Down
65 changes: 47 additions & 18 deletions src/main/java/au/org/ala/sds/util/SpeciesListUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import au.org.ala.sds.model.SDSSpeciesListDTO;
import au.org.ala.sds.model.SDSSpeciesListItemDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
Expand All @@ -25,8 +26,7 @@
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* Provides utility methods required to interface with the species list tool. Allows the SDS
Expand Down Expand Up @@ -83,21 +83,50 @@ public static Map<String,SDSSpeciesListDTO> getSDSLists(){
* Retrieves the "isSDS" species list items ordering them by guid/scientific name
* @return
*/
public static List<SDSSpeciesListItemDTO> getSDSListItems(boolean hasMatch){
public static List<SDSSpeciesListItemDTO> getSDSListItems(Collection<String> dataResourceUids, boolean hasMatch){
try{
String suffix = hasMatch ? "&guid=isNotNull:guid&sort=guid" : "&guid=isNull:guid&sort=rawScientificName";
URL url = new URL(Configuration.getInstance().getListToolUrl() + "/ws/speciesListItems?isSDS=eq:true" + suffix);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
URLConnection connection = url.openConnection();
logger.debug("Looking up location using " + url);
InputStream inStream = connection.getInputStream();
String suffix = hasMatch ? "&sort=guid" : "&sort=rawScientificName";
List<SDSSpeciesListItemDTO> values = new ArrayList();

java.util.List<SDSSpeciesListItemDTO> values = mapper.readValue(
inStream,
new TypeReference<List<SDSSpeciesListItemDTO>>(){}
);
logger.debug(values);
String drUids = StringUtils.join(dataResourceUids, ",");
int offset = 0;
int max = 400;
boolean moreRecords = true;
while(moreRecords) {
URL url = new URL(Configuration.getInstance().getListToolUrl() + "/ws/speciesListItems?isSDS=eq:true" + suffix + "&druid=" + drUids + "&includeKVP=true&max=" + max + "&offset=" + offset);
offset += max;

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
URLConnection connection = url.openConnection();
logger.error("Looking up location using " + url);
InputStream inStream = connection.getInputStream();

java.util.List<SDSSpeciesListItemDTO> drValues = mapper.readValue(
inStream,
new TypeReference<List<SDSSpeciesListItemDTO>>() {
}
);

if (!hasMatch) {
// include only records without an LSID
for (SDSSpeciesListItemDTO item : drValues) {
if (item.getGuid() == null) {
values.add(item);
}
}
} else {
// include only records with an LSID
for (SDSSpeciesListItemDTO item : drValues) {
if (item.getGuid() != null) {
values.add(item);
}
}
}

moreRecords = drValues.size() == max;
}
logger.error(values);
return values;
} catch(Exception e){
logger.error("Unable to get the list items. ", e);
Expand All @@ -106,8 +135,8 @@ public static List<SDSSpeciesListItemDTO> getSDSListItems(boolean hasMatch){
}

public static void main(String[] args){
getSDSLists();
getSDSListItems(true);
getSDSListItems(false);
Map<String, SDSSpeciesListDTO> sdsLists = getSDSLists();
getSDSListItems(sdsLists.keySet(), true);
getSDSListItems(sdsLists.keySet(), false);
}
}