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