Search in sources :

Example 6 with ResearcherUrlEntity

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

the class ResearcherUrlManagerImpl method updateResearcherUrls.

/**
 * Update the researcher urls associated with a specific account
 *
 * @param orcid
 * @param researcherUrls
 */
@Override
@Transactional
public ResearcherUrls updateResearcherUrls(String orcid, ResearcherUrls researcherUrls) {
    List<ResearcherUrlEntity> existingEntities = researcherUrlDao.getResearcherUrls(orcid, getLastModified(orcid));
    // Delete the deleted ones
    for (ResearcherUrlEntity existingEntity : existingEntities) {
        boolean deleteMe = true;
        if (researcherUrls.getResearcherUrls() != null) {
            for (ResearcherUrl updatedOrNew : researcherUrls.getResearcherUrls()) {
                if (existingEntity.getId().equals(updatedOrNew.getPutCode())) {
                    deleteMe = false;
                    break;
                }
            }
        }
        if (deleteMe) {
            try {
                researcherUrlDao.deleteResearcherUrl(existingEntity.getProfile().getId(), existingEntity.getId());
            } catch (Exception e) {
                throw new ApplicationException("Unable to delete researcher url " + existingEntity.getId(), e);
            }
        }
    }
    // Update or create new
    if (researcherUrls != null && researcherUrls.getResearcherUrls() != null) {
        for (ResearcherUrl updatedOrNew : researcherUrls.getResearcherUrls()) {
            if (updatedOrNew.getPutCode() != null) {
                // Update the existing ones
                for (ResearcherUrlEntity existingEntity : existingEntities) {
                    if (existingEntity.getId().equals(updatedOrNew.getPutCode())) {
                        existingEntity.setLastModified(new Date());
                        existingEntity.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(updatedOrNew.getVisibility().value()));
                        existingEntity.setUrl(updatedOrNew.getUrl().getValue());
                        existingEntity.setUrlName(updatedOrNew.getUrlName());
                        existingEntity.setDisplayIndex(updatedOrNew.getDisplayIndex());
                        researcherUrlDao.merge(existingEntity);
                    }
                }
            } else {
                // Add the new ones
                ResearcherUrlEntity newResearcherUrl = jpaJaxbResearcherUrlAdapter.toResearcherUrlEntity(updatedOrNew);
                SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
                ProfileEntity profile = new ProfileEntity(orcid);
                newResearcherUrl.setUser(profile);
                newResearcherUrl.setDateCreated(new Date());
                newResearcherUrl.setLastModified(new Date());
                if (sourceEntity.getSourceProfile() != null) {
                    newResearcherUrl.setSourceId(sourceEntity.getSourceProfile().getId());
                }
                if (sourceEntity.getSourceClient() != null) {
                    newResearcherUrl.setClientSourceId(sourceEntity.getSourceClient().getId());
                }
                newResearcherUrl.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(updatedOrNew.getVisibility().value()));
                newResearcherUrl.setDisplayIndex(updatedOrNew.getDisplayIndex());
                researcherUrlDao.persist(newResearcherUrl);
            }
        }
    }
    return researcherUrls;
}
Also used : ApplicationException(org.orcid.core.exception.ApplicationException) ResearcherUrlEntity(org.orcid.persistence.jpa.entities.ResearcherUrlEntity) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ResearcherUrl(org.orcid.jaxb.model.v3.dev1.record.ResearcherUrl) ApplicationException(org.orcid.core.exception.ApplicationException) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException) Date(java.util.Date) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with ResearcherUrlEntity

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

the class ResearcherUrlManagerImpl method deleteResearcherUrl.

@Override
public boolean deleteResearcherUrl(String orcid, Long id, boolean checkSource) {
    boolean result = true;
    ResearcherUrlEntity toDelete = researcherUrlDao.getResearcherUrl(orcid, id);
    if (checkSource) {
        orcidSecurityManager.checkSource(toDelete);
    }
    try {
        researcherUrlDao.deleteResearcherUrl(orcid, id);
    } catch (Exception e) {
        LOGGER.error("Unable to delete researcherUrl with ID: " + id);
        result = false;
    }
    return result;
}
Also used : ResearcherUrlEntity(org.orcid.persistence.jpa.entities.ResearcherUrlEntity) ApplicationException(org.orcid.core.exception.ApplicationException) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException)

Example 8 with ResearcherUrlEntity

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

the class ResearcherUrlManagerImpl method updateResearcherUrl.

