001/*
002 * (C) Copyright 2006-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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.template.adapters;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.adapter.DocumentAdapterFactory;
027import org.nuxeo.template.adapters.doc.TemplateBasedDocumentAdapterImpl;
028import org.nuxeo.template.adapters.source.TemplateSourceDocumentAdapterImpl;
029import org.nuxeo.template.api.adapters.TemplateBasedDocument;
030import org.nuxeo.template.api.adapters.TemplateSourceDocument;
031
032/**
033 * Pluggable {@link DocumentAdapterFactory} used to return the right {@link TemplateBasedDocument} or
034 * {@link TemplateSourceDocument} implementation according to given {@link DocumentModel}.
035 *
036 * @author Tiry (tdelprat@nuxeo.com)
037 */
038public class TemplateAdapterFactory implements DocumentAdapterFactory {
039
040    protected static final Log log = LogFactory.getLog(TemplateAdapterFactory.class);
041
042    /**
043     * Checks if the document can be adapted. Also works on a ShallowDocumentModel.
044     */
045    public static boolean isAdaptable(DocumentModel doc, Class<?> adapterClass) {
046        if (adapterClass.equals(TemplateBasedDocument.class)) {
047            return doc.hasFacet(TemplateBasedDocumentAdapterImpl.TEMPLATEBASED_FACET);
048        }
049        if (adapterClass.equals(TemplateSourceDocument.class)) {
050            return doc.hasFacet(TemplateSourceDocumentAdapterImpl.TEMPLATE_FACET);
051        }
052        return false;
053    }
054
055    @Override
056    @SuppressWarnings("rawtypes")
057    public Object getAdapter(DocumentModel doc, Class adapterClass) {
058
059        if (adapterClass.getSimpleName().equals(TemplateBasedDocument.class.getSimpleName())) {
060            if (doc.hasFacet(TemplateBasedDocumentAdapterImpl.TEMPLATEBASED_FACET)) {
061                return new TemplateBasedDocumentAdapterImpl(doc);
062            } else {
063                return null;
064            }
065        }
066
067        if (adapterClass.getSimpleName().equals(TemplateSourceDocument.class.getSimpleName())) {
068            if (doc.hasFacet(TemplateSourceDocumentAdapterImpl.TEMPLATE_FACET)) {
069                return new TemplateSourceDocumentAdapterImpl(doc);
070            } else {
071                return null;
072            }
073        }
074
075        return null;
076    }
077}