Search in sources :

Example 11 with Source

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

the class OrcidProfileManagerImplTest method testPreventDuplicatedWorks.

@Test
@Transactional
@Rollback(true)
public void testPreventDuplicatedWorks() {
    OrcidWork work1 = createWork1();
    OrcidWork work2 = createWork2();
    OrcidWork work3 = createWork3();
    OrcidProfile profile = createBasicProfile();
    profile = orcidProfileManager.createOrcidProfile(profile, false, false);
    assertNotNull(profile);
    assertNotNull(profile.getOrcidActivities());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks().getOrcidWork());
    assertEquals(1, profile.getOrcidActivities().getOrcidWorks().getOrcidWork().size());
    assertEquals(work1.getWorkTitle().getTitle().getContent(), profile.getOrcidActivities().getOrcidWorks().getOrcidWork().get(0).getWorkTitle().getTitle().getContent());
    profile.getOrcidActivities().getOrcidWorks().getOrcidWork().add(work2);
    orcidProfileManager.addOrcidWorks(profile);
    profile = orcidProfileManager.retrieveOrcidProfile(TEST_ORCID);
    boolean found1 = false;
    boolean found2 = false;
    boolean found3 = false;
    assertNotNull(profile);
    assertNotNull(profile.getOrcidActivities());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks().getOrcidWork());
    assertEquals(2, profile.getOrcidActivities().getOrcidWorks().getOrcidWork().size());
    for (OrcidWork work : profile.getOrcidActivities().getOrcidWorks().getOrcidWork()) {
        String workTitle = work.getWorkTitle().getTitle().getContent();
        if (workTitle.equals(work1.getWorkTitle().getTitle().getContent())) {
            found1 = true;
        } else if (workTitle.equals(work2.getWorkTitle().getTitle().getContent())) {
            found2 = true;
        } else if (workTitle.equals(work3.getWorkTitle().getTitle().getContent())) {
            found3 = true;
        }
    }
    assertTrue(found1);
    assertTrue(found2);
    assertFalse(found3);
    // Add work # 3 and duplicate other works
    profile.getOrcidActivities().getOrcidWorks().getOrcidWork().add(work1);
    profile.getOrcidActivities().getOrcidWorks().getOrcidWork().add(work2);
    profile.getOrcidActivities().getOrcidWorks().getOrcidWork().add(work3);
    orcidProfileManager.addOrcidWorks(profile);
    profile = orcidProfileManager.retrieveOrcidProfile(TEST_ORCID);
    // Work 3 was added and work 1 and 2 where not added twice
    assertNotNull(profile);
    assertNotNull(profile.getOrcidActivities());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks().getOrcidWork());
    assertEquals(3, profile.getOrcidActivities().getOrcidWorks().getOrcidWork().size());
    found1 = false;
    found2 = false;
    found3 = false;
    for (OrcidWork work : profile.getOrcidActivities().getOrcidWorks().getOrcidWork()) {
        String workTitle = work.getWorkTitle().getTitle().getContent();
        if (workTitle.equals(work1.getWorkTitle().getTitle().getContent())) {
            found1 = true;
        } else if (workTitle.equals(work2.getWorkTitle().getTitle().getContent())) {
            found2 = true;
        } else if (workTitle.equals(work3.getWorkTitle().getTitle().getContent())) {
            found3 = true;
        }
    }
    assertTrue(found1);
    assertTrue(found2);
    assertTrue(found3);
    // Duplicate all works
    profile.getOrcidActivities().getOrcidWorks().getOrcidWork().add(work1);
    profile.getOrcidActivities().getOrcidWorks().getOrcidWork().add(work2);
    profile.getOrcidActivities().getOrcidWorks().getOrcidWork().add(work3);
    orcidProfileManager.addOrcidWorks(profile);
    profile = orcidProfileManager.retrieveOrcidProfile(TEST_ORCID);
    // No new works are added and no duplicated was allowed
    assertNotNull(profile);
    assertNotNull(profile.getOrcidActivities());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks().getOrcidWork());
    assertEquals(3, profile.getOrcidActivities().getOrcidWorks().getOrcidWork().size());
    found1 = false;
    found2 = false;
    found3 = false;
    for (OrcidWork work : profile.getOrcidActivities().getOrcidWorks().getOrcidWork()) {
        String workTitle = work.getWorkTitle().getTitle().getContent();
        if (workTitle.equals(work1.getWorkTitle().getTitle().getContent())) {
            found1 = true;
        } else if (workTitle.equals(work2.getWorkTitle().getTitle().getContent())) {
            found2 = true;
        } else if (workTitle.equals(work3.getWorkTitle().getTitle().getContent())) {
            found3 = true;
        }
    }
    assertTrue(found1);
    assertTrue(found2);
    assertTrue(found3);
    //Test using work source
    Source source = new Source();
    source.setSourceOrcid(new SourceOrcid(APPLICATION_ORCID));
    work1.setSource(source);
    // Add work1 again, since it have a different source, it should be added
    profile.getOrcidActivities().getOrcidWorks().getOrcidWork().add(work1);
    orcidProfileManager.addOrcidWorks(profile);
    profile = orcidProfileManager.retrieveOrcidProfile(TEST_ORCID);
    assertNotNull(profile);
    assertNotNull(profile.getOrcidActivities());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks().getOrcidWork());
    assertEquals(4, profile.getOrcidActivities().getOrcidWorks().getOrcidWork().size());
    //Add work1 again, and it should not be added, since is duplicated
    profile.getOrcidActivities().getOrcidWorks().getOrcidWork().add(work1);
    orcidProfileManager.addOrcidWorks(profile);
    profile = orcidProfileManager.retrieveOrcidProfile(TEST_ORCID);
    assertNotNull(profile);
    assertNotNull(profile.getOrcidActivities());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks());
    assertNotNull(profile.getOrcidActivities().getOrcidWorks().getOrcidWork());
    assertEquals(4, profile.getOrcidActivities().getOrcidWorks().getOrcidWork().size());
}
Also used : OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) OrcidWork(org.orcid.jaxb.model.message.OrcidWork) SourceOrcid(org.orcid.jaxb.model.message.SourceOrcid) Source(org.orcid.jaxb.model.message.Source) Test(org.junit.Test) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with Source

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

