001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.api;
020
021import java.util.Collection;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.template.api.adapters.TemplateBasedDocument;
029import org.nuxeo.template.api.adapters.TemplateSourceDocument;
030import org.nuxeo.template.api.context.DocumentWrapper;
031import org.nuxeo.template.api.descriptor.ContextExtensionFactoryDescriptor;
032import org.nuxeo.template.api.descriptor.OutputFormatDescriptor;
033import org.nuxeo.template.api.descriptor.TemplateProcessorDescriptor;
034
035/**
036 * This is the service interface to manage {@link TemplateProcessor} and associated templates.
037 *
038 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
039 */
040public interface TemplateProcessorService {
041
042    /**
043     * Finds the template processor name for a given {@link Blob}. The template processor is found based on mime-types.
044     *
045     * @param templateBlob
046     * @return the {@link TemplateProcessor} name
047     */
048    String findProcessorName(Blob templateBlob);
049
050    /**
051     * Finds the {@link TemplateProcessor} for a given {@link Blob}. The template processor is found based on
052     * mime-types.
053     *
054     * @param templateBlob
055     * @return the {@link TemplateProcessor}
056     */
057    TemplateProcessor findProcessor(Blob templateBlob);
058
059    /**
060     * Get a {@link TemplateProcessor} by it's name. Name is defined in the associated descriptor.
061     *
062     * @param name
063     * @return the {@link TemplateProcessor}
064     */
065    TemplateProcessor getProcessor(String name);
066
067    /**
068     * Returns all registered {@link TemplateProcessor}s
069     *
070     * @return collection of registered {@link TemplateProcessorDescriptor}
071     */
072    Collection<TemplateProcessorDescriptor> getRegisteredTemplateProcessors();
073
074    /**
075     * Find {@link TemplateSourceDocument}s that can be bound to a given doc type.
076     *
077     * @param session
078     * @param targetType the target Document Type
079     * @return List of applicable DocumentModel
080     */
081    List<DocumentModel> getAvailableTemplateDocs(CoreSession session, String targetType);
082
083    /**
084     * Find {@link TemplateSourceDocument}s that can be bound to a given doc type.
085     *
086     * @param session
087     * @param targetType the target Document Type
088     * @return List of applicable {@link TemplateSourceDocument}
089     */
090    List<TemplateSourceDocument> getAvailableTemplates(CoreSession session, String targetType);
091
092    /**
093     * Retrieve the {@link TemplateSourceDocument} that can be used as an Office template (i.e that support to store the
094     * template file as main blob of target DocumentModel)
095     *
096     * @param session
097     * @param targetType
098     * @return
099     */
100    List<TemplateSourceDocument> getAvailableOfficeTemplates(CoreSession session, String targetType);
101
102    /**
103     * Retrieve the DocumentModels using a given {@link TemplateSourceDocument}
104     *
105     * @param source the {@link TemplateSourceDocument}
106     * @return
107     */
108    List<TemplateBasedDocument> getLinkedTemplateBasedDocuments(DocumentModel source);
109
110    /**
111     * Retrieve the Map used for mapping Document Types to Template Names. This Map represent the Templates that must be
112     * automatically bound at creation time for each Document Type.
113     *
114     * @return the Type2Template mapping
115     */
116    Map<String, List<String>> getTypeMapping();
117
118    /**
119     * Update Type2Template Mapping from the data contained in the source DocumentModel.
120     *
121     * @param doc
122     */
123    void registerTypeMapping(DocumentModel doc);
124
125    /**
126     * Associate a {@link DocumentModel} to a {@link TemplateSourceDocument}. If the DocumentModel is not already a
127     * {@link TemplateBasedDocument}, the associated facet will be automatically added.
128     *
129     * @param targetDoc the DocumentModel to associate to a template
130     * @param sourceTemplateDoc the DocumentModel holding the template
131     * @param save flag to indicate if target DocumentModel must be saved or not
132     * @return the updated DocumentModel
133     */
134    DocumentModel makeTemplateBasedDocument(DocumentModel targetDoc, DocumentModel sourceTemplateDoc, boolean save);
135
136    /**
137     * Detach a Template from a {@link DocumentModel}
138     *
139     * @param targetDoc the DocumentModel to detach
140     * @param templateName the name of the template to detach
141     * @param save save flag to indicate if target DocumentModel must be saved or not
142     * @return the updated DocumentModel
143     */
144    DocumentModel detachTemplateBasedDocument(DocumentModel targetDoc, String templateName, boolean save);
145
146    void addContextExtensions(DocumentModel currentDocument, DocumentWrapper wrapper, Map<String, Object> ctx);
147
148    Map<String, ContextExtensionFactoryDescriptor> getRegistredContextExtensions();
149
150    List<String> getReservedContextKeywords();
151
152    /**
153     * @return the list of registered Ouput formats used to convert output of a rendered document.
154     */
155    Collection<OutputFormatDescriptor> getOutputFormats();
156
157    /**
158     * The returned {@link OutputFormatDescriptor} contains either an operation chain or a mime-type use to convert the
159     * output of a rendered document.
160     *
161     * @param outputFormatId
162     * @return {@link OutputFormatDescriptor}
163     */
164    OutputFormatDescriptor getOutputFormatDescriptor(String outputFormatId);
165
166}