Search in sources :

Example 1 with I18nValue

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

the class AttributeRequestMapperImpl method updateAttributeValue.

private void updateAttributeValue(Attribute attribute, String key, Object value) {
    switch(key) {
        case "id":
            attribute.setIdentifier(getStringValue(value));
            break;
        case "name":
            attribute.setName(getStringValue(value));
            break;
        case "sequenceNr":
            setSequenceNumber(attribute, value);
            break;
        case "type":
            setAttributeType(attribute, value);
            break;
        case "refEntityType":
            setRefEntityType(attribute, value);
            break;
        case "cascadeDelete":
            String cascadeValue = getStringValue(value);
            Boolean isCascade = cascadeValue != null ? Boolean.valueOf(cascadeValue) : null;
            attribute.setCascadeDelete(isCascade);
            break;
        case "orderBy":
            mapOrderBy(value).ifPresent(attribute::setOrderBy);
            break;
        case "expression":
            attribute.setExpression(getStringValue(value));
            break;
        case "nullable":
            String nullableValue = getStringValue(value);
            attribute.setNillable(Boolean.parseBoolean(nullableValue));
            break;
        case "auto":
            String autoValue = getStringValue(value);
            attribute.setAuto(Boolean.parseBoolean(autoValue));
            break;
        case "visible":
            String visibleValue = getStringValue(value);
            attribute.setVisible(Boolean.parseBoolean(visibleValue));
            break;
        case "label":
            I18nValue i18Label = I18nValueMapper.toI18nValue(value);
            processI18nLabel(i18Label, attribute);
            break;
        case "description":
            I18nValue i18Description = I18nValueMapper.toI18nValue(value);
            processI18nDescription(i18Description, attribute);
            break;
        case "aggregatable":
            attribute.setAggregatable(Boolean.parseBoolean(value.toString()));
            break;
        case "enumOptions":
            setEnumOptions(attribute, value);
            break;
        case "range":
            Range range = mapRange(value);
            if (range != null) {
                attribute.setRange(map(range));
            }
            break;
        case "readonly":
            setBooleanValue(attribute, value, IS_READ_ONLY);
            break;
        case "unique":
            setBooleanValue(attribute, value, IS_UNIQUE);
            break;
        case "defaultValue":
            attribute.setDefaultValue(getStringValue(value));
            break;
        case "nullableExpression":
            attribute.setNullableExpression(getStringValue(value));
            break;
        case "visibleExpression":
            attribute.setVisibleExpression(getStringValue(value));
            break;
        case "validationExpression":
            attribute.setValidationExpression(getStringValue(value));
            break;
        case "mappedByAttribute":
        case "parent":
            // Skip now and process after all attributes in the request have been processed
            break;
        case "tags":
            throw new UnsupportedFieldException(key);
        case "idAttribute":
            attribute.setIdAttribute(DataConverter.toBoolean(value));
            break;
        case "labelAttribute":
            attribute.setLabelAttribute(DataConverter.toBoolean(value));
            break;
        case "lookupAttributeIndex":
            attribute.setLookupAttributeIndex(DataConverter.toInt(value));
            break;
        default:
            throw new InvalidKeyException("attribute", key);
    }
}
Also used : UnsupportedFieldException(org.molgenis.api.metadata.v3.exception.UnsupportedFieldException) I18nValue(org.molgenis.api.metadata.v3.model.I18nValue) Range(org.molgenis.api.metadata.v3.model.Range) InvalidKeyException(org.molgenis.api.metadata.v3.exception.InvalidKeyException)

Example 2 with I18nValue

use of org.molgenis.api.metadata.v3.model.I18nValue 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 3 with I18nValue

use of org.molgenis.api.metadata.v3.model.I18nValue 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

I18nValue (org.molgenis.api.metadata.v3.model.I18nValue)3 InvalidKeyException (org.molgenis.api.metadata.v3.exception.InvalidKeyException)2 UnsupportedFieldException (org.molgenis.api.metadata.v3.exception.UnsupportedFieldException)2 EntityType (org.molgenis.data.meta.model.EntityType)2 Package (org.molgenis.data.meta.model.Package)2 Test (org.junit.jupiter.api.Test)1 EmptyAttributesException (org.molgenis.api.metadata.v3.exception.EmptyAttributesException)1 ReadOnlyFieldException (org.molgenis.api.metadata.v3.exception.ReadOnlyFieldException)1 CreateAttributeRequest (org.molgenis.api.metadata.v3.model.CreateAttributeRequest)1 CreateEntityTypeRequest (org.molgenis.api.metadata.v3.model.CreateEntityTypeRequest)1 Range (org.molgenis.api.metadata.v3.model.Range)1 RepositoryCollection (org.molgenis.data.RepositoryCollection)1 UnknownEntityTypeException (org.molgenis.data.UnknownEntityTypeException)1 UnknownPackageException (org.molgenis.data.UnknownPackageException)1 Attribute (org.molgenis.data.meta.model.Attribute)1 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)1