001/*
002 * Copyright (c) 2006-2013 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 *     Benjamin JALON <bjalon@nuxeo.com>
011 */
012package org.nuxeo.ecm.automation.core.operations.document;
013
014import java.io.Serializable;
015import java.util.ArrayList;
016import java.util.Arrays;
017import java.util.List;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.nuxeo.ecm.automation.OperationException;
022import org.nuxeo.ecm.automation.core.Constants;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.model.Property;
031import org.nuxeo.ecm.core.schema.types.Type;
032
033/**
034 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
035 * @since 5.7
036 */
037@Operation(id = RemoveEntryOfMultiValuedProperty.ID, category = Constants.CAT_DOCUMENT, label = "Remove Entry Of Multivalued Property", description = "Remove the first entry of the giving value in the multivalued xpath, does nothing if does not exist: <ul<li>if 'is Remove All' is check, all entry instance in the list.</li><li>if not will remove just the first one found</li><ul>", aliases = { "RemoveEntryOfMultivaluedProperty" })
038public class RemoveEntryOfMultiValuedProperty extends AbstractOperationMultiValuedProperty {
039
040    public static final String ID = "Document.RemoveEntryOfMultivaluedProperty";
041
042    public static final Log log = LogFactory.getLog(RemoveEntryOfMultiValuedProperty.class);
043
044    @Context
045    protected CoreSession session;
046
047    @Param(name = "xpath")
048    protected String xpath;
049
050    @Param(name = "value")
051    protected Serializable value;
052
053    @Param(name = "save", required = false, values = { "true" })
054    protected boolean save = true;
055
056    @Param(name = "is Remove All", required = false, values = { "true" })
057    protected boolean isRemoveAll = true;
058
059    @OperationMethod(collector = DocumentModelCollector.class)
060    public DocumentModel run(DocumentModel doc) throws OperationException {
061
062        Property p = doc.getProperty(xpath);
063        Type type = p.getType();
064        checkFieldType(type, value);
065
066        List<Serializable> array = Arrays.asList((Serializable[]) p.getValue());
067
068        if (array == null) {
069            log.info(String.format("Value \"%s\" not found in %s, can't remove it", value, doc.getPathAsString()));
070            return doc;
071        }
072        List<Serializable> list = new ArrayList<Serializable>(array);
073
074        if (!list.contains(value)) {
075            log.info(String.format("Value \"%s\" not found in %s, can't remove it", value, doc.getPathAsString()));
076            return doc;
077        }
078
079        do {
080            list.remove(value);
081            p.setValue(list);
082        } while (list.contains(value) && isRemoveAll);
083
084        if (save) {
085            doc = session.saveDocument(doc);
086            session.save();
087        }
088
089        return doc;
090    }
091
092}