/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.forms.convert;
import java.text.ParseException;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.ModuleSupport;
import de.odysseus.calyxo.base.util.PropertyUtils;
import de.odysseus.calyxo.forms.Converter;
import de.odysseus.calyxo.forms.view.ListModel;
/**
* List model converter.
*
* @author Oliver Stuhr
*/
public class ListModelConverter implements Converter {
private static final int UNKNOWN = -1, REQUEST = 0, SESSION = 1, MODULE = 2, APPL = 3;
private int scope_;
private String scope;
private String attribute;
private String property;
/**
* Default constructor.
*/
public ListModelConverter() {
super();
}
protected ListModel getListModel(HttpServletRequest request) {
if (scope_ == UNKNOWN) // TODO? ConfigException
throw new RuntimeException("Unknown scope '" + scope + "'!");
Object object = null;
switch (scope_) {
case REQUEST: object = request.getAttribute(attribute);
break;
case SESSION: HttpSession session = request.getSession(false);
if (session != null)
object = session.getAttribute(attribute);
break;
case MODULE: ModuleContext context = ModuleSupport.getInstance(request).getModuleContext(request);
object = context.getAttribute(attribute);
break;
case APPL: context = ModuleSupport.getInstance(request).getModuleContext(request);
object = context.getServletContext().getAttribute(attribute);
break;
case UNKNOWN: throw new RuntimeException("Unknown scope '" + scope + "'!");
}
ListModel result = null;
if (property == null) {
result = (ListModel)object;
} else {
try {
result = (ListModel)PropertyUtils.getNestedProperty(object, property);
} catch (Exception e) { // TODO? ConfigException
throw new RuntimeException("ListModel '" + attribute + "." + property + "' was not found in " + scope + " scope!");
}
}
if (result == null) { // TODO? ConfigException
String s = property == null ? attribute : attribute + "." + property;
throw new RuntimeException("ListModel '" + s + "' was not found in " + scope + " scope!");
}
return result;
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.Converter#format(javax.servlet.http.HttpServletRequest, java.lang.Object)
*/
public String format(HttpServletRequest request, Object value) {
// String key = getListModel(request).getKey(value);
// if (key == null && value != null) {
// throw new IllegalArgumentException("Value is not contained in the list model: " + value);
// }
// return key;
return getListModel(request).getKey(value);
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.Converter#parse(javax.servlet.http.HttpServletRequest, java.lang.String)
*/
public Object parse(HttpServletRequest request, String key) throws ParseException {
ListModel listModel = getListModel(request);
if (!listModel.containsKey(key)) {
if (key == null || key.length() == 0) {
if (listModel.getKey(null) != null) {
throw new ParseException("Cannot parse empty/null input", 0);
}
return null;
}
throw new ParseException("Unknown list model key", 0);
}
return listModel.getValue(key);
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.impl.ValidationEngine#localize(java.util.Locale)
*/
public void localize(Locale locale) {
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.impl.ValidationEngine#isSharable()
*/
public boolean isSharable() {
return true;
}
/**
* Get the attribute property
*/
public String getAttribute() {
return attribute;
}
/**
* Get the attribute property
*/
public void setAttribute(String string) {
attribute = string;
}
/**
* Get the property property
*/
public String getProperty() {
return property;
}
/**
* Set the property property
*/
public void setProperty(String string) {
property = string;
}
/**
* Get the scope property
*/
public String getScope() {
return scope;
}
/**
* Set the scope property
* @param string one of <code>request</code>, <code>session</code>, <code>module</code>, <code>application</code>
*/
public void setScope(String string) {
scope = string;
if (scope.equals("request"))
scope_ = REQUEST;
else if (scope.equals("session"))
scope_ = SESSION;
else if (scope.equals("module"))
scope_ = MODULE;
else if (scope.equals("application"))
scope_ = APPL;
else
scope_ = UNKNOWN;
}
}