KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > common > actions > AbstractFormAction


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.common.actions;
25
26 import java.util.Locale JavaDoc;
27
28 import org.apache.log4j.Logger;
29 import org.infoglue.cms.exception.ConstraintException;
30 import org.infoglue.cms.util.StringManager;
31 import org.infoglue.cms.util.StringManagerFactory;
32
33 import webwork.action.CommandDriven;
34
35
36 /**
37  *
38  *
39  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
40  */

41 public abstract class AbstractFormAction extends AbstractAction implements CommandDriven
42 {
43     private final static Logger logger = Logger.getLogger(AbstractFormAction.class.getName());
44
45   // --- [Constants] -----------------------------------------------------------
46
// --- [Attributes] ----------------------------------------------------------
47

48   //
49
private Errors errors = new Errors();
50
51
52
53   // --- [Static] --------------------------------------------------------------
54
// --- [Constructors] --------------------------------------------------------
55
// --- [Public] --------------------------------------------------------------
56

57   /**
58    *
59    */

60   public Errors getErrors() {
61     return errors;
62   }
63
64
65
66   // --- [X Overrides] ---------------------------------------------------------
67
// --- [webwork.action.Action implementation] --------------------------------
68

69   /**
70    *
71    */

72   public String JavaDoc execute() throws Exception JavaDoc {
73     try {
74       return super.execute();
75     } catch(ConstraintException e) {
76       setErrors(e);
77       return INPUT;
78     }
79   }
80
81
82
83   // --- [Package protected] ---------------------------------------------------
84
// --- [Private] -------------------------------------------------------------
85

86   /**
87    *
88    */

89   private void setErrors(ConstraintException exception) {
90     final Locale JavaDoc locale = getSession().getLocale();
91
92     for(ConstraintException ce = exception; ce != null; ce = ce.getChainedException()) {
93       final String JavaDoc fieldName = ce.getFieldName();
94       final String JavaDoc errorCode = ce.getErrorCode();
95       final String JavaDoc localizedErrorMessage = getLocalizedErrorMessage(locale, errorCode);
96
97       getErrors().addError(fieldName, localizedErrorMessage);
98     }
99     logger.debug(getErrors().toString());
100   }
101
102   /**
103    * <todo>Move to a ConstraintExceptionHelper class?</todo>
104    */

105   private String JavaDoc getLocalizedErrorMessage(Locale JavaDoc locale, String JavaDoc errorCode) {
106     // <todo>fetch packagename from somewhere</todo>
107
final StringManager stringManager = StringManagerFactory.getPresentationStringManager("org.infoglue.cms.entities", locale);
108
109     // check if a specific error message exists - <todo/>
110
// nah, use the general error message
111
return stringManager.getString(errorCode);
112   }
113
114
115
116   // --- [Protected] -----------------------------------------------------------
117
// --- [Inner classes] -------------------------------------------------------
118
}
Popular Tags