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.service;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentModelList;
030import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
031import org.nuxeo.ecm.core.api.repository.RepositoryManager;
032import org.nuxeo.runtime.api.Framework;
033import org.nuxeo.template.adapters.source.TemplateSourceDocumentAdapterImpl;
034import org.nuxeo.template.api.adapters.TemplateSourceDocument;
035
036public class TemplateMappingFetcher extends UnrestrictedSessionRunner {
037
038    protected static String repoName;
039
040    protected static final Log log = LogFactory.getLog(TemplateMappingFetcher.class);
041
042    protected static String getRepoName() {
043        if (repoName == null) {
044            RepositoryManager rm = Framework.getService(RepositoryManager.class);
045            repoName = rm.getDefaultRepositoryName();
046        }
047        return repoName;
048    }
049
050    protected Map<String, List<String>> mapping = new HashMap<>();
051
052    protected TemplateMappingFetcher() {
053        super(getRepoName());
054    }
055
056    @Override
057    public void run() {
058        StringBuilder sb = new StringBuilder("select * from TemplateSource where ");
059        sb.append(TemplateSourceDocumentAdapterImpl.TEMPLATE_FORCED_TYPES_ITEM_PROP);
060        sb.append(" <> 'none'");
061
062        DocumentModelList docs = session.query(sb.toString());
063
064        for (DocumentModel doc : docs) {
065            TemplateSourceDocument tmpl = doc.getAdapter(TemplateSourceDocument.class);
066            if (tmpl != null) {
067                for (String type : tmpl.getForcedTypes()) {
068                    if (mapping.containsKey(type)) {
069                        mapping.get(type).add(doc.getId());
070                    } else {
071                        List<String> templates = new ArrayList<>();
072                        templates.add(doc.getId());
073                        mapping.put(type, templates);
074                    }
075                }
076            }
077        }
078    }
079
080    public Map<String, List<String>> getMapping() {
081        return mapping;
082    }
083}