Search in sources :

Example 76 with SourceEntity

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

the class EmailManagerImpl method addEmail.

@Override
@Transactional
public void addEmail(HttpServletRequest request, String orcid, Email email) {
    SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
    String sourceId = sourceEntity.getSourceProfile() == null ? null : sourceEntity.getSourceProfile().getId();
    String clientSourceId = sourceEntity.getSourceClient() == null ? null : sourceEntity.getSourceClient().getId();
    Email currentPrimaryEmail = findPrimaryEmail(orcid);
    // Create the new email
    emailDao.addEmail(orcid, email.getEmail(), email.getVisibility(), sourceId, clientSourceId);
    // if primary email changed send notification.
    if (email.isPrimary() && !StringUtils.equals(currentPrimaryEmail.getEmail(), email.getEmail())) {
        request.getSession().setAttribute(EmailConstants.CHECK_EMAIL_VALIDATED, false);
        notificationManager.sendEmailAddressChangedNotification(orcid, email.getEmail(), currentPrimaryEmail.getEmail());
    }
    // send verifcation email for new address
    notificationManager.sendVerificationEmail(orcid, email.getEmail());
}
Also used : Email(org.orcid.jaxb.model.record_v2.Email) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 77 with SourceEntity

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

the class AddressManagerImpl method createAddress.

@Override
public Address createAddress(String orcid, Address address, boolean isApiRequest) {
    SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
    // Validate the address
    PersonValidator.validateAddress(address, sourceEntity, true, isApiRequest, null);
    // Validate it is not duplicated
    List<AddressEntity> existingAddresses = addressDao.getAddresses(orcid, getLastModified(orcid));
    for (AddressEntity existing : existingAddresses) {
        if (isDuplicated(existing, address, sourceEntity)) {
            Map<String, String> params = new HashMap<String, String>();
            params.put("type", "address");
            params.put("value", address.getCountry().getValue().value());
            throw new OrcidDuplicatedElementException(params);
        }
    }
    AddressEntity newEntity = adapter.toAddressEntity(address);
    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());
    }
    DisplayIndexCalculatorHelper.setDisplayIndexOnNewEntity(newEntity, isApiRequest);
    setIncomingPrivacy(newEntity, profile);
    addressDao.persist(newEntity);
    return adapter.toAddress(newEntity);
}
Also used : HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException) AddressEntity(org.orcid.persistence.jpa.entities.AddressEntity) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Date(java.util.Date)

Example 78 with SourceEntity

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

the class AddressManagerImpl method updateAddresses.

@Override
public Addresses updateAddresses(String orcid, Addresses addresses) {
    List<AddressEntity> existingAddressList = addressDao.getAddresses(orcid, getLastModified(orcid));
    //Delete the deleted ones
    for (AddressEntity existingAddress : existingAddressList) {
        boolean deleteMe = true;
        if (addresses.getAddress() != null) {
            for (Address updatedOrNew : addresses.getAddress()) {
                if (existingAddress.getId().equals(updatedOrNew.getPutCode())) {
                    deleteMe = false;
                    break;
                }
            }
        }
        if (deleteMe) {
            try {
                addressDao.deleteAddress(orcid, existingAddress.getId());
            } catch (Exception e) {
                throw new ApplicationException("Unable to delete address " + existingAddress.getId(), e);
            }
        }
    }
    if (addresses != null && addresses.getAddress() != null) {
        for (Address updatedOrNew : addresses.getAddress()) {
            if (updatedOrNew.getPutCode() != null) {
                //Update the existing ones
                for (AddressEntity existingAddress : existingAddressList) {
                    if (existingAddress.getId().equals(updatedOrNew.getPutCode())) {
                        existingAddress.setLastModified(new Date());
                        existingAddress.setVisibility(updatedOrNew.getVisibility());
                        existingAddress.setIso2Country(updatedOrNew.getCountry().getValue());
                        existingAddress.setDisplayIndex(updatedOrNew.getDisplayIndex());
                        addressDao.merge(existingAddress);
                    }
                }
            } else {
                //Add the new ones
                AddressEntity newAddress = adapter.toAddressEntity(updatedOrNew);
                SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
                ProfileEntity profile = new ProfileEntity(orcid);
                newAddress.setUser(profile);
                newAddress.setDateCreated(new Date());
                //Set the source id
                if (sourceEntity.getSourceProfile() != null) {
                    newAddress.setSourceId(sourceEntity.getSourceProfile().getId());
                }
                if (sourceEntity.getSourceClient() != null) {
                    newAddress.setClientSourceId(sourceEntity.getSourceClient().getId());
                }
                newAddress.setVisibility(updatedOrNew.getVisibility());
                newAddress.setDisplayIndex(updatedOrNew.getDisplayIndex());
                addressDao.persist(newAddress);
            }
        }
    }
    return addresses;
}
Also used : ApplicationException(org.orcid.core.exception.ApplicationException) Address(org.orcid.jaxb.model.record_v2.Address) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) AddressEntity(org.orcid.persistence.jpa.entities.AddressEntity) ApplicationException(org.orcid.core.exception.ApplicationException) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException) Date(java.util.Date) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity)

