Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.license;

import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.opencsv.CSVReader;
import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Loads the entire CSV into this bean that way we don't have to keep calling it and can utilize helper functions
*/
public class CCLicenseCSVRepository implements InitializingBean {

@Autowired
private ConfigurationService configurationService;

private List<CCLicenseCSVRow> rows;

private Map<String, List<CCLicenseCSVRow>> byVersion = new HashMap<>();
private Map<String, CCLicenseCSVRow> byIdentifier = new HashMap<>();
private Map<String, CCLicenseCSVRow> byUri = new HashMap<>();
private List<CCLicenseCSVRow> entryPoints = new ArrayList<>();
private Map<String, String> licenseUriIndex = new HashMap<>();

@Override
public void afterPropertiesSet() throws Exception {
loadCsv();
buildIndexes();
}

/**
* Loads the Creative Commons license CSV file from the configured path and
* converts each row into a {@link CCLicenseCSVRow}.
*
* <p>The CSV header is used to dynamically map column names to indices.
* All parsed rows are stored in memory for later indexing and lookup.</p>
*
* @throws Exception if the CSV file cannot be read or parsed
*/
private void loadCsv() throws Exception {
String csvPath = configurationService.getProperty("dspace.cc-license.csv");

List<CCLicenseCSVRow> result = new ArrayList<>();

try (CSVReader reader = new CSVReader(new FileReader(csvPath, StandardCharsets.UTF_8))) {
String[] header = reader.readNext();
Map<String, Integer> col = mapHeader(header);

int i = 1;
String[] row;
while ((row = reader.readNext()) != null) {
CCLicenseCSVRow r = new CCLicenseCSVRow();
r.setId("license" + i);
r.setCategory(row[col.get("CATEGORY")]);
r.setVersion(row[col.get("VERSION")]);
r.setUnit(row[col.get("UNIT")]);
r.setJurisdiction(row[col.get("JURISDICTION")]);
r.setUrl(row[col.get("CANONICAL_URL")]);
r.setIdentifier(row[col.get("IDENTIFIER")]);
r.setTitle(row[col.get("TITLE")]);
r.setEntryPoint(row[col.get("ENTRY_POINT")]);
i++;
result.add(r);
}
}

this.rows = List.copyOf(result);
}

/**
* Builds in-memory lookup indexes from the loaded CSV rows.
*
* <p>This method populates:
* <ul>
* <li>{@code byVersion} for grouping licenses by version</li>
* <li>{@code byIdentifier} for direct lookup of special licenses</li>
* <li>{@code entryPoints} for UI entry-point licenses</li>
* <li>{@code licenseUriIndex} for fast version/unit/jurisdiction lookup</li>
* </ul>
* </p>
*/
private void buildIndexes() {
for (CCLicenseCSVRow row : rows) {

// version grouping (for CC)
byVersion.computeIfAbsent(row.getVersion(), k -> new ArrayList<>()).add(row);

// identifier lookup (for PD)
byIdentifier.put(row.getId(), row);

byUri.put(normalizeUri(row.getUrl()), row);

// entry points
if (row.getEntryPoint() != null && !row.getEntryPoint().isBlank()) {
entryPoints.add(row);
}

String key = buildKey(row.getVersion(), row.getUnit(), row.getJurisdiction());
licenseUriIndex.put(key, row.getUrl());
}
}

/**
* Builds a composite lookup key for license URI indexing.
*
* <p>The key format is:</p>
* <pre>
* version|unit|jurisdiction
* </pre>
*
* <p>Null values are normalized to empty strings and all parts are trimmed.</p>
*
* @param version the Creative Commons version (e.g., "4.0", "3.0")
* @param unit the license unit (e.g., "by-nc-sa")
* @param jurisdiction the jurisdiction code (e.g., "us", "de"), may be empty
* @return a normalized composite key used for internal map lookups
*/
private String buildKey(String version, String unit, String jurisdiction) {
return (version == null ? "" : version.trim()) + "|" +
(unit == null ? "" : unit.trim()) + "|" +
(jurisdiction == null ? "" : jurisdiction.trim());
}

public List<CCLicenseCSVRow> getEntryPoints() {
return entryPoints;
}

public CCLicenseCSVRow getByIdentifier(String id) {
return byIdentifier.get(id);
}

public String findUri(String version, String unit, String jurisdiction) {
String key = buildKey(version, unit, jurisdiction);
return licenseUriIndex.get(key);
}

public List<String> getJurisdictionsByVersion(String version) {
return rows.stream()
.filter(r -> version.equals(r.getVersion()))
.map(CCLicenseCSVRow::getJurisdiction)
.filter(j -> j != null && !j.isBlank())
.distinct()
.collect(Collectors.toList());
}

public CCLicenseCSVRow getByURI(String uri) {
return byUri.get(normalizeUri(uri));
}

/**
* Normalises a license URI for lookup. The retired CC API issued http URIs, which existing items still
* carry, and URIs entered by hand often lack the trailing slash; the CSV holds the canonical https form.
*
* @param uri the license URI
* @return the URI with an https scheme and a trailing slash, or null for a null URI
*/
private static String normalizeUri(String uri) {
if (uri == null) {
return null;
}
String normalized = uri.trim().replaceFirst("(?i)^http://", "https://");
return normalized.endsWith("/") ? normalized : normalized + "/";
}

/**
* Maps CSV header column names to their corresponding index positions.
*
* <p>This allows the CSV parser to reference columns by name rather than
* relying on fixed ordering.</p>
*
* <p>Header values are normalized by removing surrounding quotation marks.</p>
*
* @param header the first row of the CSV file containing column names
* @return a map of column name to zero-based column index
*/
private Map<String, Integer> mapHeader(String[] header) {
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < header.length; i++) {
map.put(header[i].replace("\"", ""), i);
}
return map;
}
}
101 changes: 101 additions & 0 deletions dspace-api/src/main/java/org/dspace/license/CCLicenseCSVRow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.license;

