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