Search in sources :

Example 21 with SourceEntity

use of org.orcid.persistence.jpa.entities.SourceEntity in project ORCID-Source by ORCID.

the class ActivityValidatorTest method validateDuplicatedExtIds_duplicatesFoundTest.

@SuppressWarnings("deprecation")
@Test(expected = OrcidDuplicatedActivityException.class)
public void validateDuplicatedExtIds_duplicatesFoundTest() {
    SourceEntity source1 = mock(SourceEntity.class);
    when(source1.getSourceName()).thenReturn("source name");
    when(source1.getSourceId()).thenReturn("APP-00000000000000");
    SourceClientId sourceClientId = new SourceClientId();
    sourceClientId.setPath("APP-00000000000000");
    Source source2 = mock(Source.class);
    when(source2.getSourceName()).thenReturn(new SourceName("source name"));
    when(source2.getSourceClientId()).thenReturn(sourceClientId);
    ExternalIDs extIds1 = getExternalIDs();
    ExternalIDs extIds2 = getExternalIDs();
    activityValidator.checkExternalIdentifiersForDuplicates(extIds1, extIds2, source2, source1);
}
Also used : ExternalIDs(org.orcid.jaxb.model.record_v2.ExternalIDs) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) SourceClientId(org.orcid.jaxb.model.common_v2.SourceClientId) SourceName(org.orcid.jaxb.model.common_v2.SourceName) Source(org.orcid.jaxb.model.common_v2.Source) Test(org.junit.Test)

Example 22 with SourceEntity

use of org.orcid.persistence.jpa.entities.SourceEntity in project ORCID-Source by ORCID.

the class Jaxb2JpaAdapterImpl method setExternalIdentifiers.

