001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.scripting;
013
014import java.io.Serializable;
015import java.util.Collection;
016import java.util.HashMap;
017import java.util.List;
018import java.util.Map;
019import java.util.Set;
020
021import org.nuxeo.ecm.core.api.NuxeoPrincipal;
022import org.nuxeo.ecm.core.api.PropertyException;
023import org.nuxeo.ecm.core.api.model.Property;
024
025/**
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028public class PrincipalWrapper extends HashMap<String, Serializable> {
029
030    private static final long serialVersionUID = 1L;
031
032    protected NuxeoPrincipal principal;
033
034    public PrincipalWrapper(NuxeoPrincipal principal) {
035        this.principal = principal;
036    }
037
038    public String getName() {
039        return principal.getName();
040    }
041
042    public String getCompany() {
043        return principal.getCompany();
044    }
045
046    public String getFirstName() {
047        return principal.getFirstName();
048    }
049
050    public String getLastName() {
051        return principal.getLastName();
052    }
053
054    public String getOriginatingUser() {
055        return principal.getOriginatingUser();
056    }
057
058    public String getActingUser() {
059        return principal.getActingUser();
060    }
061
062    public List<String> getAllGroups() {
063        return principal.getAllGroups();
064    }
065
066    public List<String> getGroups() {
067        return principal.getGroups();
068    }
069
070    public String getEmail() {
071        return principal.getEmail();
072    }
073
074    public NuxeoPrincipal getPrincipal() {
075        return principal;
076    }
077
078    public Serializable getProperty(String xpath) {
079        return principal.getModel().getPropertyValue(xpath);
080    }
081
082    /** property map implementation */
083
084    @Override
085    public boolean containsKey(Object key) {
086        try {
087            getProperty(key.toString());
088            return true;
089        } catch (PropertyException e) {
090            return false;
091        }
092    }
093
094    /**
095     * The behavior of this method was changed -> it is checking if an xpath has a value attached.
096     */
097    @Override
098    public boolean containsValue(Object value) {
099        try {
100            return getProperty(value.toString()) != null;
101        } catch (PropertyException e) {
102            return false;
103        }
104    }
105
106    @Override
107    public Serializable get(Object key) {
108        try {
109            return getProperty(key.toString());
110        } catch (PropertyException e) {
111            return null;
112        }
113    }
114
115    @Override
116    public boolean isEmpty() {
117        return false;
118    }
119
120    @Override
121    public int size() {
122        throw new UnsupportedOperationException("Operation not supported.");
123    }
124
125    @Override
126    public Set<String> keySet() {
127        throw new UnsupportedOperationException("Operation not supported.");
128    }
129
130    @Override
131    public Collection<Serializable> values() {
132        throw new UnsupportedOperationException("Operation not supported.");
133    }
134
135    @Override
136    public Set<Map.Entry<String, Serializable>> entrySet() {
137        throw new UnsupportedOperationException("Operation not supported.");
138    }
139
140    @Override
141    public Serializable put(String key, Serializable value) {
142        Property p = principal.getModel().getProperty(key);
143        Serializable v = p.getValue();
144        p.setValue(value);
145        return v;
146    }
147
148    @Override
149    public void putAll(Map<? extends String, ? extends Serializable> m) {
150        throw new UnsupportedOperationException("Read Only Map.");
151    }
152
153    @Override
154    public Serializable remove(Object key) {
155        throw new UnsupportedOperationException("Read Only Map.");
156    }
157
158    @Override
159    public void clear() {
160        throw new UnsupportedOperationException("Read Only Map.");
161    }
162
163}