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.forms.impl;
017:
018: import java.util.HashMap;
019: import java.util.HashSet;
020: import java.util.Iterator;
021:
022: import de.odysseus.calyxo.base.Message;
023: import de.odysseus.calyxo.base.Messages;
024: import de.odysseus.calyxo.forms.FormInput;
025: import de.odysseus.calyxo.forms.FormInputResult;
026: import de.odysseus.calyxo.forms.FormResult;
027: import de.odysseus.calyxo.forms.conf.FormConfig;
028:
029: /**
030: * Form result implementation.
031: *
032: * @author Christoph Beck
033: */
034: public class FormResultImpl implements FormResult {
035: private FormImpl form;
036: private Messages messages = new Messages();
037: private HashMap values = new HashMap();
038: private HashSet assertFailedNames;
039: private boolean invalid = false;
040:
041: /**
042: * Constructor
043: */
044: public FormResultImpl(FormImpl form) {
045: super ();
046:
047: this .form = form;
048: }
049:
050: /**
051: * Add input result
052: */
053: void add(FormInputResult value) {
054: FormInput input = value.getFormInput();
055: values.put(input.getName(), value);
056: if (!value.isValid() && !value.isIgnored()) {
057: invalid = true;
058: if (!value.isRelaxed()) {
059: messages.addMessage(input.getName(), value
060: .getFirstMessage());
061: if (input.isArray()) {
062: Iterator iter = value.getLastMessages();
063: while (iter.hasNext()) {
064: messages.addMessage(input.getName(),
065: (Message) iter.next());
066: }
067: }
068: }
069: }
070: }
071:
072: /**
073: * Add failed assertion.
074: * This method adds the specified message as global message to error
075: * messages and flags the inputs referenced during expression
076: * evaluation.
077: * @see #isAssertionFailed(String)
078: */
079: void assertionFailed(AssertVariableResolver resolver,
080: Message message) {
081: invalid = true;
082: if (assertFailedNames == null) {
083: assertFailedNames = new HashSet();
084: }
085: Iterator involved = resolver.getInvolvedFormInputs();
086: while (involved.hasNext()) {
087: FormInput input = (FormInput) involved.next();
088: assertFailedNames.add(input.getName());
089: }
090: messages.addGlobalMessage(message);
091: }
092:
093: /*
094: * (non-Javadoc)
095: * @see de.odysseus.calyxo.forms.FormResult#isAssertionFailed(java.lang.String)
096: */
097: public boolean isAssertionFailed(String name) {
098: return assertFailedNames != null
099: && assertFailedNames.contains(name);
100: }
101:
102: /*
103: * (non-Javadoc)
104: * @see de.odysseus.calyxo.forms.FormResult#isValid()
105: */
106: public boolean isValid() {
107: return !invalid;
108: }
109:
110: /*
111: * (non-Javadoc)
112: * @see de.odysseus.calyxo.forms.FormResult#getFormInputResults()
113: */
114: public Iterator getFormInputResults() {
115: return values.values().iterator();
116: }
117:
118: /*
119: * (non-Javadoc)
120: * @see de.odysseus.calyxo.forms.FormResult#getFormInputResult(java.lang.String)
121: */
122: public FormInputResult getFormInputResult(String name) {
123: return (FormInputResult) values.get(name);
124: }
125:
126: /*
127: * (non-Javadoc)
128: * @see de.odysseus.calyxo.forms.FormResult#getMessages()
129: */
130: public Messages getMessages() {
131: return messages;
132: }
133:
134: /*
135: * (non-Javadoc)
136: * @see de.odysseus.calyxo.forms.FormResult#getFormConfig()
137: */
138: public FormConfig getFormConfig() {
139: return form.getFormConfig();
140: }
141: }
|