001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.ui.web.binding.alias;
020
021import javax.el.ELContext;
022import javax.el.ValueExpression;
023import javax.faces.context.FacesContext;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027
028/**
029 * Value expression that only stores a reference the {@link AliasVariableMapper} id so that the corresponding expression
030 * is found in the context, and is evaluated against contextual values.
031 *
032 * @author Anahide Tchertchian
033 * @since 5.4
034 */
035public class AliasValueExpression extends ValueExpression {
036
037    private static final Log log = LogFactory.getLog(AliasValueExpression.class);
038
039    private static final long serialVersionUID = 1L;
040
041    protected final String id;
042
043    protected final String var;
044
045    public AliasValueExpression(String id, String var) {
046        this.id = id;
047        this.var = var;
048    }
049
050    @Override
051    public boolean equals(Object obj) {
052        if (this == obj) {
053            return true;
054        }
055        if (!(obj instanceof AliasValueExpression)) {
056            return false;
057        }
058        AliasValueExpression other = (AliasValueExpression) obj;
059        return id.equals(other.id) && var.equals(other.var);
060    }
061
062    @Override
063    public int hashCode() {
064        return id.hashCode() + var.hashCode();
065    }
066
067    @Override
068    public String getExpressionString() {
069        return null;
070    }
071
072    @Override
073    public boolean isLiteralText() {
074        return false;
075    }
076
077    @Override
078    public Class<?> getExpectedType() {
079        return Object.class;
080    }
081
082    @Override
083    public Class<?> getType(ELContext context) {
084        ValueExpression ve = resolveExpression(context);
085        if (ve != null) {
086            return ve.getType(context);
087        }
088        return Object.class;
089    }
090
091    /**
092     * Looks up the {@link AliasVariableMapper} in the context, and if found, resolve the corresponding
093     * {@link ValueExpression}.
094     */
095    @Override
096    public Object getValue(ELContext context) {
097        ValueExpression ve = resolveExpression(context);
098        Object res = null;
099        if (ve != null) {
100            res = ve.getValue(context);
101        }
102        if (log.isDebugEnabled()) {
103            log.debug("Resolved expression var='" + var + "' for mapper with id '" + id + "': " + res);
104        }
105        return res;
106    }
107
108    @Override
109    public boolean isReadOnly(ELContext context) {
110        ValueExpression ve = resolveExpression(context);
111        if (ve != null) {
112            return ve.isReadOnly(context);
113        }
114        return true;
115    }
116
117    @Override
118    public void setValue(ELContext context, Object value) {
119        ValueExpression ve = resolveExpression(context);
120        if (ve != null) {
121            ve.setValue(context, value);
122        }
123        if (log.isDebugEnabled()) {
124            log.debug("Resolved expression var='" + var + "' for mapper with id '" + id + "' and set value: " + value);
125        }
126    }
127
128    protected ValueExpression resolveExpression(ELContext context) {
129        FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
130        AliasVariableMapper vm = AliasVariableMapper.getVariableMapper(facesContext, id);
131
132        if (vm == null) {
133            if (log.isDebugEnabled()) {
134                log.debug("No alias variable mapper with id '" + id + "' found in request for var '" + var + "'");
135            }
136            return null;
137        }
138
139        ValueExpression ve = vm.resolveVariable(var);
140        if (ve == null) {
141            log.error("Variable for var '" + var + "' not found in alias variable mapper with id '" + id + "'");
142            return null;
143        }
144
145        return ve;
146    }
147}