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

io.rxmicro.annotation.processor.rest.component.AbstractModelJsonConverterBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020. https://rxmicro.io
 *
 * 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
 *
 *     http://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.rxmicro.annotation.processor.rest.component;

import io.rxmicro.annotation.processor.common.component.impl.BaseProcessorComponent;
import io.rxmicro.annotation.processor.common.model.type.ObjectModelClass;
import io.rxmicro.annotation.processor.rest.model.AbstractModelJsonConverterClassStructure;
import io.rxmicro.annotation.processor.rest.model.HttpMethodMapping;
import io.rxmicro.annotation.processor.rest.model.MappedRestObjectModelClass;
import io.rxmicro.annotation.processor.rest.model.RestModelField;
import io.rxmicro.annotation.processor.rest.model.RestObjectModelClass;
import io.rxmicro.rest.model.ExchangeFormat;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import javax.lang.model.element.ModuleElement;

import static io.rxmicro.common.util.ExCollections.unmodifiableOrderedSet;
import static io.rxmicro.common.util.ExCollectors.toOrderedSet;

/**
 * @author nedis
 * @since 0.1
 */
public abstract class AbstractModelJsonConverterBuilder
        extends BaseProcessorComponent {

    protected abstract T newInstance(ModuleElement moduleElement,
                                     RestObjectModelClass modelClass,
                                     ExchangeFormat exchangeFormat,
                                     boolean isRestClientModel);

    public final Set buildFromJson(final ModuleElement moduleElement,
                                      final List mappedRestObjectModelClasses,
                                      final ExchangeFormat exchangeFormat,
                                      final boolean isRestClientModel) {
        return build(moduleElement, mappedRestObjectModelClasses, exchangeFormat, !isRestClientModel, isRestClientModel);
    }

    public final Set buildToJson(final ModuleElement moduleElement,
                                    final List mappedRestObjectModelClasses,
                                    final ExchangeFormat exchangeFormat,
                                    final boolean isRestClientModel) {
        return build(moduleElement, mappedRestObjectModelClasses, exchangeFormat, false, isRestClientModel);
    }

    protected Set build(final ModuleElement moduleElement,
                           final List mappedRestObjectModelClasses,
                           final ExchangeFormat exchangeFormat,
                           final boolean withHttpBodyOnly,
                           final boolean isRestClientModel) {
        final Set structures = new HashSet<>();
        for (final MappedRestObjectModelClass entry : mappedRestObjectModelClasses) {
            final RestObjectModelClass modelClass = entry.getModelClass();
            final boolean shouldGenerate = !withHttpBodyOnly ||
                    entry.getHttpMethodMappings().stream().anyMatch(HttpMethodMapping::isHttpBody);
            if (shouldGenerate) {
                final Set> modelClassWithParents =
                        Stream.concat(Stream.of(modelClass), modelClass.getAllParents().stream()).collect(toOrderedSet());
                final Set> modelClasses = new HashSet<>(modelClassWithParents);
                for (final ObjectModelClass modelClassOrParent : modelClassWithParents) {
                    for (final ObjectModelClass objectModelClass : modelClassOrParent.getAllChildrenObjectModelClasses()) {
                        modelClasses.add(objectModelClass);
                        modelClasses.addAll(objectModelClass.getAllParents());
                    }
                }
                structures.addAll(
                        modelClasses.stream()
                                .filter(mc ->
                                        mc.isParamEntriesPresent() ||
                                                mc.isModelClassReturnedByRestMethod() && mc.isParamEntriesPresentAtThisOrAnyParent()
                                )
                                .map(m -> newInstance(moduleElement, (RestObjectModelClass) m, exchangeFormat, isRestClientModel))
                                .collect(toOrderedSet())
                );
            }
        }
        return unmodifiableOrderedSet(structures);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy