001/* 002 * (C) Copyright 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 * Thomas Roger 016 */ 017 018package org.nuxeo.ecm.platform.publisher.helper; 019 020import java.util.List; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.core.api.CoreSession; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.NuxeoException; 027import org.nuxeo.ecm.platform.publisher.api.PublicationTree; 028import org.nuxeo.ecm.platform.publisher.api.PublisherService; 029import org.nuxeo.ecm.platform.relations.api.Node; 030import org.nuxeo.ecm.platform.relations.api.QNameResource; 031import org.nuxeo.ecm.platform.relations.api.RelationManager; 032import org.nuxeo.ecm.platform.relations.api.Resource; 033import org.nuxeo.ecm.platform.relations.api.Statement; 034import org.nuxeo.ecm.platform.relations.api.impl.QNameResourceImpl; 035import org.nuxeo.ecm.platform.relations.api.impl.ResourceImpl; 036import org.nuxeo.ecm.platform.relations.api.impl.StatementImpl; 037import org.nuxeo.ecm.platform.relations.api.util.RelationHelper; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 042 */ 043public class PublicationRelationHelper { 044 045 public static final String PUBLICATION_GRAPH_NAME = "publication"; 046 047 public static final String PUBLICATION_TREE_NAMESPACE = "http://www.nuxeo.org/publication/tree/"; 048 049 public static final Resource PUBLISHED_BY = new ResourceImpl("http://www.nuxeo.org/publication/publishedBy"); 050 051 private static Log log = LogFactory.getLog(PublicationRelationHelper.class); 052 053 private PublicationRelationHelper() { 054 // Helper class 055 } 056 057 public static void addPublicationRelation(DocumentModel documentModel, PublicationTree publicationTree) 058 { 059 RelationManager rm = RelationHelper.getRelationManager(); 060 QNameResource docResource = RelationHelper.getDocumentResource(documentModel); 061 QNameResource treeResource = new QNameResourceImpl(PUBLICATION_TREE_NAMESPACE, publicationTree.getConfigName()); 062 Statement stmt = new StatementImpl(docResource, PUBLISHED_BY, treeResource); 063 rm.getGraphByName(PUBLICATION_GRAPH_NAME).add(stmt); 064 } 065 066 public static void removePublicationRelation(DocumentModel documentModel) { 067 List<Statement> stmts = RelationHelper.getStatements(PUBLICATION_GRAPH_NAME, documentModel, PUBLISHED_BY); 068 RelationManager rm = RelationHelper.getRelationManager(); 069 if (stmts != null) { 070 rm.getGraphByName(PUBLICATION_GRAPH_NAME).remove(stmts); 071 } 072 } 073 074 public static boolean isPublished(DocumentModel documentModel) { 075 List<Statement> stmts = RelationHelper.getStatements(PUBLICATION_GRAPH_NAME, documentModel, PUBLISHED_BY); 076 return stmts != null && !stmts.isEmpty(); 077 } 078 079 public static PublicationTree getPublicationTreeUsedForPublishing(DocumentModel documentModel, 080 CoreSession coreSession) { 081 if (!isPublished(documentModel)) { 082 throw new NuxeoException("The document " + documentModel.getPathAsString() 083 + " is not a published document"); 084 } 085 List<Statement> stmts = RelationHelper.getStatements(PUBLICATION_GRAPH_NAME, documentModel, PUBLISHED_BY); 086 Statement statement = stmts.get(0); 087 088 PublicationTree tree = null; 089 Node node = statement.getObject(); 090 if (node.isQNameResource()) { 091 QNameResource resource = (QNameResource) statement.getObject(); 092 String localName = resource.getLocalName(); 093 PublisherService publisherService = Framework.getService(PublisherService.class); 094 tree = publisherService.getPublicationTree(localName, coreSession, null); 095 } else { 096 log.error("Resource is not a QNameResource, check the namespace"); 097 098 } 099 return tree; 100 } 101 102}