001/*
002 *  Copyright 2010 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.forms.processing;
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.plugins.forms.Form;
026import org.ametys.runtime.i18n.I18nizableText;
027
028/**
029 * Form errors.
030 */
031public class FormErrors
032{
033    /** The corresponding form. */
034    protected Form _form;
035    
036    /** The errors as a Map of field ID -> error messages. */
037    protected Map<String, List<I18nizableText>> _errors;
038
039    /** True if the insertion has failed */
040    protected boolean _insertionFailed;
041    
042    /** True if the limit of entries is reached */
043    protected boolean _limitReached;
044    
045    /**
046     * Default constructor.
047     */
048    public FormErrors()
049    {
050        this(null, new LinkedHashMap<String, List<I18nizableText>>());
051    }
052    
053    /**
054     * Constructor with parameters.
055     * @param form the Form object.
056     * @param errors the errors.
057     */
058    public FormErrors(Form form, Map<String, List<I18nizableText>> errors)
059    {
060        this._form = form;
061        this._errors = errors;
062        this._insertionFailed = false;
063        this._limitReached = false;
064    }
065    
066    /**
067     * Get the form.
068     * @return the form
069     */
070    public Form getForm()
071    {
072        return _form;
073    }
074    
075    /**
076     * Set the form.
077     * @param form the form to set
078     */
079    public void setForm(Form form)
080    {
081        this._form = form;
082    }
083    
084    /**
085     * True if the insertion has failed
086     * @return <code>true</code> if the insertion has failed
087     */
088    public boolean hasInsertionFailed()
089    {
090        return _insertionFailed;
091    }
092    
093    /**
094     * Set if the insertion has failed
095     * @param insertionFailed true if the insertion has failed
096     */
097    public void setInsertionFailed(boolean insertionFailed)
098    {
099        this._insertionFailed = insertionFailed;
100    }
101    
102    /**
103     * Get the errors.
104     * @return the errors
105     */
106    public Map<String, List<I18nizableText>> getErrors()
107    {
108        return _errors;
109    }
110    
111    /**
112     * Set the errors.
113     * @param errors the errors to set
114     */
115    public void setErrors(Map<String, List<I18nizableText>> errors)
116    {
117        this._errors = errors;
118    }
119    
120    /**
121     * Add an error.
122     * @param fieldId the field ID.
123     * @param error the error message.
124     */
125    public void addError(String fieldId, I18nizableText error)
126    {
127        if (StringUtils.isNotEmpty(fieldId) && error != null)
128        {
129            if (_errors.containsKey(fieldId))
130            {
131                _errors.get(fieldId).add(error);
132            }
133            else
134            {
135                List<I18nizableText> errors = new ArrayList<>();
136                errors.add(error);
137                
138                _errors.put(fieldId, errors);
139            }
140        }
141    }
142    
143    /**
144     * Add an error list.
145     * @param fieldId the field ID.
146     * @param errors the error messages.
147     */
148    public void addErrors(String fieldId, List<I18nizableText> errors)
149    {
150        if (StringUtils.isNotEmpty(fieldId) && !errors.isEmpty())
151        {
152            if (_errors.containsKey(fieldId))
153            {
154                _errors.get(fieldId).addAll(errors);
155            }
156            else
157            {
158                _errors.put(fieldId, errors);
159            }
160        }
161    }
162    
163    /**
164     * Tests if the form has errors.
165     * @return true if there are errors, false otherwise.
166     */
167    public boolean hasErrors()
168    {
169        for (List<I18nizableText> errList : _errors.values())
170        {
171            if (!errList.isEmpty())
172            {
173                return true;
174            }
175        }
176        return false;
177    }
178
179    /**
180     * True if the entries limit has been reached
181     * @return <code>true</code> if the entries limit has been reached
182     */
183    public boolean hasLimitReached()
184    {
185        return _limitReached;
186    }
187    
188    /**
189     * Set if the entries limit has been reached
190     * @param limitReached true if the entries limit has been reached
191     */
192    public void setLimitReached(boolean limitReached)
193    {
194        this._limitReached = limitReached;
195    }
196    
197}