All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sdl.dxa.tridion.mapping.converter.SemanticModelConverter Maven / Gradle / Ivy

The newest version!
package com.sdl.dxa.tridion.mapping.converter;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sdl.dxa.tridion.mapping.ModelBuilderPipeline;
import com.sdl.dxa.tridion.mapping.impl.DefaultSemanticFieldDataProvider;
import com.sdl.webapp.common.api.mapping.semantic.SemanticFieldDataProvider;
import com.sdl.webapp.common.api.mapping.semantic.config.SemanticField;
import com.sdl.webapp.tridion.fields.exceptions.FieldConverterException;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.convert.TypeDescriptor;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * Interface used for converting complex values  while doing a semantic mapping with {@link SemanticFieldDataProvider}.
 */
public interface SemanticModelConverter {

    static TypeInformation getTypeInformation(TypeDescriptor targetType) {
        Class objectType = targetType.getObjectType();

        Class collectionType = null;

        if (Collection.class.isAssignableFrom(objectType)) {
            //typecast is safe because of if statement
            //noinspection unchecked
            collectionType = (Class) objectType;
            objectType = targetType.getElementTypeDescriptor().getObjectType();
        }

        return TypeInformation.builder()
                .objectType(objectType)
                .collectionType(collectionType)
                .build();
    }

    Object convert(T toConvert, TypeInformation targetType, SemanticField semanticField,
                   ModelBuilderPipeline pipeline, DefaultSemanticFieldDataProvider dataProvider) throws FieldConverterException;

    List> getTypes();

    default Object convertToCollectionIfNeeded(Object value, TypeInformation typeInformation) {
        Class collectionType = typeInformation.getCollectionType();
        if (collectionType != null) {
            if (Set.class.isAssignableFrom(collectionType)) {
                return Sets.newHashSet(value);
            } else if (List.class.isAssignableFrom(collectionType)) {
                return Lists.newArrayList(value);
            }
        }
        return value;
    }

    @NotNull
    default Number toSpecificNumber(Number number, Class objectType) {
        // conversion to string helps to prevent floating point accuracy issues
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(number));
        if (Float.class == objectType) {
            return bigDecimal.floatValue();
        }
        if (Double.class == objectType) {
            return bigDecimal.doubleValue();
        }
        if (Integer.class == objectType) {
            return bigDecimal.intValue();
        }
        if (Long.class == objectType) {
            return bigDecimal.longValue();
        }
        if (Byte.class == objectType) {
            return bigDecimal.byteValue();
        }
        if (Short.class == objectType) {
            return bigDecimal.shortValue();
        }
        return bigDecimal;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy