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 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.rendering.fm.adapters;
023
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.List;
027
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.PropertyException;
031import org.nuxeo.ecm.platform.rendering.api.DefaultDocumentView;
032
033import freemarker.core.CollectionAndSequence;
034import freemarker.template.AdapterTemplateModel;
035import freemarker.template.ObjectWrapper;
036import freemarker.template.SimpleSequence;
037import freemarker.template.TemplateCollectionModel;
038import freemarker.template.TemplateHashModelEx;
039import freemarker.template.TemplateModel;
040import freemarker.template.TemplateModelException;
041
042/**
043 * TODO document template should not be aware of rendering context ?
044 *
045 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
046 */
047public class DocumentTemplate implements TemplateHashModelEx, AdapterTemplateModel {
048
049    protected final ObjectWrapper wrapper;
050
051    protected final DocumentModel doc;
052
053    public DocumentTemplate(ObjectWrapper wrapper, DocumentModel doc) {
054        this.doc = doc;
055        this.wrapper = wrapper;
056    }
057
058    @Override
059    @SuppressWarnings("rawtypes")
060    public Object getAdaptedObject(Class hint) {
061        return doc;
062    }
063
064    public DocumentModel getDocument() {
065        return doc;
066    }
067
068    @Override
069    public TemplateModel get(String key) throws TemplateModelException {
070        try {
071            Object value = DefaultDocumentView.DEFAULT.get(doc, key);
072            if (value != DefaultDocumentView.UNKNOWN) {
073                return wrapper.wrap(value);
074            }
075        } catch (PropertyException e) {
076            throw new TemplateModelException("Failed to get document field: " + key, e);
077        }
078        return null;
079    }
080
081    public CoreSession getSession() {
082        return doc.getCoreSession();
083    }
084
085    /**
086     * A doc model is never empty.
087     */
088    @Override
089    public boolean isEmpty() throws TemplateModelException {
090        return false;
091    }
092
093    public Collection<String> getRawKeys() {
094        return DefaultDocumentView.DEFAULT.getFields().keySet();
095    }
096
097    @Override
098    public TemplateCollectionModel keys() throws TemplateModelException {
099        return new CollectionAndSequence(new SimpleSequence(getRawKeys(), wrapper));
100    }
101
102    public Collection<Object> getRawValues() throws TemplateModelException {
103        List<Object> values = new ArrayList<>();
104        Collection<DefaultDocumentView.Field> fields = DefaultDocumentView.DEFAULT.getFields().values();
105        for (DefaultDocumentView.Field field : fields) {
106            values.add(field.getValue(doc));
107        }
108        return values;
109    }
110
111    @Override
112    public TemplateCollectionModel values() throws TemplateModelException {
113        return new CollectionAndSequence(new SimpleSequence(getRawValues(), wrapper));
114    }
115
116    @Override
117    public int size() throws TemplateModelException {
118        return DefaultDocumentView.DEFAULT.size(doc);
119    }
120
121}