001/* 002 * (C) Copyright 2006-2009 Nuxeo SAS (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 * Eugen Ionica 016 */ 017 018package org.nuxeo.ecm.platform.relations.api.util; 019 020import java.util.Collections; 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.CoreSession; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.DocumentModelList; 029import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 030import org.nuxeo.ecm.platform.relations.api.Literal; 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.ResourceAdapter; 036import org.nuxeo.ecm.platform.relations.api.Statement; 037import org.nuxeo.ecm.platform.relations.api.impl.LiteralImpl; 038import org.nuxeo.ecm.platform.relations.api.impl.StatementImpl; 039import org.nuxeo.runtime.api.Framework; 040 041public class RelationHelper { 042 043 static RelationManager relationManager; 044 045 private static final Log log = LogFactory.getLog(RelationHelper.class); 046 047 // Utility class. 048 private RelationHelper() { 049 } 050 051 public static RelationManager getRelationManager() { 052 return Framework.getService(RelationManager.class); 053 } 054 055 /** 056 * Returns the relation node corresponding to a document model. 057 */ 058 public static QNameResource getDocumentResource(DocumentModel document) { 059 QNameResource documentResource = null; 060 RelationManager rm = getRelationManager(); 061 if (document != null && rm != null) { 062 documentResource = (QNameResource) rm.getResource(RelationConstants.DOCUMENT_NAMESPACE, document, null); 063 } 064 return documentResource; 065 } 066 067 /** 068 * Returns the document model corresponding to a relation node. 069 */ 070 public static DocumentModel getDocumentModel(Node node, CoreSession session) { 071 if (node.isQNameResource()) { 072 QNameResource resource = (QNameResource) node; 073 Map<String, Object> context = Collections.<String, Object> singletonMap( 074 ResourceAdapter.CORE_SESSION_CONTEXT_KEY, session); 075 Object o = getRelationManager().getResourceRepresentation(resource.getNamespace(), resource, context); 076 if (o instanceof DocumentModel) { 077 return (DocumentModel) o; 078 } 079 } 080 return null; 081 } 082 083 public static DocumentModelList getSubjectDocuments(Resource predicat, String stringObject, CoreSession session) { 084 return getSubjectDocuments(RelationConstants.GRAPH_NAME, predicat, stringObject, session); 085 } 086 087 public static DocumentModelList getSubjectDocuments(String graphName, Resource predicat, String stringObject, 088 CoreSession session) { 089 Literal literal = new LiteralImpl(stringObject); 090 List<Statement> stmts = getRelationManager().getGraphByName(graphName).getStatements(null, predicat, literal); 091 if (stmts != null) { 092 DocumentModelList docs = new DocumentModelListImpl(); 093 for (Statement stmt : stmts) { 094 DocumentModel d = getDocumentModel(stmt.getSubject(), session); 095 if (d != null) { 096 docs.add(d); 097 } 098 } 099 return docs; 100 } 101 return null; 102 } 103 104 public static DocumentModelList getSubjectDocuments(Resource predicat, DocumentModel objectDocument) { 105 return getSubjectDocuments(RelationConstants.GRAPH_NAME, predicat, objectDocument); 106 } 107 108 public static DocumentModelList getSubjectDocuments(String graphName, Resource predicat, 109 DocumentModel objectDocument) { 110 QNameResource docResource = getDocumentResource(objectDocument); 111 List<Statement> stmts = getRelationManager().getGraphByName(graphName).getStatements(null, predicat, 112 docResource); 113 if (stmts != null) { 114 DocumentModelList docs = new DocumentModelListImpl(); 115 for (Statement stmt : stmts) { 116 DocumentModel d = getDocumentModel(stmt.getSubject(), objectDocument.getCoreSession()); 117 if (d != null) { 118 docs.add(d); 119 } 120 } 121 return docs; 122 } 123 return null; 124 } 125 126 public static DocumentModelList getSubjectDocumentsOut(Resource predicat, DocumentModel objectDocument) { 127 return getSubjectDocumentsOut(RelationConstants.GRAPH_NAME, predicat, objectDocument); 128 } 129 130 public static DocumentModelList getSubjectDocumentsOut(String graphName, Resource predicat, 131 DocumentModel objectDocument) { 132 QNameResource docResource = getDocumentResource(objectDocument); 133 List<Statement> stmts = getRelationManager().getGraphByName(graphName).getStatements(docResource, predicat, 134 null); 135 if (stmts != null) { 136 DocumentModelList docs = new DocumentModelListImpl(); 137 for (Statement stmt : stmts) { 138 DocumentModel d = getDocumentModel(stmt.getObject(), objectDocument.getCoreSession()); 139 if (d != null) { 140 docs.add(d); 141 } 142 } 143 return docs; 144 } 145 return null; 146 } 147 148 public static DocumentModelList getObjectDocuments(DocumentModel subjectDoc, Resource predicat) { 149 return getObjectDocuments(RelationConstants.GRAPH_NAME, subjectDoc, predicat); 150 } 151 152 public static DocumentModelList getObjectDocuments(String graphName, DocumentModel subjectDoc, Resource predicat) { 153 List<Statement> stmts = getStatements(graphName, subjectDoc, predicat); 154 if (stmts != null) { 155 DocumentModelList docs = new DocumentModelListImpl(); 156 for (Statement stmt : stmts) { 157 DocumentModel d = getDocumentModel(stmt.getObject(), subjectDoc.getCoreSession()); 158 if (d != null) { 159 docs.add(d); 160 } 161 } 162 return docs; 163 } 164 return null; 165 } 166 167 public static List<Statement> getStatements(DocumentModel subjectDoc, Resource predicat) { 168 return getStatements(RelationConstants.GRAPH_NAME, subjectDoc, predicat); 169 } 170 171 public static List<Statement> getStatements(String graphName, DocumentModel subjectDoc, Resource predicat) { 172 QNameResource docResource = getDocumentResource(subjectDoc); 173 return getRelationManager().getGraphByName(graphName).getStatements(docResource, predicat, null); 174 } 175 176 public static void removeRelation(DocumentModel subjectDoc, Resource predicat, DocumentModel objectDoc) { 177 removeRelation(RelationConstants.GRAPH_NAME, subjectDoc, predicat, objectDoc); 178 } 179 180 public static void removeRelation(String graphName, DocumentModel subjectDoc, Resource predicat, 181 DocumentModel objectDoc) { 182 QNameResource subject = getDocumentResource(subjectDoc); 183 QNameResource object = getDocumentResource(objectDoc); 184 Statement stmt = new StatementImpl(subject, predicat, object); 185 getRelationManager().getGraphByName(graphName).remove(stmt); 186 } 187 188}