Search in sources :

Example 26 with SourceEntity

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

the class Jaxb2JpaAdapterImpl method setCountry.

private void setCountry(ProfileEntity profileEntity, ContactDetails contactDetails) {
    Country contactCountry = contactDetails.getAddress() != null && contactDetails.getAddress().getCountry() != null ? contactDetails.getAddress().getCountry() : null;
    Iso3166Country country = contactCountry != null ? contactCountry.getValue() : null;
    if (country != null) {
        Set<AddressEntity> addresses = profileEntity.getAddresses();
        if (addresses == null) {
            addresses = new HashSet<AddressEntity>();
            profileEntity.setAddresses(addresses);
        }
        boolean addIt = true;
        //If the address exists, don't add it
        for (AddressEntity address : addresses) {
            if (Objects.equals(country.value(), address.getIso2Country().value())) {
                addIt = false;
            }
        }
        if (addIt) {
            AddressEntity newAddress = new AddressEntity();
            newAddress.setDateCreated(new Date());
            //The default country is the smallest one, so, lets add this one as the biggest display index possible for the record
            newAddress.setIso2Country(org.orcid.jaxb.model.common_v2.Iso3166Country.fromValue(country.value()));
            newAddress.setLastModified(new Date());
            newAddress.setUser(profileEntity);
            newAddress.setVisibility(getDefaultVisibility(profileEntity, contactCountry.getVisibility(), OrcidVisibilityDefaults.COUNTRY_DEFAULT));
            //Set source
            SourceEntity source = sourceManager.retrieveSourceEntity();
            setSource(source, newAddress);
            newAddress.setDisplayIndex(0L);
            for (AddressEntity address : addresses) address.setDisplayIndex(address.getDisplayIndex() + 1L);
            addresses.add(newAddress);
        }
    }
}
Also used : SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) Country(org.orcid.jaxb.model.message.Country) Iso3166Country(org.orcid.jaxb.model.message.Iso3166Country) Iso3166Country(org.orcid.jaxb.model.message.Iso3166Country) AddressEntity(org.orcid.persistence.jpa.entities.AddressEntity) CompletionDate(org.orcid.jaxb.model.message.CompletionDate) SubmissionDate(org.orcid.jaxb.model.message.SubmissionDate) Date(java.util.Date) FuzzyDate(org.orcid.jaxb.model.message.FuzzyDate) PublicationDate(org.orcid.jaxb.model.message.PublicationDate) DeactivationDate(org.orcid.jaxb.model.message.DeactivationDate)

Example 27 with SourceEntity

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

the class Jaxb2JpaAdapterImpl method setResearcherUrls.

