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