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.user;
017
018import java.util.List;
019import java.util.Map;
020
021import org.ametys.runtime.i18n.I18nizableText;
022
023/**
024 * Exception for bad modification of the users list.
025 */
026public class InvalidModificationException extends Exception
027{
028    private Map<String, List<I18nizableText>>  _fields;
029    
030    /**
031     * Default constructor.
032     */
033    public InvalidModificationException()
034    {
035        // Nothing to do
036    }
037
038    /**
039     * Constructor with error fields
040     * @param fields The fields is having errors
041     */
042    public InvalidModificationException(Map<String, List<I18nizableText>> fields)
043    {
044        _fields = fields;
045    }
046
047    /**
048     * Constructor with a message.
049     * @param message The message.
050     */
051    public InvalidModificationException(String message)
052    {
053        super(message);
054    }
055
056    /**
057     * Constructor with a message and error fields
058     * @param message the message
059     * @param fields The fields is having errors
060     */
061    public InvalidModificationException(String message, Map<String, List<I18nizableText>> fields)
062    {
063        super(message);
064        _fields = fields;
065    }
066
067     /**
068      * Constructor with a cause.
069      * @param cause The cause.
070      */
071    public InvalidModificationException(Exception cause)
072    {
073        super(cause);
074    }
075
076    /**
077     * Constructor with cause and error fields
078     * @param cause the cause
079     * @param fields The fields is having errors
080     */
081    public InvalidModificationException(Exception cause, Map<String, List<I18nizableText>> fields)
082    {
083        super(cause);
084        _fields = fields;
085    }
086
087     /**
088      * Constructor with a message and a cause.
089      * @param message The message.
090      * @param cause The cause.
091      */
092    public InvalidModificationException(String message, Exception cause)
093    {
094        super(message, cause);
095    }
096
097    /**
098     * Constructor with a message, a cause and a set of error fields
099     * @param message The message.
100     * @param cause The cause.
101     * @param fields The fields is having errors
102     */
103    public InvalidModificationException(String message, Exception cause, Map<String, List<I18nizableText>> fields)
104    {
105        super(message, cause);
106        _fields = fields;
107    }
108    
109    @Override
110    public String getMessage()
111    {
112        if (_fields != null && _fields.size() > 0)
113        {
114            StringBuffer fields = new StringBuffer(" [");
115            for (String field : _fields.keySet())
116            {
117                if (fields.length() > 2)
118                {
119                    fields.append(", ");
120                }
121                fields.append(field);
122            }
123            fields.append("]");
124        }
125        return super.getMessage();
126    }
127    
128    /**
129     * Get the error fields
130     * @return fields as a set of ids (may be null or empty)
131     */
132    public Map<String, List<I18nizableText>> getFieldErrors()
133    {
134        return _fields;
135    }
136}