/*
* #%L
* BroadleafCommerce Open Admin Platform
* %%
* Copyright (C) 2009 - 2014 Broadleaf Commerce
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.broadleafcommerce.openadmin.server.service.persistence.validation;
import org.broadleafcommerce.openadmin.server.security.service.RowLevelSecurityService;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* DTO representing a boolean whether or not it passed validation and String error message. An error message is not required
* if the result is not an error.
*
* This is most suitable for global errors like those from {@link RowLevelSecurityService}
*
* @author Phillip Verheyden (phillipuniverse)
* @see {@link RowLevelSecurityService}
* @see {@link PropertyValidationResult}
*/
public class GlobalValidationResult {
protected boolean valid;
protected List<String> errorMessages = new ArrayList<String>();
public GlobalValidationResult(boolean valid, String errorMessage) {
setValid(valid);
setErrorMessage(errorMessage);
}
public GlobalValidationResult(boolean valid) {
setValid(valid);
}
/**
*
* @return Whether or not this property passed validation
*/
public boolean isValid() {
return valid;
}
/**
* Opposite of {@link #isValid()}
* @return
*/
public boolean isNotValid() {
return !valid;
}
/**
* Set the validation result for this property
* @param valid
*/
public void setValid(boolean valid) {
this.valid = valid;
}
/**
* Convenience method to return the first message
* @return the error message (or key in a message bundle) for the validation failure
*/
public String getErrorMessage() {
return CollectionUtils.isEmpty(errorMessages) ? null : errorMessages.get(0);
}
/**
* Sets the error message (or key in a message bundle) for the validation failure. If you have some sort
* of custom error message for the validation failure it should be set here
* @param errorMessage
* @deprecated - use {@link #addErrorMessage(String)}
*/
@Deprecated
public void setErrorMessage(String errorMessage) {
errorMessages.add(errorMessage);
}
/**
* Adds an error message to the list of error messages
* @param errorMessageOrKey
*/
public void addErrorMessage(String errorMessageOrKey) {
errorMessages.add(errorMessageOrKey);
}
public List<String> getErrorMessages() {
return errorMessages;
}
public void setErrorMessages(List<String> errorMessages) {
this.errorMessages = errorMessages;
}
}