Search in sources :

Example 1 with CreateAttributeRequest

use of org.molgenis.api.metadata.v3.model.CreateAttributeRequest in project molgenis by molgenis.

the class AttributeRequestMapperImpl method toAttribute.

private Attribute toAttribute(CreateAttributeRequest attributeRequest, EntityType entityType, @Nullable @CheckForNull Integer index) {
    Attribute attribute = attributeFactory.create();
    // set id/label/lookupIndex before setting other properties
    attribute.setIdAttribute(attributeRequest.getIdAttribute());
    attribute.setLabelAttribute(attributeRequest.getLabelAttribute());
    attribute.setLookupAttributeIndex(attributeRequest.getLookupAttributeIndex());
    String id = attributeRequest.getId();
    if (id != null) {
        attribute.setIdentifier(id);
    }
    attribute.setName(attributeRequest.getName());
    attribute.setEntity(entityType);
    Integer sequenceNumber = attributeRequest.getSequenceNr();
    if (sequenceNumber == null) {
        sequenceNumber = index;
    }
    if (sequenceNumber != null) {
        attribute.setSequenceNumber(sequenceNumber);
    }
    String type = attributeRequest.getType();
    if (type != null) {
        attribute.setDataType(AttributeType.toEnum(type));
    }
    Optional.ofNullable(attributeRequest.getMaxLength()).ifPresent(attribute::setMaxLength);
    EntityType refEntityType;
    String refEntityTypeId = attributeRequest.getRefEntityType();
    if (refEntityTypeId != null) {
        refEntityType = (EntityType) entityManager.getReference(entityTypeMetadata, refEntityTypeId);
        attribute.setRefEntity(refEntityType);
    }
    if (attributeRequest.getCascadeDelete() != null) {
        attribute.setCascadeDelete(attributeRequest.getCascadeDelete());
    }
    ImmutableList<Order> orderBy = attributeRequest.getOrderBy();
    attribute.setOrderBy(orderBy != null ? sortMapper.map(Sort.create(orderBy), entityType) : null);
    attribute.setExpression(attributeRequest.getExpression());
    Boolean nullable = attributeRequest.getNullable();
    if (nullable != null) {
        attribute.setNillable(nullable);
    }
    Boolean auto = attributeRequest.getAuto();
    if (auto != null) {
        attribute.setAuto(auto);
    }
    Boolean visible = attributeRequest.getVisible();
    if (visible != null) {
        attribute.setVisible(visible);
    }
    processI18nLabel(attributeRequest.getLabel(), attribute);
    processI18nDescription(attributeRequest.getDescription(), attribute);
    Boolean aggregatable = attributeRequest.getAggregatable();
    if (aggregatable != null) {
        attribute.setAggregatable(aggregatable);
    }
    List<String> enumOptions = attributeRequest.getEnumOptions();
    if (enumOptions != null) {
        attribute.setEnumOptions(enumOptions);
    }
    Range range = attributeRequest.getRange();
    if (range != null) {
        attribute.setRange(map(range));
    }
    Boolean readonly = attributeRequest.getReadonly();
    if (readonly != null) {
        attribute.setReadOnly(readonly);
    }
    Boolean unique = attributeRequest.getUnique();
    if (unique != null) {
        attribute.setUnique(unique);
    }
    attribute.setNullableExpression(attributeRequest.getNullableExpression());
    attribute.setVisibleExpression(attributeRequest.getVisibleExpression());
    attribute.setValidationExpression(attributeRequest.getValidationExpression());
    attribute.setDefaultValue(attributeRequest.getDefaultValue());
    return attribute;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntityType(org.molgenis.data.meta.model.EntityType) Order(org.molgenis.api.model.Order) Attribute(org.molgenis.data.meta.model.Attribute) Range(org.molgenis.api.metadata.v3.model.Range)

Example 2 with CreateAttributeRequest

use of org.molgenis.api.metadata.v3.model.CreateAttributeRequest in project molgenis by molgenis.

the class AttributeRequestMapperImpl method toAttributes.

@Override
public List<Attribute> toAttributes(List<CreateAttributeRequest> attributes, CreateEntityTypeRequest entityTypeRequest, EntityType entityType) {
    Map<String, Attribute> mappedAttributes = Maps.newHashMapWithExpectedSize(attributes.size());
    AtomicInteger index = new AtomicInteger(0);
    for (CreateAttributeRequest attribute : attributes) {
        Attribute mappedAttribute = toAttribute(attribute, entityType, index.getAndIncrement());
        mappedAttributes.put(mappedAttribute.getIdentifier(), mappedAttribute);
    }
    resolveMappedBy(attributes, mappedAttributes, entityTypeRequest.getExtends());
    return new ArrayList<>(mappedAttributes.values());
}
Also used : CreateAttributeRequest(org.molgenis.api.metadata.v3.model.CreateAttributeRequest) Attribute(org.molgenis.data.meta.model.Attribute) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList)

