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.xdocreport.jaxrs;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentModelList;
029import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
030import org.nuxeo.template.api.adapters.TemplateSourceDocument;
031
032/**
033 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
034 */
035public abstract class AbstractResourceService extends DefaultObject {
036
037    protected static Log log = LogFactory.getLog(AbstractResourceService.class);
038
039    protected CoreSession session;
040
041    public AbstractResourceService(CoreSession session) {
042        this.session = session;
043    }
044
045    protected CoreSession getCoreSession() {
046        return session;
047    }
048
049    protected List<TemplateSourceDocument> getTemplates() {
050        List<TemplateSourceDocument> result = new ArrayList<>();
051        CoreSession session = getCoreSession();
052        String query = "select * from Document where ecm:mixinType = 'Template' "
053                + "AND ecm:isTrashed = 0 AND tmpl:templateType = 'XDocReportProcessor'";
054        DocumentModelList docs = session.query(query);
055        for (DocumentModel doc : docs) {
056            TemplateSourceDocument tmpl = doc.getAdapter(TemplateSourceDocument.class);
057            if (tmpl != null) {
058                result.add(tmpl);
059            }
060        }
061        return result;
062    }
063
064}