001/*
002 * (C) Copyright 2011 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.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 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.localconf;
019
020import static org.nuxeo.ecm.automation.core.Constants.CAT_LOCAL_CONFIGURATION;
021import static org.nuxeo.ecm.localconf.SimpleConfiguration.SIMPLE_CONFIGURATION_FACET;
022
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.util.Properties;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
031
032/**
033 * Operation to put parameters on the Simple Configuration of the input Document.
034 * <p>
035 * The parameters are specified as <i>key=value</i> pairs separated by a new line.
036 * <p>
037 * The <code>SimpleConfiguration</code> facet is added to the input document if needed.
038 *
039 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
040 * @since 5.5
041 */
042@Operation(id = PutSimpleConfParams.ID, category = CAT_LOCAL_CONFIGURATION, label = "Put Simple Configuration parameters", description = "Put Simple Configuration parameters "
043        + "on the input document. "
044        + "Add the 'SimpleConfiguration' facet on the input document if needed. "
045        + "The parameters are specified as <i>key=value</i> pairs separated by a new line. "
046        + "The user adding parameters must have WRITE access on the input document.")
047public class PutSimpleConfParams {
048
049    public static final String ID = "LocalConfiguration.PutSimpleConfigurationParameters";
050
051    @Context
052    protected CoreSession session;
053
054    @Context
055    protected LocalConfigurationService localConfigurationService;
056
057    @Param(name = "parameters")
058    protected Properties parameters;
059
060    @Param(name = "save", required = false, values = "true")
061    protected boolean save = true;
062
063    @OperationMethod
064    public DocumentModel run(DocumentModel doc) {
065        if (!doc.hasFacet(SIMPLE_CONFIGURATION_FACET)) {
066            doc.addFacet(SIMPLE_CONFIGURATION_FACET);
067            doc = session.saveDocument(doc);
068        }
069
070        SimpleConfiguration simpleConfiguration = localConfigurationService.getConfiguration(SimpleConfiguration.class,
071                SIMPLE_CONFIGURATION_FACET, doc);
072        simpleConfiguration.putAll(parameters);
073        simpleConfiguration.save(session);
074
075        if (save) {
076            doc = session.saveDocument(doc);
077        }
078        return doc;
079    }
080
081}