Search in sources :

Example 1 with OrcidClientGroupManagementException

use of org.orcid.core.exception.OrcidClientGroupManagementException in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerImpl method updateClient.

/**
     * Updates a client profile, updates can be adding or removing redirect uris
     * or updating the client fields
     * 
     * @param client
     *            The updated client
     * @return the updated OrcidClient
     */
public OrcidClient updateClient(OrcidClient client) {
    ClientDetailsEntity clientDetailsEntity = null;
    if (client.getClientId() != null) {
        // Look up the existing client.
        String clientId = client.getClientId();
        clientDetailsEntity = clientDetailsDao.find(clientId);
        if (clientDetailsEntity == null) {
            // error.
            throw new OrcidClientGroupManagementException("Unable to find client: " + clientId);
        } else {
            // If the existing client is found, then update the client
            // details from the incoming client, and save using the
            // client details manager.
            updateClientDetailsEntityFromClient(client, clientDetailsEntity, true);
            clientDetailsManager.merge(clientDetailsEntity);
        }
    }
    return adapter.toOrcidClient(clientDetailsEntity);
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) OrcidClientGroupManagementException(org.orcid.core.exception.OrcidClientGroupManagementException)

Example 2 with OrcidClientGroupManagementException

use of org.orcid.core.exception.OrcidClientGroupManagementException in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerImpl method processClient.

/**
     * Get a client and evaluates if it is new or it is an update and act
     * accordingly.
     * 
     * @param groupOrcid
     *            The client owner
     * @param clientDetailsEntities
     *            The cached list of clients
     * @param client
     *            The client that is being evaluated
     * @param clientType
     *            The type of client
     */
private void processClient(String groupOrcid, SortedSet<ClientDetailsEntity> clientDetailsEntities, OrcidClient client, ClientType clientType) {
    if (client.getClientId() == null) {
        // If the client ID in the incoming client is null, then create
        // a new client.
        // Use the client details service to create the client details
        ClientDetailsEntity clientDetailsEntity = createClientDetails(groupOrcid, client, clientType);
        // And link the client to the copy of the profile cached in
        // memory by Hibernate
        clientDetailsEntities.add(clientDetailsEntity);
    } else {
        // If the client ID in the incoming client is not null, then
        // look up the existing client.
        String clientId = client.getClientId();
        ClientDetailsEntity clientDetailsEntity = clientDetailsManager.findByClientId(clientId);
        if (clientDetailsEntity == null) {
            // error.
            throw new OrcidClientGroupManagementException("Unable to find client: " + clientId);
        } else {
            if (!PojoUtil.isEmpty(clientDetailsEntity.getGroupProfileId()) && !clientDetailsEntity.getGroupProfileId().equals(groupOrcid)) {
                // error.
                throw new OrcidClientGroupManagementException(String.format("Client %s does not belong to group %s (actually belongs to group %s)", clientId, groupOrcid, clientDetailsEntity.getGroupProfileId()));
            }
            // If the existing client is found, then update the client
            // details from the incoming client
            updateClientDetailsEntityFromClient(client, clientDetailsEntity, true);
            clientDetailsManager.merge(clientDetailsEntity);
        }
    }
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) OrcidClientGroupManagementException(org.orcid.core.exception.OrcidClientGroupManagementException)

Example 3 with OrcidClientGroupManagementException

use of org.orcid.core.exception.OrcidClientGroupManagementException in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerImpl method updateGroup.

/**
     * Updates an existing group profile. If the group doesnt exists it will
     * throw a OrcidClientGroupManagementException
     * 
     * @param orcidClientGroup
     *            The group to be updated
     */
@Transactional
public void updateGroup(OrcidClientGroup orcidClientGroup) {
    String groupOrcid = orcidClientGroup.getGroupOrcid();
    // If the incoming client group ORCID is not null, then lookup the
    // existing client group.
    ProfileEntity groupProfileEntity = profileDao.find(groupOrcid);
    if (groupProfileEntity == null) {
        // then raise an error.
        throw new OrcidClientGroupManagementException("Group ORCID was specified but does not yet exist: " + groupOrcid);
    } else {
        boolean updateClientScopes = false;
        // profile DAO
        if (!orcidClientGroup.getEmail().equals(groupProfileEntity.getPrimaryEmail().getId())) {
            EmailEntity primaryEmailEntity = new EmailEntity();
            primaryEmailEntity.setId(orcidClientGroup.getEmail().toLowerCase().trim());
            primaryEmailEntity.setCurrent(true);
            primaryEmailEntity.setVerified(true);
            primaryEmailEntity.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.PRIVATE);
            groupProfileEntity.setPrimaryEmail(primaryEmailEntity);
        }
        if (groupProfileEntity.getRecordNameEntity() == null) {
            groupProfileEntity.setRecordNameEntity(new RecordNameEntity());
            groupProfileEntity.getRecordNameEntity().setProfile(groupProfileEntity);
        }
        // Set the record name entity table
        groupProfileEntity.getRecordNameEntity().setCreditName(orcidClientGroup.getGroupName());
        groupProfileEntity.getRecordNameEntity().setVisibility(org.orcid.jaxb.model.common_v2.Visibility.PUBLIC);
        groupProfileEntity.setSalesforeId(orcidClientGroup.getSalesforceId());
        // If group type changed
        if (!groupProfileEntity.getGroupType().equals(orcidClientGroup.getType())) {
            // Update the group type
            groupProfileEntity.setGroupType(orcidClientGroup.getType());
            // Set the flag to update the client scopes
            updateClientScopes = true;
        }
        // Merge changes
        profileDao.merge(groupProfileEntity);
        profileEntityManager.updateLastModifed(groupOrcid);
        // Update client types and scopes
        if (updateClientScopes)
            updateClientTypeDueGroupTypeUpdate(groupProfileEntity);
    }
}
Also used : OrcidClientGroupManagementException(org.orcid.core.exception.OrcidClientGroupManagementException) RecordNameEntity(org.orcid.persistence.jpa.entities.RecordNameEntity) EmailEntity(org.orcid.persistence.jpa.entities.EmailEntity) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with OrcidClientGroupManagementException

