001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
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: package de.odysseus.calyxo.struts.forms;
017:
018: import java.io.Serializable;
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.Locale;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.struts.action.ActionErrors;
026: import org.apache.struts.action.ActionMapping;
027: import org.apache.struts.action.ActionMessage;
028: import org.apache.struts.action.ActionMessages;
029:
030: import de.odysseus.calyxo.base.I18nSupport;
031: import de.odysseus.calyxo.base.Message;
032: import de.odysseus.calyxo.base.Messages;
033: import de.odysseus.calyxo.base.conf.ConfigException;
034: import de.odysseus.calyxo.forms.Form;
035: import de.odysseus.calyxo.forms.FormInputValues;
036: import de.odysseus.calyxo.forms.FormResult;
037: import de.odysseus.calyxo.forms.FormsSupport;
038:
039: /**
040: * This class performs the form validation.
041: *
042: * @author Christoph Beck
043: */
044: public class FormsDelegate implements Serializable {
045: private static final String FORMATTED_MESSAGE_KEY = "";
046:
047: /**
048: * Transform a calyxo message into a struts action message.
049: * Calyxo messages and struts action messages differ in that
050: * calyxo messages may specify a bundle name.
051: * If the calyxo message specifies a bundle, this method will
052: * format the message and produce a struts action message
053: * with key <code>""</code> and the formatted message string
054: * as argument.
055: *
056: * @param request the request we're in
057: * @param locale the desired locale
058: * @param i18n 18n support instance
059: * @param message the message to be transformed
060: * @return a struts action message
061: */
062: protected ActionMessage createActionMessage(
063: HttpServletRequest request, Locale locale,
064: I18nSupport i18n, Message message) {
065:
066: ArrayList values = new ArrayList();
067: Iterator args = message.getArgs();
068: while (args.hasNext()) {
069: Message.Arg arg = (Message.Arg) args.next();
070: values.add(arg.getValue(locale, i18n));
071: }
072: if (message.getBundle() != null) {
073: String string = i18n.getMessage(locale,
074: message.getBundle(), message.getKey(), values
075: .toArray());
076: return new ActionMessage(FORMATTED_MESSAGE_KEY, string);
077: }
078: return new ActionMessage(message.getKey(), values.toArray());
079: }
080:
081: protected ActionErrors doValidate(ActionMapping mapping,
082: HttpServletRequest request, FormInputValues inputs)
083: throws Exception {
084:
085: I18nSupport i18n = I18nSupport.getInstance(request);
086: Locale locale = i18n.getLocale(request);
087: FormsSupport support = FormsSupport.getInstance(request);
088: Form form = support.getForm(request, locale, mapping.getPath());
089: if (form == null) {
090: throw new ConfigException("No form for action '"
091: + mapping.getPath() + "'");
092: }
093: ActionErrors errors = null;
094: // execute validation
095: FormResult result = form.validate(request, inputs);
096: if (!result.isValid()) { // collect error messages
097: errors = new ActionErrors();
098: Messages messages = result.getMessages();
099: Iterator names = messages.getKeys();
100: while (names.hasNext()) {
101: String name = (String) names.next();
102: if (name != Messages.GLOBAL_KEY) {
103: Message message = messages.getFirstMessage(name);
104: ActionMessage error = createActionMessage(request,
105: locale, i18n, message);
106: errors.add(name, error);
107: } else {
108: Iterator iter = messages.getGlobalMessages();
109: while (iter.hasNext()) {
110: Message message = (Message) iter.next();
111: ActionMessage error = createActionMessage(
112: request, locale, i18n, message);
113: errors
114: .add(ActionMessages.GLOBAL_MESSAGE,
115: error);
116: }
117: }
118: }
119: }
120: request.setAttribute(FormsSupport.FORM_RESULT_KEY, result);
121: return errors;
122: }
123:
124: public ActionErrors handleException(Exception e) {
125: ActionErrors errors = new ActionErrors();
126: ActionMessage error = new ActionMessage(FORMATTED_MESSAGE_KEY,
127: e.getMessage());
128: errors.add(ActionMessages.GLOBAL_MESSAGE, error);
129: return errors;
130: }
131:
132: /**
133: * Validate the properties that have been set for this HTTP request,
134: * and return an <code>ActionErrors</code> object that encapsulates any
135: * validation errors that have been found. If no errors are found,
136: * return <code>null</code> or an <code>ActionErrors</code> object with
137: * no recorded error messages.
138: * <p>
139: *
140: * @param mapping The mapping used to select this instance
141: * @param request The servlet request we are processing
142: */
143: public ActionErrors validate(ActionMapping mapping,
144: HttpServletRequest request, FormInputValues inputs) {
145: try {
146: return doValidate(mapping, request, inputs);
147: } catch (Exception e) {
148: return handleException(e);
149: }
150: }
151: }
|