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
031/**
032 * @since 7.1 Untag document from one or several 'tags'.
033 */
034@Operation(id = UntagDocument.ID, category = Constants.CAT_SERVICES, label = "Untag Document", description = "Untag document from one or "
035        + "several 'tags'.", since = "7.1", addToStudio = true)
036public class UntagDocument {
037
038    public static final String ID = "Services.UntagDocument";
039
040    @Context
041    protected TagService tagService;
042
043    @Context
044    protected CoreSession session;
045
046    @Param(name = "tags", required = true, description = "Labels or tags " + "separated by comma.")
047    protected StringList tags;
048
049    @OperationMethod(collector = DocumentModelCollector.class)
050    public DocumentModel run(DocumentModel document) {
051        if (tags != null) {
052            for (String tag : tags) {
053                tagService.untag(session, document.getId(), tag, session.getPrincipal().getName());
054            }
055        }
056        return document;
057    }
058}