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