001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.forms.validation;
023
024import java.util.Collection;
025import java.util.HashMap;
026import java.util.Map;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public class ValidationException extends Exception {
032
033    private static final long serialVersionUID = 531665422854150881L;
034
035    public static final String IS_REQUIRED_MSG = "is required";
036
037    public static final String IS_INVALID_MSG = "is invalid";
038
039    protected Map<String, String> invalidFields;
040
041    protected Map<String, String> requiredFields;
042
043    protected transient Form form; // the form that has errors - should be set by the caller
044
045    public ValidationException(String message) {
046        super(message);
047        invalidFields = new HashMap<String, String>();
048        requiredFields = new HashMap<String, String>();
049    }
050
051    public ValidationException(String message, Throwable cause) {
052        super(message, cause);
053        invalidFields = new HashMap<String, String>();
054        requiredFields = new HashMap<String, String>();
055    }
056
057    public ValidationException() {
058        invalidFields = new HashMap<String, String>();
059        requiredFields = new HashMap<String, String>();
060    }
061
062    @Override
063    public String getMessage() {
064        String message = super.getMessage();
065        if (message == null) {
066            StringBuilder buf = new StringBuilder();
067            if (hasRequiredFields()) {
068                for (Map.Entry<String, String> entry : requiredFields.entrySet()) {
069                    String msg = entry.getValue();
070                    buf.append(entry.getKey()).append(": ").append(msg == null ? IS_REQUIRED_MSG : msg).append("\n");
071                }
072            }
073            if (hasInvalidFields()) {
074                for (Map.Entry<String, String> entry : invalidFields.entrySet()) {
075                    String msg = entry.getValue();
076                    buf.append(entry.getKey()).append(": ").append(msg == null ? IS_INVALID_MSG : msg).append("\n");
077                }
078            }
079            return buf.toString();
080        }
081        return message;
082    }
083
084    public String getXmlMessage() {
085        String message = super.getMessage();
086        if (message == null) {
087            StringBuilder buf = new StringBuilder();
088            if (hasRequiredFields()) {
089                for (Map.Entry<String, String> entry : requiredFields.entrySet()) {
090                    String msg = entry.getValue();
091                    buf.append("<li>").append(entry.getKey()).append(": ").append(msg == null ? IS_REQUIRED_MSG : msg);
092                }
093            }
094            if (hasInvalidFields()) {
095                for (Map.Entry<String, String> entry : invalidFields.entrySet()) {
096                    String msg = entry.getValue();
097                    buf.append("<li>").append(entry.getKey()).append(": ").append(msg == null ? IS_INVALID_MSG : msg);
098                }
099            }
100            return buf.toString();
101        }
102        return message;
103    }
104
105    public ValidationException addRequiredField(String key) {
106        requiredFields.put(key, null);
107        return this;
108    }
109
110    public ValidationException addRequiredField(String key, String message) {
111        requiredFields.put(key, message);
112        return this;
113    }
114
115    public ValidationException addInvalidField(String key) {
116        invalidFields.put(key, null);
117        return this;
118    }
119
120    public ValidationException addInvalidField(String key, String message) {
121        invalidFields.put(key, message);
122        return this;
123    }
124
125    public boolean hasFieldErrors() {
126        return !requiredFields.isEmpty() || !invalidFields.isEmpty();
127    }
128
129    public boolean hasInvalidFields() {
130        return !invalidFields.isEmpty();
131    }
132
133    public boolean hasRequiredFields() {
134        return !requiredFields.isEmpty();
135    }
136
137    public Collection<String> getRequiredFields() {
138        return requiredFields.keySet();
139    }
140
141    public Collection<String> getInvalidFields() {
142        return invalidFields.keySet();
143    }
144
145    public boolean hasErrors(String key) {
146        return requiredFields.containsKey(key) || invalidFields.containsKey(key);
147    }
148
149    public String getError(String key) {
150        String message = requiredFields.get(key);
151        return message == null ? invalidFields.get(key) : message;
152    }
153
154    public void setForm(Form form) {
155        this.form = form;
156    }
157
158    public Form getForm() {
159        return form;
160    }
161}