use of org.orcid.jaxb.model.v3.dev1.error.OrcidError in project ORCID-Source by ORCID.
the class WorkManagerImpl method createWorks.
/**
* Add a list of works to the given profile
*
* @param works
* The list of works that want to be added
* @param orcid
* The id of the user we want to add the works to
*
* @return the work bulk with the put codes of the new works or the error
* that indicates why a work can't be added
*/
@Override
@Transactional
public WorkBulk createWorks(String orcid, WorkBulk workBulk) {
SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
List<Work> existingWorks = this.findWorks(orcid);
if (workBulk.getBulk() != null && !workBulk.getBulk().isEmpty()) {
List<BulkElement> bulk = workBulk.getBulk();
Set<ExternalID> existingExternalIdentifiers = buildExistingExternalIdsSet(existingWorks, sourceEntity.getSourceId());
if ((existingWorks.size() + bulk.size()) > this.maxNumOfActivities) {
throw new ExceedMaxNumberOfElementsException();
}
// Check bulk size
if (bulk.size() > maxBulkSize) {
Locale locale = localeManager.getLocale();
throw new IllegalArgumentException(messageSource.getMessage("apiError.validation_too_many_elements_in_bulk.exception", new Object[] { maxBulkSize }, locale));
}
for (int i = 0; i < bulk.size(); i++) {
if (Work.class.isAssignableFrom(bulk.get(i).getClass())) {
Work work = (Work) bulk.get(i);
try {
activityValidator.validateWork(work, sourceEntity, true, true, null);
// Validate it is not duplicated
if (work.getExternalIdentifiers() != null) {
for (ExternalID extId : work.getExternalIdentifiers().getExternalIdentifier()) {
// normalise the provided ID
extId.setNormalized(new TransientNonEmptyString(norm.normalise(extId.getType(), extId.getValue())));
// If the external id exists and is a SELF identifier, then mark it as duplicated
if (existingExternalIdentifiers.contains(extId) && Relationship.SELF.equals(extId.getRelationship())) {
Map<String, String> params = new HashMap<String, String>();
params.put("clientName", sourceEntity.getSourceName());
throw new OrcidDuplicatedActivityException(params);
}
}
}
// Save the work
WorkEntity workEntity = jpaJaxbWorkAdapter.toWorkEntity(work);
ProfileEntity profile = profileEntityCacheManager.retrieve(orcid);
workEntity.setOrcid(orcid);
workEntity.setAddedToProfileDate(new Date());
// Set source id
if (sourceEntity.getSourceProfile() != null) {
workEntity.setSourceId(sourceEntity.getSourceProfile().getId());
}
if (sourceEntity.getSourceClient() != null) {
workEntity.setClientSourceId(sourceEntity.getSourceClient().getId());
}
setIncomingWorkPrivacy(workEntity, profile);
DisplayIndexCalculatorHelper.setDisplayIndexOnNewEntity(workEntity, true);
workDao.persist(workEntity);
// Update the element in the bulk
Work updatedWork = jpaJaxbWorkAdapter.toWork(workEntity);
bulk.set(i, updatedWork);
// Add the work extIds to the list of existing external identifiers
addExternalIdsToExistingSet(updatedWork, existingExternalIdentifiers);
} catch (Exception e) {
// Get the exception
OrcidError orcidError = orcidCoreExceptionMapper.getV3OrcidError(e);
bulk.set(i, orcidError);
}
}
}
workDao.flush();
}
return workBulk;
}
use of org.orcid.jaxb.model.v3.dev1.error.OrcidError in project ORCID-Source by ORCID.
the class PublicAPISecurityManagerV3Impl method filter.
@Override
public void filter(WorkBulk workBulk) {
if (workBulk != null && workBulk.getBulk() != null) {
List<BulkElement> filtered = new ArrayList<>();
for (int i = 0; i < workBulk.getBulk().size(); i++) {
BulkElement bulkElement = workBulk.getBulk().get(i);
if (bulkElement instanceof OrcidError) {
filtered.add(bulkElement);
} else {
try {
checkIsPublic((Work) bulkElement);
filtered.add(bulkElement);
} catch (OrcidNonPublicElementException e) {
filtered.add(orcidCoreExceptionMapper.getV3OrcidError(e));
}
}
}
workBulk.setBulk(filtered);
}
}
use of org.orcid.jaxb.model.v3.dev1.error.OrcidError in project ORCID-Source by ORCID.
the class MemberV3ApiServiceDelegator_WorksTest method testCreateBulkWorksWithBlankTitles.
@Test
public void testCreateBulkWorksWithBlankTitles() {
RequestAttributes previousAttrs = RequestContextHolder.getRequestAttributes();
RequestAttributes attrs = new ServletRequestAttributes(new MockHttpServletRequest());
attrs.setAttribute(ApiVersionFilter.API_VERSION_REQUEST_ATTRIBUTE_NAME, "3.0_dev1", RequestAttributes.SCOPE_REQUEST);
RequestContextHolder.setRequestAttributes(attrs);
Long time = System.currentTimeMillis();
SecurityContextTestUtils.setUpSecurityContext(ORCID, ScopePathType.READ_LIMITED, ScopePathType.ACTIVITIES_UPDATE);
WorkBulk bulk = new WorkBulk();
for (int i = 0; i < 5; i++) {
Work work = new Work();
WorkTitle title = new WorkTitle();
title.setTitle(i == 0 ? new Title(" ") : new Title("title " + i));
work.setWorkTitle(title);
ExternalIDs extIds = new ExternalIDs();
ExternalID extId = new ExternalID();
extId.setRelationship(Relationship.SELF);
extId.setType("doi");
extId.setUrl(new Url("http://doi/" + i + "/" + time));
extId.setValue("doi-" + i + "-" + time);
extIds.getExternalIdentifier().add(extId);
work.setWorkExternalIdentifiers(extIds);
work.setWorkType(WorkType.BOOK);
bulk.getBulk().add(work);
}
Response response = serviceDelegator.createWorks(ORCID, bulk);
assertNotNull(response);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
bulk = (WorkBulk) response.getEntity();
assertNotNull(bulk);
assertEquals(5, bulk.getBulk().size());
for (int i = 0; i < 5; i++) {
if (i == 0) {
assertTrue(bulk.getBulk().get(i) instanceof OrcidError);
} else {
assertTrue(bulk.getBulk().get(i) instanceof Work);
serviceDelegator.deleteWork(ORCID, ((Work) bulk.getBulk().get(i)).getPutCode());
}
}
RequestContextHolder.setRequestAttributes(previousAttrs);
}
use of org.orcid.jaxb.model.v3.dev1.error.OrcidError in project ORCID-Source by ORCID.
the class MemberV3ApiServiceDelegator_WorksTest method testViewBulkWorks.
@Test
public void testViewBulkWorks() {
SecurityContextTestUtils.setUpSecurityContext(ORCID, ScopePathType.READ_LIMITED);
Response response = serviceDelegator.viewBulkWorks(ORCID, "11,12,13,16");
WorkBulk workBulk = (WorkBulk) response.getEntity();
assertNotNull(workBulk);
assertNotNull(workBulk.getBulk());
assertEquals(4, workBulk.getBulk().size());
assertTrue(workBulk.getBulk().get(0) instanceof Work);
assertTrue(workBulk.getBulk().get(1) instanceof Work);
// private work but matching source
assertTrue(workBulk.getBulk().get(2) instanceof Work);
// private work not matching source
assertTrue(workBulk.getBulk().get(3) instanceof OrcidError);
}
use of org.orcid.jaxb.model.v3.dev1.error.OrcidError in project ORCID-Source by ORCID.
the class ResearcherUrlsTest method testCreateGetUpdateAndDeleteResearcherUrl.
@Test
public void testCreateGetUpdateAndDeleteResearcherUrl() throws InterruptedException, JSONException, URISyntaxException {
String accessToken = getAccessToken();
assertNotNull(accessToken);
org.orcid.jaxb.model.v3.dev1.record.ResearcherUrl rUrlToCreate = new org.orcid.jaxb.model.v3.dev1.record.ResearcherUrl();
long time = System.currentTimeMillis();
String url = "http://test.orcid.org/test/" + time;
rUrlToCreate.setUrl(new org.orcid.jaxb.model.v3.dev1.common.Url(url));
rUrlToCreate.setUrlName(url);
// Create
ClientResponse postResponse = memberV3Dev1ApiClient.createResearcherUrls(getUser1OrcidId(), rUrlToCreate, accessToken);
assertNotNull(postResponse);
assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());
String locationPath = postResponse.getLocation().getPath();
assertTrue("Location header path should match pattern, but was " + locationPath, locationPath.matches(".*/v3.0_dev1/" + getUser1OrcidId() + "/researcher-urls/\\d+"));
// Read
ClientResponse getResponse = memberV3Dev1ApiClient.viewLocationXml(postResponse.getLocation(), accessToken);
assertEquals(Response.Status.OK.getStatusCode(), getResponse.getStatus());
org.orcid.jaxb.model.v3.dev1.record.ResearcherUrl gotResearcherUrl = getResponse.getEntity(org.orcid.jaxb.model.v3.dev1.record.ResearcherUrl.class);
assertNotNull(gotResearcherUrl);
assertNotNull(gotResearcherUrl.getPutCode());
assertNotNull(gotResearcherUrl.getSource());
assertNotNull(gotResearcherUrl.getCreatedDate());
assertNotNull(gotResearcherUrl.getLastModifiedDate());
assertEquals(getClient1ClientId(), gotResearcherUrl.getSource().retrieveSourcePath());
assertEquals("http://test.orcid.org/test/" + time, gotResearcherUrl.getUrl().getValue());
assertEquals("http://test.orcid.org/test/" + time, gotResearcherUrl.getUrlName());
assertEquals("public", gotResearcherUrl.getVisibility().value());
assertNotNull(gotResearcherUrl.getDisplayIndex());
Long originalDisplayIndex = gotResearcherUrl.getDisplayIndex();
// Save the original visibility
org.orcid.jaxb.model.v3.dev1.common.Visibility originalVisibility = gotResearcherUrl.getVisibility();
org.orcid.jaxb.model.v3.dev1.common.Visibility updatedVisibility = org.orcid.jaxb.model.v3.dev1.common.Visibility.PRIVATE.equals(originalVisibility) ? org.orcid.jaxb.model.v3.dev1.common.Visibility.LIMITED : org.orcid.jaxb.model.v3.dev1.common.Visibility.PRIVATE;
// Verify you cant update the visibility
gotResearcherUrl.setVisibility(updatedVisibility);
ClientResponse putResponse = memberV3Dev1ApiClient.updateLocationXml(postResponse.getLocation(), accessToken, gotResearcherUrl);
assertEquals(Response.Status.FORBIDDEN.getStatusCode(), putResponse.getStatus());
org.orcid.jaxb.model.error_v2.OrcidError error = putResponse.getEntity(org.orcid.jaxb.model.error_v2.OrcidError.class);
assertNotNull(error);
assertEquals(Integer.valueOf(9035), error.getErrorCode());
// Set the visibility again to the initial one
gotResearcherUrl.setVisibility(originalVisibility);
// Update
org.orcid.jaxb.model.v3.dev1.common.LastModifiedDate initialLastModified = gotResearcherUrl.getLastModifiedDate();
Long currentTime = System.currentTimeMillis();
gotResearcherUrl.setUrlName(gotResearcherUrl.getUrlName() + " - " + currentTime);
gotResearcherUrl.getUrl().setValue(gotResearcherUrl.getUrl().getValue() + currentTime);
ClientResponse updatedResearcherUrlResponse = memberV3Dev1ApiClient.updateResearcherUrls(getUser1OrcidId(), gotResearcherUrl, accessToken);
assertNotNull(updatedResearcherUrlResponse);
assertEquals(Response.Status.OK.getStatusCode(), updatedResearcherUrlResponse.getStatus());
org.orcid.jaxb.model.v3.dev1.record.ResearcherUrl updatedResearcherUrl = updatedResearcherUrlResponse.getEntity(org.orcid.jaxb.model.v3.dev1.record.ResearcherUrl.class);
assertNotNull(updatedResearcherUrl);
assertEquals("http://test.orcid.org/test/" + time + currentTime, updatedResearcherUrl.getUrl().getValue());
assertEquals("http://test.orcid.org/test/" + time + " - " + currentTime, updatedResearcherUrl.getUrlName());
assertEquals(originalDisplayIndex, updatedResearcherUrl.getDisplayIndex());
// Keep it public, since it is more restrictive than the user visibility
// default
assertEquals(org.orcid.jaxb.model.v3.dev1.common.Visibility.PUBLIC, updatedResearcherUrl.getVisibility());
assertFalse(initialLastModified.equals(updatedResearcherUrl.getLastModifiedDate()));
// Delete
ClientResponse deleteResponse = memberV3Dev1ApiClient.deleteResearcherUrl(getUser1OrcidId(), gotResearcherUrl.getPutCode(), accessToken);
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), deleteResponse.getStatus());
}
Aggregations