use of org.orcid.persistence.jpa.entities.ProfileFundingEntity in project ORCID-Source by ORCID.
the class Jaxb2JpaAdapterImpl method getProfileFundingEntity.
/**
* Get a ProfileFundingEntity based on a Grant object
*
* @param funding
* @param exisitingProfileFundingEntity
* @return a ProfileFundingEntity created from the provided funding
* */
private ProfileFundingEntity getProfileFundingEntity(Funding funding, ProfileFundingEntity exisitingProfileFundingEntity) {
if (funding != null) {
// Get the org
OrgEntity orgEntity = getOrgEntity(funding);
ProfileFundingEntity profileFundingEntity = null;
if (exisitingProfileFundingEntity == null) {
String putCode = funding.getPutCode();
if (StringUtils.isNotBlank(putCode) && !"-1".equals(putCode)) {
throw new IllegalArgumentException("Invalid put-code was supplied for a funding: " + putCode);
}
profileFundingEntity = new ProfileFundingEntity();
//Set source
setSource(funding.getSource(), profileFundingEntity);
} else {
profileFundingEntity = exisitingProfileFundingEntity;
profileFundingEntity.clean();
}
FuzzyDate startDate = funding.getStartDate();
FuzzyDate endDate = funding.getEndDate();
if (funding.getAmount() != null) {
String amount = StringUtils.isNotBlank(funding.getAmount().getContent()) ? funding.getAmount().getContent() : null;
String currencyCode = funding.getAmount().getCurrencyCode() != null ? funding.getAmount().getCurrencyCode() : null;
if (StringUtils.isNotBlank(amount)) {
try {
BigDecimal bigDecimalAmount = getAmountAsBigDecimal(amount);
profileFundingEntity.setNumericAmount(bigDecimalAmount);
} catch (Exception e) {
String sample = getSampleAmountInProperFormat(localeManager.getLocale());
throw new IllegalArgumentException("Cannot cast amount: " + amount + " proper format is: " + sample);
}
profileFundingEntity.setCurrencyCode(currencyCode);
}
}
profileFundingEntity.setContributorsJson(getFundingContributorsJson(funding.getFundingContributors()));
profileFundingEntity.setDescription(StringUtils.isNotBlank(funding.getDescription()) ? funding.getDescription() : null);
profileFundingEntity.setEndDate(endDate != null ? new EndDateEntity(endDate) : null);
FundingExternalIdentifiers recordExternalIdentifiers = new FundingExternalIdentifiers(funding.getFundingExternalIdentifiers());
profileFundingEntity.setExternalIdentifiersJson(recordExternalIdentifiers.toDBJSONString());
profileFundingEntity.setStartDate(startDate != null ? new StartDateEntity(startDate) : null);
FundingTitle fundingTitle = funding.getTitle();
if (fundingTitle != null) {
String title = null, translatedTitle = null, languageCode = null;
if (fundingTitle.getTitle() != null)
title = fundingTitle.getTitle().getContent();
if (fundingTitle.getTranslatedTitle() != null) {
translatedTitle = fundingTitle.getTranslatedTitle().getContent();
languageCode = fundingTitle.getTranslatedTitle().getLanguageCode();
}
profileFundingEntity.setTitle(StringUtils.isNotBlank(title) ? title : null);
profileFundingEntity.setTranslatedTitle(StringUtils.isNotBlank(translatedTitle) ? translatedTitle : null);
profileFundingEntity.setTranslatedTitleLanguageCode(StringUtils.isNotBlank(languageCode) ? languageCode : null);
}
if (funding.getType() != null) {
profileFundingEntity.setType(org.orcid.jaxb.model.record_v2.FundingType.fromValue(funding.getType().value()));
}
profileFundingEntity.setOrganizationDefinedType(funding.getOrganizationDefinedFundingType() != null ? funding.getOrganizationDefinedFundingType().getContent() : null);
if (funding.getUrl() != null)
profileFundingEntity.setUrl(StringUtils.isNotBlank(funding.getUrl().getValue()) ? funding.getUrl().getValue() : null);
if (funding.getVisibility() != null) {
profileFundingEntity.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(funding.getVisibility().value()));
} else {
profileFundingEntity.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(OrcidVisibilityDefaults.WORKS_DEFAULT.getVisibility().value()));
}
if (funding.getCreatedDate() != null && funding.getCreatedDate().getValue() != null)
profileFundingEntity.setDateCreated(funding.getCreatedDate().getValue().toGregorianCalendar().getTime());
if (funding.getLastModifiedDate() != null && funding.getLastModifiedDate().getValue() != null)
profileFundingEntity.setLastModified(funding.getLastModifiedDate().getValue().toGregorianCalendar().getTime());
profileFundingEntity.setOrg(orgEntity);
if (profileFundingEntity.getDisplayIndex() == null) {
profileFundingEntity.setDisplayIndex(0L);
}
return profileFundingEntity;
}
return null;
}
use of org.orcid.persistence.jpa.entities.ProfileFundingEntity in project ORCID-Source by ORCID.
the class Jaxb2JpaAdapterImpl method setFundings.
private void setFundings(ProfileEntity profileEntity, FundingList orcidFundings) {
SortedSet<ProfileFundingEntity> existingProfileFundingEntities = profileEntity.getProfileFunding();
if (existingProfileFundingEntities == null) {
existingProfileFundingEntities = new TreeSet<>();
}
// Create a map containing the existing profile funding entities
Map<String, ProfileFundingEntity> existingProfileFundingEntitiesMap = createProfileFundingEntitiesMap(existingProfileFundingEntities);
// A set that will contain the updated profile funding entities that
// comes from the orcidGrant object
SortedSet<ProfileFundingEntity> updatedProfileFundingEntities = new TreeSet<>();
// from the fundingList object
if (orcidFundings != null && orcidFundings.getFundings() != null && !orcidFundings.getFundings().isEmpty()) {
for (Funding orcidFunding : orcidFundings.getFundings()) {
ProfileFundingEntity newProfileGrantEntity = getProfileFundingEntity(orcidFunding, existingProfileFundingEntitiesMap.get(orcidFunding.getPutCode()));
newProfileGrantEntity.setProfile(profileEntity);
updatedProfileFundingEntities.add(newProfileGrantEntity);
}
}
// Create a map containing the profile funding that comes in the
// orcidGrant object and that will be persisted
Map<String, ProfileFundingEntity> updatedProfileGrantEntitiesMap = createProfileFundingEntitiesMap(updatedProfileFundingEntities);
// Remove orphans
for (Iterator<ProfileFundingEntity> iterator = existingProfileFundingEntities.iterator(); iterator.hasNext(); ) {
ProfileFundingEntity existingEntity = iterator.next();
if (!updatedProfileGrantEntitiesMap.containsKey(String.valueOf(existingEntity.getId()))) {
iterator.remove();
}
}
// Add new
for (ProfileFundingEntity updatedEntity : updatedProfileFundingEntities) {
if (updatedEntity.getId() == null) {
existingProfileFundingEntities.add(updatedEntity);
}
}
profileEntity.setProfileFunding(existingProfileFundingEntities);
}
use of org.orcid.persistence.jpa.entities.ProfileFundingEntity in project ORCID-Source by ORCID.
the class Jpa2JaxbAdapterImpl method getFundingList.
private FundingList getFundingList(ProfileEntity profileEntity) {
LOGGER.debug("About to convert fundings from entity: " + profileEntity.getId());
Set<ProfileFundingEntity> profileFundings = profileEntity.getProfileFunding();
if (profileFundings != null && !profileFundings.isEmpty()) {
FundingList fundingList = new FundingList();
List<Funding> fundings = fundingList.getFundings();
for (ProfileFundingEntity profileFundingEntity : profileFundings) {
fundings.add(getFunding(profileFundingEntity));
}
return fundingList;
}
return null;
}
use of org.orcid.persistence.jpa.entities.ProfileFundingEntity in project ORCID-Source by ORCID.
the class ProfileFundingDaoImpl method getProfileFundingEntity.
/**
* Get the funding associated with the client orcid and the organization id
*
* @param clientOrcid
* The client orcid
*
* @param orgId
* The id of the organization
*
* @return the ProfileFundingEntity object
* */
@Override
public ProfileFundingEntity getProfileFundingEntity(String orgId, String clientOrcid) {
Query query = entityManager.createQuery("from ProfileFundingEntity where profile.id=:clientOrcid and org.id=:orgId");
query.setParameter("clientOrcid", clientOrcid);
query.setParameter("orgId", Long.valueOf(orgId));
return (ProfileFundingEntity) query.getSingleResult();
}
use of org.orcid.persistence.jpa.entities.ProfileFundingEntity in project ORCID-Source by ORCID.
the class ProfileFundingDaoImpl method getProfileFunding.
/**
* Find and retrieve a profile funding that have the given id and belongs to the given user
*
* @param userOrcid
* The owner of the funding
* @param profileFundingId
* The id of the element
* @return a profile funding entity that have the give id and belongs to the given user
* */
@Override
public ProfileFundingEntity getProfileFunding(String userOrcid, Long profileFundingId) {
Query query = entityManager.createQuery("from ProfileFundingEntity where profile.id=:userOrcid and id=:profileFundingId");
query.setParameter("userOrcid", userOrcid);
query.setParameter("profileFundingId", profileFundingId);
return (ProfileFundingEntity) query.getSingleResult();
}
Aggregations