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());
}
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);
}
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"));
}
}
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;
}
}
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;
}
Aggregations