the class T2OrcidApiServiceVersionedDelegatorTest method testCreateWithAffiliations.

@Test
public void testCreateWithAffiliations() throws JAXBException {
    setUpSecurityContextForClientOnly();
    OrcidMessage orcidMessage = getOrcidMessage("/orcid-message-for-create-latest.xml");
    Response createResponse = t2OrcidApiServiceDelegatorLatest.createProfile(mockedUriInfo, orcidMessage);
    assertNotNull(createResponse);
    assertEquals(HttpStatus.SC_CREATED, createResponse.getStatus());
    String location = ((URI) createResponse.getMetadata().getFirst("Location")).getPath();
    assertNotNull(location);
    String orcid = location.substring(1, 20);
    Response readResponse = t2OrcidApiServiceDelegatorLatest.findFullDetails(orcid);
    assertNotNull(readResponse);
    assertEquals(HttpStatus.SC_OK, readResponse.getStatus());
    OrcidMessage retrievedMessage = (OrcidMessage) readResponse.getEntity();
    OrcidProfile orcidProfile = retrievedMessage.getOrcidProfile();
    assertEquals(orcid, orcidProfile.getOrcidIdentifier().getPath());
    Affiliations affiliations = orcidProfile.retrieveAffiliations();
    assertNotNull(affiliations);
    assertEquals(1, affiliations.getAffiliation().size());
    Affiliation affiliation = affiliations.getAffiliation().get(0);
    assertEquals(Visibility.PRIVATE, affiliation.getVisibility());
    Source source = affiliation.getSource();
    assertNotNull(source);
    String sourceOrcid = source.retrieveSourcePath();
    assertNotNull(sourceOrcid);
    assertEquals("APP-5555555555555555", sourceOrcid);
}
Also used : Response(javax.ws.rs.core.Response) OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) Affiliations(org.orcid.jaxb.model.message.Affiliations) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) URI(java.net.URI) Source(org.orcid.jaxb.model.message.Source) Affiliation(org.orcid.jaxb.model.message.Affiliation) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 13 with Source

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

the class T2OrcidApiServiceDelegatorImpl method addExternalIdentifiers.

/**
 * Add new external identifiers to the profile. As with all calls, if the
 * message contains any other elements, a 400 Bad Request will be returned.
 *
 * @param orcidMessage
 *            the message congtaining the external ids
 * @return If successful, returns a 200 OK with the updated content.
 */