private void setExternalIdentifiers(ProfileEntity profileEntity, ExternalIdentifiers externalIdentifiers) {
    String sourceId = getSourceId();
    Set<ExternalIdentifierEntity> existingExternalIdentifierEntities = profileEntity.getExternalIdentifiers();
    Iterator<ExternalIdentifierEntity> existingIt = null;
    if (existingExternalIdentifierEntities != null) {
        existingIt = existingExternalIdentifierEntities.iterator();
    }
    //Iterate over the list of existing elements, to see which ones still exists but preserving all the ones where the calling client is not the source of
    if (existingIt != null) {
        while (existingIt.hasNext()) {
            ExternalIdentifierEntity existing = existingIt.next();
            String existingElementSource = existing.getElementSourceId();
            if (sourceId != null && !sourceId.equals(existingElementSource)) {
            //If am not the source of this element, do nothing
            } else {
                //If am the source, check if the element exists in the list of incoming elements
                Triplet<String, String, String> existingTriplet = createTripletForExternalIdentifier(existing);
                boolean found = false;
                if (externalIdentifiers != null && externalIdentifiers.getExternalIdentifier() != null) {
                    for (ExternalIdentifier newExternalIdentifier : externalIdentifiers.getExternalIdentifier()) {
                        Triplet<String, String, String> newExternalIdentifierTriplet = createTripletForExternalIdentifier(newExternalIdentifier);
                        if (Objects.equals(existingTriplet, newExternalIdentifierTriplet)) {
                            found = true;
                            break;
                        }
                    }
                }
                //If it doesn't exists, remove it from the existing elements
                if (!found) {
                    existingIt.remove();
                }
            }
        }
    }
    //Iterate over the list of all new ones and add the ones that doesn't exists yet
    if (externalIdentifiers != null && externalIdentifiers.getExternalIdentifier() != null) {
        for (ExternalIdentifier newExternalIdentifier : externalIdentifiers.getExternalIdentifier()) {
            boolean exists = false;
            Triplet<String, String, String> newExternalIdentifierTriplet = createTripletForExternalIdentifier(newExternalIdentifier);
            String sourceOfNewElement = newExternalIdentifier.getSource() == null ? null : newExternalIdentifier.getSource().retrieveSourcePath();
            if (existingExternalIdentifierEntities != null) {
                for (ExternalIdentifierEntity existingEntity : existingExternalIdentifierEntities) {
                    Triplet<String, String, String> existingTriplet = createTripletForExternalIdentifier(existingEntity);
                    String sourceOfExistingElement = existingEntity.getElementSourceId();
                    if (Objects.equals(sourceOfNewElement, sourceOfExistingElement) && Objects.equals(newExternalIdentifierTriplet, existingTriplet)) {
                        exists = true;
                        // visibility
                        if (profileEntity.getClaimed() == null || !profileEntity.getClaimed()) {
                            // Update the visibility of existing elements if
                            // the profile is not claimed
                            String existingVisibilityValue = existingEntity.getVisibility() == null ? null : existingEntity.getVisibility().value();
                            String listVisibilityValue = externalIdentifiers.getVisibility() == null ? null : externalIdentifiers.getVisibility().value();
                            if (listVisibilityValue != null && !Objects.equals(existingVisibilityValue, listVisibilityValue)) {
                                existingEntity.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(listVisibilityValue));
                            }
                        }
                        break;
                    }
                }
            }
            if (!exists) {
                if (existingExternalIdentifierEntities == null) {
                    existingExternalIdentifierEntities = new TreeSet<ExternalIdentifierEntity>();
                    profileEntity.setExternalIdentifiers(existingExternalIdentifierEntities);
                }
                ExternalIdentifierEntity newEntity = new ExternalIdentifierEntity();
                newEntity.setOwner(profileEntity);
                //Set source
                SourceEntity source = sourceManager.retrieveSourceEntity();
                setSource(source, newEntity);
                if (newExternalIdentifier.getExternalIdCommonName() != null) {
                    newEntity.setExternalIdCommonName(newExternalIdentifier.getExternalIdCommonName().getContent());
                }
                if (newExternalIdentifier.getExternalIdReference() != null) {
                    newEntity.setExternalIdReference(newExternalIdentifier.getExternalIdReference().getContent());
                }
                if (newExternalIdentifier.getExternalIdUrl() != null) {
                    newEntity.setExternalIdUrl(newExternalIdentifier.getExternalIdUrl().getValue());
                }
                newEntity.setVisibility(getDefaultVisibility(profileEntity, externalIdentifiers.getVisibility(), OrcidVisibilityDefaults.EXTERNAL_IDENTIFIER_DEFAULT));
                newEntity.setDisplayIndex(0L);
                for (ExternalIdentifierEntity tempEntity : existingExternalIdentifierEntities) tempEntity.setDisplayIndex(tempEntity.getDisplayIndex() + 1);
                existingExternalIdentifierEntities.add(newEntity);
            }
        }
    }
}
Also used : ExternalIdentifier(org.orcid.jaxb.model.message.ExternalIdentifier) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ExternalIdentifierEntity(org.orcid.persistence.jpa.entities.ExternalIdentifierEntity)

Example 23 with SourceEntity

use of org.orcid.persistence.jpa.entities.SourceEntity in project ORCID-Source by ORCID.

the class Jaxb2JpaAdapterImpl method setKeywords.

