001/*
002 *  Copyright 2011 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.plugins.survey.answer;
017
018import java.util.ArrayList;
019import java.util.LinkedHashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang.StringUtils;
024
025import org.ametys.runtime.i18n.I18nizableText;
026
027/**
028 * Survey errors.
029 */
030public class SurveyErrors
031{
032    /** The errors as a Map of field ID -> error messages. */
033    protected Map<String, List<I18nizableText>> _errors;
034    
035    /**
036     * Default constructor.
037     */
038    public SurveyErrors()
039    {
040        this(new LinkedHashMap<String, List<I18nizableText>>());
041    }
042    
043    /**
044     * Constructor with parameters.
045     * @param errors the errors.
046     */
047    public SurveyErrors(Map<String, List<I18nizableText>> errors)
048    {
049        this._errors = errors;
050    }
051    
052    /**
053     * Get the errors.
054     * @return the errors
055     */
056    public Map<String, List<I18nizableText>> getErrors()
057    {
058        return _errors;
059    }
060    
061    /**
062     * Set the errors.
063     * @param errors the errors to set
064     */
065    public void setErrors(Map<String, List<I18nizableText>> errors)
066    {
067        this._errors = errors;
068    }
069    
070    /**
071     * Add an error.
072     * @param fieldId the field ID.
073     * @param error the error message.
074     */
075    public void addError(String fieldId, I18nizableText error)
076    {
077        if (StringUtils.isNotEmpty(fieldId) && error != null)
078        {
079            if (_errors.containsKey(fieldId))
080            {
081                _errors.get(fieldId).add(error);
082            }
083            else
084            {
085                List<I18nizableText> errors = new ArrayList<>();
086                errors.add(error);
087                
088                _errors.put(fieldId, errors);
089            }
090        }
091    }
092    
093    /**
094     * Add an error list.
095     * @param fieldId the field ID.
096     * @param errors the error messages.
097     */
098    public void addErrors(String fieldId, List<I18nizableText> errors)
099    {
100        if (StringUtils.isNotEmpty(fieldId) && !errors.isEmpty())
101        {
102            if (_errors.containsKey(fieldId))
103            {
104                _errors.get(fieldId).addAll(errors);
105            }
106            else
107            {
108                _errors.put(fieldId, errors);
109            }
110        }
111    }
112    
113    /**
114     * Tests if the form has errors.
115     * @return true if there are errors, false otherwise.
116     */
117    public boolean hasErrors()
118    {
119        for (List<I18nizableText> errList : _errors.values())
120        {
121            if (!errList.isEmpty())
122            {
123                return true;
124            }
125        }
126        return false;
127    }
128    
129}