Search in sources :

Example 26 with AddressEntity

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

the class JpaJaxbAddressAdapterTest method fromOtherNameEntityToOtherNameTest.

@Test
public void fromOtherNameEntityToOtherNameTest() {
    AddressEntity entity = getAddressEntity();
    Address address = adapter.toAddress(entity);
    assertNotNull(address);
    assertEquals(Iso3166Country.US, address.getCountry().getValue());
    assertNotNull(address.getCreatedDate());
    assertNotNull(address.getLastModifiedDate());
    assertEquals(Long.valueOf(1), address.getPutCode());
    assertNotNull(address.getSource());
    assertEquals("APP-000000001", address.getSource().retrieveSourcePath());
    assertEquals(Visibility.PUBLIC, address.getVisibility());
}
Also used : Address(org.orcid.jaxb.model.v3.dev1.record.Address) AddressEntity(org.orcid.persistence.jpa.entities.AddressEntity) Test(org.junit.Test)

Example 27 with AddressEntity

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

the class ProfileEntityManagerImpl method claimProfileAndUpdatePreferences.

@Override
@Transactional
public boolean claimProfileAndUpdatePreferences(String orcid, String email, Locale locale, Claim claim) {
    // Verify the email
    boolean emailVerified = emailManager.verifySetCurrentAndPrimary(orcid, email);
    if (!emailVerified) {
        throw new InvalidParameterException("Unable to claim and verify email: " + email + " for user: " + orcid);
    }
    // Update the profile entity fields
    ProfileEntity profile = profileDao.find(orcid);
    profile.setLastModified(new Date());
    profile.setIndexingStatus(IndexingStatus.REINDEX);
    profile.setClaimed(true);
    profile.setCompletedDate(new Date());
    profile.setEncryptedPassword(encryptionManager.hashForInternalUse(claim.getPassword().getValue()));
    if (locale != null) {
        profile.setLocale(org.orcid.jaxb.model.common_v2.Locale.fromValue(locale.value()));
    }
    if (claim != null) {
        profile.setSendChangeNotifications(claim.getSendChangeNotifications().getValue());
        profile.setSendOrcidNews(claim.getSendOrcidNews().getValue());
        profile.setActivitiesVisibilityDefault(org.orcid.jaxb.model.common_v2.Visibility.valueOf(claim.getActivitiesVisibilityDefault().getVisibility().name()));
    }
    // Update the visibility for every bio element to the visibility
    // selected by the user
    // Update the bio
    org.orcid.jaxb.model.common_v2.Visibility defaultVisibility = org.orcid.jaxb.model.common_v2.Visibility.fromValue(claim.getActivitiesVisibilityDefault().getVisibility().value());
    if (profile.getBiographyEntity() != null) {
        profile.getBiographyEntity().setVisibility(defaultVisibility);
    }
    // Update address
    if (profile.getAddresses() != null) {
        for (AddressEntity a : profile.getAddresses()) {
            a.setVisibility(defaultVisibility);
        }
    }
    // Update the keywords
    if (profile.getKeywords() != null) {
        for (ProfileKeywordEntity k : profile.getKeywords()) {
            k.setVisibility(defaultVisibility);
        }
    }
    // Update the other names
    if (profile.getOtherNames() != null) {
        for (OtherNameEntity o : profile.getOtherNames()) {
            o.setVisibility(defaultVisibility);
        }
    }
    // Update the researcher urls
    if (profile.getResearcherUrls() != null) {
        for (ResearcherUrlEntity r : profile.getResearcherUrls()) {
            r.setVisibility(defaultVisibility);
        }
    }
    // Update the external identifiers
    if (profile.getExternalIdentifiers() != null) {
        for (ExternalIdentifierEntity e : profile.getExternalIdentifiers()) {
            e.setVisibility(defaultVisibility);
        }
    }
    profileDao.merge(profile);
    profileDao.flush();
    return true;
}
Also used : ProfileKeywordEntity(org.orcid.persistence.jpa.entities.ProfileKeywordEntity) ResearcherUrlEntity(org.orcid.persistence.jpa.entities.ResearcherUrlEntity) ExternalIdentifierEntity(org.orcid.persistence.jpa.entities.ExternalIdentifierEntity) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Date(java.util.Date) InvalidParameterException(java.security.InvalidParameterException) OtherNameEntity(org.orcid.persistence.jpa.entities.OtherNameEntity) AddressEntity(org.orcid.persistence.jpa.entities.AddressEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 28 with AddressEntity

use of org.orcid.persistence.jpa.entities.AddressEntity 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(org.orcid.jaxb.model.common_v2.Visibility.fromValue(updatedOrNew.getVisibility().value()));
                        existingAddress.setIso2Country(org.orcid.jaxb.model.common_v2.Iso3166Country.fromValue(updatedOrNew.getCountry().getValue().value()));
                        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(org.orcid.jaxb.model.common_v2.Visibility.fromValue(updatedOrNew.getVisibility().value()));
                newAddress.setDisplayIndex(updatedOrNew.getDisplayIndex());
                addressDao.persist(newAddress);
            }
        }
    }
    return addresses;
}
Also used : ApplicationException(org.orcid.core.exception.ApplicationException) Address(org.orcid.jaxb.model.v3.dev1.record.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 29 with AddressEntity

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

the class AddressManagerImpl method updateAddress.

@Override
@Transactional
public Address updateAddress(String orcid, Long putCode, Address address, boolean isApiRequest) {
    SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
    AddressEntity updatedEntity = addressDao.getAddress(orcid, putCode);
    Visibility originalVisibility = Visibility.fromValue(updatedEntity.getVisibility().value());
    // Save the original source
    String existingSourceId = updatedEntity.getSourceId();
    String existingClientSourceId = updatedEntity.getClientSourceId();
    // If it is an update from the API, check the source and preserve the original visibility
    if (isApiRequest) {
        orcidSecurityManager.checkSource(updatedEntity);
    }
    // Validate the address
    PersonValidator.validateAddress(address, sourceEntity, false, isApiRequest, originalVisibility);
    // Validate it is not duplicated
    List<AddressEntity> existingAddresses = addressDao.getAddresses(orcid, getLastModified(orcid));
    for (AddressEntity existing : existingAddresses) {
        // If it is not the same element
        if (!existing.getId().equals(address.getPutCode())) {
            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);
            }
        }
    }
    adapter.toAddressEntity(address, updatedEntity);
    updatedEntity.setLastModified(new Date());
    // Be sure it doesn't overwrite the source
    updatedEntity.setSourceId(existingSourceId);
    updatedEntity.setClientSourceId(existingClientSourceId);
    addressDao.merge(updatedEntity);
    return adapter.toAddress(updatedEntity);
}
Also used : HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException) Visibility(org.orcid.jaxb.model.v3.dev1.common.Visibility) AddressEntity(org.orcid.persistence.jpa.entities.AddressEntity) Date(java.util.Date) Transactional(javax.transaction.Transactional)

