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 sid;
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    public void initTree(String sid, CoreSession coreSession, Map<String, String> parameters,
061            PublishedDocumentFactory factory, String configName, String title) {
062        this.sid = sid;
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    public String getConfigName() {
089        return configName;
090    }
091
092    public String getSessionId() {
093        return sid;
094    }
095
096    public String getNodeType() {
097        return rootNode.getNodeType();
098    }
099
100    public String getType() {
101        return this.getClass().getSimpleName();
102    }
103
104    public String getTreeType() {
105        return getType();
106    }
107
108    public String getTreeTitle() {
109        return treeTitle;
110    }
111
112    public List<PublishedDocument> getPublishedDocumentInNode(PublicationNode node) {
113        return node.getChildrenDocuments();
114    }
115
116    public PublishedDocument publish(DocumentModel doc, PublicationNode targetNode) {
117        return factory.publishDocument(doc, targetNode);
118    }
119
120    public PublishedDocument publish(DocumentModel doc, PublicationNode targetNode, Map<String, String> params)
121            {
122        return factory.publishDocument(doc, targetNode, params);
123    }
124
125    public String getTitle() {
126        return rootNode.getTitle();
127    }
128
129    public String getName() {
130        return rootNode.getName();
131    }
132
133    public String getTreeConfigName() {
134        return getConfigName();
135    }
136
137    public PublicationNode getParent() {
138        return null;
139    }
140
141    public List<PublicationNode> getChildrenNodes() {
142        return rootNode.getChildrenNodes();
143    }
144
145    public List<PublishedDocument> getChildrenDocuments() {
146        return rootNode.getChildrenDocuments();
147    }
148
149    public String getPath() {
150        return rootNode.getPath();
151    }
152
153    public void setCurrentDocument(DocumentModel currentDocument) {
154        // Not used by default
155    }
156
157    public String getIconExpanded() {
158        return iconExpanded;
159    }
160
161    public String getIconCollapsed() {
162        return iconCollapsed;
163    }
164
165    public void validatorPublishDocument(PublishedDocument publishedDocument, String comment) {
166        if (!accept(publishedDocument)) {
167            return;
168        }
169        factory.validatorPublishDocument(publishedDocument, comment);
170    }
171
172    public void validatorRejectPublication(PublishedDocument publishedDocument, String comment) {
173        if (!accept(publishedDocument)) {
174            return;
175        }
176        factory.validatorRejectPublication(publishedDocument, comment);
177    }
178
179    public boolean canPublishTo(PublicationNode publicationNode) {
180        if (publicationNode == null || publicationNode.getParent() == null) {
181            // we can't publish in the root node
182            return false;
183        }
184        return true;
185    }
186
187    public boolean canUnpublish(PublishedDocument publishedDocument) {
188        if (!accept(publishedDocument)) {
189            return false;
190        }
191        return true;
192    }
193
194    public boolean hasValidationTask(PublishedDocument publishedDocument) {
195        if (!accept(publishedDocument)) {
196            return false;
197        }
198        return factory.hasValidationTask(publishedDocument);
199    }
200
201    public boolean canManagePublishing(PublishedDocument publishedDocument) {
202        if (!accept(publishedDocument)) {
203            return false;
204        }
205        return factory.canManagePublishing(publishedDocument);
206    }
207
208    public PublishedDocument wrapToPublishedDocument(DocumentModel documentModel) {
209        return factory.wrapDocumentModel(documentModel);
210    }
211
212    public boolean isPublicationNode(DocumentModel documentModel) {
213        return false;
214    }
215
216    public PublicationNode wrapToPublicationNode(DocumentModel documentModel) {
217        throw new UnsupportedOperationException("");
218    }
219
220    protected abstract boolean accept(PublishedDocument publishedDocument);
221
222}