Search in sources :

Example 21 with UnknownEntityTypeException

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

the class UntypedTagService method findAttributeEntity.

private Entity findAttributeEntity(EntityType entityType, String attributeName) {
    Entity entityTypeEntity = dataService.findOneById(ENTITY_TYPE_META_DATA, entityType.getId());
    if (entityTypeEntity == null) {
        throw new UnknownEntityTypeException(entityType.getId());
    }
    Optional<Entity> result = stream(entityTypeEntity.getEntities(ATTRIBUTES)).filter(att -> attributeName.equals(att.getString(AttributeMetadata.NAME))).findFirst();
    return result.orElse(null);
}
Also used : UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) EntityTypeMetadata(org.molgenis.data.meta.model.EntityTypeMetadata) SemanticTag(org.molgenis.data.semantic.SemanticTag) LoggerFactory(org.slf4j.LoggerFactory) TagRepository(org.molgenis.semanticsearch.repository.TagRepository) PackageMetadata(org.molgenis.data.meta.model.PackageMetadata) QueryImpl(org.molgenis.data.support.QueryImpl) Multimap(com.google.common.collect.Multimap) Attribute(org.molgenis.data.meta.model.Attribute) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem) ArrayList(java.util.ArrayList) Relation(org.molgenis.data.semantic.Relation) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) Lists(com.google.common.collect.Lists) Objects.requireNonNull(java.util.Objects.requireNonNull) LabeledResource(org.molgenis.data.semantic.LabeledResource) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) Logger(org.slf4j.Logger) TagService(org.molgenis.semanticsearch.service.TagService) EntityType(org.molgenis.data.meta.model.EntityType) Streams.stream(com.google.common.collect.Streams.stream) UnknownAttributeException(org.molgenis.data.UnknownAttributeException) PACKAGE(org.molgenis.data.meta.model.PackageMetadata.PACKAGE) List(java.util.List) ATTRIBUTES(org.molgenis.data.meta.model.EntityTypeMetadata.ATTRIBUTES) Optional(java.util.Optional) DataService(org.molgenis.data.DataService) Package(org.molgenis.data.meta.model.Package) UnknownEntityException(org.molgenis.data.UnknownEntityException) Entity(org.molgenis.data.Entity) Entity(org.molgenis.data.Entity)

Example 22 with UnknownEntityTypeException

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

the class RestControllerV2Test method testUpdateEntitiesSpecificAttributeUnknownEntity.

@Test
void testUpdateEntitiesSpecificAttributeUnknownEntity() throws Throwable {
    String unknownEntityTypeId = "entity2";
    doThrow(new UnknownEntityTypeException(unknownEntityTypeId)).when(dataService).getEntityType(unknownEntityTypeId);
    try {
        mockMvc.perform(put(BASE_URI + "/" + unknownEntityTypeId + "/" + "email").content("{entities:[{email:'test@email.com'}]}").contentType(APPLICATION_JSON));
    } catch (NestedServletException e) {
        Exception exception = assertThrows(UnknownEntityTypeException.class, () -> {
            throw e.getCause();
        });
        assertThat(exception.getMessage()).containsPattern("id:entity2");
    }
}
Also used : UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) NestedServletException(org.springframework.web.util.NestedServletException) Mockito.anyString(org.mockito.Mockito.anyString) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) UnknownEntityException(org.molgenis.data.UnknownEntityException) UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) RepositoryAlreadyExistsException(org.molgenis.data.RepositoryAlreadyExistsException) MolgenisDataException(org.molgenis.data.MolgenisDataException) EntityTypePermissionDeniedException(org.molgenis.data.security.exception.EntityTypePermissionDeniedException) RepositoryNotCapableException(org.molgenis.data.RepositoryNotCapableException) NestedServletException(org.springframework.web.util.NestedServletException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 23 with UnknownEntityTypeException

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

the class RestControllerV2Test method testUpdateEntitiesUnknownEntity.

@Test
void testUpdateEntitiesUnknownEntity() throws Throwable {
    String unknownEntityTypeId = "entity2";
    doThrow(new UnknownEntityTypeException(unknownEntityTypeId)).when(dataService).getEntityType(unknownEntityTypeId);
    try {
        mockMvc.perform(put(BASE_URI + "/" + "entity2").content("{entities:[{email:'test@email.com'}]}").contentType(APPLICATION_JSON));
    } catch (NestedServletException e) {
        Exception exception = assertThrows(UnknownEntityTypeException.class, () -> {
            throw e.getCause();
        });
        assertThat(exception.getMessage()).containsPattern("id:entity2");
    }
}
Also used : UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) NestedServletException(org.springframework.web.util.NestedServletException) Mockito.anyString(org.mockito.Mockito.anyString) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) UnknownEntityException(org.molgenis.data.UnknownEntityException) UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) RepositoryAlreadyExistsException(org.molgenis.data.RepositoryAlreadyExistsException) MolgenisDataException(org.molgenis.data.MolgenisDataException) EntityTypePermissionDeniedException(org.molgenis.data.security.exception.EntityTypePermissionDeniedException) RepositoryNotCapableException(org.molgenis.data.RepositoryNotCapableException) NestedServletException(org.springframework.web.util.NestedServletException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 24 with UnknownEntityTypeException

use of org.molgenis.data.UnknownEntityTypeException 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)

