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