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