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