001/*
002 * (C) Copyright 2009-2013 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-2.1.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 */
017
018package org.nuxeo.ecm.platform.publisher.web;
019
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.jboss.seam.ScopeType;
028import org.jboss.seam.annotations.Create;
029import org.jboss.seam.annotations.Factory;
030import org.jboss.seam.annotations.Name;
031import org.jboss.seam.annotations.Scope;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentModelList;
034import org.nuxeo.ecm.core.api.Filter;
035import org.nuxeo.ecm.core.api.IdRef;
036import org.nuxeo.ecm.core.api.Sorter;
037import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
038import org.nuxeo.ecm.core.schema.SchemaManager;
039import org.nuxeo.ecm.platform.publisher.api.PublisherService;
040import org.nuxeo.ecm.platform.publisher.helper.RootSectionFinder;
041import org.nuxeo.ecm.platform.publisher.helper.RootSectionsManager;
042import org.nuxeo.ecm.webapp.tree.DocumentTreeNode;
043import org.nuxeo.ecm.webapp.tree.DocumentTreeNodeImpl;
044import org.nuxeo.ecm.webapp.tree.TreeManager;
045import org.nuxeo.runtime.api.Framework;
046
047/**
048 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
049 */
050@Name("adminPublishActions")
051@Scope(ScopeType.CONVERSATION)
052public class AdministrationPublishActions extends AbstractPublishActions implements Serializable {
053
054    private static final long serialVersionUID = 1L;
055
056    private static final Log log = LogFactory.getLog(AdministrationPublishActions.class);
057
058    public static final String PUBLICATION_TREE_PLUGIN_NAME = "publication";
059
060    protected transient RootSectionFinder rootFinder;
061
062    protected transient RootSectionsManager rootSectionsManager;
063
064    protected transient TreeManager treeManager;
065
066    protected DocumentTreeNode sectionsTree;
067
068    protected DocumentModelList sectionRoots;
069
070    protected String currentSectionRootId;
071
072    @Create
073    public void create() {
074        rootSectionsManager = new RootSectionsManager(documentManager);
075    }
076
077    @Factory(value = "defaultPublishingRoots", scope = ScopeType.EVENT)
078    public DocumentModelList getSectionRoots() {
079        return getRootFinder().getDefaultSectionRoots(true, true);
080    }
081
082    protected RootSectionFinder getRootFinder() {
083        if (rootFinder == null) {
084            PublisherService ps = Framework.getLocalService(PublisherService.class);
085            rootFinder = ps.getRootSectionFinder(documentManager);
086        }
087        return rootFinder;
088    }
089
090    public String getCurrentSectionRootId() {
091        return currentSectionRootId;
092    }
093
094    public List<DocumentTreeNode> getCurrentSectionsTree() {
095        DocumentModel sectionsRoot = null;
096
097        sectionRoots = getSectionRoots();
098        if (currentSectionRootId == null && sectionRoots.size() > 0) {
099            currentSectionRootId = sectionRoots.get(0).getId();
100        }
101
102        if (currentSectionRootId != null) {
103            sectionsRoot = documentManager.getDocument(new IdRef(currentSectionRootId));
104        }
105
106        sectionsTree = getDocumentTreeNode(sectionsRoot);
107
108        return Collections.singletonList(sectionsTree);
109    }
110
111    public void setCurrentSectionRootId(String currentSectionRootId) {
112        this.currentSectionRootId = currentSectionRootId;
113    }
114
115    public String getDomainNameFor(final DocumentModel sectionRoot) {
116        final List<String> domainName = new ArrayList<>();
117        new UnrestrictedSessionRunner(documentManager) {
118            @Override
119            public void run() {
120                DocumentModel parent = session.getParentDocument(sectionRoot.getRef());
121                SchemaManager schemaManager = Framework.getLocalService(SchemaManager.class);
122                while (parent != null && !"/".equals(parent.getPathAsString())) {
123                    if (schemaManager.hasSuperType(parent.getType(), "Domain")) {
124                        domainName.add(parent.getTitle());
125                        return;
126                    }
127                }
128            }
129        }.runUnrestricted();
130        return domainName.isEmpty() ? null : domainName.get(0);
131    }
132
133    protected DocumentTreeNode getDocumentTreeNode(DocumentModel documentModel) {
134        DocumentTreeNode documentTreeNode = null;
135        if (documentModel != null) {
136            Filter filter = getTreeManager().getFilter(PUBLICATION_TREE_PLUGIN_NAME);
137            Sorter sorter = getTreeManager().getSorter(PUBLICATION_TREE_PLUGIN_NAME);
138            documentTreeNode = new DocumentTreeNodeImpl(documentModel, filter, sorter);
139        }
140        return documentTreeNode;
141    }
142
143    protected TreeManager getTreeManager() {
144        if (treeManager == null) {
145            treeManager = Framework.getService(TreeManager.class);
146        }
147        return treeManager;
148    }
149
150    public boolean canAddSection(DocumentModel section) {
151        DocumentModel currentDocument = navigationContext.getCurrentDocument();
152        return rootSectionsManager.canAddSection(section, currentDocument);
153    }
154
155    public String addSection(String sectionId) {
156        DocumentModel currentDocument = navigationContext.getCurrentDocument();
157        rootSectionsManager.addSection(sectionId, currentDocument);
158        getRootFinder().reset();
159        return null;
160    }
161
162    public DocumentModelList getSelectedSections() {
163        DocumentModel currentDocument = navigationContext.getCurrentDocument();
164        return getRootFinder().getSectionRootsForWorkspace(currentDocument, true);
165    }
166
167    public String removeSection(String sectionId) {
168        DocumentModel currentDocument = navigationContext.getCurrentDocument();
169        rootSectionsManager.removeSection(sectionId, currentDocument);
170        getRootFinder().reset();
171        return null;
172    }
173
174    protected static String formatPathFragments(List<String> pathFragments) {
175        String fullPath = "";
176        for (String aFragment : pathFragments) {
177            if (!"".equals(fullPath)) {
178                fullPath = ">" + fullPath;
179            }
180            fullPath = aFragment + fullPath;
181        }
182        return fullPath;
183    }
184
185    @Override
186    protected DocumentModel getParentDocument(DocumentModel documentModel) {
187        return documentManager.getDocument(documentModel.getParentRef());
188    }
189
190}