001/* 002 * (C) Copyright 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 * Florent Guillaume 018 */ 019 020package org.nuxeo.ecm.platform.rendering.fm.adapters; 021 022import org.nuxeo.ecm.core.api.DocumentModel; 023import org.nuxeo.ecm.core.api.PropertyException; 024 025import freemarker.template.TemplateHashModel; 026import freemarker.template.TemplateModel; 027import freemarker.template.TemplateModelException; 028 029/** 030 * A template for a document's schema, that can still take advantage of the document's prefetched values. 031 */ 032public class SchemaTemplate extends PropertyWrapper implements TemplateHashModel { 033 034 private final DocumentModel doc; 035 036 private final String schemaName; 037 038 public static class DocumentSchema { 039 public final DocumentModel doc; 040 041 public final String schemaName; 042 043 public DocumentSchema(DocumentModel doc, String schemaName) { 044 this.doc = doc; 045 this.schemaName = schemaName; 046 } 047 } 048 049 public SchemaTemplate(DocumentObjectWrapper wrapper, DocumentSchema schema) { 050 super(wrapper); 051 this.doc = schema.doc; 052 this.schemaName = schema.schemaName; 053 } 054 055 @Override 056 public TemplateModel get(String name) throws TemplateModelException { 057 try { 058 if (doc.isPrefetched(schemaName, name)) { 059 // simple value already available, don't load DocumentPart 060 return wrapper.wrap(doc.getProperty(schemaName, name)); 061 } else { 062 // use normal Property lookup in Part 063 return wrap(doc.getPart(schemaName).get(name)); 064 } 065 } catch (PropertyException e) { 066 throw new TemplateModelException(e); 067 } 068 } 069 070 @Override 071 public boolean isEmpty() throws TemplateModelException { 072 return false; 073 } 074 075}