Skip to content
Snippets Groups Projects
Commit 07ecbe02 authored by Thorge Petersen's avatar Thorge Petersen
Browse files

Merge branch '24-robusteres-hinzufugen-der-lizenzinformationen' into 'main'

Resolve "Robusteres Hinzufügen der Lizenzinformationen"

Closes #24

See merge request !21
parents 864fb639 26f81f51
Branches
No related tags found
1 merge request!21Resolve "Robusteres Hinzufügen der Lizenzinformationen"
Pipeline #616 passed
......@@ -25,8 +25,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
......@@ -47,6 +49,7 @@ public class MDMetadata2Dataset {
boolean ignoreInvalidMapping = true;
private CswInterface cswInterface;
private BridgeSettings settings;
private final Map<String, JSONObject> url2license = new HashMap<>();
public MDMetadata2Dataset(Model model, CswInterface cswInterface, BridgeSettings bridgeSettings) {
this(model);
......@@ -77,6 +80,12 @@ public class MDMetadata2Dataset {
final String value = entry.getString(key);
keywordMapping.put(key, value);
}
JSONArray licenses = new JSONArray(new JSONTokener(Objects.requireNonNull(getClass().getResourceAsStream("/licenses.json"))));
for (Object l : licenses) {
JSONObject license = (JSONObject) l;
url2license.put(license.getString("uri"), license);
}
}
static String getTextOrNull(Node node) {
......@@ -533,18 +542,43 @@ public class MDMetadata2Dataset {
}
private void addLicenseInformation(Resource resource, List<Node> otherLegalConstraints) {
final JSONObject licenceInformation = findLicenseInformation(otherLegalConstraints);
if (licenceInformation != null) {
resource.addLiteral(DCATAPde.licenseAttributionByText, licenceInformation.getString("quelle"));
if (licenceInformation.has("url") && licenceInformation.getString("url").startsWith("http://dcat-ap.de/def/licenses/")) {
// Some publishers specify the licence as the URL and not the id.
resource.addProperty(DCTerms.license, model.createResource(licenceInformation.getString("url")));
final JSONObject licenseInformation = findLicenseInformation(otherLegalConstraints);
if (licenseInformation != null) {
resource.addLiteral(DCATAPde.licenseAttributionByText, licenseInformation.getString("quelle"));
if (licenseInformation.has("url")) {
String licenseURL = licenseInformation.getString("url");
if (licenseURL.startsWith("http://dcat-ap.de/def/licenses/")) {
resource.addProperty(DCTerms.license, model.createResource(licenseURL));
} else {
final JSONObject license = url2license.get(licenseURL);
if (license != null && license.has("uri")) {
resource.addProperty(DCTerms.license, model.createResource(license.getString("uri")));
} else {
log.info("Unknown license: {}", licenseURL);
handleUnknownLicense(resource, licenseInformation.getString("id"));
}
}
} else {
resource.addProperty(DCTerms.license, model.createResource("http://dcat-ap.de/def/licenses/" + licenceInformation.getString("id")));
handleUnknownLicense(resource, licenseInformation.getString("id"));
}
}
}
private void handleUnknownLicense(Resource resource, String licenseId) {
try {
licenseId = URLEncoder.encode(licenseId, "UTF-8")
.replaceAll("%2F", "/"); // Replace the encoded slash with the original slash
resource.addProperty(DCTerms.license, model.createResource("http://dcat-ap.de/def/licenses/" + licenseId));
log.info("Falling back to: {}", licenseId);
} catch (UnsupportedEncodingException e) {
log.info("Unable to encode licenseId: {}", e);
}
}
private Resource guessFormat(String url) {
final String lowerCaseURL = url.toLowerCase();
if (lowerCaseURL.contains("service=wms")) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment