001/*
002 * (C) Copyright 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.theme.styling.service.registries;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.runtime.model.ContributionFragmentRegistry;
027import org.nuxeo.theme.styling.service.descriptors.PageDescriptor;
028
029/**
030 * Registry for theme page resources, handling merge of registered {@link PageDescriptor} elements.
031 *
032 * @since 5.5
033 */
034public class PageRegistry extends ContributionFragmentRegistry<PageDescriptor> {
035
036    protected Map<String, PageDescriptor> pageResources = new HashMap<String, PageDescriptor>();
037
038    @Override
039    public String getContributionId(PageDescriptor contrib) {
040        return contrib.getName();
041    }
042
043    @Override
044    public void contributionUpdated(String id, PageDescriptor contrib, PageDescriptor newOrigContrib) {
045        pageResources.put(id, contrib);
046    }
047
048    @Override
049    public synchronized void removeContribution(PageDescriptor contrib) {
050        removeContribution(contrib, true);
051    }
052
053    @Override
054    public void contributionRemoved(String id, PageDescriptor origContrib) {
055        pageResources.remove(id);
056    }
057
058    @Override
059    public PageDescriptor clone(PageDescriptor orig) {
060        if (orig == null) {
061            return null;
062        }
063        return orig.clone();
064    }
065
066    @Override
067    public void merge(PageDescriptor src, PageDescriptor dst) {
068        dst.merge(src);
069    }
070
071    public PageDescriptor getPage(String id) {
072        return pageResources.get(id);
073    }
074
075    /**
076     * @deprecated since 7.4: use {@link #getPage(String)} instead.
077     */
078    @Deprecated
079    public PageDescriptor getThemePage(String id) {
080        return getPage(id);
081    }
082
083    public List<PageDescriptor> getPages() {
084        List<PageDescriptor> res = new ArrayList<PageDescriptor>();
085        for (PageDescriptor page : pageResources.values()) {
086            if (page != null) {
087                res.add(page);
088            }
089        }
090        return res;
091    }
092
093    /**
094     * Returns all the page names.
095     *
096     * @since 7.10
097     */
098    public List<String> getPageNames() {
099        return new ArrayList<String>(pageResources.keySet());
100    }
101
102    /**
103     * @deprecated since 7.4: use {@link #getPages()} instead.
104     */
105    @Deprecated
106    public List<PageDescriptor> getThemePages() {
107        return getPages();
108    }
109
110    public PageDescriptor getConfigurationApplyingToAll() {
111        return pageResources.get("*");
112    }
113
114    /**
115     * @deprecated since 7.4: use {@link #getConfigurationApplyingToAll()} instead.
116     */
117    public PageDescriptor getConfigurationApplyingToAllThemes() {
118        return getConfigurationApplyingToAll();
119    }
120
121}