Skip to content
Open

PD-3931 #7500

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
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityExistsException;

import org.orcid.core.exception.ApplicationException;
import org.orcid.core.exception.OrcidDuplicatedElementException;
Expand Down Expand Up @@ -117,7 +118,14 @@ public ResearcherUrls updateResearcherUrls(String orcid, ResearcherUrls research

newResearcherUrl.setVisibility(updatedOrNew.getVisibility().name());
newResearcherUrl.setDisplayIndex(updatedOrNew.getDisplayIndex());
researcherUrlDao.persist(newResearcherUrl);
try {
researcherUrlDao.persistIfNotExists(newResearcherUrl);
} catch (EntityExistsException e) {
Map<String, String> params = new HashMap<String, String>();
params.put("type", "researcher-url");
params.put("value", updatedOrNew.getUrl().getValue());
throw new OrcidDuplicatedElementException(params);
}
}
}
}
Expand Down Expand Up @@ -191,7 +199,14 @@ public ResearcherUrl createResearcherUrl(String orcid, ResearcherUrl researcherU

setIncomingPrivacy(newEntity, profile);
DisplayIndexCalculatorHelper.setDisplayIndexOnNewEntity(newEntity, isApiRequest);
researcherUrlDao.persist(newEntity);
try {
newEntity = researcherUrlDao.persistIfNotExists(newEntity);
} catch (EntityExistsException e) {
Map<String, String> params = new HashMap<String, String>();
params.put("type", "researcher-url");
params.put("value", researcherUrl.getUrl().getValue());
throw new OrcidDuplicatedElementException(params);
}
return jpaJaxbResearcherUrlAdapter.toResearcherUrl(newEntity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityExistsException;

import org.orcid.core.exception.ApplicationException;
import org.orcid.core.exception.OrcidDuplicatedElementException;
Expand Down Expand Up @@ -110,14 +111,20 @@ public ResearcherUrls updateResearcherUrls(String orcid, ResearcherUrls research
// Add the new ones
ResearcherUrlEntity newResearcherUrl = jpaJaxbResearcherUrlAdapter.toResearcherUrlEntity(updatedOrNew);
Source activeSource = sourceManager.retrieveActiveSource();
ProfileEntity profile = new ProfileEntity(orcid);
newResearcherUrl.setOrcid(orcid);

SourceEntityUtils.populateSourceAwareEntityFromSource(activeSource, newResearcherUrl);

newResearcherUrl.setVisibility(updatedOrNew.getVisibility().name());
newResearcherUrl.setDisplayIndex(updatedOrNew.getDisplayIndex());
researcherUrlDao.persist(newResearcherUrl);
try {
researcherUrlDao.persistIfNotExists(newResearcherUrl);
} catch (EntityExistsException e) {
Map<String, String> params = new HashMap<String, String>();
params.put("type", "researcher-url");
params.put("value", updatedOrNew.getUrl().getValue());
throw new OrcidDuplicatedElementException(params);
}
}
}
}
Expand Down Expand Up @@ -183,7 +190,14 @@ public ResearcherUrl createResearcherUrl(String orcid, ResearcherUrl researcherU

setIncomingPrivacy(newEntity, profile);
DisplayIndexCalculatorHelper.setDisplayIndexOnNewEntity(newEntity, isApiRequest);
researcherUrlDao.persist(newEntity);
try {
newEntity = researcherUrlDao.persistIfNotExists(newEntity);
} catch (EntityExistsException e) {
Map<String, String> params = new HashMap<String, String>();
params.put("type", "researcher-url");
params.put("value", researcherUrl.getUrl().getValue());
throw new OrcidDuplicatedElementException(params);
}
return jpaJaxbResearcherUrlAdapter.toResearcherUrl(newEntity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ public interface ResearcherUrlDao extends GenericDao<ResearcherUrlEntity, Long>
List<BigInteger> getIdsOfResearcherUrlsReferencingClientProfiles(int max, List<String> clientProfileOrcidIds);

boolean updateVisibility(String orcid, Visibility visibility);

ResearcherUrlEntity persistIfNotExists(ResearcherUrlEntity entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigInteger;
import java.util.List;

import javax.persistence.EntityExistsException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

Expand Down Expand Up @@ -228,4 +229,42 @@ public void remove(ResearcherUrlEntity entity) {
super.remove(entity);
}

@Override
@Transactional
@UpdateProfileLastModifiedAndIndexingStatus
public ResearcherUrlEntity persistIfNotExists(ResearcherUrlEntity entity) {
// ORCID unit tests run with HSQL, which doesn't support PostgreSQL ON CONFLICT.
if (!isPostgresDialect()) {
super.persist(entity);
return entity;
}

Query query = entityManager.createNativeQuery(
"INSERT INTO researcher_url (id, date_created, last_modified, url, url_name, orcid, visibility, display_index, source_id, client_source_id, assertion_origin_source_id, assertion_origin_client_source_id) "
+ "VALUES (nextval('researcher_url_seq'), now(), now(), :url, :urlName, :orcid, :visibility, :displayIndex, :sourceId, :clientSourceId, NULL, :assertionOriginClientSourceId) "
+ "ON CONFLICT ON CONSTRAINT researcher_url_orcid_source_unique_key DO NOTHING "
+ "RETURNING id");
query.setParameter("url", entity.getUrl());
query.setParameter("urlName", entity.getUrlName());
query.setParameter("orcid", entity.getOrcid());
query.setParameter("visibility", entity.getVisibility());
query.setParameter("displayIndex", entity.getDisplayIndex());
query.setParameter("sourceId", entity.getSourceId());
query.setParameter("clientSourceId", entity.getClientSourceId());
query.setParameter("assertionOriginClientSourceId", entity.getAssertionOriginClientSourceId());

@SuppressWarnings("unchecked")
List<Number> ids = query.getResultList();
if (ids.isEmpty()) {
throw new EntityExistsException("Duplicate researcher URL");
}
entity.setId(ids.get(0).longValue());
return entity;
}

private boolean isPostgresDialect() {
Object dialect = entityManager.getEntityManagerFactory().getProperties().get("hibernate.dialect");
return dialect != null && dialect.toString().toLowerCase().contains("postgres");
}

}
Loading