001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.forms.validation;
021
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.Map;
025
026/**
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class ValidationException extends Exception {
030
031    private static final long serialVersionUID = 531665422854150881L;
032
033    public static final String IS_REQUIRED_MSG = "is required";
034
035    public static final String IS_INVALID_MSG = "is invalid";
036
037    protected Map<String, String> invalidFields;
038
039    protected Map<String, String> requiredFields;
040
041    protected transient Form form; // the form that has errors - should be set by the caller
042
043    public ValidationException(String message) {
044        super(message);
045        invalidFields = new HashMap<String, String>();
046        requiredFields = new HashMap<String, String>();
047    }
048
049    public ValidationException(String message, Throwable cause) {
050        super(message, cause);
051        invalidFields = new HashMap<String, String>();
052        requiredFields = new HashMap<String, String>();
053    }
054
055    public ValidationException() {
056        invalidFields = new HashMap<String, String>();
057        requiredFields = new HashMap<String, String>();
058    }
059
060    @Override
061    public String getMessage() {
062        String message = super.getMessage();
063        if (message == null) {
064            StringBuilder buf = new StringBuilder();
065            if (hasRequiredFields()) {
066                for (Map.Entry<String, String> entry : requiredFields.entrySet()) {
067                    String msg = entry.getValue();
068                    buf.append(entry.getKey()).append(": ").append(msg == null ? IS_REQUIRED_MSG : msg).append("\n");
069                }
070            }
071            if (hasInvalidFields()) {
072                for (Map.Entry<String, String> entry : invalidFields.entrySet()) {
073                    String msg = entry.getValue();
074                    buf.append(entry.getKey()).append(": ").append(msg == null ? IS_INVALID_MSG : msg).append("\n");
075                }
076            }
077            return buf.toString();
078        }
079        return message;
080    }
081
082    public String getXmlMessage() {
083        String message = super.getMessage();
084        if (message == null) {
085            StringBuilder buf = new StringBuilder();
086            if (hasRequiredFields()) {
087                for (Map.Entry<String, String> entry : requiredFields.entrySet()) {
088                    String msg = entry.getValue();
089                    buf.append("<li>").append(entry.getKey()).append(": ").append(msg == null ? IS_REQUIRED_MSG : msg);
090                }
091            }
092            if (hasInvalidFields()) {
093                for (Map.Entry<String, String> entry : invalidFields.entrySet()) {
094                    String msg = entry.getValue();
095                    buf.append("<li>").append(entry.getKey()).append(": ").append(msg == null ? IS_INVALID_MSG : msg);
096                }
097            }
098            return buf.toString();
099        }
100        return message;
101    }
102
103    public ValidationException addRequiredField(String key) {
104        requiredFields.put(key, null);
105        return this;
106    }
107
108    public ValidationException addRequiredField(String key, String message) {
109        requiredFields.put(key, message);
110        return this;
111    }
112
113    public ValidationException addInvalidField(String key) {
114        invalidFields.put(key, null);
115        return this;
116    }
117
118    public ValidationException addInvalidField(String key, String message) {
119        invalidFields.put(key, message);
120        return this;
121    }
122
123    public boolean hasFieldErrors() {
124        return !requiredFields.isEmpty() || !invalidFields.isEmpty();
125    }
126
127    public boolean hasInvalidFields() {
128        return !invalidFields.isEmpty();
129    }
130
131    public boolean hasRequiredFields() {
132        return !requiredFields.isEmpty();
133    }
134
135    public Collection<String> getRequiredFields() {
136        return requiredFields.keySet();
137    }
138
139    public Collection<String> getInvalidFields() {
140        return invalidFields.keySet();
141    }
142
143    public boolean hasErrors(String key) {
144        return requiredFields.containsKey(key) || invalidFields.containsKey(key);
145    }
146
147    public String getError(String key) {
148        String message = requiredFields.get(key);
149        return message == null ? invalidFields.get(key) : message;
150    }
151
152    public void setForm(Form form) {
153        this.form = form;
154    }
155
156    public Form getForm() {
157        return form;
158    }
159}