1 /*
2 * Copyright (c) 2002-2006 by OpenSymphony
3 * All rights reserved.
4 */
5 package com.opensymphony.xwork2;
6
7 import java.io.Serializable;
8 import java.util.Collection;
9 import java.util.List;
10 import java.util.Locale;
11 import java.util.Map;
12 import java.util.ResourceBundle;
13
14 import com.opensymphony.xwork2.util.ValueStack;
15 import com.opensymphony.xwork2.util.logging.Logger;
16 import com.opensymphony.xwork2.util.logging.LoggerFactory;
17
18
19 /**
20 * Provides a default implementation for the most common actions.
21 * See the documentation for all the interfaces this class implements for more detailed information.
22 */
23 public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
24
25 protected static Logger LOG = LoggerFactory.getLogger(ActionSupport.class);
26
27 private final transient TextProvider textProvider = new TextProviderFactory().createInstance(getClass(), this);
28 private final ValidationAwareSupport validationAware = new ValidationAwareSupport();
29
30
31 public void setActionErrors(Collection errorMessages) {
32 validationAware.setActionErrors(errorMessages);
33 }
34
35 public Collection getActionErrors() {
36 return validationAware.getActionErrors();
37 }
38
39 public void setActionMessages(Collection messages) {
40 validationAware.setActionMessages(messages);
41 }
42
43 public Collection getActionMessages() {
44 return validationAware.getActionMessages();
45 }
46
47 /**
48 * @deprecated Use {@link #getActionErrors()}.
49 */
50 public Collection getErrorMessages() {
51 return getActionErrors();
52 }
53
54 /**
55 * @deprecated Use {@link #getFieldErrors()}.
56 */
57 public Map getErrors() {
58 return getFieldErrors();
59 }
60
61 public void setFieldErrors(Map errorMap) {
62 validationAware.setFieldErrors(errorMap);
63 }
64
65 public Map getFieldErrors() {
66 return validationAware.getFieldErrors();
67 }
68
69 public Locale getLocale() {
70 ActionContext ctx = ActionContext.getContext();
71 if (ctx != null) {
72 return ctx.getLocale();
73 } else {
74 LOG.debug("Action context not initialized");
75 return null;
76 }
77 }
78
79 public String getText(String aTextName) {
80 return textProvider.getText(aTextName);
81 }
82
83 public String getText(String aTextName, String defaultValue) {
84 return textProvider.getText(aTextName, defaultValue);
85 }
86
87 public String getText(String aTextName, String defaultValue, String obj) {
88 return textProvider.getText(aTextName, defaultValue, obj);
89 }
90
91 public String getText(String aTextName, List args) {
92 return textProvider.getText(aTextName, args);
93 }
94
95 public String getText(String key, String[] args) {
96 return textProvider.getText(key, args);
97 }
98
99 public String getText(String aTextName, String defaultValue, List args) {
100 return textProvider.getText(aTextName, defaultValue, args);
101 }
102
103 public String getText(String key, String defaultValue, String[] args) {
104 return textProvider.getText(key, defaultValue, args);
105 }
106
107 public String getText(String key, String defaultValue, List args, ValueStack stack) {
108 return textProvider.getText(key, defaultValue, args, stack);
109 }
110
111 public String getText(String key, String defaultValue, String[] args, ValueStack stack) {
112 return textProvider.getText(key, defaultValue, args, stack);
113 }
114
115 public ResourceBundle getTexts() {
116 return textProvider.getTexts();
117 }
118
119 public ResourceBundle getTexts(String aBundleName) {
120 return textProvider.getTexts(aBundleName);
121 }
122
123 public void addActionError(String anErrorMessage) {
124 validationAware.addActionError(anErrorMessage);
125 }
126
127 public void addActionMessage(String aMessage) {
128 validationAware.addActionMessage(aMessage);
129 }
130
131 public void addFieldError(String fieldName, String errorMessage) {
132 validationAware.addFieldError(fieldName, errorMessage);
133 }
134
135 public String input() throws Exception {
136 return INPUT;
137 }
138
139 public String doDefault() throws Exception {
140 return SUCCESS;
141 }
142
143 /**
144 * A default implementation that does nothing an returns "success".
145 * <p/>
146 * Subclasses should override this method to provide their business logic.
147 * <p/>
148 * See also {@link com.opensymphony.xwork2.Action#execute()}.
149 *
150 * @return returns {@link #SUCCESS}
151 * @throws Exception can be thrown by subclasses.
152 */
153 public String execute() throws Exception {
154 return SUCCESS;
155 }
156
157 public boolean hasActionErrors() {
158 return validationAware.hasActionErrors();
159 }
160
161 public boolean hasActionMessages() {
162 return validationAware.hasActionMessages();
163 }
164
165 public boolean hasErrors() {
166 return validationAware.hasErrors();
167 }
168
169 public boolean hasFieldErrors() {
170 return validationAware.hasFieldErrors();
171 }
172
173 /**
174 * Clears field errors. Useful for Continuations and other situations
175 * where you might want to clear parts of the state on the same action.
176 */
177 public void clearFieldErrors() {
178 validationAware.clearFieldErrors();
179 }
180
181 /**
182 * Clears action errors. Useful for Continuations and other situations
183 * where you might want to clear parts of the state on the same action.
184 */
185 public void clearActionErrors() {
186 validationAware.clearActionErrors();
187 }
188
189 /**
190 * Clears messages. Useful for Continuations and other situations
191 * where you might want to clear parts of the state on the same action.
192 */
193 public void clearMessages() {
194 validationAware.clearMessages();
195 }
196
197 /**
198 * Clears all errors. Useful for Continuations and other situations
199 * where you might want to clear parts of the state on the same action.
200 */
201 public void clearErrors() {
202 validationAware.clearErrors();
203 }
204
205 /**
206 * Clears all errors and messages. Useful for Continuations and other situations
207 * where you might want to clear parts of the state on the same action.
208 */
209 public void clearErrorsAndMessages() {
210 validationAware.clearErrorsAndMessages();
211 }
212
213 /**
214 * A default implementation that validates nothing.
215 * Subclasses should override this method to provide validations.
216 */
217 public void validate() {
218 }
219
220 public Object clone() throws CloneNotSupportedException {
221 return super.clone();
222 }
223
224 /**
225 * <!-- START SNIPPET: pause-method -->
226 * Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return
227 * the specified result, such as {@link #SUCCESS}, {@link #INPUT}, etc.
228 * <p/>
229 *
230 * The next time this action is invoked (and using the same continuation ID), the method will resume immediately
231 * after where this method was called, with the entire call stack in the execute method restored.
232 * <p/>
233 *
234 * Note: this method can <b>only</b> be called within the {@link #execute()} method.
235 * <!-- END SNIPPET: pause-method -->
236 *
237 * @param result the result to return - the same type of return value in the {@link #execute()} method.
238 */
239 public void pause(String result) {
240 }
241
242 }