001/* 002 * Copyright 2024 Anyware Services 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 */ 016package org.ametys.runtime.parameter; 017 018import java.util.LinkedHashMap; 019import java.util.List; 020import java.util.Map; 021import java.util.Map.Entry; 022import java.util.stream.Collectors; 023 024import org.ametys.runtime.i18n.I18nizableText; 025 026/** 027 * Object for storing validation results. 028 */ 029public class ValidationResults 030{ 031 private Map<String, ValidationResult> _results = new LinkedHashMap<>(); 032 033 /** 034 * Test if there is at least one info. 035 * @return <code>true</code> if there is at least one info, 036 * <code>false</code> otherwise. 037 */ 038 public boolean hasInfos() 039 { 040 return _results.values() 041 .stream() 042 .anyMatch(ValidationResult::hasInfos); 043 } 044 045 /** 046 * Retrieves the information. 047 * @return the information. 048 */ 049 public Map<String, List<I18nizableText>> getAllInfos() 050 { 051 return _results.entrySet() 052 .stream() 053 .filter(entry -> entry.getValue().hasInfos()) 054 .map(entry -> Map.entry(entry.getKey(), entry.getValue().getInfos())) 055 .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); 056 } 057 058 /** 059 * Test if there is at least one warning. 060 * @return <code>true</code> if there is at least one warning, 061 * <code>false</code> otherwise. 062 */ 063 public boolean hasWarnings() 064 { 065 return _results.values() 066 .stream() 067 .anyMatch(ValidationResult::hasWarnings); 068 } 069 070 /** 071 * Retrieves the warnings. 072 * @return the warnings. 073 */ 074 public Map<String, List<I18nizableText>> getAllWarnings() 075 { 076 return _results.entrySet() 077 .stream() 078 .filter(entry -> entry.getValue().hasWarnings()) 079 .map(entry -> Map.entry(entry.getKey(), entry.getValue().getWarnings())) 080 .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); 081 } 082 083 /** 084 * Test if there is at least one error. 085 * @return <code>true</code> if there is at least one error, 086 * <code>false</code> otherwise. 087 */ 088 public boolean hasErrors() 089 { 090 return _results.values() 091 .stream() 092 .anyMatch(ValidationResult::hasErrors); 093 } 094 095 /** 096 * Retrieves the errors. 097 * @return the errors. 098 */ 099 public Map<String, List<I18nizableText>> getAllErrors() 100 { 101 return _results.entrySet() 102 .stream() 103 .filter(entry -> entry.getValue().hasErrors()) 104 .map(entry -> Map.entry(entry.getKey(), entry.getValue().getErrors())) 105 .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); 106 } 107 108 /** 109 * Retrieves the results. 110 * @return the results. 111 */ 112 public Map<String, ValidationResult> getResults() 113 { 114 return _results; 115 } 116 117 /** 118 * Add a validation result. 119 * @param dataPath the data path or <code>null</code> 120 * if global to the content. 121 * @param result the result to add 122 */ 123 public void addResult(String dataPath, ValidationResult result) 124 { 125 _results.put(dataPath, result); 126 } 127 128 /** 129 * Add validation results. 130 * @param results the results to add 131 */ 132 public void addResults(ValidationResults results) 133 { 134 _results.putAll(results.getResults()); 135 } 136 137 @Override 138 public String toString() 139 { 140 return _results.toString(); 141 } 142}