001    package org.tynamo.descriptor.extension;
002    
003    import ognl.Ognl;
004    import ognl.OgnlException;
005    import org.apache.commons.lang.Validate;
006    import org.apache.commons.logging.Log;
007    import org.apache.commons.logging.LogFactory;
008    
009    import java.util.Map;
010    
011    
012    public abstract class ExpressionExtension implements DescriptorExtension
013    {
014            private static Log LOG = LogFactory.getLog(PossibleValuesDescriptorExtension.class);
015    
016            /**
017             * Ognl expression that evaluated gets a list of possible values to use with
018             * the current property, cannot be null.
019             */
020            private String expression;
021    
022            /**
023             * Map of variables to put into the available namespace (scope) for OGNL expressions.
024             */
025            private Map context;
026    
027            /**
028             * Creates a {@link ExpressionExtension}.
029             *
030             * @param theExpression Ognl expression that evaluated gets a list of possible
031             *                      values to use with the current property, cannot be null.
032             */
033            public ExpressionExtension(final String theExpression)
034            {
035                    super();
036                    Validate.notNull(theExpression, "The expression cannot be null");
037                    expression = theExpression;
038            }
039    
040    
041            /**
042             * Creates a {@link ExpressionExtension}.
043             *
044             * @param theExpression Ognl expression that evaluated gets a list of possible
045             *                      values to use with the current property, cannot be null.
046             * @param context
047             */
048            public ExpressionExtension(final String theExpression, Map context)
049            {
050                    this(theExpression);
051                    this.context = context;
052            }
053    
054            /**
055             * Gets the Ognl expression that evaluated gets a list of possible values to
056             * use with the current property.
057             *
058             * @return a String, never null.
059             */
060    
061    
062            /**
063             * Method used to initialize the value of the filtering property using the
064             * value of the filtered one (for example, initialize the country based on the
065             * value of the state property.
066             *
067             * @param model the model used by the edit page, cannot be null.
068             * @return
069             * @throws ognl.OgnlException
070             */
071            public Object evaluateExpresion(final Object model) throws OgnlException
072            {
073                    Validate.notNull(model, "The model cannot be null");
074                    try
075                    {
076                            if (context != null)
077                            {
078                                    return Ognl.getValue(expression, context, model);
079                            } else
080                            {
081                                    return Ognl.getValue(expression, model);
082                            }
083                    } catch (OgnlException e)
084                    {
085                            LOG.warn("Exception thrown evaluationg " + expression, e);
086                            throw e;
087                    }
088            }
089    }