001/*
002 * (C) Copyright 2006-2011 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 *     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.lang.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 = { "Relations.GetRelations" })
048public class GetRelations {
049
050    public static final String ID = "Document.GetLinkedDocuments";
051
052    @Context
053    protected CoreSession session;
054
055    @Context
056    protected RelationManager relations;
057
058    @Param(name = "predicate")
059    // TODO use a combo box?
060    protected String predicate;
061
062    @Param(name = "outgoing", required = false)
063    protected boolean outgoing = true;
064
065    @Param(name = "graphName", required = false)
066    protected String graphName;
067
068    @OperationMethod
069    public DocumentModelList run(DocumentModel doc) {
070        QNameResource res = getDocumentResource(doc);
071        Resource predicate = getPredicate();
072        return getDocuments(res, predicate);
073    }
074
075    protected QNameResource getDocumentResource(DocumentModel document) {
076        return (QNameResource) relations.getResource(RelationConstants.DOCUMENT_NAMESPACE, document, null);
077    }
078
079    protected Resource getPredicate() {
080        return predicate != null && predicate.length() > 0 ? new ResourceImpl(predicate) : null;
081    }
082
083    protected DocumentModelList getDocuments(QNameResource res, Resource predicate) {
084        if (outgoing) {
085            List<Statement> statements = getOutgoingStatements(res, predicate);
086            DocumentModelList docs = new DocumentModelListImpl(statements.size());
087            for (Statement st : statements) {
088                DocumentModel dm = getDocumentModel(st.getObject());
089                if (dm != null) {
090                    docs.add(dm);
091                }
092            }
093            return docs;
094        } else {
095            List<Statement> statements = getIncomingStatements(res, predicate);
096            DocumentModelList docs = new DocumentModelListImpl(statements.size());
097            for (Statement st : statements) {
098                DocumentModel dm = getDocumentModel(st.getSubject());
099                if (dm != null) {
100                    docs.add(dm);
101                }
102            }
103            return docs;
104        }
105    }
106
107    protected List<Statement> getIncomingStatements(QNameResource res, Resource predicate) {
108        return relations.getGraphByName(getGraphName()).getStatements(null, predicate, res);
109    }
110
111    protected List<Statement> getOutgoingStatements(QNameResource res, Resource predicate) {
112        return relations.getGraphByName(getGraphName()).getStatements(res, predicate, null);
113    }
114
115    protected DocumentModel getDocumentModel(Node node) {
116        if (node.isQNameResource()) {
117            QNameResource resource = (QNameResource) node;
118            Map<String, Object> context = Collections.<String, Object> singletonMap(
119                    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}