private void setKeywords(ProfileEntity profileEntity, Keywords keywords) {
    String sourceId = getSourceId();
    SortedSet<ProfileKeywordEntity> existingProfileKeywordEntities = profileEntity.getKeywords();
    Iterator<ProfileKeywordEntity> existingIt = null;
    if (existingProfileKeywordEntities != null) {
        existingIt = existingProfileKeywordEntities.iterator();
    }
    //Iterate over the list of existing elements, to see which ones still exists but preserving all the ones where the calling client is not the source of
    if (existingIt != null) {
        while (existingIt.hasNext()) {
            ProfileKeywordEntity existing = existingIt.next();
            String existingElementSource = existing.getElementSourceId();
            if (sourceId != null && !sourceId.equals(existingElementSource)) {
            //If am not the source of this element, do nothing
            } else {
                //If am the source, check if the element exists in the list of incoming elements
                String value = existing.getKeywordName();
                boolean found = false;
                if (keywords != null && keywords.getKeyword() != null) {
                    for (Keyword newKeyword : keywords.getKeyword()) {
                        if (Objects.equals(value, newKeyword.getContent())) {
                            found = true;
                            break;
                        }
                    }
                }
                //If it doesn't exists, remove it from the existing elements
                if (!found) {
                    existingIt.remove();
                }
            }
        }
    }
    //Iterate over the list of all new ones and add the ones that doesn't exists yet
    if (keywords != null && keywords.getKeyword() != null) {
        for (Keyword newKeyword : keywords.getKeyword()) {
            boolean exists = false;
            if (existingProfileKeywordEntities != null) {
                for (ProfileKeywordEntity existingEntity : existingProfileKeywordEntities) {
                    if (Objects.equals(newKeyword.getContent(), existingEntity.getKeywordName())) {
                        exists = true;
                        //If the profile is not claimed, you can update the visibility
                        if (profileEntity.getClaimed() == null || !profileEntity.getClaimed()) {
                            //Update the visibility of existing elements if the profile is not claimed
                            String existingVisibilityValue = existingEntity.getVisibility() == null ? null : existingEntity.getVisibility().value();
                            String listVisibilityValue = keywords.getVisibility() == null ? null : keywords.getVisibility().value();
                            if (listVisibilityValue != null && !Objects.equals(existingVisibilityValue, listVisibilityValue)) {
                                existingEntity.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(listVisibilityValue));
                            }
                        }
                        break;
                    }
                }
            }
            if (!exists) {
                if (existingProfileKeywordEntities == null) {
                    existingProfileKeywordEntities = new TreeSet<ProfileKeywordEntity>();
                    profileEntity.setKeywords(existingProfileKeywordEntities);
                }
                ProfileKeywordEntity newEntity = new ProfileKeywordEntity();
                newEntity.setProfile(profileEntity);
                //Set source
                SourceEntity source = sourceManager.retrieveSourceEntity();
                setSource(source, newEntity);
                newEntity.setKeywordName(newKeyword.getContent());
                newEntity.setVisibility(getDefaultVisibility(profileEntity, keywords.getVisibility(), OrcidVisibilityDefaults.KEYWORD_DEFAULT));
                newEntity.setDisplayIndex(0L);
                for (ProfileKeywordEntity tempEntity : existingProfileKeywordEntities) tempEntity.setDisplayIndex(tempEntity.getDisplayIndex() + 1);
                existingProfileKeywordEntities.add(newEntity);
            }
        }
    }
}
Also used : ProfileKeywordEntity(org.orcid.persistence.jpa.entities.ProfileKeywordEntity) Keyword(org.orcid.jaxb.model.message.Keyword) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity)

Example 24 with SourceEntity

use of org.orcid.persistence.jpa.entities.SourceEntity in project ORCID-Source by ORCID.

the class Jaxb2JpaAdapterImpl method setOtherNames.

private void setOtherNames(ProfileEntity profileEntity, OtherNames otherNames) {
    String sourceId = getSourceId();
    SortedSet<OtherNameEntity> existingOtherNameEntities = profileEntity.getOtherNames();
    Iterator<OtherNameEntity> existingIt = null;
    if (existingOtherNameEntities != null) {
        existingIt = existingOtherNameEntities.iterator();
    }
    //Iterate over the list of existing elements, to see which ones still exists but preserving all the ones where the calling client is not the source of
    if (existingIt != null) {
        while (existingIt.hasNext()) {
            OtherNameEntity existing = existingIt.next();
            String existingElementSource = existing.getElementSourceId();
            if (sourceId != null && !sourceId.equals(existingElementSource)) {
            //If am not the source of this element, do nothing
            } else {
                //If am the source, check if the element exists in the list of incoming elements
                String value = existing.getDisplayName();
                boolean found = false;
                if (otherNames != null && otherNames.getOtherName() != null) {
                    for (OtherName newOtherName : otherNames.getOtherName()) {
                        if (Objects.equals(value, newOtherName.getContent())) {
                            found = true;
                            break;
                        }
                    }
                }
                //If it doesn't exists, remove it from the existing elements
                if (!found) {
                    existingIt.remove();
                }
            }
        }
    }
    //Iterate over the list of all new ones and add the ones that doesn't exists yet
    if (otherNames != null && otherNames.getOtherName() != null) {
        for (OtherName newOtherName : otherNames.getOtherName()) {
            boolean exists = false;
            if (existingOtherNameEntities != null) {
                for (OtherNameEntity existingEntity : existingOtherNameEntities) {
                    if (Objects.equals(newOtherName.getContent(), existingEntity.getDisplayName())) {
                        exists = true;
                        //If the profile is not claimed, you can update the visibility
                        if (profileEntity.getClaimed() == null || !profileEntity.getClaimed()) {
                            //Update the visibility of existing elements if the profile is not claimed
                            String existingVisibilityValue = existingEntity.getVisibility() == null ? null : existingEntity.getVisibility().value();
                            String listVisibilityValue = otherNames.getVisibility() == null ? null : otherNames.getVisibility().value();
                            if (listVisibilityValue != null && !Objects.equals(existingVisibilityValue, listVisibilityValue)) {
                                existingEntity.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(listVisibilityValue));
                            }
                        }
                        break;
                    }
                }
            }
            if (!exists) {
                if (existingOtherNameEntities == null) {
                    existingOtherNameEntities = new TreeSet<OtherNameEntity>();
                    profileEntity.setOtherNames(existingOtherNameEntities);
                }
                OtherNameEntity newEntity = new OtherNameEntity();
                newEntity.setProfile(profileEntity);
                //Set source
                SourceEntity source = sourceManager.retrieveSourceEntity();
                setSource(source, newEntity);
                newEntity.setDisplayName(newOtherName.getContent());
                newEntity.setVisibility(getDefaultVisibility(profileEntity, otherNames.getVisibility(), OrcidVisibilityDefaults.OTHER_NAMES_DEFAULT));
                newEntity.setDisplayIndex(0L);
                for (OtherNameEntity tempEntity : existingOtherNameEntities) tempEntity.setDisplayIndex(tempEntity.getDisplayIndex() + 1);
                existingOtherNameEntities.add(newEntity);
            }
        }
    }
}
Also used : SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) OtherName(org.orcid.jaxb.model.message.OtherName) OtherNameEntity(org.orcid.persistence.jpa.entities.OtherNameEntity)

