Search in sources :

Example 21 with OrcidError

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;
}
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 22 with OrcidError

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);
    }
}
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)

Example 23 with OrcidError

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);
}
Also used : OrcidError(org.orcid.jaxb.model.v3.dev1.error.OrcidError) ExternalIDs(org.orcid.jaxb.model.v3.dev1.record.ExternalIDs) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WorkBulk(org.orcid.jaxb.model.v3.dev1.record.WorkBulk) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ExternalID(org.orcid.jaxb.model.v3.dev1.record.ExternalID) Title(org.orcid.jaxb.model.v3.dev1.common.Title) TranslatedTitle(org.orcid.jaxb.model.v3.dev1.common.TranslatedTitle) WorkTitle(org.orcid.jaxb.model.v3.dev1.record.WorkTitle) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ResearcherUrl(org.orcid.jaxb.model.v3.dev1.record.ResearcherUrl) Url(org.orcid.jaxb.model.v3.dev1.common.Url) Response(javax.ws.rs.core.Response) WorkTitle(org.orcid.jaxb.model.v3.dev1.record.WorkTitle) Work(org.orcid.jaxb.model.v3.dev1.record.Work) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 24 with OrcidError

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);
}
Also used : Response(javax.ws.rs.core.Response) 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) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 25 with 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());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) BlackBoxBaseV3_0_dev1(org.orcid.integration.blackbox.api.v3.dev1.BlackBoxBaseV3_0_dev1) Test(org.junit.Test)

Aggregations

OrcidError (org.orcid.jaxb.model.v3.dev1.error.OrcidError)33 Test (org.junit.Test)30 Work (org.orcid.jaxb.model.v3.dev1.record.Work)18 WorkBulk (org.orcid.jaxb.model.v3.dev1.record.WorkBulk)18 ClientResponse (com.sun.jersey.api.client.ClientResponse)15 Visibility (org.orcid.jaxb.model.v3.dev1.common.Visibility)11 ExternalID (org.orcid.jaxb.model.v3.dev1.record.ExternalID)9 BulkElement (org.orcid.jaxb.model.record.bulk.BulkElement)7 BaseTest (org.orcid.core.BaseTest)5 Url (org.orcid.jaxb.model.v3.dev1.common.Url)5 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)5 Response (javax.ws.rs.core.Response)4 Title (org.orcid.jaxb.model.v3.dev1.common.Title)4 ExternalIDs (org.orcid.jaxb.model.v3.dev1.record.ExternalIDs)4 WorkTitle (org.orcid.jaxb.model.v3.dev1.record.WorkTitle)4 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)4 DBUnitTest (org.orcid.test.DBUnitTest)4 ArrayList (java.util.ArrayList)3 AccessControlException (java.security.AccessControlException)1 Date (java.util.Date)1