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.core; 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.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentLocation; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.DocumentModelList; 032import org.nuxeo.ecm.core.api.DocumentRef; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.core.api.PathRef; 035import org.nuxeo.ecm.core.api.security.SecurityConstants; 036import org.nuxeo.ecm.platform.publisher.api.AbstractBasePublicationTree; 037import org.nuxeo.ecm.platform.publisher.api.PublicationNode; 038import org.nuxeo.ecm.platform.publisher.api.PublicationTree; 039import org.nuxeo.ecm.platform.publisher.api.PublishedDocument; 040import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory; 041import org.nuxeo.ecm.platform.publisher.helper.PublicationRelationHelper; 042 043/** 044 * Simple implementation of a {@link PublicationTree} using the Core Sections. 045 * 046 * @author tiry 047 */ 048public class SectionPublicationTree extends AbstractBasePublicationTree { 049 050 private static final long serialVersionUID = 1L; 051 052 private static final Log log = LogFactory.getLog(SectionPublicationTree.class); 053 054 public static final String CAN_ASK_FOR_PUBLISHING = "CanAskForPublishing"; 055 056 protected static final String DEFAULT_ROOT_PATH = "/default-domain/sections"; 057 058 protected DocumentModel treeRoot; 059 060 @Override 061 public void initTree(CoreSession coreSession, Map<String, String> parameters, PublishedDocumentFactory factory, 062 String configName, String title) { 063 super.initTree(coreSession, parameters, factory, configName, title); 064 065 DocumentRef ref = new PathRef(rootPath); 066 boolean exists = coreSession.exists(ref); 067 if (!exists) { 068 log.debug("Root section " + rootPath + " doesn't exist. Check " + "publicationTreeConfig with name " 069 + configName); 070 } 071 if (exists && coreSession.hasPermission(ref, SecurityConstants.READ)) { 072 treeRoot = coreSession.getDocument(new PathRef(rootPath)); 073 rootNode = new CoreFolderPublicationNode(treeRoot, this, factory); 074 } else { 075 rootNode = new VirtualCoreFolderPublicationNode(coreSession, rootPath, this, factory); 076 } 077 } 078 079 @Override 080 public List<PublishedDocument> getExistingPublishedDocument(DocumentLocation docLoc) { 081 List<PublishedDocument> publishedDocs = new ArrayList<>(); 082 DocumentModelList proxies = coreSession.getProxies(docLoc.getDocRef(), null); 083 for (DocumentModel proxy : proxies) { 084 if (proxy.getPathAsString().startsWith(rootPath)) { 085 publishedDocs.add(factory.wrapDocumentModel(proxy)); 086 } 087 } 088 return publishedDocs; 089 } 090 091 @Override 092 public PublishedDocument publish(DocumentModel doc, PublicationNode targetNode) { 093 SimpleCorePublishedDocument publishedDocument = (SimpleCorePublishedDocument) super.publish(doc, targetNode); 094 PublicationRelationHelper.addPublicationRelation(publishedDocument.getProxy(), this); 095 return publishedDocument; 096 } 097 098 @Override 099 public PublishedDocument publish(DocumentModel doc, PublicationNode targetNode, Map<String, String> params) 100 { 101 SimpleCorePublishedDocument publishedDocument = (SimpleCorePublishedDocument) super.publish(doc, targetNode, 102 params); 103 PublicationRelationHelper.addPublicationRelation(publishedDocument.getProxy(), this); 104 return publishedDocument; 105 } 106 107 @Override 108 public void unpublish(DocumentModel doc, PublicationNode targetNode) { 109 List<PublishedDocument> publishedDocs = getPublishedDocumentInNode(targetNode); 110 for (PublishedDocument pubDoc : publishedDocs) { 111 if (pubDoc.getSourceDocumentRef().equals(doc.getRef())) { 112 unpublish(pubDoc); 113 } 114 } 115 } 116 117 @Override 118 public void unpublish(PublishedDocument publishedDocument) { 119 if (!accept(publishedDocument)) { 120 return; 121 } 122 DocumentModel proxy = ((SimpleCorePublishedDocument) publishedDocument).getProxy(); 123 PublicationRelationHelper.removePublicationRelation(proxy); 124 coreSession.removeDocument(proxy.getRef()); 125 coreSession.save(); 126 } 127 128 @Override 129 public PublicationNode getNodeByPath(String path) { 130 DocumentRef docRef = new PathRef(path); 131 if (coreSession.hasPermission(docRef, SecurityConstants.READ)) { 132 return new CoreFolderPublicationNode(coreSession.getDocument(new PathRef(path)), this, factory); 133 } else { 134 return new VirtualCoreFolderPublicationNode(coreSession, path, this, factory); 135 } 136 137 } 138 139 @Override 140 public void release() { 141 // TODO Auto-generated method stub 142 } 143 144 @Override 145 protected String getDefaultRootPath() { 146 return DEFAULT_ROOT_PATH; 147 } 148 149 @Override 150 protected PublishedDocumentFactory getDefaultFactory() { 151 return new CoreProxyFactory(); 152 } 153 154 @Override 155 public boolean canPublishTo(PublicationNode publicationNode) { 156 if (publicationNode == null || publicationNode.getParent() == null) { 157 // we can't publish in the root node 158 return false; 159 } 160 DocumentRef docRef = new PathRef(publicationNode.getPath()); 161 return coreSession.hasPermission(docRef, CAN_ASK_FOR_PUBLISHING); 162 } 163 164 @Override 165 public boolean canUnpublish(PublishedDocument publishedDocument) { 166 if (!accept(publishedDocument)) { 167 return false; 168 } 169 DocumentRef docRef = new PathRef(publishedDocument.getParentPath()); 170 return coreSession.hasPermission(docRef, SecurityConstants.WRITE); 171 } 172 173 @Override 174 public PublishedDocument wrapToPublishedDocument(DocumentModel documentModel) { 175 return factory.wrapDocumentModel(documentModel); 176 } 177 178 @Override 179 public boolean isPublicationNode(DocumentModel documentModel) { 180 return documentModel.getPathAsString().startsWith(rootPath); 181 } 182 183 @Override 184 public PublicationNode wrapToPublicationNode(DocumentModel documentModel) { 185 if (!isPublicationNode(documentModel)) { 186 throw new NuxeoException("Document " + documentModel.getPathAsString() 187 + " is not a valid publication node."); 188 } 189 return new CoreFolderPublicationNode(documentModel, this, factory); 190 } 191 192 @Override 193 protected boolean accept(PublishedDocument publishedDocument) { 194 return publishedDocument instanceof SimpleCorePublishedDocument; 195 } 196 197}