@Override
@Transactional
public ResearcherUrl updateResearcherUrl(String orcid, ResearcherUrl researcherUrl, boolean isApiRequest) {
    ResearcherUrlEntity updatedResearcherUrlEntity = researcherUrlDao.getResearcherUrl(orcid, researcherUrl.getPutCode());
    Visibility originalVisibility = Visibility.fromValue(updatedResearcherUrlEntity.getVisibility().value());
    SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
    // Save the original source
    String existingSourceId = updatedResearcherUrlEntity.getSourceId();
    String existingClientSourceId = updatedResearcherUrlEntity.getClientSourceId();
    // Validate the researcher url
    PersonValidator.validateResearcherUrl(researcherUrl, sourceEntity, false, isApiRequest, originalVisibility);
    // Validate it is not duplicated
    List<ResearcherUrlEntity> existingResearcherUrls = researcherUrlDao.getResearcherUrls(orcid, getLastModified(orcid));
    for (ResearcherUrlEntity existing : existingResearcherUrls) {
        if (isDuplicated(existing, researcherUrl, sourceEntity)) {
            Map<String, String> params = new HashMap<String, String>();
            params.put("type", "researcher-url");
            params.put("value", researcherUrl.getUrlName());
            throw new OrcidDuplicatedElementException(params);
        }
    }
    orcidSecurityManager.checkSource(updatedResearcherUrlEntity);
    jpaJaxbResearcherUrlAdapter.toResearcherUrlEntity(researcherUrl, updatedResearcherUrlEntity);
    updatedResearcherUrlEntity.setLastModified(new Date());
    // Be sure it doesn't overwrite the source
    updatedResearcherUrlEntity.setSourceId(existingSourceId);
    updatedResearcherUrlEntity.setClientSourceId(existingClientSourceId);
    researcherUrlDao.merge(updatedResearcherUrlEntity);
    return jpaJaxbResearcherUrlAdapter.toResearcherUrl(updatedResearcherUrlEntity);
}
Also used : HashMap(java.util.HashMap) ResearcherUrlEntity(org.orcid.persistence.jpa.entities.ResearcherUrlEntity) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException) Visibility(org.orcid.jaxb.model.common_v2.Visibility) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with ResearcherUrlEntity

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

the class ResearcherUrlManagerImpl method updateResearcherUrls.

/**
 * Update the researcher urls associated with a specific account
 *
 * @param orcid
 * @param researcherUrls
 */
@Override
@Transactional
public ResearcherUrls updateResearcherUrls(String orcid, ResearcherUrls researcherUrls) {
    List<ResearcherUrlEntity> existingEntities = researcherUrlDao.getResearcherUrls(orcid, getLastModified(orcid));
    // Delete the deleted ones
    for (ResearcherUrlEntity existingEntity : existingEntities) {
        boolean deleteMe = true;
        if (researcherUrls.getResearcherUrls() != null) {
            for (ResearcherUrl updatedOrNew : researcherUrls.getResearcherUrls()) {
                if (existingEntity.getId().equals(updatedOrNew.getPutCode())) {
                    deleteMe = false;
                    break;
                }
            }
        }
        if (deleteMe) {
            try {
                researcherUrlDao.deleteResearcherUrl(existingEntity.getProfile().getId(), existingEntity.getId());
            } catch (Exception e) {
                throw new ApplicationException("Unable to delete researcher url " + existingEntity.getId(), e);
            }
        }
    }
    // Update or create new
    if (researcherUrls != null && researcherUrls.getResearcherUrls() != null) {
        for (ResearcherUrl updatedOrNew : researcherUrls.getResearcherUrls()) {
            if (updatedOrNew.getPutCode() != null) {
                // Update the existing ones
                for (ResearcherUrlEntity existingEntity : existingEntities) {
                    if (existingEntity.getId().equals(updatedOrNew.getPutCode())) {
                        existingEntity.setLastModified(new Date());
                        existingEntity.setVisibility(updatedOrNew.getVisibility());
                        existingEntity.setUrl(updatedOrNew.getUrl().getValue());
                        existingEntity.setUrlName(updatedOrNew.getUrlName());
                        existingEntity.setDisplayIndex(updatedOrNew.getDisplayIndex());
                        researcherUrlDao.merge(existingEntity);
                    }
                }
            } else {
                // Add the new ones
                ResearcherUrlEntity newResearcherUrl = jpaJaxbResearcherUrlAdapter.toResearcherUrlEntity(updatedOrNew);
                SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
                ProfileEntity profile = new ProfileEntity(orcid);
                newResearcherUrl.setUser(profile);
                newResearcherUrl.setDateCreated(new Date());
                newResearcherUrl.setLastModified(new Date());
                if (sourceEntity.getSourceProfile() != null) {
                    newResearcherUrl.setSourceId(sourceEntity.getSourceProfile().getId());
                }
                if (sourceEntity.getSourceClient() != null) {
                    newResearcherUrl.setClientSourceId(sourceEntity.getSourceClient().getId());
                }
                newResearcherUrl.setVisibility(updatedOrNew.getVisibility());
                newResearcherUrl.setDisplayIndex(updatedOrNew.getDisplayIndex());
                researcherUrlDao.persist(newResearcherUrl);
            }
        }
    }
    return researcherUrls;
}
Also used : ApplicationException(org.orcid.core.exception.ApplicationException) ResearcherUrlEntity(org.orcid.persistence.jpa.entities.ResearcherUrlEntity) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ResearcherUrl(org.orcid.jaxb.model.record_v2.ResearcherUrl) ApplicationException(org.orcid.core.exception.ApplicationException) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException) Date(java.util.Date) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with ResearcherUrlEntity

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

