001/*
002 * (C) Copyright 2006-2009 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 *     Nuxeo
016 */
017
018package org.nuxeo.ecm.platform.publisher.api;
019
020import java.util.List;
021import java.util.Map;
022
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModel;
025
026public abstract class AbstractBasePublicationTree implements PublicationTree {
027
028    public static final String ROOT_PATH_KEY = "RootPath";
029
030    public static final String ICON_EXPANDED_KEY = "iconExpanded";
031
032    public static final String ICON_COLLAPSED_KEY = "iconCollapsed";
033
034    public static final String TITLE_KEY = "title";
035
036    protected PublicationNode rootNode;
037
038    protected PublishedDocumentFactory factory;
039
040    protected CoreSession coreSession;
041
042    protected String configName;
043
044    protected String sid;
045
046    protected String rootPath;
047
048    protected String treeTitle;
049
050    protected String iconCollapsed = "/icons/folder.gif";
051
052    protected String iconExpanded = "/icons/folder_open.gif";
053
054    protected abstract String getDefaultRootPath();
055
056    protected abstract PublishedDocumentFactory getDefaultFactory();
057
058    public void initTree(String sid, CoreSession coreSession, Map<String, String> parameters,
059            PublishedDocumentFactory factory, String configName, String title) {
060        this.sid = sid;
061        this.coreSession = coreSession;
062        if (factory != null) {
063            this.factory = factory;
064        } else {
065            this.factory = getDefaultFactory();
066            this.factory.init(coreSession, parameters);
067        }
068
069        if (parameters.containsKey(ROOT_PATH_KEY)) {
070            rootPath = parameters.get(ROOT_PATH_KEY);
071        } else {
072            rootPath = getDefaultRootPath();
073        }
074
075        if (parameters.containsKey(ICON_COLLAPSED_KEY)) {
076            iconCollapsed = parameters.get(ICON_COLLAPSED_KEY);
077        }
078        if (parameters.containsKey(ICON_EXPANDED_KEY)) {
079            iconExpanded = parameters.get(ICON_EXPANDED_KEY);
080        }
081        treeTitle = title != null ? title : configName;
082
083        this.configName = configName;
084    }
085
086    public String getConfigName() {
087        return configName;
088    }
089
090    public String getSessionId() {
091        return sid;
092    }
093
094    public String getNodeType() {
095        return rootNode.getNodeType();
096    }
097
098    public String getType() {
099        return this.getClass().getSimpleName();
100    }
101
102    public String getTreeType() {
103        return getType();
104    }
105
106    public String getTreeTitle() {
107        return treeTitle;
108    }
109
110    public List<PublishedDocument> getPublishedDocumentInNode(PublicationNode node) {
111        return node.getChildrenDocuments();
112    }
113
114    public PublishedDocument publish(DocumentModel doc, PublicationNode targetNode) {
115        return factory.publishDocument(doc, targetNode);
116    }
117
118    public PublishedDocument publish(DocumentModel doc, PublicationNode targetNode, Map<String, String> params)
119            {
120        return factory.publishDocument(doc, targetNode, params);
121    }
122
123    public String getTitle() {
124        return rootNode.getTitle();
125    }
126
127    public String getName() {
128        return rootNode.getName();
129    }
130
131    public String getTreeConfigName() {
132        return getConfigName();
133    }
134
135    public PublicationNode getParent() {
136        return null;
137    }
138
139    public List<PublicationNode> getChildrenNodes() {
140        return rootNode.getChildrenNodes();
141    }
142
143    public List<PublishedDocument> getChildrenDocuments() {
144        return rootNode.getChildrenDocuments();
145    }
146
147    public String getPath() {
148        return rootNode.getPath();
149    }
150
151    public void setCurrentDocument(DocumentModel currentDocument) {
152        // Not used by default
153    }
154
155    public String getIconExpanded() {
156        return iconExpanded;
157    }
158
159    public String getIconCollapsed() {
160        return iconCollapsed;
161    }
162
163    public void validatorPublishDocument(PublishedDocument publishedDocument, String comment) {
164        if (!accept(publishedDocument)) {
165            return;
166        }
167        factory.validatorPublishDocument(publishedDocument, comment);
168    }
169
170    public void validatorRejectPublication(PublishedDocument publishedDocument, String comment) {
171        if (!accept(publishedDocument)) {
172            return;
173        }
174        factory.validatorRejectPublication(publishedDocument, comment);
175    }
176
177    public boolean canPublishTo(PublicationNode publicationNode) {
178        if (publicationNode == null || publicationNode.getParent() == null) {
179            // we can't publish in the root node
180            return false;
181        }
182        return true;
183    }
184
185    public boolean canUnpublish(PublishedDocument publishedDocument) {
186        if (!accept(publishedDocument)) {
187            return false;
188        }
189        return true;
190    }
191
192    public boolean hasValidationTask(PublishedDocument publishedDocument) {
193        if (!accept(publishedDocument)) {
194            return false;
195        }
196        return factory.hasValidationTask(publishedDocument);
197    }
198
199    public boolean canManagePublishing(PublishedDocument publishedDocument) {
200        if (!accept(publishedDocument)) {
201            return false;
202        }
203        return factory.canManagePublishing(publishedDocument);
204    }
205
206    public PublishedDocument wrapToPublishedDocument(DocumentModel documentModel) {
207        return factory.wrapDocumentModel(documentModel);
208    }
209
210    public boolean isPublicationNode(DocumentModel documentModel) {
211        return false;
212    }
213
214    public PublicationNode wrapToPublicationNode(DocumentModel documentModel) {
215        throw new UnsupportedOperationException("");
216    }
217
218    protected abstract boolean accept(PublishedDocument publishedDocument);
219
220}