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