Search in sources :

Example 1 with RepositoryAlreadyExistsException

use of org.molgenis.data.RepositoryAlreadyExistsException in project molgenis by molgenis.

the class RestControllerV2 method copyEntity.

/**
 * Copy an entity.
 *
 * @deprecated use the navigator in the ui to copy entities.
 * @param entityTypeId name of the entity that will be copied.
 * @param request CopyEntityRequestV2
 * @param response HttpServletResponse
 * @return String name of the new entity
 */
@Deprecated
@Transactional
@WithJsMagmaScriptContext
@PostMapping(value = "copy/{entityTypeId}", produces = APPLICATION_JSON_VALUE)
public String copyEntity(@PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid CopyEntityRequestV2 request, HttpServletResponse response) {
    UriComponentsBuilder uriBuilder = createUriBuilder();
    // No repo
    if (!dataService.hasRepository(entityTypeId)) {
        throw new UnknownEntityTypeException(entityTypeId);
    }
    Repository<Entity> repositoryToCopyFrom = dataService.getRepository(entityTypeId);
    // Validate the new name
    NameValidator.validateEntityName(request.getNewEntityName());
    // Check if the entity already exists
    String newFullName = EntityTypeUtils.buildFullName(repositoryToCopyFrom.getEntityType().getPackage(), request.getNewEntityName());
    if (dataService.hasRepository(newFullName)) {
        throw new RepositoryAlreadyExistsException(newFullName);
    }
    // Permission
    boolean readPermission = permissionService.hasPermission(new EntityTypeIdentity(repositoryToCopyFrom.getName()), EntityTypePermission.READ_DATA);
    if (!readPermission) {
        throw new EntityTypePermissionDeniedException(EntityTypePermission.READ_DATA, entityTypeId);
    }
    // Capabilities
    boolean writableCapabilities = dataService.getCapabilities(repositoryToCopyFrom.getName()).contains(RepositoryCapability.WRITABLE);
    if (!writableCapabilities) {
        throw new RepositoryNotCapableException(repositoryToCopyFrom.getName(), RepositoryCapability.WRITABLE);
    }
    // Copy
    Repository<Entity> repository = repoCopier.copyRepository(repositoryToCopyFrom, request.getNewEntityName(), repositoryToCopyFrom.getEntityType().getPackage(), request.getNewEntityName());
    // Retrieve new repo
    permissionSystemService.giveUserWriteMetaPermissions(repository.getEntityType());
    response.addHeader("Location", UriUtils.createEntityCollectionUriPath(uriBuilder, repository.getName()));
    response.setStatus(HttpServletResponse.SC_CREATED);
    return repository.getName();
}
Also used : UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) Entity(org.molgenis.data.Entity) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) EntityTypePermissionDeniedException(org.molgenis.data.security.exception.EntityTypePermissionDeniedException) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) RepositoryAlreadyExistsException(org.molgenis.data.RepositoryAlreadyExistsException) RepositoryNotCapableException(org.molgenis.data.RepositoryNotCapableException) WithJsMagmaScriptContext(org.molgenis.js.magma.WithJsMagmaScriptContext) PostMapping(org.springframework.web.bind.annotation.PostMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Entity (org.molgenis.data.Entity)1 RepositoryAlreadyExistsException (org.molgenis.data.RepositoryAlreadyExistsException)1 RepositoryNotCapableException (org.molgenis.data.RepositoryNotCapableException)1 UnknownEntityTypeException (org.molgenis.data.UnknownEntityTypeException)1 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)1 EntityTypePermissionDeniedException (org.molgenis.data.security.exception.EntityTypePermissionDeniedException)1 WithJsMagmaScriptContext (org.molgenis.js.magma.WithJsMagmaScriptContext)1 Transactional (org.springframework.transaction.annotation.Transactional)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1