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.convert;
017:
018: import java.text.ParseException;
019: import java.util.Locale;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpSession;
023:
024: import de.odysseus.calyxo.base.ModuleContext;
025: import de.odysseus.calyxo.base.ModuleSupport;
026: import de.odysseus.calyxo.base.util.PropertyUtils;
027: import de.odysseus.calyxo.forms.Converter;
028: import de.odysseus.calyxo.forms.view.ListModel;
029:
030: /**
031: * List model converter.
032: *
033: * @author Oliver Stuhr
034: */
035: public class ListModelConverter implements Converter {
036: private static final int UNKNOWN = -1, REQUEST = 0, SESSION = 1,
037: MODULE = 2, APPL = 3;
038: private int scope_;
039:
040: private String scope;
041: private String attribute;
042: private String property;
043:
044: /**
045: * Default constructor.
046: */
047: public ListModelConverter() {
048: super ();
049: }
050:
051: protected ListModel getListModel(HttpServletRequest request) {
052: if (scope_ == UNKNOWN) // TODO? ConfigException
053: throw new RuntimeException("Unknown scope '" + scope + "'!");
054:
055: Object object = null;
056: switch (scope_) {
057: case REQUEST:
058: object = request.getAttribute(attribute);
059: break;
060: case SESSION:
061: HttpSession session = request.getSession(false);
062: if (session != null)
063: object = session.getAttribute(attribute);
064: break;
065: case MODULE:
066: ModuleContext context = ModuleSupport.getInstance(request)
067: .getModuleContext(request);
068: object = context.getAttribute(attribute);
069: break;
070: case APPL:
071: context = ModuleSupport.getInstance(request)
072: .getModuleContext(request);
073: object = context.getServletContext()
074: .getAttribute(attribute);
075: break;
076: case UNKNOWN:
077: throw new RuntimeException("Unknown scope '" + scope + "'!");
078: }
079:
080: ListModel result = null;
081: if (property == null) {
082: result = (ListModel) object;
083: } else {
084: try {
085: result = (ListModel) PropertyUtils.getNestedProperty(
086: object, property);
087: } catch (Exception e) { // TODO? ConfigException
088: throw new RuntimeException("ListModel '" + attribute
089: + "." + property + "' was not found in "
090: + scope + " scope!");
091: }
092: }
093: if (result == null) { // TODO? ConfigException
094: String s = property == null ? attribute : attribute + "."
095: + property;
096: throw new RuntimeException("ListModel '" + s
097: + "' was not found in " + scope + " scope!");
098: }
099: return result;
100: }
101:
102: /* (non-Javadoc)
103: * @see de.odysseus.calyxo.forms.Converter#format(javax.servlet.http.HttpServletRequest, java.lang.Object)
104: */
105: public String format(HttpServletRequest request, Object value) {
106: // String key = getListModel(request).getKey(value);
107: // if (key == null && value != null) {
108: // throw new IllegalArgumentException("Value is not contained in the list model: " + value);
109: // }
110: // return key;
111: return getListModel(request).getKey(value);
112: }
113:
114: /* (non-Javadoc)
115: * @see de.odysseus.calyxo.forms.Converter#parse(javax.servlet.http.HttpServletRequest, java.lang.String)
116: */
117: public Object parse(HttpServletRequest request, String key)
118: throws ParseException {
119: ListModel listModel = getListModel(request);
120: if (!listModel.containsKey(key)) {
121: if (key == null || key.length() == 0) {
122: if (listModel.getKey(null) != null) {
123: throw new ParseException(
124: "Cannot parse empty/null input", 0);
125: }
126: return null;
127: }
128: throw new ParseException("Unknown list model key", 0);
129: }
130: return listModel.getValue(key);
131: }
132:
133: /* (non-Javadoc)
134: * @see de.odysseus.calyxo.forms.impl.ValidationEngine#localize(java.util.Locale)
135: */
136: public void localize(Locale locale) {
137: }
138:
139: /* (non-Javadoc)
140: * @see de.odysseus.calyxo.forms.impl.ValidationEngine#isSharable()
141: */
142: public boolean isSharable() {
143: return true;
144: }
145:
146: /**
147: * Get the attribute property
148: */
149: public String getAttribute() {
150: return attribute;
151: }
152:
153: /**
154: * Get the attribute property
155: */
156: public void setAttribute(String string) {
157: attribute = string;
158: }
159:
160: /**
161: * Get the property property
162: */
163: public String getProperty() {
164: return property;
165: }
166:
167: /**
168: * Set the property property
169: */
170: public void setProperty(String string) {
171: property = string;
172: }
173:
174: /**
175: * Get the scope property
176: */
177: public String getScope() {
178: return scope;
179: }
180:
181: /**
182: * Set the scope property
183: * @param string one of <code>request</code>, <code>session</code>, <code>module</code>, <code>application</code>
184: */
185: public void setScope(String string) {
186: scope = string;
187:
188: if (scope.equals("request"))
189: scope_ = REQUEST;
190: else if (scope.equals("session"))
191: scope_ = SESSION;
192: else if (scope.equals("module"))
193: scope_ = MODULE;
194: else if (scope.equals("application"))
195: scope_ = APPL;
196: else
197: scope_ = UNKNOWN;
198: }
199: }
|