001/*
002 * (C) Copyright 2012 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.activity;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.Comparator;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.nuxeo.runtime.model.ContributionFragmentRegistry;
030
031/**
032 * Registry for activity upgraders, handling merge of registered
033 * {@link org.nuxeo.ecm.activity.ActivityUpgraderDescriptor} elements.
034 *
035 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
036 * @since 5.7
037 */
038public class ActivityUpgraderRegistry extends ContributionFragmentRegistry<ActivityUpgraderDescriptor> {
039
040    protected Map<String, ActivityUpgrader> activityUpgraders = new HashMap<String, ActivityUpgrader>();
041
042    public List<ActivityUpgrader> getOrderedActivityUpgraders() {
043        List<ActivityUpgrader> upgraders = new ArrayList<ActivityUpgrader>(activityUpgraders.values());
044        Collections.sort(upgraders, new Comparator<ActivityUpgrader>() {
045            @Override
046            public int compare(ActivityUpgrader o1, ActivityUpgrader o2) {
047                return o1.getOrder() - o2.getOrder();
048            }
049        });
050        return upgraders;
051    }
052
053    @Override
054    public String getContributionId(ActivityUpgraderDescriptor contrib) {
055        return contrib.getName();
056    }
057
058    @Override
059    public void contributionUpdated(String id, ActivityUpgraderDescriptor contrib,
060            ActivityUpgraderDescriptor newOrigContrib) {
061        activityUpgraders.put(id, contrib.getActivityUpgrader());
062    }
063
064    @Override
065    public void contributionRemoved(String id, ActivityUpgraderDescriptor origContrib) {
066        activityUpgraders.remove(id);
067    }
068
069    @Override
070    public ActivityUpgraderDescriptor clone(ActivityUpgraderDescriptor orig) {
071        return orig.clone();
072    }
073
074    @Override
075    public void merge(ActivityUpgraderDescriptor src, ActivityUpgraderDescriptor dst) {
076        Class<? extends ActivityUpgrader> clazz = src.getActivityUpgraderClass();
077        if (clazz != null) {
078            dst.setActivityUpgraderClass(clazz);
079        }
080        dst.setOrder(src.getOrder());
081
082        boolean enabled = src.isEnabled();
083        if (enabled != dst.isEnabled()) {
084            dst.setEnabled(enabled);
085        }
086    }
087
088}