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