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