001/*
002 * (C) Copyright 2012-2014 Nuxeo SA (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-2.1.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 *      Vladimir Pasquier <vpasquier@nuxeo.com>
016 *      Thibaud Arguillere <targuillere@nuxeo.com>
017 */
018package org.nuxeo.ecm.platform.tag.operations;
019
020import org.nuxeo.ecm.automation.core.Constants;
021import org.nuxeo.ecm.automation.core.annotations.Context;
022import org.nuxeo.ecm.automation.core.annotations.Operation;
023import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
024import org.nuxeo.ecm.automation.core.annotations.Param;
025import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
026import org.nuxeo.ecm.automation.core.util.StringList;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.platform.tag.TagService;
030
031import java.util.Collections;
032
033/**
034 * @since 7.1 Tag a document with one or several 'tags'.
035 */
036@Operation(id = TagDocument.ID, category = Constants.CAT_SERVICES, label = "Tag Document", description = "Tag document with one or "
037        + "several 'tags'.", since = "7.1", addToStudio = true)
038public class TagDocument {
039
040    public static final String ID = "Services.TagDocument";
041
042    @Context
043    protected TagService tagService;
044
045    @Context
046    protected CoreSession session;
047
048    @Param(name = "tags", required = true, description = "Labels or tags " + "separated by comma.")
049    protected StringList tags;
050
051    @OperationMethod(collector = DocumentModelCollector.class)
052    public DocumentModel run(DocumentModel document) {
053        if (tags != null) {
054            tags.removeAll(Collections.singleton(""));
055            for (String tag : tags) {
056                tagService.tag(session, document.getId(), tag, session.getPrincipal().getName());
057            }
058        }
059        return document;
060    }
061}