001/* 002 * (C) Copyright 2013 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-2.1.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; 018 019import java.util.Collection; 020 021import javax.el.ELContext; 022import javax.el.PropertyNotFoundException; 023import javax.el.ValueExpression; 024 025/** 026 * Value expression holding a default value expression for resolve on get, when original value expression defaults to 027 * null. 028 * <p> 029 * Accepts a null original value expression in case default value should be resolved even if no mapping should be done. 030 * 031 * @since 5.7.3 032 */ 033public class DefaultValueExpression extends ValueExpression { 034 035 private static final long serialVersionUID = 1L; 036 037 protected final ValueExpression originalExpression; 038 039 protected final ValueExpression defaultExpression; 040 041 public DefaultValueExpression(ValueExpression originalExpression, ValueExpression defaultExpression) { 042 super(); 043 this.originalExpression = originalExpression; 044 this.defaultExpression = defaultExpression; 045 } 046 047 @Override 048 public Class<?> getExpectedType() { 049 if (originalExpression != null) { 050 return originalExpression.getExpectedType(); 051 } else { 052 return defaultExpression.getExpectedType(); 053 } 054 } 055 056 @Override 057 public Class<?> getType(ELContext arg0) throws PropertyNotFoundException { 058 if (originalExpression != null) { 059 return originalExpression.getType(arg0); 060 } else { 061 return defaultExpression.getType(arg0); 062 } 063 } 064 065 @Override 066 @SuppressWarnings("rawtypes") 067 public Object getValue(ELContext arg0) throws PropertyNotFoundException { 068 Object value = null; 069 if (originalExpression != null) { 070 value = originalExpression.getValue(arg0); 071 } 072 if (value == null || ((value instanceof Object[]) && ((Object[]) value).length == 0) 073 || ((value instanceof Collection) && ((Collection) value).size() == 0)) { 074 value = defaultExpression.getValue(arg0); 075 } 076 return value; 077 } 078 079 @Override 080 public boolean isReadOnly(ELContext arg0) throws PropertyNotFoundException { 081 if (originalExpression != null) { 082 return originalExpression.isReadOnly(arg0); 083 } else { 084 return true; 085 } 086 } 087 088 @Override 089 public void setValue(ELContext arg0, Object arg1) throws PropertyNotFoundException { 090 if (originalExpression != null) { 091 originalExpression.setValue(arg0, arg1); 092 } 093 } 094 095 @Override 096 public boolean equals(Object obj) { 097 if (this == obj) { 098 return true; 099 } 100 if (!(obj instanceof DefaultValueExpression)) { 101 return false; 102 } 103 DefaultValueExpression other = (DefaultValueExpression) obj; 104 if (originalExpression != null) { 105 return originalExpression.equals(other.originalExpression) 106 && defaultExpression.equals(other.defaultExpression); 107 } else { 108 return defaultExpression.equals(other.defaultExpression); 109 } 110 } 111 112 @Override 113 public String getExpressionString() { 114 if (originalExpression != null) { 115 return originalExpression.getExpressionString(); 116 } 117 return null; 118 } 119 120 @Override 121 public int hashCode() { 122 if (originalExpression != null) { 123 return originalExpression.hashCode() + defaultExpression.hashCode(); 124 } else { 125 return defaultExpression.hashCode(); 126 } 127 } 128 129 @Override 130 public boolean isLiteralText() { 131 return (originalExpression != null && originalExpression.isLiteralText()) 132 || (defaultExpression != null && defaultExpression.isLiteralText()); 133 } 134}