Search in sources :

Example 1 with Order

use of org.molgenis.api.model.Order in project molgenis by molgenis.

the class SortMapper method map.

/**
 * @param sort api-sort that will be mapped to a data-sort
 * @param entityType data-sort entity type
 * @throws UnknownSortAttributeException if api-sort contains an unknown entity type attribute
 */
public Sort map(org.molgenis.api.model.Sort sort, EntityType entityType) {
    Sort newSort = new Sort();
    sort.getOrders().forEach(order -> {
        String attributeName = order.getId();
        if (entityType.getAttribute(attributeName) == null) {
            throw new UnknownSortAttributeException(entityType, attributeName);
        }
        Order.Direction direction = order.getDirection();
        newSort.on(attributeName, direction != null ? Direction.valueOf(direction.name()) : Direction.ASC);
    });
    return newSort;
}
Also used : Order(org.molgenis.api.model.Order) UnknownSortAttributeException(org.molgenis.data.UnknownSortAttributeException) Sort(org.molgenis.data.Sort)

Example 2 with Order

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

use of org.molgenis.api.model.Order in project molgenis by molgenis.

the class AttributeRequestMapperImpl method mapOrderBy.

@SuppressWarnings({ "unchecked", "UnstableApiUsage" })
private Optional<org.molgenis.data.Sort> mapOrderBy(Object value) {
    List<Object> values = (List<Object>) value;
    ImmutableList<Order> orders = values.stream().map(item -> mapOrderByItem((Map<String, String>) item)).collect(toImmutableList());
    Sort sort = Sort.create(orders);
    return Optional.of(sortMapper.map(sort, attributeFactory.getAttributeMetadata()));
}
Also used : Order(org.molgenis.api.model.Order) Range(org.molgenis.api.metadata.v3.model.Range) UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) EntityTypeMetadata(org.molgenis.data.meta.model.EntityTypeMetadata) InvalidKeyException(org.molgenis.api.metadata.v3.exception.InvalidKeyException) UnknownRepositoryException(org.molgenis.data.UnknownRepositoryException) HashMap(java.util.HashMap) Attribute(org.molgenis.data.meta.model.Attribute) MetaDataService(org.molgenis.data.meta.MetaDataService) ArrayList(java.util.ArrayList) CreateEntityTypeRequest(org.molgenis.api.metadata.v3.model.CreateEntityTypeRequest) IS_READ_ONLY(org.molgenis.data.meta.model.AttributeMetadata.IS_READ_ONLY) I18nValue(org.molgenis.api.metadata.v3.model.I18nValue) Order(org.molgenis.api.model.Order) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SortMapper(org.molgenis.api.data.SortMapper) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) Sort(org.molgenis.api.model.Sort) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) MetadataUtils.setBooleanValue(org.molgenis.api.metadata.v3.MetadataUtils.setBooleanValue) Nullable(javax.annotation.Nullable) DataConverter(org.molgenis.data.DataConverter) MetadataUtils.setEnumOptions(org.molgenis.api.metadata.v3.MetadataUtils.setEnumOptions) IS_UNIQUE(org.molgenis.data.meta.model.AttributeMetadata.IS_UNIQUE) AttributeType(org.molgenis.data.meta.AttributeType) LanguageService.getLanguageCodes(org.molgenis.util.i18n.LanguageService.getLanguageCodes) AttributeFactory(org.molgenis.data.meta.model.AttributeFactory) MetadataUtils.setSequenceNumber(org.molgenis.api.metadata.v3.MetadataUtils.setSequenceNumber) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Maps(com.google.common.collect.Maps) EntityType(org.molgenis.data.meta.model.EntityType) MetadataUtils.getStringValue(org.molgenis.api.metadata.v3.MetadataUtils.getStringValue) UnknownAttributeException(org.molgenis.data.UnknownAttributeException) Builder(org.molgenis.api.model.Order.Builder) List(java.util.List) Component(org.springframework.stereotype.Component) Entry(java.util.Map.Entry) Direction(org.molgenis.api.model.Order.Direction) Repository(org.molgenis.data.Repository) Optional(java.util.Optional) UnsupportedFieldException(org.molgenis.api.metadata.v3.exception.UnsupportedFieldException) CreateAttributeRequest(org.molgenis.api.metadata.v3.model.CreateAttributeRequest) MetadataUtils.setAttributeType(org.molgenis.api.metadata.v3.MetadataUtils.setAttributeType) CheckForNull(javax.annotation.CheckForNull) EntityManager(org.molgenis.data.EntityManager) Sort(org.molgenis.api.model.Sort) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List)

Aggregations

Order (org.molgenis.api.model.Order)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Range (org.molgenis.api.metadata.v3.model.Range)2 Attribute (org.molgenis.data.meta.model.Attribute)2 EntityType (org.molgenis.data.meta.model.EntityType)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Maps (com.google.common.collect.Maps)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 SortMapper (org.molgenis.api.data.SortMapper)1 MetadataUtils.getStringValue (org.molgenis.api.metadata.v3.MetadataUtils.getStringValue)1 MetadataUtils.setAttributeType (org.molgenis.api.metadata.v3.MetadataUtils.setAttributeType)1