/**
* Represents a single row in the Creative Commons license CSV configuration.
*
* <p>This class is used as an in-memory representation of the CSV schema and
* acts as the source of truth for CC license metadata loaded at startup.</p>
*
* <p>Each instance maps directly to one row in the CSV file and is used to
* build lookup indexes for license resolution (version, unit, jurisdiction).</p>
*/
public class CCLicenseCSVRow {
private String id;
private String category;
private String version;
private String unit;
private String jurisdiction;
private String url;
private String identifier;
private String title;
private String entryPoint;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getUnit() {
return unit;
}

public void setUnit(String unit) {
this.unit = unit;
}

public String getJurisdiction() {
return jurisdiction;
}

public void setJurisdiction(String jurisdiction) {
this.jurisdiction = jurisdiction;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getIdentifier() {
return identifier;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getEntryPoint() {
return entryPoint;
}

public void setEntryPoint(String entryPoint) {
this.entryPoint = entryPoint;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@
*/
package org.dspace.license;

import java.io.IOException;
import java.util.Map;

import org.jdom2.Document;

/**
* Service interface class for the Creative commons license connector service.
* The implementation of this class is responsible for all the calls to the CC license API and parsing the response
* The implementation of this class is responsible for providing the CC licenses from the configured CSV
* The service is autowired by spring
*/
public interface CCLicenseConnectorService {

/**
* Retrieves the CC Licenses for the provided language from the CC License API
* Retrieves the CC Licenses for the provided language from the configured CSV
*
* @param language - the language to retrieve the licenses for
* @return a map of licenses with the id and the license for the provided language
Expand All @@ -29,7 +26,7 @@ public interface CCLicenseConnectorService {

/**
* Retrieve the CC License URI based on the provided license id, language and answers to the field questions from
* the CC License API
* the configured CSV
*
* @param licenseId - the ID of the license
* @param language - the language for which to retrieve the full answerMap
Expand All @@ -41,20 +38,11 @@ public String retrieveRightsByQuestion(String licenseId,
Map<String, String> answerMap);

/**
* Retrieve the license RDF document based on the license URI
*
* @param licenseURI - The license URI for which to retrieve the license RDF document
* @return the license RDF document
* @throws IOException
*/
public Document retrieveLicenseRDFDoc(String licenseURI) throws IOException;

/**
* Retrieve the license Name from the license document
* Retrieves the CC License name of the given licenseURI from the CCLicenseCSVRow
*
* @param doc - The license document from which to retrieve the license name
* @return the license name
* @param licenseURI - The license URI
* @return - The license name
*/
public String retrieveLicenseName(final Document doc);
public String retrieveLicenseName(String licenseURI);

}
Loading
Loading