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.io.Serializable;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.Map;
025
026import javax.el.ELContext;
027import javax.el.PropertyNotFoundException;
028import javax.el.ValueExpression;
029
030/**
031 * Value expression representing a map of value expressions, to allow resolution, at runtime, of all of the map values.
032 * <p>
033 * Useful to pass resolved widget properties to third party code (typically javascript code)
034 *
035 * @since 5.8
036 */
037public class MapValueExpression extends ValueExpression {
038
039    private static final long serialVersionUID = 1L;
040
041    protected final Map<String, ValueExpression> map;
042
043    public MapValueExpression(Map<String, ValueExpression> map) {
044        super();
045        this.map = map == null ? Collections.<String, ValueExpression> emptyMap() : map;
046    }
047
048    @Override
049    public Class<?> getExpectedType() {
050        return Map.class;
051    }
052
053    @Override
054    public Class<?> getType(ELContext arg0) throws PropertyNotFoundException {
055        return Map.class;
056    }
057
058    @Override
059    public Object getValue(ELContext elContext) throws PropertyNotFoundException {
060        Map<String, Serializable> res = new HashMap<String, Serializable>();
061        if (map != null) {
062            for (Map.Entry<String, ValueExpression> entry : map.entrySet()) {
063                ValueExpression ve = entry.getValue();
064                if (ve != null) {
065                    res.put(entry.getKey(), (Serializable) ve.getValue(elContext));
066                } else {
067                    res.put(entry.getKey(), null);
068                }
069            }
070        }
071        return res;
072    }
073
074    @Override
075    public boolean isReadOnly(ELContext arg0) throws PropertyNotFoundException {
076        return true;
077    }
078
079    @Override
080    public void setValue(ELContext arg0, Object arg1) throws PropertyNotFoundException {
081        // do nothing
082    }
083
084    @Override
085    public String getExpressionString() {
086        return null;
087    }
088
089    @Override
090    public boolean isLiteralText() {
091        return false;
092    }
093
094    @Override
095    public boolean equals(Object obj) {
096        if (this == obj) {
097            return true;
098        }
099        if (!(obj instanceof MapValueExpression)) {
100            return false;
101        }
102        MapValueExpression other = (MapValueExpression) obj;
103        return map.equals(other.map);
104    }
105
106    @Override
107    public int hashCode() {
108        return map.hashCode();
109    }
110
111}