Example 79 with SourceEntity

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

the class ActivityValidator method validateWork.

public void validateWork(Work work, SourceEntity sourceEntity, boolean createFlag, boolean isApiRequest, Visibility originalVisibility) {
    WorkTitle title = work.getWorkTitle();
    if (title == null || title.getTitle() == null || StringUtils.isEmpty(title.getTitle().getContent())) {
        throw new ActivityTitleValidationException();
    }
    if (work.getCountry() != null) {
        if (work.getCountry().getValue() == null) {
            Map<String, String> params = new HashMap<String, String>();
            String values = Arrays.stream(Iso3166Country.values()).map(element -> element.value()).collect(Collectors.joining(", "));
            params.put("type", "country");
            params.put("values", values);
            throw new ActivityTypeValidationException(params);
        }
    }
    //translated title language code
    if (title != null && title.getTranslatedTitle() != null) {
        String translatedTitle = title.getTranslatedTitle().getContent();
        String languageCode = title.getTranslatedTitle().getLanguageCode();
        if (PojoUtil.isEmpty(translatedTitle) && !PojoUtil.isEmpty(languageCode)) {
            throw new OrcidValidationException("Please specify a translated title or remove the language code");
        }
        //If translated title language code is null or invalid
        if (!PojoUtil.isEmpty(translatedTitle) && (PojoUtil.isEmpty(title.getTranslatedTitle().getLanguageCode()) || !Arrays.stream(SiteConstants.AVAILABLE_ISO_LANGUAGES).anyMatch(title.getTranslatedTitle().getLanguageCode()::equals))) {
            Map<String, String> params = new HashMap<String, String>();
            String values = Arrays.stream(SiteConstants.AVAILABLE_ISO_LANGUAGES).collect(Collectors.joining(", "));
            params.put("type", "translated title -> language code");
            params.put("values", values);
            throw new ActivityTypeValidationException(params);
        }
    }
    if (work.getWorkType() == null) {
        Map<String, String> params = new HashMap<String, String>();
        String values = Arrays.stream(WorkType.values()).map(element -> element.value()).collect(Collectors.joining(", "));
        params.put("type", "work type");
        params.put("values", values);
        throw new ActivityTypeValidationException(params);
    }
    if (!PojoUtil.isEmpty(work.getLanguageCode())) {
        if (!Arrays.stream(SiteConstants.AVAILABLE_ISO_LANGUAGES).anyMatch(work.getLanguageCode()::equals)) {
            Map<String, String> params = new HashMap<String, String>();
            String values = Arrays.stream(SiteConstants.AVAILABLE_ISO_LANGUAGES).collect(Collectors.joining(", "));
            params.put("type", "language code");
            params.put("values", values);
            throw new ActivityTypeValidationException(params);
        }
    }
    //publication date
    if (work.getPublicationDate() != null) {
        PublicationDate pd = work.getPublicationDate();
        Year year = pd.getYear();
        Month month = pd.getMonth();
        Day day = pd.getDay();
        if (year != null) {
            try {
                Integer.valueOf(year.getValue());
            } catch (NumberFormatException n) {
                Map<String, String> params = new HashMap<String, String>();
                params.put("type", "publication date -> year");
                params.put("values", "integers");
                throw new ActivityTypeValidationException(params);
            }
            if (year.getValue().length() != 4) {
                throw new OrcidValidationException("Invalid year " + year.getValue() + " please specify a four digits value");
            }
        }
        if (month != null) {
            try {
                Integer.valueOf(month.getValue());
            } catch (NumberFormatException n) {
                Map<String, String> params = new HashMap<String, String>();
                params.put("type", "publication date -> month");
                params.put("values", "integers");
                throw new ActivityTypeValidationException(params);
            }
            if (month.getValue().length() != 2) {
                throw new OrcidValidationException("Invalid month " + month.getValue() + " please specify a two digits value");
            }
        }
        if (day != null) {
            try {
                Integer.valueOf(day.getValue());
            } catch (NumberFormatException n) {
                Map<String, String> params = new HashMap<String, String>();
                params.put("type", "publication date -> day");
                params.put("values", "integers");
                throw new ActivityTypeValidationException(params);
            }
            if (day.getValue().length() != 2) {
                throw new OrcidValidationException("Invalid day " + day.getValue() + " please specify a two digits value");
            }
        }
        //Check the date is valid
        boolean isYearEmpty = (year == null || year.getValue() == null) ? true : false;
        boolean isMonthEmpty = (month == null || month.getValue() == null) ? true : false;
        boolean isDayEmpty = (day == null || day.getValue() == null) ? true : false;
        if (isYearEmpty && (!isMonthEmpty || !isDayEmpty)) {
            throw new OrcidValidationException("Invalid date, please specify a year element");
        } else if (!isYearEmpty && isMonthEmpty && !isDayEmpty) {
            throw new OrcidValidationException("Invalid date, please specify a month element");
        } else if (isYearEmpty && isMonthEmpty && !isDayEmpty) {
            throw new OrcidValidationException("Invalid date, please specify a year and month elements");
        }
    }
    //citation
    if (work.getWorkCitation() != null) {
        String citation = work.getWorkCitation().getCitation();
        CitationType type = work.getWorkCitation().getWorkCitationType();
        if (type == null) {
            Map<String, String> params = new HashMap<String, String>();
            String values = Arrays.stream(CitationType.values()).map(element -> element.value()).collect(Collectors.joining(", "));
            params.put("type", "citation type");
            params.put("values", values);
            throw new ActivityTypeValidationException(params);
        }
        if (PojoUtil.isEmpty(citation)) {
            throw new OrcidValidationException("Please specify a citation or remove the parent tag");
        }
    }
    if (work.getWorkExternalIdentifiers() == null || work.getWorkExternalIdentifiers().getExternalIdentifier() == null || work.getExternalIdentifiers().getExternalIdentifier().isEmpty()) {
        throw new ActivityIdentifierValidationException();
    }
    if (work.getWorkContributors() != null) {
        WorkContributors contributors = work.getWorkContributors();
        if (!contributors.getContributor().isEmpty()) {
            for (Contributor contributor : contributors.getContributor()) {
                if (contributor.getContributorOrcid() != null) {
                    ContributorOrcid contributorOrcid = contributor.getContributorOrcid();
                    if (!PojoUtil.isEmpty(contributorOrcid.getUri())) {
                        if (!OrcidStringUtils.isValidOrcidUri(contributorOrcid.getUri())) {
                            throw new OrcidValidationException("Invalid contributor URI");
                        }
                    }
                    if (!PojoUtil.isEmpty(contributorOrcid.getPath())) {
                        if (!OrcidStringUtils.isValidOrcid(contributorOrcid.getPath())) {
                            throw new OrcidValidationException("Invalid contributor ORCID");
                        }
                    }
                }
                if (contributor.getCreditName() != null) {
                    if (PojoUtil.isEmpty(contributor.getCreditName().getContent())) {
                        throw new OrcidValidationException("Please specify a contributor credit name or remove the empty tag");
                    }
                }
                if (contributor.getContributorEmail() != null) {
                    if (PojoUtil.isEmpty(contributor.getContributorEmail().getValue())) {
                        throw new OrcidValidationException("Please specify a contributor email or remove the empty tag");
                    }
                }
            }
        }
    }
    if (work.getPutCode() != null && createFlag) {
        Map<String, String> params = new HashMap<String, String>();
        if (sourceEntity != null) {
            params.put("clientName", sourceEntity.getSourceName());
        }
        throw new InvalidPutCodeException(params);
    }
    // Check that we are not changing the visibility
    if (isApiRequest && !createFlag) {
        Visibility updatedVisibility = work.getVisibility();
        validateVisibilityDoesntChange(updatedVisibility, originalVisibility);
    }
    externalIDValidator.validateWorkOrPeerReview(work.getExternalIdentifiers());
}
Also used : FundingTitle(org.orcid.jaxb.model.record_v2.FundingTitle) Arrays(java.util.Arrays) Year(org.orcid.jaxb.model.common_v2.Year) InvalidPutCodeException(org.orcid.core.exception.InvalidPutCodeException) WorkTitle(org.orcid.jaxb.model.record_v2.WorkTitle) OrcidStringUtils(org.orcid.utils.OrcidStringUtils) ActivityTitleValidationException(org.orcid.core.exception.ActivityTitleValidationException) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) Contributor(org.orcid.jaxb.model.common_v2.Contributor) ContributorOrcid(org.orcid.jaxb.model.common_v2.ContributorOrcid) Amount(org.orcid.jaxb.model.common_v2.Amount) PojoUtil(org.orcid.pojo.ajaxForm.PojoUtil) HashMap(java.util.HashMap) GroupIdRecord(org.orcid.jaxb.model.groupid_v2.GroupIdRecord) StringUtils(org.apache.commons.lang3.StringUtils) OrcidValidationException(org.orcid.core.exception.OrcidValidationException) Day(org.orcid.jaxb.model.common_v2.Day) Matcher(java.util.regex.Matcher) ActivityTypeValidationException(org.orcid.core.exception.ActivityTypeValidationException) Source(org.orcid.jaxb.model.common_v2.Source) Map(java.util.Map) VisibilityMismatchException(org.orcid.core.exception.VisibilityMismatchException) CitationType(org.orcid.jaxb.model.record_v2.CitationType) Employment(org.orcid.jaxb.model.record_v2.Employment) Funding(org.orcid.jaxb.model.record_v2.Funding) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) PublicationDate(org.orcid.jaxb.model.common_v2.PublicationDate) Resource(javax.annotation.Resource) Month(org.orcid.jaxb.model.common_v2.Month) Collectors(java.util.stream.Collectors) WorkContributors(org.orcid.jaxb.model.record_v2.WorkContributors) ActivityIdentifierValidationException(org.orcid.core.exception.ActivityIdentifierValidationException) WorkType(org.orcid.jaxb.model.record_v2.WorkType) Iso3166Country(org.orcid.jaxb.model.common_v2.Iso3166Country) Visibility(org.orcid.jaxb.model.common_v2.Visibility) Relationship(org.orcid.jaxb.model.record_v2.Relationship) Work(org.orcid.jaxb.model.record_v2.Work) ExternalID(org.orcid.jaxb.model.record_v2.ExternalID) Pattern(java.util.regex.Pattern) PeerReview(org.orcid.jaxb.model.record_v2.PeerReview) SiteConstants(org.orcid.persistence.constants.SiteConstants) Education(org.orcid.jaxb.model.record_v2.Education) ExternalIDs(org.orcid.jaxb.model.record_v2.ExternalIDs) PeerReviewType(org.orcid.jaxb.model.record_v2.PeerReviewType) PublicationDate(org.orcid.jaxb.model.common_v2.PublicationDate) HashMap(java.util.HashMap) WorkContributors(org.orcid.jaxb.model.record_v2.WorkContributors) OrcidValidationException(org.orcid.core.exception.OrcidValidationException) Contributor(org.orcid.jaxb.model.common_v2.Contributor) ActivityTypeValidationException(org.orcid.core.exception.ActivityTypeValidationException) ActivityIdentifierValidationException(org.orcid.core.exception.ActivityIdentifierValidationException) Month(org.orcid.jaxb.model.common_v2.Month) ActivityTitleValidationException(org.orcid.core.exception.ActivityTitleValidationException) Year(org.orcid.jaxb.model.common_v2.Year) WorkTitle(org.orcid.jaxb.model.record_v2.WorkTitle) InvalidPutCodeException(org.orcid.core.exception.InvalidPutCodeException) CitationType(org.orcid.jaxb.model.record_v2.CitationType) Visibility(org.orcid.jaxb.model.common_v2.Visibility) ContributorOrcid(org.orcid.jaxb.model.common_v2.ContributorOrcid) Day(org.orcid.jaxb.model.common_v2.Day) HashMap(java.util.HashMap) Map(java.util.Map)

Example 80 with SourceEntity

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

the class GroupIdRecordManagerImpl method createGroupIdRecord.

@Override
public GroupIdRecord createGroupIdRecord(GroupIdRecord groupIdRecord) {
    SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
    activityValidator.validateGroupIdRecord(groupIdRecord, true, sourceEntity);
    validateDuplicate(groupIdRecord);
    GroupIdRecordEntity entity = jpaJaxbGroupIdRecordAdapter.toGroupIdRecordEntity(groupIdRecord);
    if (sourceEntity != null) {
        if (sourceEntity.getSourceClient() != null) {
            entity.setClientSourceId(sourceEntity.getSourceClient().getClientId());
        } else if (sourceEntity.getSourceProfile() != null) {
            entity.setSourceId(sourceEntity.getSourceProfile().getId());
        }
    }
    groupIdRecordDao.persist(entity);
    return jpaJaxbGroupIdRecordAdapter.toGroupIdRecord(entity);
}
Also used : SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) GroupIdRecordEntity(org.orcid.persistence.jpa.entities.GroupIdRecordEntity)

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