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