Search in sources :

Example 16 with BulkElement

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

the class ActivityUtilsTest method cleanBulkElementTest.

@Test
public void cleanBulkElementTest() {
    // Test it cleans empty fields
    BulkElement b = getEmptyWork();
    assertNotNull(((Work) b).getWorkTitle().getTranslatedTitle());
    assertNotNull(((Work) b).getWorkCitation());
    assertNotNull(((Work) b).getWorkContributors().getContributor().get(0).getCreditName());
    ActivityUtils.cleanEmptyFields(b);
    assertNull(((Work) b).getWorkTitle().getTranslatedTitle());
    assertNull(((Work) b).getWorkCitation());
    assertNull(((Work) b).getWorkContributors().getContributor().get(0).getCreditName());
    // Test it doesn't remove non empty fields
    b = getEmptyWork();
    ((Work) b).getWorkTitle().getTranslatedTitle().setContent("translated_title");
    ((Work) b).getWorkTitle().getTranslatedTitle().setLanguageCode("en_us");
    ((Work) b).getWorkCitation().setCitation("citation");
    ((Work) b).getWorkCitation().setWorkCitationType(CitationType.BIBTEX);
    ((Work) b).getWorkContributors().getContributor().get(0).getCreditName().setContent("credit_name");
    assertEquals("translated_title", ((Work) b).getWorkTitle().getTranslatedTitle().getContent());
    assertEquals("en_us", ((Work) b).getWorkTitle().getTranslatedTitle().getLanguageCode());
    assertEquals("citation", ((Work) b).getWorkCitation().getCitation());
    assertEquals(CitationType.BIBTEX, ((Work) b).getWorkCitation().getWorkCitationType());
    assertEquals("credit_name", ((Work) b).getWorkContributors().getContributor().get(0).getCreditName().getContent());
    ActivityUtils.cleanEmptyFields(b);
    assertEquals("translated_title", ((Work) b).getWorkTitle().getTranslatedTitle().getContent());
    assertEquals("en_us", ((Work) b).getWorkTitle().getTranslatedTitle().getLanguageCode());
    assertEquals("citation", ((Work) b).getWorkCitation().getCitation());
    assertEquals(CitationType.BIBTEX, ((Work) b).getWorkCitation().getWorkCitationType());
    assertEquals("credit_name", ((Work) b).getWorkContributors().getContributor().get(0).getCreditName().getContent());
}
Also used : BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) Work(org.orcid.jaxb.model.record_v2.Work) Test(org.junit.Test)

Example 17 with BulkElement

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

the class ActivityUtilsTest method cleanBulkElementTest.

@Test
public void cleanBulkElementTest() {
    // Test it cleans empty fields
    BulkElement b = getEmptyWork();
    assertNotNull(((Work) b).getWorkTitle().getTranslatedTitle());
    assertNotNull(((Work) b).getWorkCitation());
    assertNotNull(((Work) b).getWorkContributors().getContributor().get(0).getCreditName());
    ActivityUtils.cleanEmptyFields(b);
    assertNull(((Work) b).getWorkTitle().getTranslatedTitle());
    assertNull(((Work) b).getWorkCitation());
    assertNull(((Work) b).getWorkContributors().getContributor().get(0).getCreditName());
    // Test it doesn't remove non empty fields
    b = getEmptyWork();
    ((Work) b).getWorkTitle().getTranslatedTitle().setContent("translated_title");
    ((Work) b).getWorkTitle().getTranslatedTitle().setLanguageCode("en_us");
    ((Work) b).getWorkCitation().setCitation("citation");
    ((Work) b).getWorkCitation().setWorkCitationType(CitationType.BIBTEX);
    ((Work) b).getWorkContributors().getContributor().get(0).getCreditName().setContent("credit_name");
    assertEquals("translated_title", ((Work) b).getWorkTitle().getTranslatedTitle().getContent());
    assertEquals("en_us", ((Work) b).getWorkTitle().getTranslatedTitle().getLanguageCode());
    assertEquals("citation", ((Work) b).getWorkCitation().getCitation());
    assertEquals(CitationType.BIBTEX, ((Work) b).getWorkCitation().getWorkCitationType());
    assertEquals("credit_name", ((Work) b).getWorkContributors().getContributor().get(0).getCreditName().getContent());
    ActivityUtils.cleanEmptyFields(b);
    assertEquals("translated_title", ((Work) b).getWorkTitle().getTranslatedTitle().getContent());
    assertEquals("en_us", ((Work) b).getWorkTitle().getTranslatedTitle().getLanguageCode());
    assertEquals("citation", ((Work) b).getWorkCitation().getCitation());
    assertEquals(CitationType.BIBTEX, ((Work) b).getWorkCitation().getWorkCitationType());
    assertEquals("credit_name", ((Work) b).getWorkContributors().getContributor().get(0).getCreditName().getContent());
}
Also used : BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) Work(org.orcid.jaxb.model.v3.dev1.record.Work) Test(org.junit.Test)

Example 18 with BulkElement

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()) {
                            // 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.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) ExceedMaxNumberOfElementsException(org.orcid.core.exception.ExceedMaxNumberOfElementsException) 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) BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) Work(org.orcid.jaxb.model.record_v2.Work) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with BulkElement

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.getV3OrcidError(e);
            filteredElements.add(error);
        }
    }
    workBulk.setBulk(filteredElements);
}
Also used : OrcidError(org.orcid.jaxb.model.v3.dev1.error.OrcidError) BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) OrcidUnauthorizedException(org.orcid.core.exception.OrcidUnauthorizedException) ArrayList(java.util.ArrayList) NoResultException(javax.persistence.NoResultException) OrcidNotClaimedException(org.orcid.core.exception.OrcidNotClaimedException) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) WrongSourceException(org.orcid.core.exception.WrongSourceException) LockedException(org.orcid.core.security.aop.LockedException) AccessControlException(java.security.AccessControlException) OrcidAccessControlException(org.orcid.core.exception.OrcidAccessControlException) OrcidVisibilityException(org.orcid.core.exception.OrcidVisibilityException) OrcidUnauthorizedException(org.orcid.core.exception.OrcidUnauthorizedException) DeactivatedException(org.orcid.core.exception.DeactivatedException)

Example 20 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.getOrcidError(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.record_v2.WorkBulk) ArrayList(java.util.ArrayList) ExceedMaxNumberOfPutCodesException(org.orcid.core.exception.ExceedMaxNumberOfPutCodesException) PutCodeFormatException(org.orcid.core.exception.PutCodeFormatException)

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