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

org.defendev.common.domain.command.result.CommandResult Maven / Gradle / Ivy

Go to download

Common utils purely based on JDK and no other dependencies. Only exception being for org.jetbrains:annotations for building stratgic Kotlin language compatibility.Another only-exception is Apache Commons Lang3.

The newest version!
package org.defendev.common.domain.command.result;



public class CommandResult {

    public enum Status {
        REQUEST_INVALID,
        AGGREGATE_NOT_FOUND,

        /*
         * Some time in the future, I would like to have implementation of this mechanism of letting know that
         * a service have accepted the Command, but it's still being processed. And let the client know that they
         * can subscribe to a queue/topic/Websocket to obtain result when it's available.
         *
         */
        ACCEPTED_FOR_PROCESSING,     // to be processed by "this" service
        DISPATCHED_FOR_PROCESSING,   // to be processed by "some other" service

        SUCCESS_NO_MODIFICATION,
        SUCCESS;

        // Intentionally removing (comment-out) the INSUFFICIENT_PERMISSIONS value as I favour immediate
        // throwing of org.springframework.security.access.AccessDeniedException.
        // INSUFFICIENT_PERMISSIONS,
    }

    public static  CommandResult requestInvalid(String statusDetail, String statusUserMessage) {
        return new CommandResult<>(Status.REQUEST_INVALID, statusDetail, statusUserMessage, null);
    }

    public static  CommandResult aggregateNotFound() {
        return new CommandResult<>(Status.AGGREGATE_NOT_FOUND, null, null, null);
    }

    public static  CommandResult success(T data) {
        return new CommandResult<>(Status.SUCCESS, null, null, data);
    }

    private final Status status;

    /*
     * Meant to be an additional clarification to the 'status' field. Also, with growing number of various
     * use cases, expected to evolve into new standardized values in the 'Status' enum.
     */
    private final String statusDetail;

    /*
     * Message related with (supplementing) the 'status' in a form that can be displayed to the end user.
     * Usually will be present for 'status' different than SUCCESS
     */
    private final String statusUserMessage;

    private final T data;

    public CommandResult(Status status, String statusDetail, String statusUserMessage, T data) {
        this.status = status;
        this.statusDetail = statusDetail;
        this.statusUserMessage = statusUserMessage;
        this.data = data;
    }

    public Status getStatus() {
        return status;
    }

    public boolean isSuccess() {
        return Status.SUCCESS == status || Status.SUCCESS_NO_MODIFICATION == status;
    }

    public String getStatusDetail() {
        return statusDetail;
    }

    public String getStatusUserMessage() {
        return statusUserMessage;
    }

    public T getData() {
        return data;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy