Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If a record was member created and the member added activities to it … #6887

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ProfileEntityManagerReadOnly extends ManagerReadOnlyBase {
String getLockedReason(String orcid);

Boolean isOrcidValidAsDelegate(String orcid);

Boolean haveMemberPushedWorksOrAffiliationsToRecord(String orcid, String clientId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.orcid.core.manager.v3.read_only.ProfileEntityManagerReadOnly;
import org.orcid.persistence.dao.ProfileDao;
import org.orcid.persistence.jpa.entities.ProfileEntity;
import org.orcid.pojo.ajaxForm.PojoUtil;

public class ProfileEntityManagerReadOnlyImpl extends ManagerReadOnlyBaseImpl implements ProfileEntityManagerReadOnly {

Expand Down Expand Up @@ -58,5 +59,13 @@ public String getLockedReason(String orcid) {
@Override
public Boolean isOrcidValidAsDelegate(String orcid) {
return profileDao.isOrcidValidAsDelegate(orcid);
}
}

@Override
public Boolean haveMemberPushedWorksOrAffiliationsToRecord(String orcid, String clientId) {
if(PojoUtil.isEmpty(orcid) || PojoUtil.isEmpty(clientId)) {
return false;
}
return profileDao.haveMemberPushedWorksOrAffiliationsToRecord(orcid, clientId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,6 @@ public interface ProfileDao extends GenericDao<ProfileEntity, String> {
public void resetSigninLock(String orcid);

public void updateSigninLock(String orcid, Integer count);

boolean haveMemberPushedWorksOrAffiliationsToRecord(String orcid, String clientId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -809,5 +809,21 @@ public void updateSigninLock(String orcid, Integer count) {
query.executeUpdate();
return;
}

public boolean haveMemberPushedWorksOrAffiliationsToRecord(String orcid, String clientId) {
try {
String queryString = "select p.orcid from profile p where p.orcid = :orcid and ( exists (select 1 from work w where w.orcid = p.orcid and w.client_source_id = :clientId) or exists (select 1 from org_affiliation_relation o where o.orcid = p.orcid and o.client_source_id = :clientId));";
Query query = entityManager.createNativeQuery(queryString);
query.setParameter("orcid", orcid);
query.setParameter("clientId", clientId);
String result = (String) query.getSingleResult();
if(orcid.equals(result)) {
return true;
}
} catch(NoResultException nre) {
return false;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.orcid.core.manager.v3.read_only.GroupIdRecordManagerReadOnly;
import org.orcid.core.manager.v3.read_only.PeerReviewManagerReadOnly;
import org.orcid.core.manager.v3.read_only.PersonalDetailsManagerReadOnly;
import org.orcid.core.manager.v3.read_only.ProfileEntityManagerReadOnly;
import org.orcid.core.manager.v3.read_only.ProfileFundingManagerReadOnly;
import org.orcid.core.manager.v3.read_only.ResearchResourceManagerReadOnly;
import org.orcid.core.manager.v3.read_only.WorkManagerReadOnly;
Expand Down Expand Up @@ -44,6 +45,7 @@
import org.orcid.jaxb.model.v3.release.record.summary.PeerReviews;
import org.orcid.persistence.jpa.entities.CountryIsoEntity;
import org.orcid.persistence.jpa.entities.ProfileEntity;
import org.orcid.persistence.jpa.entities.SourceEntity;
import org.orcid.pojo.OrgDisambiguated;
import org.orcid.pojo.PeerReviewMinimizedSummary;
import org.orcid.pojo.PublicRecordPersonDetails;
Expand Down Expand Up @@ -147,6 +149,9 @@ public class PublicProfileController extends BaseWorkspaceController {

@Value("${org.orcid.core.work.contributors.ui.max:50}")
private int maxContributorsForUI;

@Resource(name = "profileEntityManagerReadOnlyV3")
private ProfileEntityManagerReadOnly profileEntityManagerReadOnly;

public static int ORCID_HASH_LENGTH = 8;
private static final String PAGE_SIZE_DEFAULT = "50";
Expand Down Expand Up @@ -269,8 +274,22 @@ private boolean isRecordReadyForIndexing(ProfileEntity profile) {

// False if it is not reviewed and doesn't have any integration
if(!profile.isReviewed()) {
if (!orcidOauth2TokenService.hasToken(profile.getId(), getLastModifiedTime(profile.getId()))) {
return false;
String userOrcid = profile.getId();
if (!orcidOauth2TokenService.hasToken(userOrcid, getLastModifiedTime(userOrcid))) {
// If the user doesn't have any token, check if it was created by member, if so,
// verify if that member pushed any work of affiliation on creation time
SourceEntity source = profile.getSource();
if(source != null) {
// If it was created by a member, verify if it have any activity that belongs to that member
String clientId = source.getSourceClient() == null ? null : source.getSourceClient().getId();
if(profileEntityManagerReadOnly.haveMemberPushedWorksOrAffiliationsToRecord(userOrcid, clientId)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

Expand All @@ -31,6 +32,7 @@
import org.orcid.core.locale.LocaleManager;
import org.orcid.core.manager.EncryptionManager;
import org.orcid.core.manager.ProfileEntityCacheManager;
import org.orcid.core.manager.v3.read_only.ProfileEntityManagerReadOnly;
import org.orcid.core.oauth.OrcidOauth2TokenDetailService;
import org.orcid.jaxb.model.common.Iso3166Country;
import org.orcid.jaxb.model.v3.release.common.Visibility;
Expand All @@ -42,7 +44,9 @@
import org.orcid.jaxb.model.v3.release.record.PersonExternalIdentifier;
import org.orcid.jaxb.model.v3.release.record.ResearcherUrl;
import org.orcid.persistence.dao.ProfileDao;
import org.orcid.persistence.jpa.entities.ClientDetailsEntity;
import org.orcid.persistence.jpa.entities.ProfileEntity;
import org.orcid.persistence.jpa.entities.SourceEntity;
import org.orcid.pojo.PublicRecordPersonDetails;
import org.orcid.pojo.ajaxForm.AffiliationGroupContainer;
import org.orcid.pojo.ajaxForm.AffiliationGroupForm;
Expand All @@ -51,6 +55,7 @@
import org.orcid.test.TargetProxyHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.server.MethodNotAllowedException;
import org.springframework.web.servlet.ModelAndView;

/**
Expand All @@ -75,6 +80,8 @@ public class PublicProfileControllerTest extends DBUnitTest {
String reviewedWithIntegrationsOrcid = "0009-0000-0000-0002";
String unreviewedNoIntegrationsOrcid = "0009-0000-0000-0003";
String unreviewedWithIntegrationsOrcid = "0009-0000-0000-0004";
String unreviewedCreatedByMembersWithActivitiesOrcid = "0009-0000-0000-0005";
String unreviewedCreatedByMembersWithNoActivitiesOrcid = "0009-0000-0000-0006";
String primaryRecord = "0000-0000-0000-0000";

@Resource
Expand All @@ -101,6 +108,9 @@ public class PublicProfileControllerTest extends DBUnitTest {
@Mock
private OrcidOauth2TokenDetailService orcidOauth2TokenServiceMock;

@Mock
private ProfileEntityManagerReadOnly profileEntityManagerReadOnlyMock;

@Before
public void before() {
MockitoAnnotations.initMocks(this);
Expand Down Expand Up @@ -317,6 +327,22 @@ public void getUserInfoTest() {
assertEquals(map1.get("READY_FOR_INDEXING"), "true");
assertFalse(map1.containsKey("PRIMARY_RECORD"));

// Check member created, un-reviewed record with no integrations and no activities
map1 = publicProfileController.getUserInfo(unreviewedCreatedByMembersWithNoActivitiesOrcid);
assertEquals(map1.get("EFFECTIVE_USER_ORCID"), unreviewedCreatedByMembersWithNoActivitiesOrcid);
assertEquals(map1.get("IS_LOCKED"), "false");
assertEquals(map1.get("IS_DEACTIVATED"), "false");
assertEquals(map1.get("READY_FOR_INDEXING"), "false");
assertFalse(map1.containsKey("PRIMARY_RECORD"));

// Check member created, un-reviewed record with no integrations and activities
map1 = publicProfileController.getUserInfo(unreviewedCreatedByMembersWithActivitiesOrcid);
assertEquals(map1.get("EFFECTIVE_USER_ORCID"), unreviewedCreatedByMembersWithActivitiesOrcid);
assertEquals(map1.get("IS_LOCKED"), "false");
assertEquals(map1.get("IS_DEACTIVATED"), "false");
assertEquals(map1.get("READY_FOR_INDEXING"), "true");
assertFalse(map1.containsKey("PRIMARY_RECORD"));

// Check reviewed record with no integrations
map1 = publicProfileController.getUserInfo(reviewedNoIntegrationsOrcid);
assertEquals(map1.get("EFFECTIVE_USER_ORCID"), reviewedNoIntegrationsOrcid);
Expand Down Expand Up @@ -359,16 +385,23 @@ public void getUserInfoTest() {
}

private void setupUserInfoMocks() {
SourceEntity sourceEntity = new SourceEntity(new ClientDetailsEntity("APP-000000000001"));
TargetProxyHelper.injectIntoProxy(publicProfileController, "orcidOauth2TokenService", orcidOauth2TokenServiceMock);
TargetProxyHelper.injectIntoProxy(publicProfileController, "profileEntityCacheManager", profileEntityCacheManagerMock);
TargetProxyHelper.injectIntoProxy(publicProfileController, "profileEntityManagerReadOnly", profileEntityManagerReadOnlyMock);

//This function must be used just by a specific user, if it is called by any other throw an exception
when(profileEntityManagerReadOnlyMock.haveMemberPushedWorksOrAffiliationsToRecord(anyString(), anyString())).thenThrow(MethodNotAllowedException.class);
when(profileEntityManagerReadOnlyMock.haveMemberPushedWorksOrAffiliationsToRecord(eq(unreviewedCreatedByMembersWithActivitiesOrcid), anyString())).thenReturn(true);
when(profileEntityManagerReadOnlyMock.haveMemberPushedWorksOrAffiliationsToRecord(eq(unreviewedCreatedByMembersWithNoActivitiesOrcid), anyString())).thenReturn(false);

// Reviewed record with no integrations
ProfileEntity reviewedNoIntegrations = new ProfileEntity(reviewedNoIntegrationsOrcid);
reviewedNoIntegrations.setRecordLocked(false);
reviewedNoIntegrations.setReviewed(true);

when(orcidOauth2TokenServiceMock.hasToken(eq(reviewedNoIntegrationsOrcid), anyLong())).thenReturn(false);
when(profileEntityCacheManagerMock.retrieve(reviewedNoIntegrationsOrcid)).thenReturn(reviewedNoIntegrations);
when(profileEntityCacheManagerMock.retrieve(reviewedNoIntegrationsOrcid)).thenReturn(reviewedNoIntegrations);

// Reviewed record with integrations
ProfileEntity reviewedWithIntegrations = new ProfileEntity(reviewedWithIntegrationsOrcid);
Expand All @@ -378,22 +411,41 @@ private void setupUserInfoMocks() {
when(orcidOauth2TokenServiceMock.hasToken(eq(reviewedWithIntegrationsOrcid), anyLong())).thenReturn(true);
when(profileEntityCacheManagerMock.retrieve(reviewedWithIntegrationsOrcid)).thenReturn(reviewedWithIntegrations);

// Un reviewed record with no integrations
// Un-reviewed record with no integrations
ProfileEntity unreviewedNoIntegrations = new ProfileEntity(unreviewedNoIntegrationsOrcid);
unreviewedNoIntegrations.setRecordLocked(false);
unreviewedNoIntegrations.setReviewed(false);

when(orcidOauth2TokenServiceMock.hasToken(eq(unreviewedNoIntegrationsOrcid), anyLong())).thenReturn(false);
when(profileEntityCacheManagerMock.retrieve(unreviewedNoIntegrationsOrcid)).thenReturn(unreviewedNoIntegrations);

// Un reviewed record with integrations
// Un-reviewed record with integrations
ProfileEntity unreviewedWithIntegrations = new ProfileEntity(unreviewedWithIntegrationsOrcid);
unreviewedWithIntegrations.setRecordLocked(false);
unreviewedWithIntegrations.setReviewed(false);

when(orcidOauth2TokenServiceMock.hasToken(eq(unreviewedWithIntegrationsOrcid), anyLong())).thenReturn(true);
when(profileEntityCacheManagerMock.retrieve(unreviewedWithIntegrationsOrcid)).thenReturn(unreviewedWithIntegrations);

// Un reviewed record, created by member, with no integrations and no activities
ProfileEntity unreviewedCreatedByMembersWithNoActivities = new ProfileEntity(unreviewedCreatedByMembersWithNoActivitiesOrcid);
unreviewedCreatedByMembersWithNoActivities.setSource(sourceEntity);
unreviewedWithIntegrations.setRecordLocked(false);
unreviewedWithIntegrations.setReviewed(false);

when(orcidOauth2TokenServiceMock.hasToken(eq(unreviewedCreatedByMembersWithNoActivitiesOrcid), anyLong())).thenReturn(false);
when(profileEntityCacheManagerMock.retrieve(eq(unreviewedCreatedByMembersWithNoActivitiesOrcid))).thenReturn(unreviewedCreatedByMembersWithNoActivities);


// Un reviewed record, created by member, with no integrations and activities
ProfileEntity unreviewedCreatedByMembersWithActivities = new ProfileEntity(unreviewedCreatedByMembersWithActivitiesOrcid);
unreviewedCreatedByMembersWithActivities.setSource(sourceEntity);
unreviewedWithIntegrations.setRecordLocked(false);
unreviewedWithIntegrations.setReviewed(false);

when(orcidOauth2TokenServiceMock.hasToken(eq(unreviewedCreatedByMembersWithActivitiesOrcid), anyLong())).thenReturn(false);
when(profileEntityCacheManagerMock.retrieve(eq(unreviewedCreatedByMembersWithActivitiesOrcid))).thenReturn(unreviewedCreatedByMembersWithActivities);

// Deprecated
ProfileEntity deprecated = new ProfileEntity(deprecatedUserOrcid);
deprecated.setRecordLocked(false);
Expand Down