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