Search in sources :

Example 1 with BulkElement

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

the class MemberV2ApiServiceVersionedDelegatorImpl method createWorks.

@Override
public Response createWorks(String orcid, Object works) {
    checkProfileStatus(orcid, false);
    // validate works object before changing version
    Map<Integer, BulkElement> errors = new HashMap<>();
    if (works != null) {
        BulkElementContainer workBulk = (BulkElementContainer) works;
        for (int i = workBulk.getBulk().size() - 1; i >= 0; i--) {
            BulkElement bulkElement = workBulk.getBulk().get(i);
            try {
                schemaValidator.validate(bulkElement);
            } catch (WebApplicationException e) {
                if (org.orcid.jaxb.model.record_rc3.Work.class.isAssignableFrom(bulkElement.getClass())) {
                    org.orcid.jaxb.model.error_rc3.OrcidError error = orcidCoreExceptionMapper.getOrcidErrorV2Rc3(9001, 400, e);
                    workBulk.getBulk().remove(i);
                    errors.put(i, error);
                    workBulk.getBulk().add(i, error);
                } else if (org.orcid.jaxb.model.record_rc4.Work.class.isAssignableFrom(bulkElement.getClass())) {
                    org.orcid.jaxb.model.error_rc4.OrcidError error = orcidCoreExceptionMapper.getOrcidErrorV2Rc4(9001, 400, e);
                    workBulk.getBulk().remove(i);
                    errors.put(i, error);
                    workBulk.getBulk().add(i, error);
                } else {
                    org.orcid.jaxb.model.error_v2.OrcidError error = orcidCoreExceptionMapper.getOrcidErrorV2(9001, 400, e);
                    workBulk.getBulk().remove(i);
                    errors.put(i, error);
                    workBulk.getBulk().add(i, error);
                }
            }
        }
    }
    works = processObject(works);
    return memberV2ApiServiceDelegator.createWorks(orcid, works);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) BulkElementContainer(org.orcid.jaxb.model.record.bulk.BulkElementContainer) BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement)

Example 2 with BulkElement

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

the class WorkManagerReadOnlyImpl method findWorkBulk.

@Override
public WorkBulk findWorkBulk(String orcid, String putCodesAsString) {
    List<BulkElement> works = new ArrayList<>();
    String[] putCodes = getPutCodeArray(putCodesAsString);
    for (String putCode : putCodes) {
        try {
            Long id = Long.valueOf(putCode);
            WorkEntity workEntity = workEntityCacheManager.retrieveFullWork(orcid, id, getLastModified(orcid));
            works.add(jpaJaxbWorkAdapter.toWork(workEntity));
        } catch (Exception e) {
            works.add(orcidCoreExceptionMapper.getV3OrcidError(new PutCodeFormatException("'" + putCode + "' is not a valid put code")));
        }
    }
    WorkBulk bulk = new WorkBulk();
    bulk.setBulk(works);
    return bulk;
}
Also used : WorkEntity(org.orcid.persistence.jpa.entities.WorkEntity) MinimizedWorkEntity(org.orcid.persistence.jpa.entities.MinimizedWorkEntity) PutCodeFormatException(org.orcid.core.exception.PutCodeFormatException) BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) WorkBulk(org.orcid.jaxb.model.v3.dev1.record.WorkBulk) ArrayList(java.util.ArrayList) ExceedMaxNumberOfPutCodesException(org.orcid.core.exception.ExceedMaxNumberOfPutCodesException) PutCodeFormatException(org.orcid.core.exception.PutCodeFormatException)

Example 3 with BulkElement

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

the class WorksTest method testCreateBulkWithAllErrors.

