001/*
002 * (C) Copyright 2006-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.activity;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.runtime.model.ContributionFragmentRegistry;
028
029/**
030 * Registry for activity streams, handling merge of registered {@link ActivityStream} elements.
031 *
032 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
033 * @since 5.5
034 */
035public class ActivityStreamRegistry extends ContributionFragmentRegistry<ActivityStream> {
036
037    protected Map<String, ActivityStream> activityStreams = new HashMap<String, ActivityStream>();
038
039    public ActivityStream get(String name) {
040        return activityStreams.get(name);
041    }
042
043    @Override
044    public String getContributionId(ActivityStream contrib) {
045        return contrib.getName();
046    }
047
048    @Override
049    public void contributionUpdated(String id, ActivityStream contrib, ActivityStream newOrigContrib) {
050        activityStreams.put(id, contrib);
051    }
052
053    @Override
054    public void contributionRemoved(String id, ActivityStream origContrib) {
055        activityStreams.remove(id);
056    }
057
058    @Override
059    public ActivityStream clone(ActivityStream orig) {
060        return orig.clone();
061    }
062
063    @Override
064    public void merge(ActivityStream src, ActivityStream dst) {
065        List<String> newVerbs = src.getVerbs();
066        if (newVerbs != null) {
067            List<String> merged = new ArrayList<String>();
068            merged.addAll(newVerbs);
069            boolean keepOld = src.isAppendVerbs() || (newVerbs.isEmpty() && !src.isAppendVerbs());
070            if (keepOld) {
071                // add back old contributions
072                List<String> oldVerbs = dst.getVerbs();
073                if (oldVerbs != null) {
074                    merged.addAll(0, oldVerbs);
075                }
076            }
077            dst.setVerbs(merged);
078        }
079
080        List<String> newRelationshipKinds = src.getRelationshipKinds();
081        if (newRelationshipKinds != null) {
082            List<String> merged = new ArrayList<String>();
083            merged.addAll(newRelationshipKinds);
084            boolean keepOld = src.isAppendRelationshipKinds()
085                    || (newRelationshipKinds.isEmpty() && !src.isAppendRelationshipKinds());
086            if (keepOld) {
087                // add back old contributions
088                List<String> oldRelationshipKinds = dst.getRelationshipKinds();
089                if (oldRelationshipKinds != null) {
090                    merged.addAll(0, oldRelationshipKinds);
091                }
092            }
093            dst.setRelationshipKinds(merged);
094        }
095    }
096}