001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.fm;
020
021import java.util.List;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.NuxeoException;
027import org.nuxeo.ecm.platform.audit.api.LogEntry;
028import org.nuxeo.ecm.platform.rendering.fm.adapters.DocumentObjectWrapper;
029import org.nuxeo.template.api.context.DocumentWrapper;
030import org.nuxeo.template.context.AbstractContextBuilder;
031
032import freemarker.template.TemplateModelException;
033
034public class FMContextBuilder extends AbstractContextBuilder {
035
036    protected static final Log log = LogFactory.getLog(FMContextBuilder.class);
037
038    protected DocumentWrapper nuxeoWrapper;
039
040    public FMContextBuilder() {
041        final DocumentObjectWrapper fmWrapper = new DocumentObjectWrapper(null);
042
043        nuxeoWrapper = new DocumentWrapper() {
044            @Override
045            public Object wrap(DocumentModel doc) {
046                try {
047                    return fmWrapper.wrap(doc);
048                } catch (TemplateModelException e) {
049                    throw new NuxeoException(e);
050                }
051            }
052
053            public Object wrap(List<LogEntry> auditEntries) {
054                try {
055                    return fmWrapper.wrap(auditEntries);
056                } catch (TemplateModelException e) {
057                    throw new NuxeoException(e);
058                }
059            }
060        };
061    }
062
063    @Override
064    protected DocumentWrapper getWrapper() {
065        return nuxeoWrapper;
066    }
067
068}