Skip to content

Commit

Permalink
feat(pid): add dataset version pid configuration code IQSS#4499
Browse files Browse the repository at this point in the history
This commit adds a new scope and setting to the JvmSettings,
enabling the configuration of different modes for Dataset Version PIDs.
These modes are depicted in VersionPidMode. A test ensures the
parsability.

In addition, VersionPidMode also contains a fine grained option
to change the conduct of Dataverse collections and their datasets
for these PIDs.
  • Loading branch information
poikilotherm committed Mar 22, 2023
1 parent 51ff682 commit 8c8cc23
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package edu.harvard.iq.dataverse.pidproviders;

import edu.harvard.iq.dataverse.Dataverse;

import java.util.Arrays;
import java.util.Objects;
import java.util.Set;

/**
* Enumlike class to bundle available options for PIDs of Dataset Versions.
* Must be a class to ensure case-insensitive configuration values (not possible/reliable with enum)
*/
public final class VersionPidMode {

/**
* Means feature is switched off, no version PIDs will be minted
*/
public static final VersionPidMode OFF = new VersionPidMode("OFF");

/**
* Means the feature is activated instance wide for all collections and datasets
* (No opt-out so far!)
*/
public static final VersionPidMode GLOBAL = new VersionPidMode("GLOBAL");

/**
* Means the feature must be activated per Dataverse Collection (opt-in)
*/
public static final VersionPidMode COLLECTION = new VersionPidMode("COLLECTION");

/**
* A collection of conducts for mode {@link #COLLECTION}, used in {@link edu.harvard.iq.dataverse.Dataverse}
* and {@link edu.harvard.iq.dataverse.DataverseServiceBean#wantsDatasetVersionPids(Dataverse)}:
* <ol>
* <li>Collection may inherit version pid behaviour from the parent collection(s),</li>
* <li>Collection may choose to be actively enabling it</li>
* <li>Collection may choose to opt out and skip the minting</li>
* </ol>
*/
public enum CollectionConduct {
INHERIT("inherit"),
ACTIVE("active"),
SKIP("skip");

private final String name;

CollectionConduct(String name) {
this.name = name;
}

@Override
public String toString() {
return this.name;
}

public static CollectionConduct findBy(String name) {
return Arrays.stream(CollectionConduct.values())
.filter(cs -> cs.name.equalsIgnoreCase(name))
.findFirst()
.orElse(null);
}
}


// Init as unmodifiable set
public static final Set<VersionPidMode> values;
static {
values = Set.of(OFF, GLOBAL, COLLECTION);
}

private final String mode;

// Hide the no-arg constructor - no one shall get fancy ideas of extension.
private VersionPidMode() {
this.mode = "";
}

// Hide the constructor - no one shall get fancy ideas of extension.
private VersionPidMode(String mode) {
this.mode = mode;
}

/**
* Used to enable auto-conversion for MicroProfile Config.
* Comparison is done case-insensitive to enable all variants of writing the config value.
*
* Note that a non-matching value will return null, which will trigger a {@link java.util.NoSuchElementException}
* for the conversion.
*
* @see <a href="https://download.eclipse.org/microprofile/microprofile-config-3.0/microprofile-config-spec-3.0.html#_automatic_converters">MicroProfile Config Spec for Autoconverts</a>
*
* @param mode The mode to lookup
* @return A matching constant or null if not matching.
*/
public static VersionPidMode of(String mode) {
return values.stream()
.filter(m -> m.mode.equalsIgnoreCase(mode))
.findAny()
.orElse(null);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof VersionPidMode)) return false;
VersionPidMode that = (VersionPidMode) o;
return mode.equalsIgnoreCase(that.mode);
}

@Override
public int hashCode() {
return Objects.hash(mode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public enum JvmSettings {
// Avoids adding flag entries twice.
FEATURE_FLAG(SCOPE_FLAGS),

// PID SETTINGS
SCOPE_PID(PREFIX, "pid"),

SCOPE_PID_VERSIONS(SCOPE_PID, "version"),
PID_VERSIONS_MODE(SCOPE_PID_VERSIONS, "mode"),

;

private static final String SCOPE_SEPARATOR = ".";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package edu.harvard.iq.dataverse.pidproviders;

import edu.harvard.iq.dataverse.settings.JvmSettings;
import edu.harvard.iq.dataverse.util.testing.JvmSetting;
import org.junit.jupiter.api.Test;

import java.util.NoSuchElementException;

import static org.junit.jupiter.api.Assertions.*;
class VersionPidModeTest {

@Test
@JvmSetting(key = JvmSettings.PID_VERSIONS_MODE, value = "collection")
void setToValidValue() {
assertEquals(VersionPidMode.COLLECTION, JvmSettings.PID_VERSIONS_MODE.lookup(VersionPidMode.class));
}

@Test
@JvmSetting(key = JvmSettings.PID_VERSIONS_MODE, value = "GloBal")
void setToOtherValidValue() {
assertEquals(VersionPidMode.GLOBAL, JvmSettings.PID_VERSIONS_MODE.lookup(VersionPidMode.class));
}

@Test
@JvmSetting(key = JvmSettings.PID_VERSIONS_MODE, value = "foobar")
void setToInvalidValue() {
assertThrows(NoSuchElementException.class, () -> JvmSettings.PID_VERSIONS_MODE.lookup(VersionPidMode.class));
}

}

0 comments on commit 8c8cc23

Please sign in to comment.