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 org.nuxeo.ecm.core.api.Blob;
018import org.nuxeo.ecm.core.api.DocumentModel;
019import org.nuxeo.ecm.core.api.PropertyException;
020import org.nuxeo.ecm.core.api.model.Property;
021import org.nuxeo.ecm.core.api.model.impl.ArrayProperty;
022import org.nuxeo.ecm.core.api.model.impl.ListProperty;
023import org.nuxeo.ecm.core.api.model.impl.primitives.BlobProperty;
024import org.nuxeo.ecm.platform.rendering.fm.FreemarkerEngine;
025
026import freemarker.ext.beans.ArrayModel;
027import freemarker.template.DefaultObjectWrapper;
028import freemarker.template.TemplateModel;
029import freemarker.template.TemplateModelException;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class DocumentObjectWrapper extends DefaultObjectWrapper {
035
036    protected final FreemarkerEngine engine;
037
038    public DocumentObjectWrapper(FreemarkerEngine engine) {
039        this.engine = engine;
040    }
041
042    @Override
043    public final TemplateModel wrap(Object obj) throws TemplateModelException {
044        if (obj == null) {
045            return null;
046        }
047        if (obj instanceof DocumentModel) {
048            return new DocumentTemplate(this, (DocumentModel) obj);
049        } else if (obj instanceof SchemaTemplate.DocumentSchema) {
050            return new SchemaTemplate(this, (SchemaTemplate.DocumentSchema) obj);
051        } else if (obj instanceof Property) {
052            Property p = (Property) obj;
053            if (p.isScalar()) {
054                return new PropertyWrapper(this).wrap(p);
055            } else if (p.isList()) {
056                if (obj instanceof ListProperty) {
057                    return new ListPropertyTemplate(this, (ListProperty) obj);
058                } else if (obj instanceof ArrayProperty) {
059                    Object value;
060                    try {
061                        value = ((ArrayProperty) obj).getValue();
062                    } catch (PropertyException e) {
063                        throw new IllegalArgumentException("Cannot get array from array property " + obj);
064                    }
065                    if (value == null) {
066                        return TemplateModel.NOTHING;
067                    }
068                    return new ArrayModel(value, this);
069                }
070            } else if (p.getClass() == BlobProperty.class) {
071                try {
072                    Blob blob = (Blob) p.getValue();
073                    if (blob == null) {
074                        return TemplateModel.NOTHING;
075                    } else {
076                        return new BlobTemplate(this, blob);
077                    }
078                } catch (PropertyException e) {
079                    throw new TemplateModelException(e);
080                }
081            } else {
082                return new ComplexPropertyTemplate(this, (Property) obj);
083            }
084        }
085        return super.wrap(obj);
086    }
087
088}