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