@Override
@AccessControl(requiredScope = ScopePathType.ORCID_BIO_EXTERNAL_IDENTIFIERS_CREATE)
public Response addExternalIdentifiers(UriInfo uriInfo, String orcid, OrcidMessage orcidMessage) {
    OrcidProfile orcidProfile = orcidMessage.getOrcidProfile();
    try {
        ExternalIdentifiers updatedExternalIdentifiers = orcidProfile.getOrcidBio().getExternalIdentifiers();
        // Get the client profile information
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String clientId = null;
        if (OAuth2Authentication.class.isAssignableFrom(authentication.getClass())) {
            OAuth2Request authorizationRequest = ((OAuth2Authentication) authentication).getOAuth2Request();
            clientId = authorizationRequest.getClientId();
        }
        for (ExternalIdentifier ei : updatedExternalIdentifiers.getExternalIdentifier()) {
            // Set the client profile to each external identifier
            if (ei.getSource() == null) {
                Source source = new Source();
                source.setSourceClientId(new SourceClientId(clientId));
                ei.setSource(source);
            } else {
                // Check if the provided external orcid exists
                Source source = ei.getSource();
                String sourceOrcid = source.retrieveSourcePath();
                if (sourceOrcid != null) {
                    if (StringUtils.isBlank(sourceOrcid) || (!profileEntityManager.orcidExists(sourceOrcid) && !clientDetailsManager.exists(sourceOrcid))) {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("orcid", sourceOrcid);
                        throw new OrcidNotFoundException(params);
                    }
                }
            }
        }
        orcidProfile = orcidProfileManager.addExternalIdentifiers(orcidProfile);
        return getOrcidMessageResponse(orcidProfile, orcid);
    } catch (DataAccessException e) {
        throw new OrcidBadRequestException(localeManager.resolveMessage("apiError.badrequest_createorcid.exception"));
    }
}
Also used : ExternalIdentifier(org.orcid.jaxb.model.message.ExternalIdentifier) HashMap(java.util.HashMap) SourceClientId(org.orcid.jaxb.model.message.SourceClientId) Source(org.orcid.jaxb.model.message.Source) OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OrcidBadRequestException(org.orcid.core.exception.OrcidBadRequestException) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OrcidNotFoundException(org.orcid.core.exception.OrcidNotFoundException) ExternalIdentifiers(org.orcid.jaxb.model.message.ExternalIdentifiers) DataAccessException(org.springframework.dao.DataAccessException) AccessControl(org.orcid.core.security.visibility.aop.AccessControl)

Example 14 with Source

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

the class VisibilityFilterImpl method filter.

/**
 * Remove the elements that are not present in the list of set of
 * {@link org.orcid.jaxb.model.message .Visibility}s present in the array
 * passed in.
 *
 * @param messageToBeFiltered
 *            the {@link org.orcid.jaxb.model.message.OrcidMessage} that
 *            will be traversed looking for
 *            {@link org .orcid.jaxb.model.message.VisibilityType} elements.
 * @param source
 *            The orcid source that is executing the request
 * @param removeAttribute
 *            should all {@link org.orcid.jaxb.model.message.Visibility}
 *            elements be removed from the object graph. This has the effect
 *            that they will not be present in the resulting JAXB
 *            serialisation.
 * @param visibilities
 *            What {@link org.orcid.jaxb.model.message.Visibility} elements
 *            should be allowed.
 * @return the cleansed {@link org.orcid.jaxb.model.message.OrcidMessage}
 */
