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

IQSS/9544 - Migrate API fixes #9545

Merged
merged 11 commits into from
Jul 7, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,20 @@ public Map<String, String> getMetadataForTargetURL(DvObject dvObject) {
}

@Override
public boolean alreadyExists(DvObject dvo) throws Exception {
public boolean alreadyRegistered(DvObject dvo) throws Exception {
if(dvo==null) {
logger.severe("Null DvObject sent to alreadyExists().");
logger.severe("Null DvObject sent to alreadyRegistered().");
return false;
}
GlobalId globalId = dvo.getGlobalId();
if(globalId == null) {
return false;
}
return alreadyExists(globalId);
return alreadyRegistered(globalId, false);
}

public abstract boolean alreadyRegistered(GlobalId globalId, boolean noProviderDefault) throws Exception;

/*
* ToDo: the DvObject being sent in provides partial support for the case where
* it has a different authority/protocol than what is configured (i.e. a legacy
Expand Down Expand Up @@ -188,7 +190,7 @@ public boolean isGlobalIdUnique(GlobalId globalId) {

// not in local DB, look in the persistent identifier service
try {
return ! alreadyExists(globalId);
return ! alreadyRegistered(globalId, false);
} catch (Exception e){
//we can live with failure - means identifier not found remotely
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,22 @@ public boolean registerWhenPublished() {


@Override
public boolean alreadyExists(GlobalId pid) {
logger.log(Level.FINE,"alreadyExists");
public boolean alreadyRegistered(GlobalId pid, boolean noProviderDefault) {
logger.log(Level.FINE,"alreadyRegistered");
if(pid==null || pid.asString().isEmpty()) {
logger.fine("No identifier sent.");
return false;
}
boolean alreadyExists;
boolean alreadyRegistered;
String identifier = pid.asString();
try{
alreadyExists = doiDataCiteRegisterService.testDOIExists(identifier);
alreadyRegistered = doiDataCiteRegisterService.testDOIExists(identifier);
} catch (Exception e){
logger.log(Level.WARNING, "alreadyExists failed");
logger.log(Level.WARNING, "alreadyRegistered failed");
return false;
}
return alreadyExists;
return alreadyRegistered;
}


@Override
public String createIdentifier(DvObject dvObject) throws Exception {
Expand Down
17 changes: 4 additions & 13 deletions src/main/java/edu/harvard/iq/dataverse/DOIEZIdServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,10 @@ public boolean registerWhenPublished() {
}

@Override
public boolean alreadyExists(DvObject dvObject) throws Exception {
if(dvObject==null) {
logger.severe("Null DvObject sent to alreadyExists().");
return false;
}
return alreadyExists(dvObject.getGlobalId());
}

@Override
public boolean alreadyExists(GlobalId pid) throws Exception {
logger.log(Level.FINE,"alreadyExists");
public boolean alreadyRegistered(GlobalId pid, boolean noProviderDefault) throws Exception {
logger.log(Level.FINE,"alreadyRegistered");
try {
HashMap<String, String> result = ezidService.getMetadata(pid.asString());
HashMap<String, String> result = ezidService.getMetadata(pid.asString());
return result != null && !result.isEmpty();
// TODO just check for HTTP status code 200/404, sadly the status code is swept under the carpet
} catch (EZIDException e ){
Expand All @@ -87,7 +78,7 @@ public boolean alreadyExists(GlobalId pid) throws Exception {
if (e.getLocalizedMessage().contains("no such identifier")){
return false;
}
logger.log(Level.WARNING, "alreadyExists failed");
logger.log(Level.WARNING, "alreadyRegistered failed");
logger.log(Level.WARNING, "getIdentifier(dvObject) {0}", pid.asString());
logger.log(Level.WARNING, "String {0}", e.toString());
logger.log(Level.WARNING, "localized message {0}", e.getLocalizedMessage());
Expand Down
1 change: 0 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/DOIServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,4 @@ public boolean isConfigured() {
protected String getProviderKeyName() {
return null;
}

}
32 changes: 0 additions & 32 deletions src/main/java/edu/harvard/iq/dataverse/DataFileServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -1084,38 +1084,6 @@ public List<Long> selectFilesWithMissingOriginalSizes() {
* @param idServiceBean
* @return {@code true} iff the global identifier is unique.
*/
/* public boolean isGlobalIdUnique(String userIdentifier, DataFile datafile, GlobalIdServiceBean idServiceBean) {
String testProtocol = "";
String testAuthority = "";
if (datafile.getAuthority() != null){
testAuthority = datafile.getAuthority();
} else {
testAuthority = settingsService.getValueForKey(SettingsServiceBean.Key.Authority);
}
if (datafile.getProtocol() != null){
testProtocol = datafile.getProtocol();
} else {
testProtocol = settingsService.getValueForKey(SettingsServiceBean.Key.Protocol);
}

boolean u = em.createNamedQuery("DvObject.findByProtocolIdentifierAuthority")
.setParameter("protocol", testProtocol)
.setParameter("authority", testAuthority)
.setParameter("identifier",userIdentifier)
.getResultList().isEmpty();

try{
if (idServiceBean.alreadyExists(new GlobalId(testProtocol, testAuthority, userIdentifier))) {
u = false;
}
} catch (Exception e){
//we can live with failure - means identifier not found remotely
}


return u;
}
*/
public void finalizeFileDelete(Long dataFileId, String storageLocation) throws IOException {
// Verify that the DataFile no longer exists:
if (find(dataFileId) != null) {
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/edu/harvard/iq/dataverse/DvObjectServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,14 @@ private Long runFindIdByGlobalId(Query query, GlobalId gid, DvObject.DType dtype
}

public DvObject findByGlobalId(GlobalId globalId) {
return (DvObject) em.createNamedQuery("DvObject.findByProtocolIdentifierAuthority")
.setParameter("identifier", globalId.getIdentifier())
.setParameter("authority", globalId.getAuthority())
.setParameter("protocol", globalId.getProtocol()).getSingleResult();
try {
return (DvObject) em.createNamedQuery("DvObject.findByProtocolIdentifierAuthority")
.setParameter("identifier", globalId.getIdentifier())
.setParameter("authority", globalId.getAuthority()).setParameter("protocol", globalId.getProtocol())
.getSingleResult();
} catch (NoResultException nre) {
return null;
}
}

public boolean isGlobalIdLocallyUnique(GlobalId globalId) {
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/edu/harvard/iq/dataverse/GlobalIdServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,25 @@ public interface GlobalIdServiceBean {

static final Logger logger = Logger.getLogger(GlobalIdServiceBean.class.getCanonicalName());

boolean alreadyExists(DvObject dvo) throws Exception;
boolean alreadyRegistered(DvObject dvo) throws Exception;

/**
* This call reports whether a PID is registered with the external Provider
* service. For providers like DOIs/Handles with an external service, this call
* should accurately report whether the PID has been registered in the service.
* For providers with no external service, the call should return true if the
* PID is defined locally. If it isn't, these no-service providers need to know
* whether use case of the caller requires that the returned value should
* default to true or false - via the noProviderDefault parameter.
*
* @param globalId
* @param noProviderDefault - when there is no external service, and no local
* use of the PID, this should be returned
* @return whether the PID should be considered registered or not.
* @throws Exception
*/
boolean alreadyRegistered(GlobalId globalId, boolean noProviderDefault) throws Exception;

boolean alreadyExists(GlobalId globalId) throws Exception;

boolean registerWhenPublished();
boolean canManagePID();
boolean isConfigured();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,13 @@ private String getAuthenticationHandle(String handlePrefix) {
}

@Override
public boolean alreadyExists(DvObject dvObject) throws Exception {
public boolean alreadyRegistered(DvObject dvObject) throws Exception {
String handle = getDvObjectHandle(dvObject);
return isHandleRegistered(handle);
}

@Override
public boolean alreadyExists(GlobalId pid) throws Exception {
public boolean alreadyRegistered(GlobalId pid, boolean noProviderDefault) throws Exception {
String handle = pid.getAuthority() + "/" + pid.getIdentifier();
return isHandleRegistered(handle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,16 @@ protected void registerExternalIdentifier(Dataset theDataset, CommandContext ctx
if (!theDataset.isIdentifierRegistered()) {
GlobalIdServiceBean globalIdServiceBean = GlobalIdServiceBean.getBean(theDataset.getProtocol(), ctxt);
if ( globalIdServiceBean != null ) {
if (globalIdServiceBean instanceof FakePidProviderServiceBean) {
retry=false; //No reason to allow a retry with the FakeProvider, so set false for efficiency
}
try {
if (globalIdServiceBean.alreadyExists(theDataset)) {
if (globalIdServiceBean.alreadyRegistered(theDataset)) {
int attempts = 0;
if(retry) {
do {
theDataset.setIdentifier(globalIdServiceBean.generateDatasetIdentifier(theDataset));
logger.log(Level.INFO, "Attempting to register external identifier for dataset {0} (trying: {1}).",
new Object[]{theDataset.getId(), theDataset.getIdentifier()});
attempts++;
} while (globalIdServiceBean.alreadyExists(theDataset) && attempts <= FOOLPROOF_RETRIAL_ATTEMPTS_LIMIT);
} while (globalIdServiceBean.alreadyRegistered(theDataset) && attempts <= FOOLPROOF_RETRIAL_ATTEMPTS_LIMIT);
}
if(!retry) {
logger.warning("Reserving PID for: " + getDataset().getId() + " during publication failed.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public FileVisitResult postVisitDirectory(final Path dir, final IOException e)
}
GlobalIdServiceBean idServiceBean = GlobalIdServiceBean.getBean(ctxt);
try {
if (idServiceBean.alreadyExists(doomed)) {
if (idServiceBean.alreadyRegistered(doomed)) {
idServiceBean.deleteIdentifier(doomed);
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected void executeImpl(CommandContext ctxt) throws CommandException {
if (!doomed.isHarvested()) {
GlobalIdServiceBean idServiceBean = GlobalIdServiceBean.getBean(ctxt);
try {
if (idServiceBean.alreadyExists(doomed)) {
if (idServiceBean.alreadyRegistered(doomed)) {
idServiceBean.deleteIdentifier(doomed);
for (DataFile df : doomed.getFiles()) {
idServiceBean.deleteIdentifier(df);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,10 @@ protected void additionalParameterTests(CommandContext ctxt) throws CommandExcep
* that exist (and accessible in the PID provider account configured in
* Dataverse) but aren't findable to be used. That could be the case if, for
* example, someone was importing a draft dataset from elsewhere.
*
* Also note that just replacing the call above with the alreadyExists() call
* here would break import cases where a DOI is public but not managable with
* the currently configured PID provider credentials. If this is not a valid use
* case, the GET above could be removed.
*/
GlobalIdServiceBean globalIdServiceBean = GlobalIdServiceBean.getBean(ds.getProtocol(), ctxt);
if (globalIdServiceBean != null) {
if (globalIdServiceBean.alreadyExists(ds)) {
if (globalIdServiceBean.alreadyRegistered(ds.getGlobalId(), true)) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected void executeImpl(CommandContext ctxt) throws CommandException {
target.setAuthority(authority);
}
}
if (idServiceBean.alreadyExists(target)) {
if (idServiceBean.alreadyRegistered(target)) {
return;
}
String doiRetString = idServiceBean.createIdentifier(target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ public class FakePidProviderServiceBean extends DOIServiceBean {
//Only need to check locally
public boolean isGlobalIdUnique(GlobalId globalId) {
try {
return ! alreadyExists(globalId);
return ! alreadyRegistered(globalId, false);
} catch (Exception e){
//we can live with failure - means identifier not found remotely
}
return true;
}

@Override
public boolean alreadyExists(GlobalId globalId) throws Exception {
return ! dvObjectService.isGlobalIdLocallyUnique(globalId);
public boolean alreadyRegistered(GlobalId globalId, boolean noProviderDefault) {
boolean existsLocally = !dvObjectService.isGlobalIdLocallyUnique(globalId);
return existsLocally ? existsLocally : noProviderDefault;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ public String getSeparator() {
}

@Override
public boolean alreadyExists(GlobalId globalId) throws Exception {
return ! dvObjectService.isGlobalIdLocallyUnique(globalId);
public boolean alreadyRegistered(GlobalId globalId, boolean noProviderDefault) {
// Perma doesn't manage registration, so we assume all local PIDs can be treated
// as registered
boolean existsLocally = !dvObjectService.isGlobalIdLocallyUnique(globalId);
return existsLocally ? existsLocally : noProviderDefault;
}

@Override
public boolean registerWhenPublished() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public boolean registerWhenPublished() {
}

@Override
public boolean alreadyExists(GlobalId pid) {
return false;
public boolean alreadyRegistered(GlobalId pid, boolean noProviderDefault) {
throw new NotImplementedException();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public boolean registerWhenPublished() {
}

@Override
public boolean alreadyExists(GlobalId pid) throws Exception {
public boolean alreadyRegistered(GlobalId pid, boolean noProviderDefault) throws Exception {
throw new NotImplementedException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ else if (key.equals("https://dataverse.org/schema/core#fileRequestAccess")) {
}
}
dsv.setTermsOfUseAndAccess(terms);
terms.setDatasetVersion(dsv);
dsv.setDatasetFields(dsfl);

return dsv;
Expand Down