001: /*
002: * Copyright 2004-2007 the original author or authors.
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 org.springframework.webflow.executor.jsf;
017:
018: import javax.faces.el.EvaluationException;
019: import javax.faces.el.PropertyNotFoundException;
020: import javax.faces.el.PropertyResolver;
021: import javax.faces.el.ReferenceSyntaxException;
022:
023: import org.springframework.webflow.execution.FlowExecution;
024:
025: /**
026: * Base class for property resolvers that get and set flow execution attributes.
027: *
028: * @author Keith Donald
029: */
030: public abstract class AbstractFlowExecutionPropertyResolver extends
031: PropertyResolver {
032:
033: /**
034: * The standard property resolver to delegate to if this one doesn't apply.
035: */
036: private final PropertyResolver resolverDelegate;
037:
038: /**
039: * Creates a new flow executon property resolver
040: * @param resolverDelegate the resolver to delegate to when the property is not a flow execution attribute
041: */
042: public AbstractFlowExecutionPropertyResolver(
043: PropertyResolver resolverDelegate) {
044: this .resolverDelegate = resolverDelegate;
045: }
046:
047: /**
048: * Returns the property resolver this resolver delegates to if necessary.
049: */
050: protected final PropertyResolver getResolverDelegate() {
051: return resolverDelegate;
052: }
053:
054: public Class getType(Object base, Object property)
055: throws EvaluationException, PropertyNotFoundException {
056: if (base instanceof FlowExecution) {
057: FlowExecution execution = (FlowExecution) base;
058: assertPropertyNameValid(property);
059: return doGetAttributeType(execution, (String) property);
060: } else {
061: return resolverDelegate.getType(base, property);
062: }
063: }
064:
065: public Class getType(Object base, int index)
066: throws EvaluationException, PropertyNotFoundException {
067: if (base instanceof FlowExecution) {
068: // cannot access flow execution by index so we cannot determine type. Return null per JSF spec
069: return null;
070: } else {
071: return resolverDelegate.getType(base, index);
072: }
073: }
074:
075: public Object getValue(Object base, Object property)
076: throws EvaluationException, PropertyNotFoundException {
077: if (base instanceof FlowExecution) {
078: FlowExecution execution = (FlowExecution) base;
079: assertPropertyNameValid(property);
080: return doGetAttribute(execution, (String) property);
081: } else {
082: return resolverDelegate.getValue(base, property);
083: }
084: }
085:
086: public Object getValue(Object base, int index)
087: throws EvaluationException, PropertyNotFoundException {
088: if (base instanceof FlowExecution) {
089: throw new ReferenceSyntaxException(
090: "Cannot apply an index value to a flow execution");
091: } else {
092: return resolverDelegate.getValue(base, index);
093: }
094: }
095:
096: public boolean isReadOnly(Object base, Object property)
097: throws EvaluationException, PropertyNotFoundException {
098: if (base instanceof FlowExecution) {
099: return false;
100: } else {
101: return resolverDelegate.isReadOnly(base, property);
102: }
103: }
104:
105: public boolean isReadOnly(Object base, int index)
106: throws EvaluationException, PropertyNotFoundException {
107: if (base instanceof FlowExecution) {
108: return false;
109: } else {
110: return resolverDelegate.isReadOnly(base, index);
111: }
112: }
113:
114: public void setValue(Object base, Object property, Object value)
115: throws EvaluationException, PropertyNotFoundException {
116: if ((base instanceof FlowExecution)) {
117: FlowExecution execution = (FlowExecution) base;
118: assertPropertyNameValid(property);
119: doSetAttribute(execution, (String) property, value);
120: } else {
121: resolverDelegate.setValue(base, property, value);
122: }
123: }
124:
125: public void setValue(Object base, int index, Object value)
126: throws EvaluationException, PropertyNotFoundException {
127: if (base instanceof FlowExecution) {
128: throw new ReferenceSyntaxException(
129: "Cannot apply an index value to a flow execution");
130: } else {
131: resolverDelegate.setValue(base, index, value);
132: }
133: }
134:
135: // helpers
136:
137: private void assertPropertyNameValid(Object property) {
138: if (property == null) {
139: throw new PropertyNotFoundException(
140: "The name of the flow execution attribute cannot be null");
141: }
142: if (!(property instanceof String)) {
143: throw new PropertyNotFoundException(
144: "Flow execution attribute names must be strings but "
145: + property + " was not");
146: }
147: if (((String) property).length() == 0) {
148: throw new PropertyNotFoundException(
149: "The name of the flow execution attribute cannot be blank");
150: }
151: }
152:
153: /**
154: * Gets the type of value returned by the flow execution attribute.
155: * @param execution the flow execution
156: * @param attributeName the name of the attribute
157: * @return the type of value returned by the attribute
158: */
159: protected abstract Class doGetAttributeType(
160: FlowExecution execution, String attributeName);
161:
162: /**
163: * Gets the value of the flow execution attribute.
164: * @param execution the flow execution
165: * @param attributeName the name of the attribute
166: * @return the attribute value
167: */
168: protected abstract Object doGetAttribute(FlowExecution execution,
169: String attributeName);
170:
171: /**
172: * Sets the value of the flow execution attribute.
173: * @param execution the flow execution
174: * @param attributeName the name of the attribute
175: * @param attributeValue the attribute value
176: */
177: protected abstract void doSetAttribute(FlowExecution execution,
178: String attributeName, Object attributeValue);
179:
180: }
|