001/* 002 * (C) Copyright 2006-2018 Nuxeo (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 * bstefanescu 018 */ 019package org.nuxeo.ecm.automation.core.operations.services; 020 021import java.util.Collections; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.commons.lang3.StringUtils; 026import org.nuxeo.ecm.automation.core.Constants; 027import org.nuxeo.ecm.automation.core.annotations.Context; 028import org.nuxeo.ecm.automation.core.annotations.Operation; 029import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 030import org.nuxeo.ecm.automation.core.annotations.Param; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.DocumentModelList; 034import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 035import org.nuxeo.ecm.platform.relations.api.Node; 036import org.nuxeo.ecm.platform.relations.api.QNameResource; 037import org.nuxeo.ecm.platform.relations.api.RelationManager; 038import org.nuxeo.ecm.platform.relations.api.Resource; 039import org.nuxeo.ecm.platform.relations.api.ResourceAdapter; 040import org.nuxeo.ecm.platform.relations.api.Statement; 041import org.nuxeo.ecm.platform.relations.api.impl.ResourceImpl; 042import org.nuxeo.ecm.platform.relations.api.util.RelationConstants; 043 044/** 045 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 046 */ 047@Operation(id = GetRelations.ID, category = Constants.CAT_SERVICES, label = "Get Linked Documents", description = "Get the relations for the input document. The 'outgoing' parameter ca be used to specify whether outgoing or incoming relations should be returned. Retuns a document list.", aliases = { 048 "Relations.GetRelations" }) 049public class GetRelations { 050 051 public static final String ID = "Document.GetLinkedDocuments"; 052 053 @Context 054 protected CoreSession session; 055 056 @Context 057 protected RelationManager relations; 058 059 @Param(name = "predicate") 060 // TODO use a combo box? 061 protected String predicate; 062 063 @Param(name = "outgoing", required = false) 064 protected boolean outgoing = true; 065 066 @Param(name = "graphName", required = false) 067 protected String graphName; 068 069 @OperationMethod 070 public DocumentModelList run(DocumentModel doc) { 071 QNameResource res = getDocumentResource(doc); 072 Resource predicate = getPredicate(); 073 return getDocuments(res, predicate); 074 } 075 076 protected QNameResource getDocumentResource(DocumentModel document) { 077 return (QNameResource) relations.getResource(RelationConstants.DOCUMENT_NAMESPACE, document, null); 078 } 079 080 protected Resource getPredicate() { 081 return predicate != null && predicate.length() > 0 ? new ResourceImpl(predicate) : null; 082 } 083 084 protected DocumentModelList getDocuments(QNameResource res, Resource predicate) { 085 if (outgoing) { 086 List<Statement> statements = getOutgoingStatements(res, predicate); 087 DocumentModelList docs = new DocumentModelListImpl(statements.size()); 088 for (Statement st : statements) { 089 DocumentModel dm = getDocumentModel(st.getObject()); 090 if (dm != null) { 091 docs.add(dm); 092 } 093 } 094 return docs; 095 } else { 096 List<Statement> statements = getIncomingStatements(res, predicate); 097 DocumentModelList docs = new DocumentModelListImpl(statements.size()); 098 for (Statement st : statements) { 099 DocumentModel dm = getDocumentModel(st.getSubject()); 100 if (dm != null) { 101 docs.add(dm); 102 } 103 } 104 return docs; 105 } 106 } 107 108 protected List<Statement> getIncomingStatements(QNameResource res, Resource predicate) { 109 return relations.getGraphByName(getGraphName()).getStatements(null, predicate, res); 110 } 111 112 protected List<Statement> getOutgoingStatements(QNameResource res, Resource predicate) { 113 return relations.getGraphByName(getGraphName()).getStatements(res, predicate, null); 114 } 115 116 protected DocumentModel getDocumentModel(Node node) { 117 if (node.isQNameResource()) { 118 QNameResource resource = (QNameResource) node; 119 Map<String, Object> context = Collections.singletonMap(ResourceAdapter.CORE_SESSION_CONTEXT_KEY, session); 120 Object o = relations.getResourceRepresentation(resource.getNamespace(), resource, context); 121 if (o instanceof DocumentModel) { 122 return (DocumentModel) o; 123 } 124 } 125 return null; 126 } 127 128 /** 129 * @since 5.5 130 */ 131 public String getGraphName() { 132 if (StringUtils.isEmpty(graphName)) { 133 return RelationConstants.GRAPH_NAME; 134 } 135 return graphName; 136 } 137 138}