001/*
002 * (C) Copyright 2006-2015 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     dmetzler
016 */
017package org.nuxeo.ecm.platform.ec.notification.service;
018
019import java.util.List;
020
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.IdRef;
023import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
024
025public class UnrestrictedDocFetcher extends UnrestrictedSessionRunner {
026
027    private String docId;
028
029    private DocumentModel doc;
030
031    private List<DocumentModel> queryResult;
032
033    private String query;
034
035    private UnrestrictedDocFetcher() {
036        super("default");
037    }
038
039    @Override
040    public void run() {
041
042        if(docId != null) {
043            doc = session.getDocument(new IdRef(docId));
044        }
045        if(query != null) {
046            queryResult = session.query(query);
047        }
048    }
049
050    public DocumentModel getDocument() {
051        return doc;
052    }
053
054    public static DocumentModel fetch(String docId) {
055        UnrestrictedDocFetcher fetcher = new UnrestrictedDocFetcher();
056        fetcher.docId = docId;
057        fetcher.runUnrestricted();
058        return fetcher.getDocument();
059    }
060
061    public static List<DocumentModel> query(String nxql) {
062        UnrestrictedDocFetcher fetcher = new UnrestrictedDocFetcher();
063        fetcher.query = nxql;
064        fetcher.runUnrestricted();
065        return fetcher.queryResult;
066    }
067
068}