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