001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thomas Roger <troger@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.localconf;
021
022import static org.nuxeo.ecm.automation.core.Constants.CAT_LOCAL_CONFIGURATION;
023import static org.nuxeo.ecm.localconf.SimpleConfiguration.SIMPLE_CONFIGURATION_FACET;
024
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
032
033/**
034 * Operation to put a parameter on the Simple Configuration of the input Document.
035 * <p>
036 * The <code>SimpleConfiguration</code> facet is added to the input document if needed.
037 *
038 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
039 * @since 5.5
040 */
041@Operation(id = PutSimpleConfParam.ID, category = CAT_LOCAL_CONFIGURATION, label = "Put a Simple Configuration Parameter", description = "Put a Simple Configuration parameter "
042        + "on the input document. "
043        + "Add the 'SimpleConfiguration' facet on the input document if needed. "
044        + "The user adding a parameter must have WRITE access on the input document")
045public class PutSimpleConfParam {
046
047    public static final String ID = "LocalConfiguration.PutSimpleConfigurationParameter";
048
049    @Context
050    protected CoreSession session;
051
052    @Context
053    protected LocalConfigurationService localConfigurationService;
054
055    @Param(name = "key")
056    protected String key;
057
058    @Param(name = "value")
059    protected String value;
060
061    @Param(name = "save", required = false, values = "true")
062    protected boolean save = true;
063
064    @OperationMethod
065    public DocumentModel run(DocumentModel doc) {
066        if (!doc.hasFacet(SIMPLE_CONFIGURATION_FACET)) {
067            doc.addFacet(SIMPLE_CONFIGURATION_FACET);
068            doc = session.saveDocument(doc);
069        }
070
071        SimpleConfiguration simpleConfiguration = localConfigurationService.getConfiguration(SimpleConfiguration.class,
072                SIMPLE_CONFIGURATION_FACET, doc);
073        simpleConfiguration.put(key, value);
074        simpleConfiguration.save(session);
075
076        if (save) {
077            doc = session.saveDocument(doc);
078        }
079        return doc;
080    }
081
082}