001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.common.command;
018
019import com.echothree.util.common.form.ValidationResult;
020import com.echothree.util.common.message.Messages;
021import java.io.Serializable;
022
023public class CommandResult<R extends BaseResult>
024        implements Serializable {
025
026    final private SecurityResult securityResult;
027    final private ValidationResult validationResult;
028    final private ExecutionResult<R> executionResult;
029
030    /** Creates a new instance of CommandResult */
031    public CommandResult(final SecurityResult securityResult, final ValidationResult validationResult, final ExecutionResult<R> executionResult) {
032        this.securityResult = securityResult;
033        this.validationResult = validationResult;
034        this.executionResult = executionResult;
035    }
036
037    public boolean hasSecurityMessages() {
038        return securityResult != null && securityResult.getHasMessages();
039    }
040
041    public boolean hasValidationErrors() {
042        return validationResult != null && validationResult.getHasErrors();
043    }
044
045    public boolean hasExecutionWarnings() {
046        return executionResult != null && executionResult.getHasWarnings();
047    }
048
049    public boolean hasExecutionErrors() {
050        return executionResult != null && executionResult.getHasErrors();
051    }
052
053    public boolean hasErrors() {
054        return hasSecurityMessages() || hasValidationErrors() || hasExecutionErrors();
055    }
056
057    public Boolean getHasErrors() {
058        return hasErrors();
059    }
060
061    public boolean hasWarnings() {
062        return hasExecutionWarnings();
063    }
064
065    public Boolean getHasWarnings() {
066        return hasWarnings();
067    }
068
069    public SecurityResult getSecurityResult() {
070        return securityResult;
071    }
072
073    public ValidationResult getValidationResult() {
074        return validationResult;
075    }
076
077    public ExecutionResult<R> getExecutionResult() {
078        return executionResult;
079    }
080
081    public R getResult() {
082        return executionResult == null ? null : executionResult.getResult();
083    }
084
085    public <T extends BaseResult> T getResult(Class<T> resultClass) {
086        return executionResult == null ? null : executionResult.getResult(resultClass);
087    }
088
089    public boolean containsSecurityMessage(String key) {
090        boolean result;
091
092        if(securityResult != null) {
093            var securityMessages = securityResult.getSecurityMessages();
094
095            if(securityMessages != null) {
096                result = securityMessages.containsKey(Messages.SECURITY_MESSAGE, key);
097            } else {
098                result = false;
099            }
100        } else {
101            result = false;
102        }
103
104        return result;
105    }
106
107    public boolean containsExecutionWarning(String key) {
108        boolean result;
109
110        if(executionResult != null) {
111            var executionWarnings = executionResult.getExecutionWarnings();
112
113            if(executionWarnings != null) {
114                result = executionWarnings.containsKey(Messages.EXECUTION_WARNING, key);
115            } else {
116                result = false;
117            }
118        } else {
119            result = false;
120        }
121
122        return result;
123    }
124
125    public boolean containsExecutionError(String key) {
126        boolean result;
127
128        if(executionResult != null) {
129            var executionErrors = executionResult.getExecutionErrors();
130
131            if(executionErrors != null) {
132                result = executionErrors.containsKey(Messages.EXECUTION_ERROR, key);
133            } else {
134                result = false;
135            }
136        } else {
137            result = false;
138        }
139
140        return result;
141    }
142
143    @Override
144    public String toString() {
145        return "{ securityResult = " + securityResult + ", validationResult = " + validationResult + ", executionResult = " + executionResult + " }";
146    }
147
148}