Skip to content

Commit

Permalink
add spatialCoverage #4371
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Oct 11, 2018
1 parent 0d260fa commit a84f1d1
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 9 deletions.
63 changes: 54 additions & 9 deletions src/main/java/edu/harvard/iq/dataverse/DatasetVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,8 @@ public List<String> getLanguages() {
}
return languages;
}


// TODO: consider calling the newer getSpatialCoverages method below with the commaSeparated boolean set to true.
public List<String> getSpatialCoverages() {
List<String> retList = new ArrayList<>();
for (DatasetField dsf : this.getDatasetFields()) {
Expand Down Expand Up @@ -945,6 +946,47 @@ public List<String> getSpatialCoverages() {
}
return retList;
}

public List<String> getSpatialCoverages(boolean commaSeparated) {
List<String> retList = new ArrayList<>();
for (DatasetField dsf : this.getDatasetFields()) {
if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.geographicCoverage)) {
for (DatasetFieldCompoundValue geoValue : dsf.getDatasetFieldCompoundValues()) {
List<String> coverage = new ArrayList<>();
for (DatasetField subField : geoValue.getChildDatasetFields()) {
if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.country)) {
if (!subField.isEmptyForDisplay()) {
coverage.add(subField.getValue());
}
}
if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.state)) {
if (!subField.isEmptyForDisplay()) {
coverage.add(subField.getValue());
}
}
if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.city)) {
if (!subField.isEmptyForDisplay()) {
coverage.add(subField.getValue());
}
}
if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.otherGeographicCoverage)) {
if (!subField.isEmptyForDisplay()) {
coverage.add(subField.getValue());
}
}
}
if (!coverage.isEmpty()) {
if (commaSeparated) {
retList.add(String.join(",", coverage));
} else {
retList.addAll(coverage);
}
}
}
}
}
return retList;
}

/**
* @return List of Strings containing the version's Keywords
Expand Down Expand Up @@ -1520,14 +1562,7 @@ public String getJsonLd() {
}
job.add("temporalCoverage", temporalCoverage);
}

/**
* spatialCoverage (if available)
* TODO
* (punted, for now - see #2243)
*
*/


job.add("schemaVersion", "https://schema.org/version/3.3");

TermsOfUseAndAccess terms = this.getTermsOfUseAndAccess();
Expand Down Expand Up @@ -1571,6 +1606,16 @@ public String getJsonLd() {
job.add("funder", funderArray);
}

boolean commaSeparated = false;
List<String> spatialCoverages = getSpatialCoverages(commaSeparated);
if (!spatialCoverages.isEmpty()) {
JsonArrayBuilder spatialArray = Json.createArrayBuilder();
for (String spatialCoverage : spatialCoverages) {
spatialArray.add(spatialCoverage);
}
job.add("spatialCoverage", spatialArray);
}

jsonLd = job.build().toString();
return jsonLd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,26 @@ public void setUp() {
}
contributorType.setChildDatasetFieldTypes(contributorChildTypes);

DatasetFieldType geographicCoverageType = datasetFieldTypeSvc.add(new DatasetFieldType("geographicCoverage", DatasetFieldType.FieldType.TEXT, true));
Set<DatasetFieldType> geographicCoverageChildTypes = new HashSet<>();
DatasetFieldType countries = datasetFieldTypeSvc.add(new DatasetFieldType("country", DatasetFieldType.FieldType.TEXT, false));
countries.setAllowControlledVocabulary(true);
countries.setControlledVocabularyValues(Arrays.asList(
// Why aren't these enforced?
new ControlledVocabularyValue(1l, "Afghanistan", countries),
new ControlledVocabularyValue(2l, "Albania", countries)
// And many more countries.
));
geographicCoverageChildTypes.add(datasetFieldTypeSvc.add(countries));
geographicCoverageChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("state", DatasetFieldType.FieldType.TEXT, false)));
geographicCoverageChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("city", DatasetFieldType.FieldType.TEXT, false)));
geographicCoverageChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("otherGeographicCoverage", DatasetFieldType.FieldType.TEXT, false)));
geographicCoverageChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("geographicUnit", DatasetFieldType.FieldType.TEXT, false)));
for (DatasetFieldType t : geographicCoverageChildTypes) {
t.setParentDatasetFieldType(geographicCoverageType);
}
geographicCoverageType.setChildDatasetFieldTypes(geographicCoverageChildTypes);

}

@After
Expand Down Expand Up @@ -222,6 +242,14 @@ public void testExportDataset() throws Exception {
assertEquals("Organization", json2.getJsonArray("funder").getJsonObject(0).getString("@type"));
assertEquals("National Science Foundation", json2.getJsonArray("funder").getJsonObject(0).getString("name"));
assertEquals(1, json2.getJsonArray("funder").size());
assertEquals("Ohio", json2.getJsonArray("spatialCoverage").getString(0));
assertEquals("Columbus", json2.getJsonArray("spatialCoverage").getString(1));
assertEquals("GeographicCoverageOther1", json2.getJsonArray("spatialCoverage").getString(2));
assertEquals("GeographicCoverageStateProvince2", json2.getJsonArray("spatialCoverage").getString(3));
assertEquals("GeographicCoverageCity2", json2.getJsonArray("spatialCoverage").getString(4));
assertEquals("GeographicCoverageOther2", json2.getJsonArray("spatialCoverage").getString(5));
assertEquals("Afghanistan", json2.getJsonArray("spatialCoverage").getString(6));
assertEquals(7, json2.getJsonArray("spatialCoverage").size());
}

/**
Expand Down
58 changes: 58 additions & 0 deletions src/test/resources/json/dataset-finch2.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,64 @@
}
],
"displayName": "Citation Metadata"
},
"geospatial": {
"displayName": "Geospatial Metadata",
"fields": [
{
"typeName": "geographicCoverage",
"multiple": true,
"typeClass": "compound",
"value": [
{
"state": {
"typeName": "state",
"multiple": false,
"typeClass": "primitive",
"value": "Ohio"
},
"city": {
"typeName": "city",
"multiple": false,
"typeClass": "primitive",
"value": "Columbus"
},
"otherGeographicCoverage": {
"typeName": "otherGeographicCoverage",
"multiple": false,
"typeClass": "primitive",
"value": "GeographicCoverageOther1"
}
},
{
"state": {
"typeName": "state",
"multiple": false,
"typeClass": "primitive",
"value": "GeographicCoverageStateProvince2"
},
"city": {
"typeName": "city",
"multiple": false,
"typeClass": "primitive",
"value": "GeographicCoverageCity2"
},
"otherGeographicCoverage": {
"typeName": "otherGeographicCoverage",
"multiple": false,
"typeClass": "primitive",
"value": "GeographicCoverageOther2"
},
"country": {
"typeName": "country",
"multiple": false,
"typeClass": "controlledVocabulary",
"value": "Afghanistan"
}
}
]
}
]
}
}
}
Expand Down

0 comments on commit a84f1d1

Please sign in to comment.