01: /*
02: * (C) Copyright 2004 Nabh Information Systems, Inc.
03: *
04: * All copyright notices regarding Nabh's products MUST remain
05: * intact in the scripts and in the outputted HTML.
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21: package com.nabhinc.portlet.mvcportlet.core;
22:
23: import java.util.HashMap;
24: import java.util.Vector;
25:
26: /**
27: * Represents a collection errors resulting from a form validation.
28: *
29: * @author Padmanabh Dabke
30: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
31: */
32: public class FormErrors {
33: private String feFormName = null;
34: private FormError[] feFormErrors = null;
35: private HashMap feFormErrorMap = new HashMap(5);
36:
37: /**
38: * Creates a new error collection.
39: * @param formName Form corresponding to teh errors
40: * @param errors Vector of FormError objects.
41: */
42: @SuppressWarnings("unchecked")
43: public FormErrors(String formName, Vector errors) {
44: feFormName = formName;
45: int num = errors.size();
46: feFormErrors = new FormError[num];
47: for (int i = 0; i < num; i++) {
48: feFormErrors[i] = (FormError) errors.elementAt(i);
49: feFormErrorMap.put(feFormErrors[i].getFieldName(),
50: feFormErrors[i].getError());
51: }
52: }
53:
54: /**
55: * Returns an array of FormError objects in the collection.
56: * @return
57: */
58: public FormError[] getFormErrors() {
59: return feFormErrors;
60: }
61:
62: /**
63: * Returns true if the specified field was invalid.
64: * @param fieldName Field name
65: * @return true if there is an error associated with the field, false otherwise.
66: */
67: public boolean isErrorField(String fieldName) {
68: if (feFormErrorMap.get(fieldName) == null) {
69: return false;
70: } else {
71: return true;
72: }
73: }
74:
75: /**
76: * Returns error key for the requested field name. If
77: * there is no error for the field, it returns null.
78: * @param fieldName
79: * @return
80: */
81: public String getError(String fieldName) {
82: return (String) feFormErrorMap.get(fieldName);
83: }
84:
85: /**
86: * Returns form name that corresponds to the errors.
87: * @return
88: */
89: public String getFormName() {
90: return feFormName;
91: }
92: }
|