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.List;
026
027import org.nuxeo.ecm.core.api.PropertyException;
028import org.nuxeo.ecm.core.api.model.Property;
029
030import freemarker.core.CollectionAndSequence;
031import freemarker.template.AdapterTemplateModel;
032import freemarker.template.SimpleSequence;
033import freemarker.template.TemplateCollectionModel;
034import freemarker.template.TemplateHashModelEx;
035import freemarker.template.TemplateModel;
036import freemarker.template.TemplateModelException;
037
038/**
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class ComplexPropertyTemplate extends PropertyWrapper implements TemplateHashModelEx, AdapterTemplateModel {
042
043    protected final Property property;
044
045    public ComplexPropertyTemplate(DocumentObjectWrapper wrapper, Property property) {
046        super(wrapper);
047        this.property = property;
048    }
049
050    @Override
051    @SuppressWarnings("rawtypes")
052    public Object getAdaptedObject(Class hint) {
053        return property;
054    }
055
056    @Override
057    public TemplateCollectionModel keys() throws TemplateModelException {
058        List<String> list = new ArrayList<String>(property.size());
059        for (Property p : property.getChildren()) {
060            list.add(p.getName());
061        }
062        return new CollectionAndSequence(new SimpleSequence(list, wrapper));
063    }
064
065    @Override
066    public int size() throws TemplateModelException {
067        return property.size();
068    }
069
070    @Override
071    public TemplateCollectionModel values() throws TemplateModelException {
072        try {
073            List<Object> list = new ArrayList<Object>(property.size());
074            for (Property p : property.getChildren()) {
075                Object value = p.getValue();
076                list.add(value == null ? "" : value);
077            }
078            return new CollectionAndSequence(new SimpleSequence(list, wrapper));
079        } catch (PropertyException e) {
080            throw new TemplateModelException("Failed to adapt complex property values", e);
081        }
082    }
083
084    @Override
085    public TemplateModel get(String name) throws TemplateModelException {
086        try {
087            Property p = property.get(name);
088            return wrap(p);
089        } catch (PropertyException e) {
090            throw new TemplateModelException(e);
091        }
092    }
093
094    @Override
095    public boolean isEmpty() throws TemplateModelException {
096        return property.size() == 0;
097    }
098
099}