Example 25 with SourceEntity

use of org.orcid.persistence.jpa.entities.SourceEntity in project ORCID-Source by ORCID.

the class Jpa2JaxbAdapterImpl method getSponsor.

private Source getSponsor(ProfileEntity profileEntity) {
    SourceEntity sourceEntity = profileEntity.getSource();
    if (sourceEntity != null) {
        Source sponsor = new Source();
        SourceName sponsorName = new SourceName(sourceEntity.getSourceName());
        sponsor.setSourceName(sponsorName);
        ClientDetailsEntity sourceClient = sourceEntity.getSourceClient();
        if (sourceClient != null && !OrcidStringUtils.isValidOrcid(sourceClient.getClientId())) {
            SourceClientId sourceClientId = new SourceClientId(getOrcidIdBase(sourceClient.getId()));
            sponsor.setSourceClientId(sourceClientId);
        } else {
            SourceOrcid sponsorOrcid = StringUtils.isNotBlank(sourceEntity.getSourceId()) ? new SourceOrcid(getOrcidIdBase(sourceEntity.getSourceId())) : null;
            sponsor.setSourceOrcid(sponsorOrcid);
        }
        return sponsor;
    }
    return null;
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity)

Aggregations

SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)111 Test (org.junit.Test)58 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)57 BaseTest (org.orcid.core.BaseTest)44 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)28 Date (java.util.Date)19 HashMap (java.util.HashMap)15 OrcidDuplicatedElementException (org.orcid.core.exception.OrcidDuplicatedElementException)14 Work (org.orcid.jaxb.model.record_v2.Work)14 OrgEntity (org.orcid.persistence.jpa.entities.OrgEntity)13 Visibility (org.orcid.jaxb.model.common_v2.Visibility)12 PeerReview (org.orcid.jaxb.model.record_v2.PeerReview)11 Transactional (org.springframework.transaction.annotation.Transactional)11 Funding (org.orcid.jaxb.model.record_v2.Funding)10 ExternalID (org.orcid.jaxb.model.record_v2.ExternalID)6 ExternalIDs (org.orcid.jaxb.model.record_v2.ExternalIDs)6 OrgAffiliationRelationEntity (org.orcid.persistence.jpa.entities.OrgAffiliationRelationEntity)6 PeerReviewEntity (org.orcid.persistence.jpa.entities.PeerReviewEntity)6 WorkEntity (org.orcid.persistence.jpa.entities.WorkEntity)6 GroupIdRecord (org.orcid.jaxb.model.groupid_v2.GroupIdRecord)5