001/*
002 * (C) Copyright 2016 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 */
017package org.nuxeo.automation.scripting.internals;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.nuxeo.ecm.automation.AutomationService;
023import org.nuxeo.ecm.automation.OperationException;
024import org.nuxeo.ecm.automation.OperationType;
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.runtime.model.SimpleContributionRegistry;
027
028public class AutomationScriptingRegistry extends SimpleContributionRegistry<ScriptingOperationDescriptor> {
029
030    protected AutomationScriptingServiceImpl scripting;
031
032    protected AutomationService automation;
033
034    protected final Map<String,OperationType> registration = new HashMap<>();
035
036    @Override
037    public String getContributionId(ScriptingOperationDescriptor contrib) {
038        return contrib.getId();
039    }
040
041    @Override
042    public void contributionRemoved(String id, ScriptingOperationDescriptor origContrib) {
043        automation.removeOperation(registration.remove(id));
044    }
045
046    @Override
047    public void contributionUpdated(String id, ScriptingOperationDescriptor contrib,
048            ScriptingOperationDescriptor newOrigContrib) {
049        ScriptingOperationTypeImpl type = new ScriptingOperationTypeImpl(scripting,
050                automation, contrib);
051        try {
052            automation.putOperation(type, true);
053        } catch (OperationException cause) {
054            throw new NuxeoException("Cannot update scripting operation " + id, cause);
055        }
056        registration.put(id, type);
057    }
058
059
060}