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