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