@Test
public void testCreateBulkWithAllErrors() throws InterruptedException, JSONException {
    String accessToken = getAccessToken();
    WorkBulk bulk = createBulk(10, "existing-ext-id-" + System.currentTimeMillis());
    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());
    boolean first = true;
    // All elements might be ok
    for (BulkElement element : bulk.getBulk()) {
        if (first) {
            assertTrue(Work.class.isAssignableFrom(element.getClass()));
            Work work = (Work) element;
            // Remove the work
            memberV2_1ApiClient.deleteWorkXml(this.getUser1OrcidId(), work.getPutCode(), accessToken);
            first = false;
        } else {
            assertTrue(OrcidError.class.isAssignableFrom(element.getClass()));
            OrcidError error = (OrcidError) element;
            assertEquals(Integer.valueOf(9021), error.getErrorCode());
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) WorkBulk(org.orcid.jaxb.model.record_v2.WorkBulk) Work(org.orcid.jaxb.model.record_v2.Work) Test(org.junit.Test)

Example 4 with BulkElement

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

the class WorksTest method testCreateBulkWithAllErrors.

@Test
public void testCreateBulkWithAllErrors() throws InterruptedException, JSONException {
    String accessToken = getAccessToken();
    WorkBulk bulk = createBulk(10, "existing-ext-id-" + System.currentTimeMillis());
    ClientResponse postResponse = memberV3Dev1ApiClient.createWorksJson(this.getUser1OrcidId(), bulk, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.OK.getStatusCode(), postResponse.getStatus());
    bulk = postResponse.getEntity(WorkBulk.class);
    assertNotNull(bulk);
    assertNotNull(bulk.getBulk());
    boolean first = true;
    // All elements might be ok
    for (BulkElement element : bulk.getBulk()) {
        if (first) {
            assertTrue(Work.class.isAssignableFrom(element.getClass()));
            Work work = (Work) element;
            // Remove the work
            memberV3Dev1ApiClient.deleteWorkXml(this.getUser1OrcidId(), work.getPutCode(), accessToken);
            first = false;
        } else {
            assertTrue(OrcidError.class.isAssignableFrom(element.getClass()));
            OrcidError error = (OrcidError) element;
            assertEquals(Integer.valueOf(9021), error.getErrorCode());
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) OrcidError(org.orcid.jaxb.model.v3.dev1.error.OrcidError) BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) WorkBulk(org.orcid.jaxb.model.v3.dev1.record.WorkBulk) Work(org.orcid.jaxb.model.v3.dev1.record.Work) Test(org.junit.Test)

Example 5 with BulkElement

use of org.orcid.jaxb.model.record.bulk.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 = memberV2ApiClient.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(9001), error.getErrorCode());
                    assertTrue(error.getDeveloperMessage().endsWith("\"http://www.orcid.org/ns/work\":type}' is expected.)"));
                    break;
                case 5:
                    assertEquals(Integer.valueOf(9001), error.getErrorCode());
                    assertTrue(error.getDeveloperMessage().contains("Object must have some value in its @XmlValue field: org.orcid.jaxb.model.common_v2.Title"));
                    break;
                case 7:
                    assertEquals(Integer.valueOf(9001), error.getErrorCode());
                    assertTrue(error.getDeveloperMessage().endsWith("Attribute 'language-code' must appear on element 'common:translated-title'.)"));
                    break;
            }
        } else {
            assertTrue(Work.class.isAssignableFrom(element.getClass()));
            Work work = (Work) element;
            assertNotNull(work.getPutCode());
            memberV2ApiClient.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.bulk.BulkElement) WorkBulk(org.orcid.jaxb.model.record_v2.WorkBulk) Work(org.orcid.jaxb.model.record_v2.Work) Test(org.junit.Test)

Aggregations

BulkElement (org.orcid.jaxb.model.record.bulk.BulkElement)30 Test (org.junit.Test)21 ClientResponse (com.sun.jersey.api.client.ClientResponse)15 Work (org.orcid.jaxb.model.record_v2.Work)10 OrcidError (org.orcid.jaxb.model.error_v2.OrcidError)9 WorkBulk (org.orcid.jaxb.model.record_v2.WorkBulk)9 ArrayList (java.util.ArrayList)8 OrcidError (org.orcid.jaxb.model.v3.dev1.error.OrcidError)7 Work (org.orcid.jaxb.model.v3.dev1.record.Work)7 WorkBulk (org.orcid.jaxb.model.v3.dev1.record.WorkBulk)6 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)6 BaseTest (org.orcid.core.BaseTest)4 WorkEntity (org.orcid.persistence.jpa.entities.WorkEntity)4 HashMap (java.util.HashMap)3 Work (org.orcid.jaxb.model.record_rc3.Work)3 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)3 AccessControlException (java.security.AccessControlException)2 Date (java.util.Date)2 Locale (java.util.Locale)2 NoResultException (javax.persistence.NoResultException)2