Example 30 with AddressEntity

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

the class AddressManagerReadOnlyImpl method getPrimaryAddress.

@Override
@Cacheable(value = "primary-address", key = "#orcid.concat('-').concat(#lastModified)")
public Address getPrimaryAddress(String orcid, long lastModified) {
    List<AddressEntity> addresses = addressDao.getAddresses(orcid, getLastModified(orcid));
    Address address = null;
    if (addresses != null) {
        // Look for the address with the largest display index
        for (AddressEntity entity : addresses) {
            if (address == null || address.getDisplayIndex() < entity.getDisplayIndex()) {
                address = adapter.toAddress(entity);
            }
        }
    }
    return address;
}
Also used : Address(org.orcid.jaxb.model.v3.dev1.record.Address) AddressEntity(org.orcid.persistence.jpa.entities.AddressEntity) Cacheable(org.springframework.cache.annotation.Cacheable)

Aggregations

AddressEntity (org.orcid.persistence.jpa.entities.AddressEntity)31 Date (java.util.Date)13 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)13 Test (org.junit.Test)9 OrcidDuplicatedElementException (org.orcid.core.exception.OrcidDuplicatedElementException)8 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)7 Address (org.orcid.jaxb.model.record_v2.Address)6 ExternalIdentifierEntity (org.orcid.persistence.jpa.entities.ExternalIdentifierEntity)6 OtherNameEntity (org.orcid.persistence.jpa.entities.OtherNameEntity)6 ProfileKeywordEntity (org.orcid.persistence.jpa.entities.ProfileKeywordEntity)6 ResearcherUrlEntity (org.orcid.persistence.jpa.entities.ResearcherUrlEntity)6 ApplicationException (org.orcid.core.exception.ApplicationException)5 Address (org.orcid.jaxb.model.v3.dev1.record.Address)5 Transactional (org.springframework.transaction.annotation.Transactional)5 HashMap (java.util.HashMap)4 Transactional (javax.transaction.Transactional)4 Visibility (org.orcid.jaxb.model.common_v2.Visibility)3 DBUnitTest (org.orcid.test.DBUnitTest)3 InvalidParameterException (java.security.InvalidParameterException)2 MapperFactory (ma.glasnost.orika.MapperFactory)2