private void setResearcherUrls(ProfileEntity profileEntity, ResearcherUrls researcherUrls) {
    String sourceId = getSourceId();
    SortedSet<ResearcherUrlEntity> existingResearcherUrlEntities = profileEntity.getResearcherUrls();
    Iterator<ResearcherUrlEntity> existingIt = null;
    if (existingResearcherUrlEntities != null) {
        existingIt = existingResearcherUrlEntities.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()) {
            ResearcherUrlEntity 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
                Pair<String, String> existingPair = createResearcherUrlPair(existing);
                boolean found = false;
                if (researcherUrls != null && researcherUrls.getResearcherUrl() != null) {
                    for (ResearcherUrl newResearcherUrl : researcherUrls.getResearcherUrl()) {
                        Pair<String, String> newResearcherUrlPair = createResearcherUrlPair(newResearcherUrl);
                        if (Objects.equals(existingPair, newResearcherUrlPair)) {
                            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 (researcherUrls != null && researcherUrls.getResearcherUrl() != null) {
        for (ResearcherUrl newResearcherUrl : researcherUrls.getResearcherUrl()) {
            boolean exists = false;
            Pair<String, String> newResearcherUrlPair = createResearcherUrlPair(newResearcherUrl);
            if (existingResearcherUrlEntities != null) {
                for (ResearcherUrlEntity existingEntity : existingResearcherUrlEntities) {
                    Pair<String, String> existingPair = createResearcherUrlPair(existingEntity);
                    if (Objects.equals(newResearcherUrlPair, existingPair)) {
                        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 = researcherUrls.getVisibility() == null ? null : researcherUrls.getVisibility().value();
                            if (listVisibilityValue != null && !Objects.equals(existingVisibilityValue, listVisibilityValue)) {
                                existingEntity.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(listVisibilityValue));
                            }
                        }
                        break;
                    }
                }
            }
            if (!exists) {
                if (existingResearcherUrlEntities == null) {
                    existingResearcherUrlEntities = new TreeSet<ResearcherUrlEntity>();
                    profileEntity.setResearcherUrls(existingResearcherUrlEntities);
                }
                ResearcherUrlEntity newEntity = new ResearcherUrlEntity();
                newEntity.setUser(profileEntity);
                //Set source
                SourceEntity source = sourceManager.retrieveSourceEntity();
                setSource(source, newEntity);
                if (newResearcherUrl.getUrl() != null) {
                    newEntity.setUrl(newResearcherUrl.getUrl().getValue());
                }
                if (newResearcherUrl.getUrlName() != null) {
                    newEntity.setUrlName(newResearcherUrl.getUrlName().getContent());
                }
                newEntity.setVisibility(getDefaultVisibility(profileEntity, researcherUrls.getVisibility(), OrcidVisibilityDefaults.RESEARCHER_URLS_DEFAULT));
                newEntity.setDisplayIndex(0L);
                for (ResearcherUrlEntity tempEntity : existingResearcherUrlEntities) tempEntity.setDisplayIndex(tempEntity.getDisplayIndex() + 1);
                existingResearcherUrlEntities.add(newEntity);
            }
        }
    }
}
Also used : ResearcherUrlEntity(org.orcid.persistence.jpa.entities.ResearcherUrlEntity) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ResearcherUrl(org.orcid.jaxb.model.message.ResearcherUrl)

Example 28 with SourceEntity

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

the class OrcidSecurityManagerImpl method clientIsProfileSource.

private boolean clientIsProfileSource(String clientId, ProfileEntity profile) {
    Boolean claimed = profile.getClaimed();
    SourceEntity source = profile.getSource();
    return source != null && (claimed == null || !claimed) && clientId.equals(source.getSourceId());
}
Also used : SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity)

Example 29 with SourceEntity

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

the class OrcidSecurityManagerImpl method checkProfile.

/**
     * Checks a record status and throw an exception indicating if the profile
     * have any of the following conditions: - The record is not claimed and is
     * not old enough nor being accessed by its creator - It is locked - It is
     * deprecated - It is deactivated
     * 
     * @throws OrcidDeprecatedException
     *             in case the account is deprecated
     * @throws OrcidNotClaimedException
     *             in case the account is not claimed
     * @throws LockedException
     *             in the case the account is locked
     */
@Override
public void checkProfile(String orcid) throws NoResultException, OrcidDeprecatedException, OrcidNotClaimedException, LockedException {
    ProfileEntity profile = null;
    try {
        profile = profileEntityCacheManager.retrieve(orcid);
    } catch (IllegalArgumentException e) {
        throw new NoResultException();
    }
    // Check if the user record is deprecated
    if (profile.getPrimaryRecord() != null) {
        StringBuffer primary = new StringBuffer(baseUrl).append("/").append(profile.getPrimaryRecord().getId());
        Map<String, String> params = new HashMap<String, String>();
        params.put(OrcidDeprecatedException.ORCID, primary.toString());
        if (profile.getDeprecatedDate() != null) {
            XMLGregorianCalendar calendar = DateUtils.convertToXMLGregorianCalendar(profile.getDeprecatedDate());
            params.put(OrcidDeprecatedException.DEPRECATED_DATE, calendar.toString());
        }
        throw new OrcidDeprecatedException(params);
    }
    // Check if the profile is not claimed and not old enough
    if ((profile.getClaimed() == null || Boolean.FALSE.equals(profile.getClaimed())) && !isOldEnough(profile)) {
        // Let the creator access the profile even if it is not claimed and
        // not old enough
        SourceEntity currentSourceEntity = sourceManager.retrieveSourceEntity();
        String profileSource = profile.getSource() == null ? null : profile.getSource().getSourceId();
        String currentSource = currentSourceEntity == null ? null : currentSourceEntity.getSourceId();
        // the profile source, throw an exception
        if (profileSource == null || !Objects.equals(profileSource, currentSource)) {
            throw new OrcidNotClaimedException();
        }
    }
    // Check if the record is locked
    if (!profile.isAccountNonLocked()) {
        LockedException lockedException = new LockedException();
        lockedException.setOrcid(profile.getId());
        throw lockedException;
    }
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) LockedException(org.orcid.core.security.aop.LockedException) HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) OrcidNotClaimedException(org.orcid.core.exception.OrcidNotClaimedException) NoResultException(javax.persistence.NoResultException) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity)

Example 30 with SourceEntity

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

the class ProfileFundingManagerImpl method createFunding.

/**
     * Add a new funding to the given user
     * @param orcid
     *          The user to add the funding
     * @param funding
     *          The funding to add
     * @return the added funding                  
     * */
@Override
@Transactional
public Funding createFunding(String orcid, Funding funding, boolean isApiRequest) {
    SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
    activityValidator.validateFunding(funding, sourceEntity, true, isApiRequest, null);
    //Check for duplicates
    List<ProfileFundingEntity> existingFundings = profileFundingDao.getByUser(orcid);
    List<Funding> fundings = jpaJaxbFundingAdapter.toFunding(existingFundings);
    if (fundings != null && isApiRequest) {
        for (Funding exstingFunding : fundings) {
            activityValidator.checkFundingExternalIdentifiersForDuplicates(funding.getExternalIdentifiers(), exstingFunding.getExternalIdentifiers(), exstingFunding.getSource(), sourceEntity);
        }
    }
    ProfileFundingEntity profileFundingEntity = jpaJaxbFundingAdapter.toProfileFundingEntity(funding);
    //Updates the give organization with the latest organization from database
    OrgEntity updatedOrganization = orgManager.getOrgEntity(funding);
    profileFundingEntity.setOrg(updatedOrganization);
    //Set the source
    if (sourceEntity.getSourceProfile() != null) {
        profileFundingEntity.setSourceId(sourceEntity.getSourceProfile().getId());
    }
    if (sourceEntity.getSourceClient() != null) {
        profileFundingEntity.setClientSourceId(sourceEntity.getSourceClient().getId());
    }
    ProfileEntity profile = profileEntityCacheManager.retrieve(orcid);
    profileFundingEntity.setProfile(profile);
    setIncomingWorkPrivacy(profileFundingEntity, profile);
    DisplayIndexCalculatorHelper.setDisplayIndexOnNewEntity(profileFundingEntity, isApiRequest);
    profileFundingDao.persist(profileFundingEntity);
    profileFundingDao.flush();
    if (isApiRequest) {
        notificationManager.sendAmendEmail(orcid, AmendedSection.FUNDING, createItem(profileFundingEntity));
    }
    return jpaJaxbFundingAdapter.toFunding(profileFundingEntity);
}
Also used : Funding(org.orcid.jaxb.model.record_v2.Funding) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) ProfileFundingEntity(org.orcid.persistence.jpa.entities.ProfileFundingEntity) OrgEntity(org.orcid.persistence.jpa.entities.OrgEntity) Transactional(org.springframework.transaction.annotation.Transactional)

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