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.Calendar;
019
020import org.nuxeo.ecm.core.api.Blob;
021import org.nuxeo.ecm.core.api.PropertyException;
022import org.nuxeo.ecm.core.api.model.Property;
023import org.nuxeo.ecm.core.api.model.impl.ListProperty;
024import org.nuxeo.ecm.core.api.model.impl.primitives.BlobProperty;
025import org.nuxeo.ecm.core.schema.types.primitives.DateType;
026
027import freemarker.ext.beans.ArrayModel;
028import freemarker.template.SimpleDate;
029import freemarker.template.TemplateDateModel;
030import freemarker.template.TemplateModel;
031import freemarker.template.TemplateModelException;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class PropertyWrapper {
037
038    protected final DocumentObjectWrapper wrapper;
039
040    public PropertyWrapper(DocumentObjectWrapper wrapper) {
041        this.wrapper = wrapper;
042    }
043
044    public TemplateModel wrap(Property property) throws TemplateModelException {
045        try {
046            if (property.isScalar()) {
047                Object value = property.getValue();
048                if (value == null) {
049                    return TemplateModel.NOTHING;
050                }
051                if (property.getType() == DateType.INSTANCE) {
052                    return new SimpleDate(((Calendar) value).getTime(), TemplateDateModel.DATETIME);
053                }
054                return wrapper.wrap(value);
055            } else if (property.isList()) {
056                if (property.isContainer()) {
057                    return new ListPropertyTemplate(wrapper, (ListProperty) property);
058                } else {
059                    Object value;
060                    try {
061                        value = property.getValue();
062                    } catch (PropertyException e) {
063                        throw new IllegalArgumentException("Cannot get array from array property " + property);
064                    }
065                    if (value == null) {
066                        return TemplateModel.NOTHING;
067                    }
068                    if (value instanceof ArrayList) {
069                        // FIXME: some instances of ListProperty will answer "false"
070                        // to isContainer()
071                        return new ListPropertyTemplate(wrapper, (ListProperty) property);
072                    }
073                    return new ArrayModel(value, wrapper);
074                }
075            } else if (property.getClass() == BlobProperty.class) {
076                return new BlobTemplate(wrapper, (Blob) property.getValue());
077            } else {
078                return new ComplexPropertyTemplate(wrapper, property);
079            }
080        } catch (PropertyException e) {
081            throw new TemplateModelException(e);
082        }
083    }
084
085}