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.impl.service;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.DocumentLocation;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
031import org.nuxeo.ecm.platform.publisher.api.PublicationTree;
032import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
033import org.nuxeo.ecm.platform.publisher.api.RemotePublicationTreeManager;
034
035/**
036 * Abstract class for {@link PublicationTree} that delegates method calls to a remote service.
037 *
038 * @author tiry
039 */
040public abstract class AbstractRemotableTree implements PublicationTree {
041
042    private static final Log log = LogFactory.getLog(AbstractRemotableTree.class);
043
044    protected RemotePublicationTreeManager treeService;
045
046    protected String sessionId;
047
048    protected String configName;
049
050    protected abstract RemotePublicationTreeManager getTreeService();
051
052    protected abstract String getTargetTreeName();
053
054    protected abstract String getServerTreeSessionId();
055
056    public List<PublishedDocument> getExistingPublishedDocument(DocumentLocation docLoc) {
057        return getTreeService().getExistingPublishedDocument(getServerTreeSessionId(), docLoc);
058    }
059
060    public List<PublishedDocument> getPublishedDocumentInNode(PublicationNode node) {
061        return getTreeService().getPublishedDocumentInNode(switchToServerNode(node));
062    }
063
064    public PublishedDocument publish(DocumentModel doc, PublicationNode targetNode) {
065        return publish(doc, targetNode, null);
066    }
067
068    public PublishedDocument publish(DocumentModel doc, PublicationNode targetNode, Map<String, String> params)
069            {
070        return getTreeService().publish(doc, switchToServerNode(targetNode), params);
071    }
072
073    public void unpublish(DocumentModel doc, PublicationNode targetNode) {
074        getTreeService().unpublish(doc, switchToServerNode(targetNode));
075    }
076
077    public void unpublish(PublishedDocument publishedDocument) {
078        getTreeService().unpublish(getServerTreeSessionId(), publishedDocument);
079    }
080
081    protected abstract PublicationNode switchToClientNode(PublicationNode node);
082
083    protected abstract PublicationNode switchToServerNode(PublicationNode node);
084
085    protected List<PublicationNode> switchToClientNodes(List<PublicationNode> nodes) {
086        List<PublicationNode> wrappedNodes = new ArrayList<PublicationNode>();
087
088        for (PublicationNode node : nodes) {
089            wrappedNodes.add(switchToClientNode(node));
090        }
091        return wrappedNodes;
092    }
093
094    public PublicationNode getNodeByPath(String path) {
095        return switchToClientNode(getTreeService().getNodeByPath(getServerTreeSessionId(), path));
096    }
097
098    public String getConfigName() {
099        return configName;
100    }
101
102    public PublicationNode getParent() {
103        return null;
104    }
105
106    public void setCurrentDocument(DocumentModel currentDocument) {
107        getTreeService().setCurrentDocument(getServerTreeSessionId(), currentDocument);
108    }
109
110    protected boolean released = false;
111
112    public void release() {
113        if (!released) {
114            getTreeService().release(getServerTreeSessionId());
115        }
116        released = true;
117    }
118
119    public void validatorPublishDocument(PublishedDocument publishedDocument, String comment) {
120        getTreeService().validatorPublishDocument(getServerTreeSessionId(), publishedDocument, comment);
121    }
122
123    public void validatorRejectPublication(PublishedDocument publishedDocument, String comment) {
124        getTreeService().validatorRejectPublication(getServerTreeSessionId(), publishedDocument, comment);
125    }
126
127    public boolean canPublishTo(PublicationNode publicationNode) {
128        return getTreeService().canPublishTo(getServerTreeSessionId(), publicationNode);
129    }
130
131    public boolean canUnpublish(PublishedDocument publishedDocument) {
132        return getTreeService().canUnpublish(getServerTreeSessionId(), publishedDocument);
133    }
134
135    public boolean hasValidationTask(PublishedDocument publishedDocument) {
136        return getTreeService().hasValidationTask(getServerTreeSessionId(), publishedDocument);
137    }
138
139    public boolean canManagePublishing(PublishedDocument publishedDocument) {
140        return getTreeService().canManagePublishing(getServerTreeSessionId(), publishedDocument);
141    }
142
143    public PublishedDocument wrapToPublishedDocument(DocumentModel documentModel) {
144        return getTreeService().wrapToPublishedDocument(getServerTreeSessionId(), documentModel);
145    }
146
147    public boolean isPublicationNode(DocumentModel documentModel) {
148        return getTreeService().isPublicationNode(getServerTreeSessionId(), documentModel);
149    }
150
151    public PublicationNode wrapToPublicationNode(DocumentModel documentModel) {
152        return getTreeService().wrapToPublicationNode(getServerTreeSessionId(), documentModel);
153    }
154
155}