Example 3 with CreateAttributeRequest

use of org.molgenis.api.metadata.v3.model.CreateAttributeRequest in project molgenis by molgenis.

the class EntityTypeRequestMapperImplTest method toEntityType.

@Test
void toEntityType() {
    String id = "MyEntityTypeId";
    I18nValue label = I18nValue.builder().setDefaultValue("My Entity Type").build();
    String packageId = "MyPackageId";
    String extendsEntityTypeId = "MyExtendsEntityTypeId";
    ImmutableList<CreateAttributeRequest> attributes = ImmutableList.of();
    CreateEntityTypeRequest createEntityTypeRequest = CreateEntityTypeRequest.builder().setId(id).setLabel(label).setDescription(null).setAbstract(false).setPackage(packageId).setExtends(extendsEntityTypeId).setAttributes(attributes).build();
    Package aPackage = mock(Package.class);
    when(metaDataService.getPackage(packageId)).thenReturn(Optional.of(aPackage));
    EntityType extendsEntityType = mock(EntityType.class);
    when(metaDataService.getEntityType(extendsEntityTypeId)).thenReturn(Optional.of(extendsEntityType));
    EntityType entityType = mock(EntityType.class);
    RepositoryCollection repositoryCollection = mock(RepositoryCollection.class);
    when(repositoryCollection.getName()).thenReturn("PostgreSQL");
    when(metaDataService.getDefaultBackend()).thenReturn(repositoryCollection);
    when(entityTypeFactory.create()).thenReturn(entityType);
    EntityType mappedEntityType = entityTypeRequestMapper.toEntityType(createEntityTypeRequest);
    assertAll(() -> verify(mappedEntityType).setId("MyEntityTypeId"), () -> verify(mappedEntityType).setLabel("My Entity Type"), () -> verify(mappedEntityType).setBackend("PostgreSQL"), () -> verify(mappedEntityType).setPackage(aPackage), () -> verify(mappedEntityType).setExtends(extendsEntityType), () -> verify(mappedEntityType).setOwnAllAttributes(emptyList()), () -> verify(mappedEntityType).setAbstract(false), () -> verifyNoMoreInteractions(mappedEntityType));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) RepositoryCollection(org.molgenis.data.RepositoryCollection) CreateAttributeRequest(org.molgenis.api.metadata.v3.model.CreateAttributeRequest) CreateEntityTypeRequest(org.molgenis.api.metadata.v3.model.CreateEntityTypeRequest) I18nValue(org.molgenis.api.metadata.v3.model.I18nValue) Package(org.molgenis.data.meta.model.Package) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 4 with CreateAttributeRequest

use of org.molgenis.api.metadata.v3.model.CreateAttributeRequest in project molgenis by molgenis.

the class MetadataApiControllerTest method testUpdateAttribute.