@Override
public OrcidMessage filter(OrcidMessage messageToBeFiltered, final String sourceId, final boolean allowPrivateWorks, final boolean allowPrivateFunding, final boolean allowPrivateAffiliations, Visibility... visibilities) {
    if (messageToBeFiltered == null || visibilities == null || visibilities.length == 0) {
        return null;
    }
    String messageIdForLog = getMessageIdForLog(messageToBeFiltered);
    LOGGER.debug("About to filter message: " + messageIdForLog);
    final Set<Visibility> visibilitySet = new HashSet<Visibility>(Arrays.asList(visibilities));
    if (visibilitySet.contains(Visibility.SYSTEM)) {
        return messageToBeFiltered;
    } else {
        TreeCleaner treeCleaner = new TreeCleaner();
        treeCleaner.clean(messageToBeFiltered, new TreeCleaningStrategy() {

            public TreeCleaningDecision needsStripping(Object obj) {
                TreeCleaningDecision decision = TreeCleaningDecision.DEFAULT;
                if (obj != null) {
                    Class<?> clazz = obj.getClass();
                    if (!PojoUtil.isEmpty(sourceId)) {
                        if (allowPrivateAffiliations && Affiliation.class.isAssignableFrom(clazz)) {
                            Affiliation affiliation = (Affiliation) obj;
                            Source source = affiliation.getSource();
                            if (source != null) {
                                String sourcePath = source.retrieveSourcePath();
                                if (sourcePath != null) {
                                    if (sourceId.equals(sourcePath)) {
                                        decision = TreeCleaningDecision.IGNORE;
                                    }
                                }
                            }
                        } else if (allowPrivateFunding && Funding.class.isAssignableFrom(clazz)) {
                            Funding funding = (Funding) obj;
                            Source source = funding.getSource();
                            if (source != null) {
                                String sourcePath = source.retrieveSourcePath();
                                if (sourcePath != null) {
                                    if (sourceId.equals(sourcePath)) {
                                        decision = TreeCleaningDecision.IGNORE;
                                    }
                                }
                            }
                        } else if (allowPrivateWorks && OrcidWork.class.isAssignableFrom(clazz)) {
                            OrcidWork work = (OrcidWork) obj;
                            Source source = work.getSource();
                            if (source != null) {
                                if (sourceId.equals(source.retrieveSourcePath())) {
                                    decision = TreeCleaningDecision.IGNORE;
                                }
                            }
                        }
                    }
                    // fields are inside the country element
                    if (Address.class.isAssignableFrom(clazz)) {
                        Address address = (Address) obj;
                        // Remove empty addresses
                        if (address.getCountry() == null) {
                            decision = TreeCleaningDecision.CLEANING_REQUIRED;
                        } else {
                            Country country = address.getCountry();
                            // Allow public addresses
                            if (Visibility.PUBLIC.equals(country.getVisibility())) {
                                decision = TreeCleaningDecision.IGNORE;
                            } else if (visibilitySet.contains(Visibility.LIMITED)) {
                                // Allow limited visibility when possible
                                if (Visibility.LIMITED.equals(country.getVisibility())) {
                                    decision = TreeCleaningDecision.IGNORE;
                                } else {
                                    // As last resource, check the source
                                    Source source = country.getSource();
                                    if (source != null && sourceId != null && sourceId.equals(source.retrieveSourcePath())) {
                                        decision = TreeCleaningDecision.IGNORE;
                                    } else {
                                        decision = TreeCleaningDecision.CLEANING_REQUIRED;
                                    }
                                }
                            }
                        }
                    }
                    if (Email.class.isAssignableFrom(clazz)) {
                        // include all emails if present
                        try {
                            Authentication authentication = getAuthentication();
                            if (authentication != null && messageToBeFiltered.getOrcidProfile() != null) {
                                permissionChecker.checkPermissions(getAuthentication(), ScopePathType.EMAIL_READ_PRIVATE, messageToBeFiltered.getOrcidProfile().retrieveOrcidPath());
                                decision = TreeCleaningDecision.IGNORE;
                            }
                        } catch (AccessControlException e) {
                        // private email can't be read, do nothing here
                        }
                    }
                    // that implements PrivateVisibleToSource
                    if (sourceId != null)
                        if (PrivateVisibleToSource.class.isAssignableFrom(clazz) && visibilitySet.contains(Visibility.LIMITED)) {
                            Source source = ((PrivateVisibleToSource) obj).getSource();
                            if (source != null) {
                                if (sourceId.equals(source.retrieveSourcePath())) {
                                    decision = TreeCleaningDecision.IGNORE;
                                }
                            }
                        }
                    if (TreeCleaningDecision.DEFAULT.equals(decision)) {
                        if (WorkContributors.class.isAssignableFrom(clazz)) {
                            decision = TreeCleaningDecision.IGNORE;
                        } else if (VisibilityType.class.isAssignableFrom(clazz)) {
                            VisibilityType visibilityType = (VisibilityType) obj;
                            if ((visibilityType.getVisibility() == null || !visibilitySet.contains(visibilityType.getVisibility()))) {
                                decision = TreeCleaningDecision.CLEANING_REQUIRED;
                            }
                        }
                    }
                }
                return decision;
            }
        });
        OrcidProfile orcidProfile = messageToBeFiltered.getOrcidProfile();
        if (orcidProfile != null) {
            orcidProfile.setOrcidInternal(null);
        }
        LOGGER.debug("Finished filtering message: " + messageIdForLog);
        return messageToBeFiltered;
    }
}
Also used : Email(org.orcid.jaxb.model.message.Email) Address(org.orcid.jaxb.model.message.Address) Funding(org.orcid.jaxb.model.message.Funding) WorkContributors(org.orcid.jaxb.model.message.WorkContributors) TreeCleaner(org.orcid.core.tree.TreeCleaner) OrcidWork(org.orcid.jaxb.model.message.OrcidWork) AccessControlException(java.security.AccessControlException) PrivateVisibleToSource(org.orcid.jaxb.model.message.PrivateVisibleToSource) Source(org.orcid.jaxb.model.message.Source) TreeCleaningDecision(org.orcid.core.tree.TreeCleaningDecision) OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) PrivateVisibleToSource(org.orcid.jaxb.model.message.PrivateVisibleToSource) VisibilityType(org.orcid.jaxb.model.message.VisibilityType) Authentication(org.springframework.security.core.Authentication) Country(org.orcid.jaxb.model.message.Country) Visibility(org.orcid.jaxb.model.message.Visibility) TreeCleaningStrategy(org.orcid.core.tree.TreeCleaningStrategy) HashSet(java.util.HashSet) Affiliation(org.orcid.jaxb.model.message.Affiliation)

