Search in sources :

Example 16 with BulkElement

use of org.orcid.jaxb.model.record_rc3.BulkElement in project ORCID-Source by ORCID.

the class WorksTest method testThreeWithInvalidTypeAllOthersAreFine.

@Test
public void testThreeWithInvalidTypeAllOthersAreFine() throws InterruptedException, JSONException {
    String accessToken = getAccessToken();
    WorkBulk bulk = createBulk(10, null);
    //Work 3: no type
    Work work3 = (Work) bulk.getBulk().get(3);
    work3.setWorkType(null);
    bulk.getBulk().set(3, work3);
    //Work 5: empty title
    Work work5 = (Work) bulk.getBulk().get(5);
    work5.getWorkTitle().getTitle().setContent(null);
    bulk.getBulk().set(5, work5);
    //Work 7: translated title language code empty
    Work work7 = (Work) bulk.getBulk().get(7);
    work7.getWorkTitle().getTranslatedTitle().setLanguageCode(null);
    bulk.getBulk().set(7, work7);
    ClientResponse postResponse = memberV2_1ApiClient.createWorksJson(this.getUser1OrcidId(), bulk, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.OK.getStatusCode(), postResponse.getStatus());
    bulk = postResponse.getEntity(WorkBulk.class);
    assertNotNull(bulk);
    assertNotNull(bulk.getBulk());
    for (int i = 0; i < bulk.getBulk().size(); i++) {
        BulkElement element = bulk.getBulk().get(i);
        if (i == 3 || i == 5 || i == 7) {
            assertTrue(OrcidError.class.isAssignableFrom(element.getClass()));
            OrcidError error = (OrcidError) element;
            switch(i) {
                case 3:
                    assertEquals(Integer.valueOf(9037), error.getErrorCode());
                    assertTrue(error.getDeveloperMessage().startsWith("Invalid work type"));
                    break;
                case 5:
                    assertEquals(Integer.valueOf(9022), error.getErrorCode());
                    break;
                case 7:
                    assertEquals(Integer.valueOf(9037), error.getErrorCode());
                    assertTrue(error.getDeveloperMessage().startsWith("Invalid translated title"));
                    break;
            }
        } else {
            assertTrue(Work.class.isAssignableFrom(element.getClass()));
            Work work = (Work) element;
            assertNotNull(work.getPutCode());
            memberV2_1ApiClient.deleteWorkXml(this.getUser1OrcidId(), work.getPutCode(), accessToken);
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) BulkElement(org.orcid.jaxb.model.record_v2.BulkElement) WorkBulk(org.orcid.jaxb.model.record_v2.WorkBulk) Work(org.orcid.jaxb.model.record_v2.Work) Test(org.junit.Test)

Example 17 with BulkElement

use of org.orcid.jaxb.model.record_rc3.BulkElement in project ORCID-Source by ORCID.

the class WorksTest method testCreateBulkWork.

@Test
public void testCreateBulkWork() throws InterruptedException, JSONException {
    String accessToken = getAccessToken();
    WorkBulk bulk = createBulk(10, null);
    ClientResponse postResponse = memberV2_1ApiClient.createWorksJson(this.getUser1OrcidId(), bulk, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.OK.getStatusCode(), postResponse.getStatus());
    bulk = postResponse.getEntity(WorkBulk.class);
    assertNotNull(bulk);
    assertNotNull(bulk.getBulk());
    //All elements might be ok
    for (BulkElement element : bulk.getBulk()) {
        assertTrue(Work.class.isAssignableFrom(element.getClass()));
        Work work = (Work) element;
        //Remove the work
        memberV2_1ApiClient.deleteWorkXml(this.getUser1OrcidId(), work.getPutCode(), accessToken);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) BulkElement(org.orcid.jaxb.model.record_v2.BulkElement) WorkBulk(org.orcid.jaxb.model.record_v2.WorkBulk) Work(org.orcid.jaxb.model.record_v2.Work) Test(org.junit.Test)

Example 18 with BulkElement

use of org.orcid.jaxb.model.record_rc3.BulkElement in project ORCID-Source by ORCID.

the class PublicAPISecurityManagerV2Impl 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.getOrcidError(e));
                }
            }
        }
        workBulk.setBulk(filtered);
    }
}
Also used : OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) BulkElement(org.orcid.jaxb.model.record_v2.BulkElement) OrcidNonPublicElementException(org.orcid.core.exception.OrcidNonPublicElementException) ArrayList(java.util.ArrayList)

Example 19 with BulkElement

use of org.orcid.jaxb.model.record_rc3.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();
    Set<ExternalID> existingExternalIdentifiers = buildExistingExternalIdsSet(orcid, sourceEntity.getSourceId());
    if (workBulk.getBulk() != null && !workBulk.getBulk().isEmpty()) {
        List<BulkElement> bulk = workBulk.getBulk();
        //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 {
                    //Validate the work
                    activityValidator.validateWork(work, sourceEntity, true, true, null);
                    //Validate it is not duplicated
                    if (work.getExternalIdentifiers() != null) {
                        for (ExternalID extId : work.getExternalIdentifiers().getExternalIdentifier()) {
                            if (existingExternalIdentifiers.contains(extId)) {
                                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.setProfile(profile);
                    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.getOrcidError(e);
                    bulk.set(i, orcidError);
                }
            }
        }
        workDao.flush();
    }
    return workBulk;
}
Also used : Locale(java.util.Locale) OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ExternalID(org.orcid.jaxb.model.record_v2.ExternalID) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Date(java.util.Date) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) WorkEntity(org.orcid.persistence.jpa.entities.WorkEntity) BulkElement(org.orcid.jaxb.model.record_v2.BulkElement) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) Work(org.orcid.jaxb.model.record_v2.Work) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Test (org.junit.Test)14 BulkElement (org.orcid.jaxb.model.record_v2.BulkElement)13 ClientResponse (com.sun.jersey.api.client.ClientResponse)12 OrcidError (org.orcid.jaxb.model.error_v2.OrcidError)9 Work (org.orcid.jaxb.model.record_v2.Work)9 WorkBulk (org.orcid.jaxb.model.record_v2.WorkBulk)9 ArrayList (java.util.ArrayList)4 BulkElement (org.orcid.jaxb.model.record_rc3.BulkElement)3 Work (org.orcid.jaxb.model.record_rc3.Work)3 WorkBulk (org.orcid.jaxb.model.record_rc3.WorkBulk)3 BulkElement (org.orcid.jaxb.model.record_rc4.BulkElement)3 Work (org.orcid.jaxb.model.record_rc4.Work)3 WorkBulk (org.orcid.jaxb.model.record_rc4.WorkBulk)3 ExternalID (org.orcid.jaxb.model.record_v2.ExternalID)3 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)3 BaseTest (org.orcid.core.BaseTest)2 Url (org.orcid.jaxb.model.common_v2.Url)2 OrcidError (org.orcid.jaxb.model.error_rc3.OrcidError)2 OrcidError (org.orcid.jaxb.model.error_rc4.OrcidError)2 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)2