001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (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 *     Thomas Roger
016 *     Florent Guillaume
017 */
018package org.nuxeo.ecm.platform.publisher.impl.service;
019
020import java.io.Serializable;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
027import org.nuxeo.ecm.platform.query.api.PageProvider;
028import org.nuxeo.ecm.platform.query.api.PageProviderService;
029import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Finds the domains for a session.
034 */
035public class DomainsFinder extends UnrestrictedSessionRunner {
036
037    public static final String PUBLISHING_DOMAINS_PROVIDER = "domains_for_publishing";
038
039    protected List<DocumentModel> domains;
040
041    public DomainsFinder(String repositoryName) {
042        super(repositoryName);
043    }
044
045    @Override
046    public void run() {
047        domains = getDomainsFiltered();
048    }
049
050    @SuppressWarnings("unchecked")
051    protected List<DocumentModel> getDomainsFiltered() {
052        PageProviderService pps = Framework.getService(PageProviderService.class);
053        Map<String, Serializable> props = new HashMap<String, Serializable>();
054        props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session);
055        PageProvider<DocumentModel> pageProvider = (PageProvider<DocumentModel>) pps.getPageProvider(
056                PUBLISHING_DOMAINS_PROVIDER, null, null, null, props,
057                new Object[] { session.getRootDocument().getId() });
058        return pageProvider.getCurrentPage();
059    }
060
061    public List<DocumentModel> getDomains() {
062        if (domains == null) {
063            runUnrestricted();
064        }
065        return domains;
066    }
067
068}