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.OperationContext;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
032
033/**
034 * Operation to set a context variable with the value of the given parameter name of the SimpleConfiguration retrieve
035 * from the input Document.
036 *
037 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
038 * @since 5.5
039 */
040@Operation(id = SetSimpleConfParamVar.ID, category = CAT_LOCAL_CONFIGURATION, label = "Set Context Variable From a Simple Configuration Parameter", description = "Set a context variable "
041        + "that points to the value of the given parameter name in "
042        + "the SimpleConfiguration from the input Document. " + "You must give a name for the variable.")
043public class SetSimpleConfParamVar {
044
045    public static final String ID = "LocalConfiguration.SetSimpleConfigurationParameterAsVar";
046
047    @Context
048    protected OperationContext ctx;
049
050    @Context
051    protected LocalConfigurationService localConfigurationService;
052
053    @Param(name = "name")
054    protected String name;
055
056    @Param(name = "parameterName")
057    protected String parameterName;
058
059    @Param(name = "defaultValue", required = false)
060    protected String defaultValue;
061
062    @OperationMethod
063    public DocumentModel run(DocumentModel doc) {
064        SimpleConfiguration simpleConfiguration = localConfigurationService.getConfiguration(SimpleConfiguration.class,
065                SIMPLE_CONFIGURATION_FACET, doc);
066        String value = simpleConfiguration.get(parameterName, defaultValue);
067        ctx.put(name, value);
068        return doc;
069    }
070
071}