/*
 * Copyright 2020, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package io.spine.server.model;

import com.google.errorprone.annotations.Immutable;
import io.spine.core.Subscribe;
import io.spine.server.command.Command;
import io.spine.server.event.React;

import java.lang.reflect.Method;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * A meta-attribute of the {@code Method}, telling whether this method handles the objects,
 * produced outside of the current bounded context.
 *
 * @see Subscribe#external()
 * @see React#external()
 * @see Command#external()
 */
@Immutable
enum ExternalAttribute implements Attribute<Boolean> {

    /** An attribute value for the methods, designed to handle external objects only. */
    EXTERNAL(true),

    /** An attribute value for the methods, designed to handle domestic objects only. */
    DOMESTIC(false);

    private final boolean value;

    ExternalAttribute(boolean value) {
        this.value = value;
    }

    @Override
    public String parameter() {
        return "external";
    }

    @Override
    public Boolean value() {
        return value;
    }

    /**
     * Obtains the value of this attribute for a given method.
     *
     * @param method
     *         the method to inspect
     * @return the value of {@code ExternalAttribute} for the given {@code Method}
     */
    public static ExternalAttribute of(Method method) {
        checkNotNull(method);
        boolean isExternal =
                isExternalReactor(method)
                        || isExternalSubscriber(method)
                        || isExternalCommander(method);
        return isExternal ? EXTERNAL : DOMESTIC;
    }

    private static boolean isExternalReactor(Method method) {
        React reactAnnotation = method.getAnnotation(React.class);
        boolean result = (reactAnnotation != null && reactAnnotation.external());
        return result;
    }

    private static boolean isExternalSubscriber(Method method) {
        Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
        boolean result = (subscribeAnnotation != null && subscribeAnnotation.external());
        return result;
    }

    private static boolean isExternalCommander(Method method) {
        Command commandAnnotation = method.getAnnotation(Command.class);
        boolean result = (commandAnnotation != null && commandAnnotation.external());
        return result;
    }
}

@@ -1089,16 +1089,47 @@
 utable;%0A
+import io.spine.core.External;%0A
 import i
@@ -1152,16 +1152,16 @@
 scribe;%0A
-
 import i
@@ -1261,16 +1261,52 @@
 .Method;
+%0Aimport java.lang.reflect.Parameter;
 %0A%0Aimport
@@ -1528,80 +1528,16 @@
 see 
-Subscribe#external()%0A * @see React#external()%0A * @see Command#external()
+External
 %0A */
@@ -1998,18 +1998,38 @@
 urn 
-%22e
+E
 xternal
-%22
+.class.getSimpleName()
 ;%0A  
@@ -2410,32 +2410,245 @@
 otNull(method);%0A
+        if (isExternal(method)) %7B%0A            return EXTERNAL;%0A        %7D else %7B%0A            return isLegacyExternal(method);%0A        %7D%0A    %7D%0A%0A    private static ExternalAttribute isLegacyExternal(Method method) %7B%0A
         boolean 
@@ -2862,32 +2862,371 @@
 OMESTIC;%0A    %7D%0A%0A
+    private static boolean isExternal(Method method) %7B%0A        Parameter%5B%5D params = method.getParameters();%0A        if (params.length == 0) %7B%0A            return false;%0A        %7D%0A        Parameter firstParam = params%5B0%5D;%0A        boolean hasAnnotation = firstParam.getAnnotation(External.class) != null;%0A        return hasAnnotation;%0A    %7D%0A%0A
     private stat
@@ -3330,32 +3330,118 @@
 n(React.class);%0A
+        @SuppressWarnings(%22deprecation%22) // To be deleted when %60external%60 is deleted.%0A
         boolean 
@@ -3672,32 +3672,118 @@
 bscribe.class);%0A
+        @SuppressWarnings(%22deprecation%22) // To be deleted when %60external%60 is deleted.%0A
         boolean 
@@ -3942,32 +3942,32 @@
 ethod method) %7B%0A
-
         Command 
@@ -4015,32 +4015,118 @@
 Command.class);%0A
+        @SuppressWarnings(%22deprecation%22) // To be deleted when %60external%60 is deleted.%0A
         boolean 

Add support for `@External` in `ExternalAttribute`