001package org.nuxeo.template.context; 002 003import java.util.HashMap; 004import java.util.Map; 005 006import org.apache.commons.logging.Log; 007import org.apache.commons.logging.LogFactory; 008import org.nuxeo.ecm.core.api.CoreSession; 009import org.nuxeo.ecm.core.api.DocumentModel; 010import org.nuxeo.runtime.api.Framework; 011import org.nuxeo.template.api.TemplateProcessorService; 012import org.nuxeo.template.api.adapters.TemplateBasedDocument; 013import org.nuxeo.template.api.context.DocumentWrapper; 014 015public abstract class AbstractContextBuilder { 016 017 protected static final Log log = LogFactory.getLog(AbstractContextBuilder.class); 018 019 public static final String[] RESERVED_VAR_NAMES = { "doc", "document", "blobHolder", "username", "principal", 020 "templateName" }; 021 022 public Map<String, Object> build(DocumentModel doc, DocumentWrapper nuxeoWrapper, String templateName) { 023 024 Map<String, Object> ctx = new HashMap<String, Object>(); 025 026 CoreSession session = doc.getCoreSession(); 027 028 // doc infos 029 ctx.put("doc", nuxeoWrapper.wrap(doc)); 030 ctx.put("document", nuxeoWrapper.wrap(doc)); 031 032 // blob wrapper 033 ctx.put("blobHolder", new BlobHolderWrapper(doc)); 034 035 // user info 036 ctx.put("username", session.getPrincipal().getName()); 037 ctx.put("principal", session.getPrincipal()); 038 039 ctx.put("templateName", templateName); 040 041 // fetch extensions 042 TemplateProcessorService tps = Framework.getLocalService(TemplateProcessorService.class); 043 tps.addContextExtensions(doc, nuxeoWrapper, ctx); 044 045 return ctx; 046 } 047 048 public Map<String, Object> build(TemplateBasedDocument templateBasedDocument, String templateName) { 049 050 DocumentModel doc = templateBasedDocument.getAdaptedDoc(); 051 052 Map<String, Object> context = build(doc, templateName); 053 054 return context; 055 } 056 057 protected abstract DocumentWrapper getWrapper(); 058 059 public Map<String, Object> build(DocumentModel doc, String templateName) { 060 061 return build(doc, getWrapper(), templateName); 062 } 063}