Search in sources :

Example 16 with WorkBulk

use of org.orcid.jaxb.model.v3.dev1.record.WorkBulk in project ORCID-Source by ORCID.

the class OrcidSecurityManager_WorkBulkTest method testPrivateWorkBulkReadLimitedToken.

@Test
public void testPrivateWorkBulkReadLimitedToken() {
    WorkBulk workBulk = new WorkBulk();
    workBulk.setBulk(Arrays.asList(createWork(Visibility.PRIVATE, CLIENT_2)));
    SecurityContextTestUtils.setUpSecurityContext(ORCID_1, CLIENT_1, ScopePathType.READ_LIMITED);
    orcidSecurityManager.checkAndFilter(ORCID_1, workBulk, ScopePathType.ORCID_WORKS_READ_LIMITED);
    assertNotNull(workBulk);
    assertEquals(1, workBulk.getBulk().size());
    assertTrue(workBulk.getBulk().get(0) instanceof OrcidError);
}
Also used : OrcidError(org.orcid.jaxb.model.v3.dev1.error.OrcidError) WorkBulk(org.orcid.jaxb.model.v3.dev1.record.WorkBulk) Test(org.junit.Test)

Example 17 with WorkBulk

use of org.orcid.jaxb.model.v3.dev1.record.WorkBulk in project ORCID-Source by ORCID.

the class OrcidSecurityManager_WorkBulkTest method testMixedPublicAndLimitedWorkBulkReadPublicToken.

@Test
public void testMixedPublicAndLimitedWorkBulkReadPublicToken() {
    WorkBulk workBulk = new WorkBulk();
    workBulk.setBulk(Arrays.asList(createWork(Visibility.PUBLIC, CLIENT_2), createWork(Visibility.LIMITED, CLIENT_2)));
    SecurityContextTestUtils.setUpSecurityContext(ORCID_1, CLIENT_1, ScopePathType.READ_PUBLIC);
    orcidSecurityManager.checkAndFilter(ORCID_1, workBulk, ScopePathType.ORCID_WORKS_READ_LIMITED);
    assertNotNull(workBulk);
    assertEquals(2, workBulk.getBulk().size());
    assertTrue(workBulk.getBulk().get(0) instanceof Work);
    assertTrue(workBulk.getBulk().get(1) instanceof OrcidError);
}
Also used : OrcidError(org.orcid.jaxb.model.v3.dev1.error.OrcidError) WorkBulk(org.orcid.jaxb.model.v3.dev1.record.WorkBulk) Work(org.orcid.jaxb.model.v3.dev1.record.Work) Test(org.junit.Test)

Example 18 with WorkBulk

use of org.orcid.jaxb.model.v3.dev1.record.WorkBulk in project ORCID-Source by ORCID.

the class OrcidSecurityManager_WorkBulkTest method testPrivateWorkBulkReadLimitedTokenNoSource.

@Test
public void testPrivateWorkBulkReadLimitedTokenNoSource() {
    WorkBulk workBulk = new WorkBulk();
    workBulk.setBulk(Arrays.asList(createWork(Visibility.PRIVATE, null)));
    SecurityContextTestUtils.setUpSecurityContext(ORCID_1, CLIENT_2, ScopePathType.READ_LIMITED);
    orcidSecurityManager.checkAndFilter(ORCID_1, workBulk, ScopePathType.ORCID_WORKS_READ_LIMITED);
    assertNotNull(workBulk);
    assertEquals(1, workBulk.getBulk().size());
    assertTrue(workBulk.getBulk().get(0) instanceof OrcidError);
}
Also used : OrcidError(org.orcid.jaxb.model.v3.dev1.error.OrcidError) WorkBulk(org.orcid.jaxb.model.v3.dev1.record.WorkBulk) Test(org.junit.Test)

Example 19 with WorkBulk

use of org.orcid.jaxb.model.v3.dev1.record.WorkBulk 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;
}
Also used : Locale(java.util.Locale) OrcidError(org.orcid.jaxb.model.v3.dev1.error.OrcidError) HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ExternalID(org.orcid.jaxb.model.v3.dev1.record.ExternalID) ExceedMaxNumberOfElementsException(org.orcid.core.exception.ExceedMaxNumberOfElementsException) TransientNonEmptyString(org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString) TransientNonEmptyString(org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Date(java.util.Date) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) ExceedMaxNumberOfElementsException(org.orcid.core.exception.ExceedMaxNumberOfElementsException) WorkEntity(org.orcid.persistence.jpa.entities.WorkEntity) MinimizedWorkEntity(org.orcid.persistence.jpa.entities.MinimizedWorkEntity) BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) Work(org.orcid.jaxb.model.v3.dev1.record.Work) Transactional(org.springframework.transaction.annotation.Transactional)

Example 20 with WorkBulk

use of org.orcid.jaxb.model.v3.dev1.record.WorkBulk 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);
    }
}
Also used : OrcidError(org.orcid.jaxb.model.v3.dev1.error.OrcidError) BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) OrcidNonPublicElementException(org.orcid.core.exception.OrcidNonPublicElementException) ArrayList(java.util.ArrayList)

Aggregations

WorkBulk (org.orcid.jaxb.model.v3.dev1.record.WorkBulk)33 Test (org.junit.Test)29 Work (org.orcid.jaxb.model.v3.dev1.record.Work)23 OrcidError (org.orcid.jaxb.model.v3.dev1.error.OrcidError)22 BulkElement (org.orcid.jaxb.model.record.bulk.BulkElement)9 BaseTest (org.orcid.core.BaseTest)8 ExternalID (org.orcid.jaxb.model.v3.dev1.record.ExternalID)8 Title (org.orcid.jaxb.model.v3.dev1.common.Title)7 ExternalIDs (org.orcid.jaxb.model.v3.dev1.record.ExternalIDs)7 WorkTitle (org.orcid.jaxb.model.v3.dev1.record.WorkTitle)7 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)7 Url (org.orcid.jaxb.model.v3.dev1.common.Url)6 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)6 Response (javax.ws.rs.core.Response)5 DBUnitTest (org.orcid.test.DBUnitTest)5 ClientResponse (com.sun.jersey.api.client.ClientResponse)4 ArrayList (java.util.ArrayList)4 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)3 OrcidNoResultException (org.orcid.core.exception.OrcidNoResultException)2 TranslatedTitle (org.orcid.jaxb.model.v3.dev1.common.TranslatedTitle)2