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