the class ResearcherUrlManagerImpl method createResearcherUrl.

@Override
public ResearcherUrl createResearcherUrl(String orcid, ResearcherUrl researcherUrl, boolean isApiRequest) {
    SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
    // Validate the researcher url
    PersonValidator.validateResearcherUrl(researcherUrl, sourceEntity, true, isApiRequest, null);
    // Validate it is not duplicated
    List<ResearcherUrlEntity> existingResearcherUrls = researcherUrlDao.getResearcherUrls(orcid, getLastModified(orcid));
    for (ResearcherUrlEntity existing : existingResearcherUrls) {
        if (isDuplicated(existing, researcherUrl, sourceEntity)) {
            Map<String, String> params = new HashMap<String, String>();
            params.put("type", "researcher-url");
            params.put("value", researcherUrl.getUrlName());
            throw new OrcidDuplicatedElementException(params);
        }
    }
    ResearcherUrlEntity newEntity = jpaJaxbResearcherUrlAdapter.toResearcherUrlEntity(researcherUrl);
    ProfileEntity profile = profileEntityCacheManager.retrieve(orcid);
    newEntity.setUser(profile);
    newEntity.setDateCreated(new Date());
    // Set the source
    if (sourceEntity.getSourceProfile() != null) {
        newEntity.setSourceId(sourceEntity.getSourceProfile().getId());
    }
    if (sourceEntity.getSourceClient() != null) {
        newEntity.setClientSourceId(sourceEntity.getSourceClient().getId());
    }
    setIncomingPrivacy(newEntity, profile);
    DisplayIndexCalculatorHelper.setDisplayIndexOnNewEntity(newEntity, isApiRequest);
    researcherUrlDao.persist(newEntity);
    return jpaJaxbResearcherUrlAdapter.toResearcherUrl(newEntity);
}
Also used : HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ResearcherUrlEntity(org.orcid.persistence.jpa.entities.ResearcherUrlEntity) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Date(java.util.Date)

Aggregations

ResearcherUrlEntity (org.orcid.persistence.jpa.entities.ResearcherUrlEntity)28 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)12 Transactional (org.springframework.transaction.annotation.Transactional)12 Date (java.util.Date)10 Test (org.junit.Test)10 OrcidDuplicatedElementException (org.orcid.core.exception.OrcidDuplicatedElementException)8 ExternalIdentifierEntity (org.orcid.persistence.jpa.entities.ExternalIdentifierEntity)7 OtherNameEntity (org.orcid.persistence.jpa.entities.OtherNameEntity)7 ProfileKeywordEntity (org.orcid.persistence.jpa.entities.ProfileKeywordEntity)7 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)7 AddressEntity (org.orcid.persistence.jpa.entities.AddressEntity)6 DBUnitTest (org.orcid.test.DBUnitTest)6 ApplicationException (org.orcid.core.exception.ApplicationException)5 HashMap (java.util.HashMap)4 ResearcherUrl (org.orcid.jaxb.model.record_v2.ResearcherUrl)3 ResearcherUrl (org.orcid.jaxb.model.v3.dev1.record.ResearcherUrl)3 OrgAffiliationRelationEntity (org.orcid.persistence.jpa.entities.OrgAffiliationRelationEntity)3 ProfileFundingEntity (org.orcid.persistence.jpa.entities.ProfileFundingEntity)3 Rollback (org.springframework.test.annotation.Rollback)3 InvalidParameterException (java.security.InvalidParameterException)2