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