Example 25 with UnknownEntityTypeException

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

the class EntityTypeRequestMapperImpl method updateEntityType.

@SuppressWarnings({ "java:S1192" })
private // ATTRIBUTES constant in this class is not related to the one in this switch
void updateEntityType(EntityType entityType, Entry<String, Object> entry) {
    switch(entry.getKey()) {
        case "package":
            String packageId = String.valueOf(entry.getValue());
            Package pack = metaDataService.getPackage(packageId).orElseThrow(() -> new UnknownPackageException(packageId));
            entityType.setPackage(pack);
            break;
        case "id":
        case "abstract":
            throw new ReadOnlyFieldException(entry.getKey(), "entityType");
        case "extends":
            String extendsValue = String.valueOf(entry.getValue());
            EntityType parent = metaDataService.getEntityType(extendsValue).orElseThrow(() -> new UnknownEntityTypeException(extendsValue));
            entityType.setExtends(parent);
            break;
        case "label":
            I18nValue label = I18nValueMapper.toI18nValue(entry.getValue());
            processI18nLabel(label, entityType);
            break;
        case "description":
            I18nValue description = I18nValueMapper.toI18nValue(entry.getValue());
            processI18nDescription(description, entityType);
            break;
        case "attributes":
            if (entry.getValue() != null) {
                Iterable<Attribute> attributes = mapAttributes(entityType, entry);
                entityType.setOwnAllAttributes(attributes);
            } else {
                throw new EmptyAttributesException();
            }
            break;
        case "tags":
        case "backend":
            throw new UnsupportedFieldException(entry.getKey());
        default:
            throw new InvalidKeyException("entityType", entry.getKey());
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) ReadOnlyFieldException(org.molgenis.api.metadata.v3.exception.ReadOnlyFieldException) Attribute(org.molgenis.data.meta.model.Attribute) UnknownPackageException(org.molgenis.data.UnknownPackageException) UnsupportedFieldException(org.molgenis.api.metadata.v3.exception.UnsupportedFieldException) Package(org.molgenis.data.meta.model.Package) I18nValue(org.molgenis.api.metadata.v3.model.I18nValue) InvalidKeyException(org.molgenis.api.metadata.v3.exception.InvalidKeyException) EmptyAttributesException(org.molgenis.api.metadata.v3.exception.EmptyAttributesException)

Aggregations

UnknownEntityTypeException (org.molgenis.data.UnknownEntityTypeException)31 EntityType (org.molgenis.data.meta.model.EntityType)21 Test (org.junit.jupiter.api.Test)15 UnknownEntityException (org.molgenis.data.UnknownEntityException)9 MolgenisDataException (org.molgenis.data.MolgenisDataException)8 Attribute (org.molgenis.data.meta.model.Attribute)8 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 Order (org.junit.jupiter.api.Order)7 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)7 Entity (org.molgenis.data.Entity)7 EntityTypePermissionDeniedException (org.molgenis.data.security.exception.EntityTypePermissionDeniedException)7 NestedServletException (org.springframework.web.util.NestedServletException)7 RepositoryAlreadyExistsException (org.molgenis.data.RepositoryAlreadyExistsException)5 RepositoryNotCapableException (org.molgenis.data.RepositoryNotCapableException)5 Package (org.molgenis.data.meta.model.Package)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Mockito.anyString (org.mockito.Mockito.anyString)4 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)4 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)4 UnknownAttributeException (org.molgenis.data.UnknownAttributeException)3