use of org.orcid.core.exception.OrcidClientGroupManagementException in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerImpl method updateClient.

/**
     * Updates a client profile, updates can be adding or removing redirect uris
     * or updating the client fields
     * 
     * @param groupOrcid
     *            The group owner for this client
     * @param client
     *            The updated client
     * @return the updated OrcidClient
     */
public OrcidClient updateClient(String groupOrcid, OrcidClient client) {
    ClientDetailsEntity clientDetailsEntity = null;
    if (client.getClientId() != null) {
        // Look up the existing client.
        String clientId = client.getClientId();
        clientDetailsEntity = clientDetailsDao.find(clientId);
        if (clientDetailsEntity == null) {
            // error.
            throw new OrcidClientGroupManagementException("Unable to find client: " + clientId);
        } else {
            if (!PojoUtil.isEmpty(clientDetailsEntity.getGroupProfileId()) && !clientDetailsEntity.getGroupProfileId().equals(groupOrcid)) {
                // error.
                throw new OrcidClientGroupManagementException(String.format("Client %s does not belong to group %s (actually belongs to group %s)", clientId, groupOrcid, clientDetailsEntity.getGroupProfileId()));
            }
            // If the existing client is found, then update the client
            // details from the incoming client, and save using the client
            // details manager
            updateClientDetailsEntityFromClient(client, clientDetailsEntity, true);
            clientDetailsManager.merge(clientDetailsEntity);
        }
    }
    return adapter.toOrcidClient(clientDetailsEntity);
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) OrcidClientGroupManagementException(org.orcid.core.exception.OrcidClientGroupManagementException)

Example 5 with OrcidClientGroupManagementException

use of org.orcid.core.exception.OrcidClientGroupManagementException in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerImpl method createAndPersistClientProfile.

/**
     * Creates a new client and set the group orcid as the owner of that client
     * 
     * @param groupOrcid
     *            The group owner for this client
     * @param client
     *            The new client
     * @return the new OrcidClient
     */
public OrcidClient createAndPersistClientProfile(String groupOrcid, OrcidClient client) throws OrcidClientGroupManagementException {
    if (!isAllowedToAddNewClient(groupOrcid))
        throw new OrcidClientGroupManagementException("Your contract allows you to have only 1 client.");
    ProfileEntity groupProfileEntity = profileDao.find(groupOrcid);
    checkAndSetClientType(client, groupProfileEntity.getGroupType());
    // Use the client details service to create the client details
    ClientDetailsEntity clientDetailsEntity = createClientDetails(groupOrcid, client, client.getType());
    // Link the client to the copy of the profile cached in
    // memory by Hibernate
    SortedSet<ClientDetailsEntity> clientProfileEntities = groupProfileEntity.getClients();
    if (clientProfileEntities == null) {
        clientProfileEntities = new TreeSet<>(new OrcidEntityIdComparator<String>());
        groupProfileEntity.setClients(clientProfileEntities);
    }
    clientProfileEntities.add(clientDetailsEntity);
    return adapter.toOrcidClient(clientDetailsEntity);
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) OrcidEntityIdComparator(org.orcid.persistence.jpa.entities.OrcidEntityIdComparator) OrcidClientGroupManagementException(org.orcid.core.exception.OrcidClientGroupManagementException) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity)

Aggregations

OrcidClientGroupManagementException (org.orcid.core.exception.OrcidClientGroupManagementException)15 OrcidClient (org.orcid.jaxb.model.clientgroup.OrcidClient)10 OrcidClientGroup (org.orcid.jaxb.model.clientgroup.OrcidClientGroup)7 Transactional (org.springframework.transaction.annotation.Transactional)7 Test (org.junit.Test)6 BaseTest (org.orcid.core.BaseTest)6 RedirectUri (org.orcid.jaxb.model.clientgroup.RedirectUri)6 RedirectUris (org.orcid.jaxb.model.clientgroup.RedirectUris)6 TransactionStatus (org.springframework.transaction.TransactionStatus)6 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)4 Produces (javax.ws.rs.Produces)3 ErrorDesc (org.orcid.jaxb.model.message.ErrorDesc)3 OrcidProfile (org.orcid.jaxb.model.message.OrcidProfile)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)2 RedirectUri (org.orcid.pojo.ajaxForm.RedirectUri)2 ArrayList (java.util.ArrayList)1 EmailEntity (org.orcid.persistence.jpa.entities.EmailEntity)1 OrcidEntityIdComparator (org.orcid.persistence.jpa.entities.OrcidEntityIdComparator)1