001/*
002 * (C) Copyright 2012 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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.contentview.jsf;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.HashMap;
022import java.util.LinkedHashSet;
023import java.util.List;
024import java.util.Map;
025import java.util.Set;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.platform.query.core.CoreQueryPageProviderDescriptor;
030import org.nuxeo.ecm.platform.query.core.GenericPageProviderDescriptor;
031import org.nuxeo.ecm.platform.query.core.ReferencePageProviderDescriptor;
032import org.nuxeo.runtime.model.ContributionFragmentRegistry;
033
034/**
035 * Registry for content view contributions, handling accurate merge on hot reload.
036 *
037 * @since 5.6
038 */
039public class ContentViewRegistry extends ContributionFragmentRegistry<ContentViewDescriptor> {
040
041    protected static final Log log = LogFactory.getLog(ContentViewRegistry.class);
042
043    protected final Map<String, ContentViewDescriptor> contentViews = new HashMap<String, ContentViewDescriptor>();
044
045    protected final Map<String, Set<String>> contentViewsByFlag = new HashMap<String, Set<String>>();
046
047    @Override
048    public String getContributionId(ContentViewDescriptor contrib) {
049        return contrib.getName();
050    }
051
052    @Override
053    public void contributionUpdated(String id, ContentViewDescriptor contrib, ContentViewDescriptor newOrigContrib) {
054        String name = contrib.getName();
055        if (name == null) {
056            log.error("Cannot register content view without a name");
057            return;
058        }
059        if (contentViews.containsKey(id)) {
060            contentViews.remove(id);
061            removeContentViewFlags(name);
062        }
063        if (contrib.isEnabled()) {
064            contentViews.put(name, contrib);
065            addContentViewFlags(contrib);
066            log.info("Registering content view with name " + id);
067        }
068    }
069
070    @Override
071    public void contributionRemoved(String id, ContentViewDescriptor origContrib) {
072        contentViews.remove(id);
073        removeContentViewFlags(origContrib);
074        log.info("Unregistering content view with name " + id);
075    }
076
077    protected void addContentViewFlags(ContentViewDescriptor desc) {
078        String name = desc.getName();
079        List<String> flags = desc.getFlags();
080        if (flags != null) {
081            for (String flag : flags) {
082                Set<String> items = contentViewsByFlag.get(flag);
083                if (items == null) {
084                    items = new LinkedHashSet<String>();
085                }
086                items.add(name);
087                contentViewsByFlag.put(flag, items);
088            }
089        }
090    }
091
092    protected void removeContentViewFlags(String contentViewName) {
093        for (Set<String> items : contentViewsByFlag.values()) {
094            if (items != null) {
095                items.remove(contentViewName);
096            }
097        }
098    }
099
100    protected void removeContentViewFlags(ContentViewDescriptor desc) {
101        String name = desc.getName();
102        List<String> flags = desc.getFlags();
103        if (flags != null) {
104            for (String flag : flags) {
105                Set<String> items = contentViewsByFlag.get(flag);
106                if (items != null) {
107                    items.remove(name);
108                }
109            }
110        }
111    }
112
113    @Override
114    public ContentViewDescriptor clone(ContentViewDescriptor orig) {
115        return orig.clone();
116    }
117
118    @Override
119    public void merge(ContentViewDescriptor src, ContentViewDescriptor dst) {
120        dst.merge(src);
121    }
122
123    // API
124
125    public ContentViewDescriptor getContentView(String id) {
126        return contentViews.get(id);
127    }
128
129    public boolean hasContentView(String id) {
130        return contentViews.containsKey(id);
131    }
132
133    public Set<String> getContentViewsByFlag(String flag) {
134        return contentViewsByFlag.get(flag);
135    }
136
137    public Set<String> getContentViewNames() {
138        return contentViews.keySet();
139    }
140
141    public Collection<ContentViewDescriptor> getContentViews() {
142        return contentViews.values();
143    }
144
145}