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

io.micronaut.data.runtime.mapper.DTOMapper Maven / Gradle / Ivy

There is a newer version: 4.10.5
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.data.runtime.mapper;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.exceptions.ConversionErrorException;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.data.annotation.TypeDef;
import io.micronaut.data.exceptions.DataAccessException;
import io.micronaut.data.model.DataType;
import io.micronaut.data.model.JsonDataType;
import io.micronaut.data.model.PersistentEntity;
import io.micronaut.data.model.runtime.RuntimePersistentEntity;
import io.micronaut.data.model.runtime.RuntimePersistentProperty;
import io.micronaut.data.runtime.convert.DataConversionService;

/**
 * A {@link BeanIntrospectionMapper} that reads the result using the specified
 * {@link PersistentEntity} and {@link ResultReader} and using the {@link #map(Object, Class)} allows mapping a result to an introspected Data Transfer Object (DTO).
 *
 * @param  The entity type
 * @param  The source type.
 * @param  The result type
 */
public class DTOMapper implements BeanIntrospectionMapper {

    private final RuntimePersistentEntity persistentEntity;
    private final RuntimePersistentEntity dtoEntity;
    private final ResultReader resultReader;
    private final @Nullable JsonColumnReader jsonColumnReader;
    private final DataConversionService conversionService;

    /**
     * Default constructor.
     * @param persistentEntity The entity
     * @param resultReader The result reader
     * @param conversionService
     */
    public DTOMapper(RuntimePersistentEntity persistentEntity,
                     ResultReader resultReader,
                     DataConversionService conversionService) {
        this(persistentEntity, resultReader, null, conversionService);
    }

    /**
     * Default constructor.
     * @param persistentEntity The entity
     * @param resultReader The result reader
     * @param jsonColumnReader The JSON column reader
     * @param conversionService
     */
    public DTOMapper(RuntimePersistentEntity persistentEntity,
                     ResultReader resultReader,
                     @Nullable JsonColumnReader jsonColumnReader,
                     DataConversionService conversionService) {
        this(persistentEntity, persistentEntity, resultReader, jsonColumnReader, conversionService);
    }

    /**
     * Default constructor.
     * @param persistentEntity The entity
     * @param dtoEntity The dto entity
     * @param resultReader The result reader
     * @param jsonColumnReader The JSON column reader
     * @param conversionService
     */
    public DTOMapper(RuntimePersistentEntity persistentEntity,
                     RuntimePersistentEntity dtoEntity,
                     ResultReader resultReader,
                     @Nullable JsonColumnReader jsonColumnReader,
                     DataConversionService conversionService) {
        this.conversionService = conversionService;
        ArgumentUtils.requireNonNull("persistentEntity", persistentEntity);
        ArgumentUtils.requireNonNull("resultReader", resultReader);
        this.persistentEntity = persistentEntity;
        this.dtoEntity = dtoEntity;
        this.resultReader = resultReader;
        this.jsonColumnReader = jsonColumnReader;
    }

    @Override
    public DataConversionService getConversionService() {
        return conversionService;
    }

    @Nullable
    @Override
    public Object read(@NonNull S object, @NonNull String name) throws ConversionErrorException {
        RuntimePersistentProperty pp = persistentEntity.getPropertyByName(name);
        if (pp == null && persistentEntity == dtoEntity) {
            throw new DataAccessException("DTO projection defines a property [" + name + "] that doesn't exist on root entity: " + persistentEntity.getName());
        }
        pp = dtoEntity.getPropertyByName(name);
        if (pp == null) {
            throw new DataAccessException("DTO projection doesn't define a property [" + name + "] on DTO entity: " + dtoEntity.getName());
        }
        return read(object, pp);
    }

    @Nullable
    @Override
    public Object read(@NonNull S object, @NonNull Argument argument) {
        String name = argument.getName();
        RuntimePersistentProperty pp = persistentEntity.getPropertyByName(name);
        if (pp == null) {
            if (persistentEntity != dtoEntity) {
                RuntimePersistentProperty rp = dtoEntity.getPropertyByName(name);
                if (rp != null) {
                    return read(object, rp);
                }
            }
            DataType type = argument.getAnnotationMetadata()
                    .enumValue(TypeDef.class, "type", DataType.class)
                    .orElseGet(() -> DataType.forType(argument.getType()));
            return read(object, name, type);
        } else {
            return read(object, pp);
        }
    }

    /**
     * Read the given property.
     * @param resultSet The result set
     * @param property THe property
     * @return The result
     */
    public @Nullable Object read(@NonNull S resultSet, @NonNull RuntimePersistentProperty property) {
        String propertyName = property.getPersistedName();
        DataType dataType = property.getDataType();
        String aliasPropertyName = property.getAlias();
        if (StringUtils.isNotEmpty(aliasPropertyName)) {
            propertyName = aliasPropertyName;
        }
        if (dataType == DataType.JSON && jsonColumnReader != null) {
            JsonDataType jsonDataType = property.getJsonDataType();
            return jsonColumnReader.readJsonColumn(resultReader, resultSet, propertyName, jsonDataType, property.getArgument());
        } else {
            return read(resultSet, propertyName, dataType);
        }
    }

    /**
     * Read the value from the given result set for the given persisted name and data type.
     * @param resultSet The result set
     * @param persistedName The persisted name
     * @param dataType The data type
     * @return The result
     */
    public @Nullable Object read(@NonNull S resultSet, @NonNull String persistedName, @NonNull DataType dataType) {
        return resultReader.readDynamic(
                resultSet,
                persistedName,
                dataType
        );
    }

    /**
     * @return The entity in use
     */
    public PersistentEntity getPersistentEntity() {
        return persistentEntity;
    }

    /**
     * @return the result reader
     */
    public ResultReader getResultReader() {
        return resultReader;
    }
}