use of org.orcid.jaxb.model.record.bulk.BulkElement in project ORCID-Source by ORCID.
the class WorkManagerTest method testCreateWorksWithBulkSomeOKSomeErrors.
@Test
public void testCreateWorksWithBulkSomeOKSomeErrors() {
String orcid = "0000-0000-0000-0003";
when(sourceManager.retrieveSourceEntity()).thenReturn(new SourceEntity(new ClientDetailsEntity(CLIENT_2_ID)));
// Lets send a bulk of 6 works
WorkBulk bulk = new WorkBulk();
// Work # 1 - Fine
Work work1 = getWork(null);
work1.getExternalIdentifiers().getExternalIdentifier().clear();
work1.getExternalIdentifiers().getExternalIdentifier().add(getDuplicateExternalId());
work1.getWorkTitle().getTitle().setContent("Work # 1");
bulk.getBulk().add(work1);
// Work # 2 - Fine
Work work2 = getWork(null);
work2.getWorkTitle().getTitle().setContent("Work # 2");
bulk.getBulk().add(work2);
// Work # 3 - Duplicated of Work # 1
Work work3 = getWork(null);
work3.getExternalIdentifiers().getExternalIdentifier().clear();
work3.getExternalIdentifiers().getExternalIdentifier().add(getDuplicateExternalId());
work3.getWorkTitle().getTitle().setContent("Work # 3");
bulk.getBulk().add(work3);
// Work # 4 - Fine
Work work4 = getWork("new-ext-id-" + System.currentTimeMillis());
work4.getWorkTitle().getTitle().setContent("Work # 4");
bulk.getBulk().add(work4);
// Work # 5 - Duplicated of existing work
Work work5 = getWork(null);
ExternalID dupExtId = new ExternalID();
dupExtId.setRelationship(Relationship.SELF);
dupExtId.setType("doi");
dupExtId.setValue("1");
work5.getExternalIdentifiers().getExternalIdentifier().clear();
work5.getExternalIdentifiers().getExternalIdentifier().add(dupExtId);
work5.getWorkTitle().getTitle().setContent("Work # 5");
bulk.getBulk().add(work5);
// Work # 6 - No title specified
Work work6 = getWork(null);
work6.getWorkTitle().getTitle().setContent(null);
bulk.getBulk().add(work6);
bulk = workManager.createWorks(orcid, bulk);
assertNotNull(bulk);
assertEquals(6, bulk.getBulk().size());
List<Long> worksToDelete = new ArrayList<Long>();
for (int i = 0; i < bulk.getBulk().size(); i++) {
BulkElement element = bulk.getBulk().get(i);
switch(i) {
case 0:
case 1:
case 3:
assertTrue(Work.class.isAssignableFrom(element.getClass()));
Work work = (Work) element;
assertNotNull(work);
assertNotNull(work.getPutCode());
if (i == 0) {
assertEquals("Work # 1", work.getWorkTitle().getTitle().getContent());
} else if (i == 1) {
assertEquals("Work # 2", work.getWorkTitle().getTitle().getContent());
} else {
assertEquals("Work # 4", work.getWorkTitle().getTitle().getContent());
}
worksToDelete.add(work.getPutCode());
break;
case 2:
case 4:
case 5:
assertTrue("Error on id: " + i, OrcidError.class.isAssignableFrom(element.getClass()));
OrcidError error = (OrcidError) element;
if (i == 2) {
assertEquals(Integer.valueOf(9021), error.getErrorCode());
} else if (i == 4) {
assertEquals(Integer.valueOf(9021), error.getErrorCode());
} else {
assertEquals(Integer.valueOf(9022), error.getErrorCode());
}
break;
}
}
// Delete new works
for (Long putCode : worksToDelete) {
assertTrue(workManager.checkSourceAndRemoveWork(orcid, putCode));
}
}
use of org.orcid.jaxb.model.record.bulk.BulkElement in project ORCID-Source by ORCID.
the class WorkManagerTest method testCreateWorksWithBulkSomeOKSomeErrors.
@Test
public void testCreateWorksWithBulkSomeOKSomeErrors() {
String orcid = "0000-0000-0000-0003";
when(sourceManager.retrieveSourceEntity()).thenReturn(new SourceEntity(new ClientDetailsEntity(CLIENT_2_ID)));
// Lets send a bulk of 6 works
WorkBulk bulk = new WorkBulk();
// Work # 1 - Fine
Work work1 = getWork(null);
ExternalID extId = new ExternalID();
extId.setRelationship(Relationship.SELF);
extId.setType("doi");
extId.setUrl(new Url("http://doi/1"));
extId.setValue("doi-1");
work1.getExternalIdentifiers().getExternalIdentifier().clear();
work1.getExternalIdentifiers().getExternalIdentifier().add(extId);
work1.getWorkTitle().getTitle().setContent("Work # 1");
bulk.getBulk().add(work1);
// Work # 2 - Fine
Work work2 = getWork(null);
work2.getWorkTitle().getTitle().setContent("Work # 2");
bulk.getBulk().add(work2);
// Work # 3 - Duplicated of Work # 1
Work work3 = getWork(null);
work3.getExternalIdentifiers().getExternalIdentifier().clear();
work3.getExternalIdentifiers().getExternalIdentifier().add(extId);
work3.getWorkTitle().getTitle().setContent("Work # 3");
bulk.getBulk().add(work3);
// Work # 4 - Fine
Work work4 = getWork("new-ext-id-" + System.currentTimeMillis());
work4.getWorkTitle().getTitle().setContent("Work # 4");
bulk.getBulk().add(work4);
// Work # 5 - Duplicated of existing work
Work work5 = getWork(null);
ExternalID dupExtId = new ExternalID();
dupExtId.setRelationship(Relationship.SELF);
dupExtId.setType("doi");
dupExtId.setValue("1");
work5.getExternalIdentifiers().getExternalIdentifier().clear();
work5.getExternalIdentifiers().getExternalIdentifier().add(dupExtId);
work5.getWorkTitle().getTitle().setContent("Work # 5");
bulk.getBulk().add(work5);
// Work # 6 - No title specified
Work work6 = getWork(null);
work6.getWorkTitle().getTitle().setContent(null);
bulk.getBulk().add(work6);
bulk = workManager.createWorks(orcid, bulk);
assertNotNull(bulk);
assertEquals(6, bulk.getBulk().size());
List<Long> worksToDelete = new ArrayList<Long>();
for (int i = 0; i < bulk.getBulk().size(); i++) {
BulkElement element = bulk.getBulk().get(i);
switch(i) {
case 0:
case 1:
case 3:
assertTrue(Work.class.isAssignableFrom(element.getClass()));
Work work = (Work) element;
assertNotNull(work);
assertNotNull(work.getPutCode());
if (i == 0) {
assertEquals("Work # 1", work.getWorkTitle().getTitle().getContent());
} else if (i == 1) {
assertEquals("Work # 2", work.getWorkTitle().getTitle().getContent());
} else {
assertEquals("Work # 4", work.getWorkTitle().getTitle().getContent());
}
worksToDelete.add(work.getPutCode());
break;
case 2:
case 4:
case 5:
assertTrue("Error on id: " + i, OrcidError.class.isAssignableFrom(element.getClass()));
OrcidError error = (OrcidError) element;
if (i == 2) {
assertEquals(Integer.valueOf(9021), error.getErrorCode());
} else if (i == 4) {
assertEquals(Integer.valueOf(9021), error.getErrorCode());
} else {
assertEquals(Integer.valueOf(9022), error.getErrorCode());
}
break;
}
}
// Delete new works
for (Long putCode : worksToDelete) {
assertTrue(workManager.checkSourceAndRemoveWork(orcid, putCode));
}
}
use of org.orcid.jaxb.model.record.bulk.BulkElement 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.record.bulk.BulkElement in project ORCID-Source by ORCID.
the class OrcidSecurityManagerImpl method checkAndFilter.
@Override
public void checkAndFilter(String orcid, WorkBulk workBulk, ScopePathType scopePathType) {
isMyToken(orcid);
List<BulkElement> bulkElements = workBulk.getBulk();
List<BulkElement> filteredElements = new ArrayList<>();
for (int i = 0; i < bulkElements.size(); i++) {
BulkElement element = bulkElements.get(i);
if (element instanceof OrcidError) {
filteredElements.add(element);
continue;
}
try {
checkAndFilter(orcid, (Work) element, scopePathType, true);
filteredElements.add(element);
} catch (Exception e) {
if (e instanceof OrcidUnauthorizedException) {
throw e;
}
OrcidError error = orcidCoreExceptionMapper.getOrcidError(e);
filteredElements.add(error);
}
}
workBulk.setBulk(filteredElements);
}
use of org.orcid.jaxb.model.record.bulk.BulkElement 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);
}
}
Aggregations