001/* 002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.platform.ui.web.binding.alias; 018 019import javax.el.ELContext; 020import javax.el.ValueExpression; 021import javax.faces.context.FacesContext; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025 026/** 027 * Value expression that only stores a reference the {@link AliasVariableMapper} id so that the corresponding expression 028 * is found in the context, and is evaluated against contextual values. 029 * 030 * @author Anahide Tchertchian 031 * @since 5.4 032 */ 033public class AliasValueExpression extends ValueExpression { 034 035 private static final Log log = LogFactory.getLog(AliasValueExpression.class); 036 037 private static final long serialVersionUID = 1L; 038 039 protected final String id; 040 041 protected final String var; 042 043 public AliasValueExpression(String id, String var) { 044 this.id = id; 045 this.var = var; 046 } 047 048 @Override 049 public boolean equals(Object obj) { 050 if (this == obj) { 051 return true; 052 } 053 if (!(obj instanceof AliasValueExpression)) { 054 return false; 055 } 056 AliasValueExpression other = (AliasValueExpression) obj; 057 return id.equals(other.id) && var.equals(other.var); 058 } 059 060 @Override 061 public int hashCode() { 062 return id.hashCode() + var.hashCode(); 063 } 064 065 @Override 066 public String getExpressionString() { 067 return null; 068 } 069 070 @Override 071 public boolean isLiteralText() { 072 return false; 073 } 074 075 @Override 076 public Class<?> getExpectedType() { 077 return Object.class; 078 } 079 080 @Override 081 public Class<?> getType(ELContext context) { 082 ValueExpression ve = resolveExpression(context); 083 if (ve != null) { 084 return ve.getType(context); 085 } 086 return Object.class; 087 } 088 089 /** 090 * Looks up the {@link AliasVariableMapper} in the context, and if found, resolve the corresponding 091 * {@link ValueExpression}. 092 */ 093 @Override 094 public Object getValue(ELContext context) { 095 ValueExpression ve = resolveExpression(context); 096 Object res = null; 097 if (ve != null) { 098 res = ve.getValue(context); 099 } 100 if (log.isDebugEnabled()) { 101 log.debug(String.format("Resolved expression var='%s' for mapper with id '%s': %s", var, id, res)); 102 } 103 return res; 104 } 105 106 @Override 107 public boolean isReadOnly(ELContext context) { 108 ValueExpression ve = resolveExpression(context); 109 if (ve != null) { 110 return ve.isReadOnly(context); 111 } 112 return true; 113 } 114 115 @Override 116 public void setValue(ELContext context, Object value) { 117 ValueExpression ve = resolveExpression(context); 118 if (ve != null) { 119 ve.setValue(context, value); 120 } 121 if (log.isDebugEnabled()) { 122 log.debug(String.format("Resolved expression var='%s' for mapper with id '%s' and set value: %s", var, id, 123 value)); 124 } 125 } 126 127 protected ValueExpression resolveExpression(ELContext context) { 128 FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class); 129 AliasVariableMapper vm = AliasVariableMapper.getVariableMapper(facesContext, id); 130 131 if (vm == null) { 132 if (log.isDebugEnabled()) { 133 log.debug(String.format("No alias variable mapper with id '%s' " + "found in request for var '%s'", id, 134 var)); 135 } 136 return null; 137 } 138 139 ValueExpression ve = vm.resolveVariable(var); 140 if (ve == null) { 141 log.error(String.format("Variable for var '%s' not found in alias variable mapper " + "with id '%s'", var, 142 id)); 143 return null; 144 } 145 146 return ve; 147 } 148}