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