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;
}
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());
}
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));
}
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));
}
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);
}
Aggregations