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