KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > models > ErrorModel


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: ErrorModel.java,v 1.4 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.models;
21
22 import java.util.*;
23 import org.enhydra.barracuda.core.comp.*;
24 import org.enhydra.barracuda.core.event.*;
25 import org.enhydra.barracuda.core.forms.*;
26 import org.enhydra.barracuda.contrib.dbroggisch.display.*;
27 import org.apache.log4j.*;
28
29 /** ErrorModel is a special Model for displaying form validation errors.
30  * During the control events, the model if filled by calling the static {@link
31  * #create } methods with the form validation exceptions<br>
32  * During the view phase, the model acts as a IterativeModel and displays the error
33  * messages.
34  */

35 public class ErrorModel extends IterativeCollectionModel {
36
37     protected static Logger logger = Logger.getLogger(ErrorModel.class.getName());
38
39     public static final String JavaDoc DEFAULT_ERRORMODEL = "Error";
40     public static final String JavaDoc DIRECTIVE_VISIBLE = "Has_Errors";
41
42     public ErrorModel(ViewContext vc) {
43         this(DEFAULT_ERRORMODEL, vc);
44     }
45
46     public ErrorModel(String JavaDoc name, ViewContext vc) {
47         super(name);
48         List errorList = (List)vc.getEventContext().getState(name);
49         if (logger.isDebugEnabled()) logger.debug("retrieving exception list from event context: " + errorList);
50         if (errorList == null) errorList = new Vector();
51         if (logger.isDebugEnabled()) logger.debug("errorlist has " + errorList.size() + " items.");
52         setModels(errorList);
53     }
54
55     public Object JavaDoc getItem(String JavaDoc key) {
56         if (logger.isDebugEnabled()) {
57             if (_current== null) {
58                 logger.error("_current is null");
59             } else {
60                 logger.debug("_current: " + _current + _current.getClass().getName());
61             }
62         }
63         return ((Exception JavaDoc)_current).getMessage();
64     }
65
66     public boolean processDirective(TemplateDirective td) {
67         if (td.getCommand().equals(DIRECTIVE_VISIBLE) && (getModels().size()==0)) return false;
68         return true;
69     }
70
71     /*public static void create(EventContext context, ValidationException ex) {
72         create(DEFAULT_ERRORMODEL, context, ex);
73     }
74
75     public static void create(String name, EventContext context, ValidationException ex) {
76         if (logger.isDebugEnabled()) logger.debug("Storing exception list to event context");
77         List l = ex.getExceptionList();
78         context.putState(name, l);
79     }*/

80
81     public static void create(EventContext context, Exception JavaDoc ex) {
82         create(DEFAULT_ERRORMODEL, context, ex);
83     }
84
85     public static void create(String JavaDoc name, EventContext context, Exception JavaDoc ex) {
86         if (logger.isDebugEnabled()) logger.debug("Storing single exception to event context");
87             List l;
88           if (ex instanceof ValidationException) {
89            l = ((ValidationException)ex).getExceptionList();
90             } else {
91                 l = (List)context.getState(name);
92                 if (l==null) l = new Vector();
93                 l.add(ex);
94             }
95         context.putState(name, l);
96     }
97
98     public static void remove(EventContext context) {
99         remove(DEFAULT_ERRORMODEL, context);
100     }
101
102     public static void remove(String JavaDoc name, EventContext context) {
103         context.removeState(name);
104     }
105
106     public static boolean exists(EventContext context) {
107         return exists(DEFAULT_ERRORMODEL, context);
108     }
109
110     public static boolean exists(String JavaDoc name, EventContext context) {
111         return (context.getState(name) != null);
112     }
113 }
114
115
116
Popular Tags