Example 15 with Source

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

the class RegistrationManagerImplTest method createBasicProfile.

private OrcidProfile createBasicProfile(String email, boolean claimed, String sourceId) {
    OrcidProfile profile = new OrcidProfile();
    profile.setPassword("password");
    profile.setVerificationCode("1234");
    OrcidBio bio = new OrcidBio();
    ContactDetails contactDetails = new ContactDetails();
    contactDetails.addOrReplacePrimaryEmail(new Email(email));
    bio.setContactDetails(contactDetails);
    profile.setOrcidBio(bio);
    PersonalDetails personalDetails = new PersonalDetails();
    bio.setPersonalDetails(personalDetails);
    personalDetails.setGivenNames(new GivenNames("New"));
    personalDetails.setFamilyName(new FamilyName("User"));
    OrcidHistory orcidHistory = new OrcidHistory();
    orcidHistory.setClaimed(new Claimed(claimed));
    orcidHistory.setCreationMethod(CreationMethod.DIRECT);
    orcidHistory.setSubmissionDate(new SubmissionDate(DateUtils.convertToXMLGregorianCalendar(new Date())));
    profile.setOrcidHistory(orcidHistory);
    // Set the source
    profile.getOrcidHistory().setSource(new Source(sourceId));
    return profile;
}
Also used : OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) Email(org.orcid.jaxb.model.message.Email) OrcidBio(org.orcid.jaxb.model.message.OrcidBio) ContactDetails(org.orcid.jaxb.model.message.ContactDetails) FamilyName(org.orcid.jaxb.model.message.FamilyName) GivenNames(org.orcid.jaxb.model.message.GivenNames) OrcidHistory(org.orcid.jaxb.model.message.OrcidHistory) PersonalDetails(org.orcid.jaxb.model.message.PersonalDetails) SubmissionDate(org.orcid.jaxb.model.message.SubmissionDate) Claimed(org.orcid.jaxb.model.message.Claimed) Date(java.util.Date) SubmissionDate(org.orcid.jaxb.model.message.SubmissionDate) Source(org.orcid.jaxb.model.message.Source)

Aggregations

Source (org.orcid.jaxb.model.message.Source)30 OrcidWork (org.orcid.jaxb.model.message.OrcidWork)14 OrcidProfile (org.orcid.jaxb.model.message.OrcidProfile)11 SourceOrcid (org.orcid.jaxb.model.message.SourceOrcid)10 SubmissionDate (org.orcid.jaxb.model.message.SubmissionDate)9 Title (org.orcid.jaxb.model.message.Title)9 FundingTitle (org.orcid.jaxb.model.message.FundingTitle)8 OrcidHistory (org.orcid.jaxb.model.message.OrcidHistory)8 Test (org.junit.Test)7 Subtitle (org.orcid.jaxb.model.message.Subtitle)7 WorkExternalIdentifier (org.orcid.jaxb.model.message.WorkExternalIdentifier)7 Transactional (org.springframework.transaction.annotation.Transactional)7 Date (java.util.Date)6 WorkTitle (org.orcid.jaxb.model.message.WorkTitle)6 HashMap (java.util.HashMap)5 Claimed (org.orcid.jaxb.model.message.Claimed)5 Email (org.orcid.jaxb.model.message.Email)5 ExternalIdentifier (org.orcid.jaxb.model.message.ExternalIdentifier)5 OrcidWorks (org.orcid.jaxb.model.message.OrcidWorks)5 SourceClientId (org.orcid.jaxb.model.message.SourceClientId)5