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.Map;
025import java.util.Set;
026
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentRef;
030import org.nuxeo.ecm.core.api.PathRef;
031import org.nuxeo.ecm.core.api.PropertyException;
032import org.nuxeo.ecm.core.api.model.Property;
033import org.nuxeo.ecm.core.schema.DocumentType;
034
035/**
036 * Wrap a {@link DocumentModel} to expose in a pretty way more information to mvel scripts.
037 *
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040public class DocumentWrapper extends HashMap<String, Serializable> {
041
042    private static final long serialVersionUID = 1L;
043
044    protected final CoreSession session;
045
046    protected final DocumentModel doc;
047
048    public DocumentWrapper(CoreSession session, DocumentModel doc) {
049        this.session = session;
050        this.doc = doc;
051    }
052
053    public DocumentModel getDoc() {
054        return doc;
055    }
056
057    public CoreSession getSession() {
058        return session;
059    }
060
061    public DocumentWrapper getParent() {
062        DocumentModel parent = session.getParentDocument(doc.getRef());
063        return parent != null ? new DocumentWrapper(session, parent) : null;
064    }
065
066    public DocumentWrapper getParent(String type) {
067        DocumentModel parent = session.getParentDocument(doc.getRef());
068        while (parent != null && !type.equals(parent.getType())) {
069            parent = session.getParentDocument(parent.getRef());
070        }
071        if (parent == null) {
072            return null;
073        }
074        return new DocumentWrapper(session, parent);
075    }
076
077    public DocumentWrapper getWorkspace() {
078        return getParent("Workspace");
079    }
080
081    public DocumentWrapper getDomain() {
082        return getParent("Domain");
083    }
084
085    public String getTitle() {
086        return doc.getTitle();
087    }
088
089    public String getPath() {
090        return doc.getPathAsString();
091    }
092
093    public String resolvePath(String relative) {
094        return doc.getPath().append(relative).toString();
095    }
096
097    /**
098     * @return the document ref
099     * @since 5.6
100     */
101    public DocumentRef getRef() {
102        return doc.getRef();
103    }
104
105    public DocumentRef resolvePathAsRef(String relative) {
106        return new PathRef(doc.getPath().append(relative).toString());
107    }
108
109    public String getDescription() {
110        return (String) doc.getPropertyValue("dc:description");
111    }
112
113    public boolean hasFacet(String facet) {
114        return doc.hasFacet(facet);
115    }
116
117    public boolean hasSchema(String schema) {
118        return doc.hasSchema(schema);
119    }
120
121    public boolean addFacet(String facet) {
122        return doc.addFacet(facet);
123    }
124
125    public boolean removeFacet(String facet) {
126        return doc.removeFacet(facet);
127    }
128
129    public String getType() {
130        return doc.getType();
131    }
132
133    public DocumentType getDocumentType() {
134        return doc.getDocumentType();
135    }
136
137    public String getLifeCycle() {
138        return doc.getCurrentLifeCycleState();
139    }
140
141    public boolean isLocked() {
142        return doc.isLocked();
143    }
144
145    public boolean isFolder() {
146        return doc.isFolder();
147    }
148
149    public boolean isImmutable() {
150        return doc.isImmutable();
151    }
152
153    public boolean isProxy() {
154        return doc.isProxy();
155    }
156
157    public boolean isVersion() {
158        return doc.isVersion();
159    }
160
161    public boolean isDownloadable() {
162        return doc.isDownloadable();
163    }
164
165    public boolean isVersionable() {
166        return doc.isVersionable();
167    }
168
169    public String getId() {
170        return doc.getId();
171    }
172
173    public String getName() {
174        return doc.getName();
175    }
176
177    public String[] getSchemas() {
178        return doc.getSchemas();
179    }
180
181    public Set<String> getFacets() {
182        return doc.getFacets();
183    }
184
185    public Serializable getProperty(String key) {
186        return doc.getPropertyValue(key);
187    }
188
189    /**
190     * @since 5.7.3 Alias for #getProperty.
191     */
192    public Serializable getPropertyValue(String key) {
193        return doc.getPropertyValue(key);
194    }
195
196    public void setProperty(String key, Serializable value) {
197        doc.setPropertyValue(key, value);
198    }
199
200    /**
201     * @since 5.7.3 Alias for #setProperty.
202     */
203    public void setPropertyValue(String key, Serializable value) {
204        doc.setPropertyValue(key, value);
205    }
206
207    public String getVersionLabel() {
208        return doc.getVersionLabel();
209    }
210
211    /** property map implementation */
212
213    @Override
214    public boolean containsKey(Object key) {
215        try {
216            doc.getProperty(key.toString());
217            return true;
218        } catch (PropertyException e) {
219            return false;
220        }
221    }
222
223    /**
224     * The behavior of this method was changed -&gt; it is checking if an xpath has a value attached.
225     */
226    @Override
227    public boolean containsValue(Object value) {
228        try {
229            return doc.getProperty(value.toString()).getValue() != null;
230        } catch (PropertyException e) {
231            return false;
232        }
233    }
234
235    @Override
236    public Serializable get(Object key) {
237        try {
238            return doc.getProperty(key.toString()).getValue();
239        } catch (PropertyException e) {
240            return null;
241        }
242    }
243
244    @Override
245    public boolean isEmpty() {
246        return false;
247    }
248
249    @Override
250    public int size() {
251        throw new UnsupportedOperationException("Operation not supported.");
252    }
253
254    @Override
255    public Set<String> keySet() {
256        throw new UnsupportedOperationException("Operation not supported.");
257    }
258
259    @Override
260    public Collection<Serializable> values() {
261        throw new UnsupportedOperationException("Operation not supported.");
262    }
263
264    @Override
265    public Set<Map.Entry<String, Serializable>> entrySet() {
266        throw new UnsupportedOperationException("Operation not supported.");
267    }
268
269    @Override
270    public Serializable put(String key, Serializable value) {
271        Property p = doc.getProperty(key);
272        Serializable v = p.getValue();
273        p.setValue(value);
274        return v;
275    }
276
277    @Override
278    public void putAll(Map<? extends String, ? extends Serializable> m) {
279        throw new UnsupportedOperationException("Read Only Map.");
280    }
281
282    @Override
283    public Serializable remove(Object key) {
284        throw new UnsupportedOperationException("Read Only Map.");
285    }
286
287    @Override
288    public void clear() {
289        throw new UnsupportedOperationException("Read Only Map.");
290    }
291
292}