001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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
024        implements Serializable {
025
026    SecurityResult securityResult;
027    ValidationResult validationResult;
028    ExecutionResult executionResult;
029
030    /** Creates a new instance of CommandResult */
031    public CommandResult(SecurityResult securityResult, ValidationResult validationResult, ExecutionResult executionResult) {
032        this.securityResult = securityResult;
033        this.validationResult = validationResult;
034        this.executionResult = executionResult;
035    }
036
037    public boolean hasSecurityMessages() {
038        return securityResult == null ? false : securityResult.getHasMessages();
039    }
040
041    public boolean hasValidationErrors() {
042        return validationResult == null ? false : validationResult.getHasErrors();
043    }
044
045    public boolean hasExecutionWarnings() {
046        return executionResult == null ? false : executionResult.getHasWarnings();
047    }
048
049    public boolean hasExecutionErrors() {
050        return executionResult == null ? false : 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 getExecutionResult() {
078        return executionResult;
079    }
080
081    public boolean containsSecurityMessage(String key) {
082        boolean result;
083
084        if(securityResult != null) {
085            Messages securityMessages = securityResult.getSecurityMessages();
086
087            if(securityMessages != null) {
088                result = securityMessages.containsKey(Messages.SECURITY_MESSAGE, key);
089            } else {
090                result = false;
091            }
092        } else {
093            result = false;
094        }
095
096        return result;
097    }
098
099    public boolean containsExecutionWarning(String key) {
100        boolean result;
101
102        if(executionResult != null) {
103            Messages executionWarnings = executionResult.getExecutionWarnings();
104
105            if(executionWarnings != null) {
106                result = executionWarnings.containsKey(Messages.EXECUTION_WARNING, key);
107            } else {
108                result = false;
109            }
110        } else {
111            result = false;
112        }
113
114        return result;
115    }
116
117    public boolean containsExecutionError(String key) {
118        boolean result;
119
120        if(executionResult != null) {
121            Messages executionErrors = executionResult.getExecutionErrors();
122
123            if(executionErrors != null) {
124                result = executionErrors.containsKey(Messages.EXECUTION_ERROR, key);
125            } else {
126                result = false;
127            }
128        } else {
129            result = false;
130        }
131
132        return result;
133    }
134
135    @Override
136    public String toString() {
137        return new StringBuilder().append("{ securityResult = ").append(securityResult).append(", validationResult = ").append(validationResult).append(", executionResult = ").append(executionResult).append(" }").toString();
138    }
139
140}