@Test
void testUpdateAttribute() throws URISyntaxException {
    String entityTypeId = "MyEntityTypeId";
    String attibuteId = "myAttributeId";
    CreateAttributeRequest createAttributeRequest = CreateAttributeRequest.builder().setId(attibuteId).setName("updatedMyAttribute").build();
    EntityType entityType = mock(EntityType.class);
    Attribute currentAttribute = when(mock(Attribute.class).getIdentifier()).thenReturn(attibuteId).getMock();
    when(entityType.getOwnAllAttributes()).thenReturn(singletonList(currentAttribute));
    when(entityType.getOwnAttributeById(attibuteId)).thenReturn(currentAttribute);
    when(metadataApiService.findEntityType(entityTypeId)).thenReturn(entityType);
    Attribute newAttribute = when(mock(Attribute.class).getIdentifier()).thenReturn(attibuteId).getMock();
    when(attributeRequestMapper.toAttribute(createAttributeRequest, entityType)).thenReturn(newAttribute);
    EntityType jobEntityType = mock(EntityType.class);
    when(jobEntityType.getId()).thenReturn("MyJobEntityTypeId");
    MetadataUpsertJobExecution metadataUpsertJobExecution = mock(MetadataUpsertJobExecution.class);
    when(metadataUpsertJobExecution.getEntityType()).thenReturn(jobEntityType);
    when(metadataUpsertJobExecution.getIdentifier()).thenReturn("MyJobEntityId");
    when(metadataApiService.updateEntityTypeAsync(entityType)).thenReturn(metadataUpsertJobExecution);
    ResponseEntity<?> responseEntity = ResponseEntity.accepted().location(new URI("http://localhost/api/data/MyJobEntityTypeId/MyJobEntityId")).build();
    assertEquals(responseEntity, metadataApiController.updateAttribute(entityTypeId, attibuteId, createAttributeRequest));
    verify(entityType).setOwnAllAttributes(singletonList(newAttribute));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) MetadataUpsertJobExecution(org.molgenis.api.metadata.v3.job.MetadataUpsertJobExecution) CreateAttributeRequest(org.molgenis.api.metadata.v3.model.CreateAttributeRequest) Attribute(org.molgenis.data.meta.model.Attribute) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 5 with CreateAttributeRequest

use of org.molgenis.api.metadata.v3.model.CreateAttributeRequest in project molgenis by molgenis.

the class MetadataApiControllerTest method testCreateAttribute.

@Test
void testCreateAttribute() throws URISyntaxException {
    String entityTypeId = "MyEntityTypeId";
    String attibuteId = "myAttributeId";
    CreateAttributeRequest createAttributeRequest = CreateAttributeRequest.builder().setId(attibuteId).setName("updatedMyAttribute").build();
    EntityType entityType = mock(EntityType.class);
    when(metadataApiService.findEntityType(entityTypeId)).thenReturn(entityType);
    Attribute newAttribute = mock(Attribute.class);
    when(attributeRequestMapper.toAttribute(createAttributeRequest, entityType)).thenReturn(newAttribute);
    EntityType jobEntityType = mock(EntityType.class);
    when(jobEntityType.getId()).thenReturn("MyJobEntityTypeId");
    MetadataUpsertJobExecution metadataUpsertJobExecution = mock(MetadataUpsertJobExecution.class);
    when(metadataUpsertJobExecution.getEntityType()).thenReturn(jobEntityType);
    when(metadataUpsertJobExecution.getIdentifier()).thenReturn("MyJobEntityId");
    when(metadataApiService.updateEntityTypeAsync(entityType)).thenReturn(metadataUpsertJobExecution);
    ResponseEntity<?> responseEntity = ResponseEntity.accepted().location(new URI("http://localhost/api/data/MyJobEntityTypeId/MyJobEntityId")).build();
    assertEquals(responseEntity, metadataApiController.createAttribute(entityTypeId, createAttributeRequest));
    verify(entityType).addAttribute(newAttribute);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) MetadataUpsertJobExecution(org.molgenis.api.metadata.v3.job.MetadataUpsertJobExecution) CreateAttributeRequest(org.molgenis.api.metadata.v3.model.CreateAttributeRequest) Attribute(org.molgenis.data.meta.model.Attribute) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

CreateAttributeRequest (org.molgenis.api.metadata.v3.model.CreateAttributeRequest)6 Attribute (org.molgenis.data.meta.model.Attribute)6 EntityType (org.molgenis.data.meta.model.EntityType)5 Test (org.junit.jupiter.api.Test)4 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)4 URI (java.net.URI)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 MetadataUpsertJobExecution (org.molgenis.api.metadata.v3.job.MetadataUpsertJobExecution)2 CreateEntityTypeRequest (org.molgenis.api.metadata.v3.model.CreateEntityTypeRequest)2 Range (org.molgenis.api.metadata.v3.model.Range)2 ArrayList (java.util.ArrayList)1 I18nValue (org.molgenis.api.metadata.v3.model.I18nValue)1 Order (org.molgenis.api.model.Order)1 RepositoryCollection (org.molgenis.data.RepositoryCollection)1 Package (org.molgenis.data.meta.model.Package)1