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

fr.ird.observe.toolkit.maven.plugin.service.ServiceLocalMethodDescriptionImpl Maven / Gradle / Ivy

package fr.ird.observe.toolkit.maven.plugin.service;

/*-
 * #%L
 * ObServe Toolkit :: Maven plugin
 * %%
 * Copyright (C) 2017 - 2022 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import fr.ird.observe.security.Permission;
import fr.ird.observe.spi.MethodCredential;
import io.ultreia.java4all.http.maven.plugin.model.ImportManager;
import io.ultreia.java4all.http.maven.plugin.model.MethodDescription;
import io.ultreia.java4all.http.spi.SpiHelper;

import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Created on 26/12/2020.
 *
 * @author Tony Chemit - [email protected]
 * @since 8.0.3
 */
@SuppressWarnings("unused")
public class ServiceLocalMethodDescriptionImpl implements MethodDescription {

    private final String name;
    private final Method method;
    private final String returnType;
    private final String returnInvocation;
    private final List> exceptions;
    private final List parameterNames;
    private final List parameterTypes;
    private final Permission methodeCredentials;
    private final boolean write;
    private final boolean noTransaction;

    public  ServiceLocalMethodDescriptionImpl(ImportManager importManager, Class serviceType, Method method, Map currentMapping) {
        this.name = method.getName();
        this.method = method;

        String returnTypePrefix = importManager.getGenericDefinitionOfReturnType(method);
        String simpleReturnType = importManager.importReturnType(method, currentMapping);
        this.returnType = returnTypePrefix + simpleReturnType;

        parameterNames = new LinkedList<>();
        parameterTypes = new LinkedList<>();
        for (java.lang.reflect.Parameter parameter : method.getParameters()) {
            String name = parameter.getName();
            parameterNames.add(name);
            parameterTypes.add(importManager.importParameterType(parameter, currentMapping));
        }

        this.returnInvocation = returnType.contains("void") ? "" : "return ";
        this.exceptions = MethodDescription.getExceptions(method, importManager);

        this.methodeCredentials = Optional.ofNullable(method.getAnnotation(MethodCredential.class)).map(MethodCredential::value).orElse(null);
        this.write = SpiHelper.write(method);
        this.noTransaction = !SpiHelper.getRequestAnnotation(method).isAddAuthToken();
    }

    public Method getMethod() {
        return method;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getReturnType() {
        return returnType;
    }

    @Override
    public String getParametersDefinition() {
        return null;
    }

    @Override
    public List> getExceptions() {
        return exceptions;
    }

    @Override
    public boolean isWrite() {
        return write;
    }

    public Permission getMethodeCredentials() {
        return methodeCredentials;
    }

    public String getParametersInvocation() {
        return null;
    }

    public String getReturnInvocation() {
        return returnInvocation;
    }

    public List getParameterNames() {
        return parameterNames;
    }

    public List getParameterTypes() {
        return parameterTypes;
    }

    public boolean isNoTransaction() {
        return noTransaction;
    }
}