001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations.services;
013
014import org.nuxeo.ecm.automation.core.Constants;
015import org.nuxeo.ecm.automation.core.annotations.Context;
016import org.nuxeo.ecm.automation.core.annotations.Operation;
017import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
018import org.nuxeo.ecm.automation.core.annotations.Param;
019import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
020import org.nuxeo.ecm.core.api.CoreSession;
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.platform.relations.api.DocumentRelationManager;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027@Operation(id = CreateRelation.ID, category = Constants.CAT_SERVICES, label = "Create Relation", description = "Create a relation between 2 documents. The subject of the relation will be the input of the operation and the object of the relation will be retrieved from the context using the 'object' field. The 'predicate' field specifies the relation predicate (When using a known predicate, use the full URL like 'purl.org/dc/terms/IsBasedOn', unknown predicates will be treated as plain strings and be the same on the subject and object). The 'outgoing' flag indicates the direction of the relation - the default is false which means the relation will go from the input object to the object specified as 'object' parameter. Return back the subject document.", aliases = { "Relations.CreateRelation" })
028public class CreateRelation {
029
030    public static final String ID = "Document.AddRelation";
031
032    @Context
033    protected CoreSession session;
034
035    @Context
036    protected DocumentRelationManager relations;
037
038    @Param(name = "object")
039    protected DocumentModel object;
040
041    @Param(name = "predicate")
042    // TODO use a combo box?
043    protected String predicate;
044
045    @Param(name = "outgoing", required = false, values = "false")
046    protected boolean outgoing = false;
047
048    @OperationMethod(collector = DocumentModelCollector.class)
049    public DocumentModel run(DocumentModel doc) {
050        relations.addRelation(session, doc, object, predicate, outgoing);
051        return doc;
052    }
053}