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.nuxeo.ecm.automation.OperationException;
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.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.model.Property;
029import org.nuxeo.ecm.core.schema.types.Type;
030
031/**
032 * See operation documentation
033 *
034 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
035 * @since 5.7
036 */
037@Operation(id = AddEntryToMultiValuedProperty.ID, category = Constants.CAT_DOCUMENT, label = "Add entry into multi-valued metadata", description = "Add value into the field expressed by the xpath parameter. This field must be a multivalued metadata.<p> 'checkExists' parameter enables to add value only if doesn't already exists in the field: <ul><li> if checked, the value will not be added if it exists already in the list</li><li>if not checked the value will be always added</li</ul>.<p> Remark: <b>only works for a field that stores a list of scalars (string, boolean, date, int, long) and not list of complex types.<b>", aliases = { "AddEntryToMultivaluedProperty" })
038public class AddEntryToMultiValuedProperty extends AbstractOperationMultiValuedProperty {
039
040    public static final String ID = "DocumentMultivaluedProperty.addItem";
041
042    @Context
043    protected CoreSession session;
044
045    @Param(name = "xpath")
046    protected String xpath;
047
048    @Param(name = "value")
049    protected Serializable value;
050
051    @Param(name = "save", required = false, values = { "true" })
052    protected boolean save = true;
053
054    @Param(name = "checkExists", required = false, values = { "true" })
055    protected boolean checkExists = true;
056
057    @OperationMethod(collector = DocumentModelCollector.class)
058    public DocumentModel run(DocumentModel doc) throws OperationException {
059        Property p = doc.getProperty(xpath);
060        Type type = p.getType();
061        checkFieldType(type, value);
062
063        List<Serializable> array = p.getValue() != null ? Arrays.asList((Serializable[]) p.getValue()) : null;
064
065        Serializable newValue = addValueIntoList(array, value);
066
067        p.setValue(newValue);
068
069        if (save) {
070            doc = session.saveDocument(doc);
071            session.save();
072        }
073
074        return doc;
075    }
076
077    private Serializable addValueIntoList(List<Serializable> array, Object valueToAdd) {
078
079        List<Object> list = new ArrayList<Object>();
080
081        if (array != null) {
082            list.addAll(array);
083        }
084
085        if (!list.contains(valueToAdd) || !checkExists) {
086            list.add(valueToAdd);
087        }
088
089        return (Serializable) list;
090
091    }
092}