001/*
002 * (C) Copyright 2006-2015 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 *     dmetzler
018 */
019package org.nuxeo.ecm.platform.ec.notification.service;
020
021import java.util.List;
022
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.IdRef;
025import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
026
027public class UnrestrictedDocFetcher extends UnrestrictedSessionRunner {
028
029    private String docId;
030
031    private DocumentModel doc;
032
033    private List<DocumentModel> queryResult;
034
035    private String query;
036
037    private UnrestrictedDocFetcher() {
038        super("default");
039    }
040
041    @Override
042    public void run() {
043
044        if(docId != null) {
045            doc = session.getDocument(new IdRef(docId));
046        }
047        if(query != null) {
048            queryResult = session.query(query);
049        }
050    }
051
052    public DocumentModel getDocument() {
053        return doc;
054    }
055
056    public static DocumentModel fetch(String docId) {
057        UnrestrictedDocFetcher fetcher = new UnrestrictedDocFetcher();
058        fetcher.docId = docId;
059        fetcher.runUnrestricted();
060        return fetcher.getDocument();
061    }
062
063    public static List<DocumentModel> query(String nxql) {
064        UnrestrictedDocFetcher fetcher = new UnrestrictedDocFetcher();
065        fetcher.query = nxql;
066        fetcher.runUnrestricted();
067        return fetcher.queryResult;
068    }
069
070}