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