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

com.metaring.framework.persistence.UpdateFunctionality Maven / Gradle / Ivy

There is a newer version: 1.5.9
Show newest version
package com.metaring.framework.persistence;

import java.util.concurrent.CompletableFuture;
import com.metaring.framework.functionality.AbstractFunctionality;
import com.metaring.framework.functionality.GeneratedFunctionality;
import com.metaring.framework.functionality.FunctionalityInfo;
import com.metaring.framework.persistence.OperationResult;

abstract class UpdateFunctionality extends AbstractFunctionality implements GeneratedFunctionality {

    static final FunctionalityInfo INFO = FunctionalityInfo.create("com.metaring.framework.persistence.update", true, false, false, "java.lang.String", "com.metaring.framework.persistence.OperationResult");

    static final UpdateFunctionality INSTANCE = new UpdateFunctionalityImpl();

    protected UpdateFunctionality() {
        super(INFO, OperationResult.class);
    }

    @Override
    protected final CompletableFuture beforePreConditionCheck(Object input) throws Exception {
        CompletableFuture response = beforePreConditionCheck(input == null ? null : (String) input);
        return response == null ? end : response;
    }

    protected CompletableFuture beforePreConditionCheck(String input) throws Exception {
        return end;
    }

    @Override
    protected final CompletableFuture preConditionCheck(Object input) throws Exception {
        CompletableFuture response = preConditionCheck(input == null ? null : (String) input);
        return response == null ? end : response;
    }

    protected abstract CompletableFuture preConditionCheck(String input) throws Exception;

    @Override
    protected final CompletableFuture afterPreConditionCheck(Object input) throws Exception {
        CompletableFuture response = afterPreConditionCheck(input == null ? null : (String) input);
        return response == null ? end : response;
    }

    protected CompletableFuture afterPreConditionCheck(String input) throws Exception {
        return end;
    }

    @Override
    protected final CompletableFuture beforeCall(Object input) throws Exception {
        CompletableFuture response = beforeCall(input == null ? null : (String) input);
        return response == null ? end : response;
    }

    protected CompletableFuture beforeCall(String input) throws Exception {
        return end;
    }

    @Override
    protected final CompletableFuture call(Object input) throws Exception {
        CompletableFuture call = call((String) input);
        if(call == null) {
            return end(null);
        }
        final CompletableFuture response = new CompletableFuture<>();
        call.whenCompleteAsync((result, error) -> {
            if(error != null) {
                response.completeExceptionally(error);
                return;
            }
            response.complete(result);
        }, EXECUTOR);
        return response;
    }

    protected abstract CompletableFuture call(String input) throws Exception;

    @Override
    protected final CompletableFuture afterCall(Object input, Object output) throws Exception {
        CompletableFuture response = afterCall(input == null ? null : (String) input, output == null ? null : (OperationResult) output);
        return response == null ? end : response;
    }

    protected CompletableFuture afterCall(String input, OperationResult output) throws Exception {
        return end;
    }

    @Override
    protected final CompletableFuture beforePostConditionCheck(Object input, Object output) throws Exception {
        CompletableFuture response = beforePostConditionCheck(input == null ? null : (String) input, output == null ? null : (OperationResult) output);
        return response == null ? end : response;
    }

    protected CompletableFuture beforePostConditionCheck(String input, OperationResult output) throws Exception {
        return end;
    }

    @Override
    protected final CompletableFuture postConditionCheck(Object input, Object output) throws Exception {
        CompletableFuture response = postConditionCheck(input == null ? null : (String) input, output == null ? null : (OperationResult) output);
        return response == null ? end : response;
    }

    protected abstract CompletableFuture postConditionCheck(String input, OperationResult output) throws Exception;

    @Override
    protected final CompletableFuture afterPostConditionCheck(Object input, Object output) throws Exception {
        CompletableFuture response = afterPostConditionCheck(input == null ? null : (String) input, output == null ? null : (OperationResult) output);
        return response == null ? end : response;
    }

    protected CompletableFuture afterPostConditionCheck(String input, OperationResult output) throws Exception {
        return end;
    }

    @Override
    protected final Object getInputFromJsonWork(String inputJson) {
        return inputJson == null ? null : inputJson.trim().isEmpty() ? null : inputJson.equals("null") ? null : inputJson.substring(1, inputJson.length() - 1);
    }
}