001/*
002 * (C) Copyright 2011 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.theme.styling.service.registries;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.commons.lang.StringUtils;
025import org.nuxeo.runtime.model.ContributionFragmentRegistry;
026import org.nuxeo.theme.styling.service.descriptors.FlavorDescriptor;
027
028/**
029 * Registry for theme flavors, handling merge of registered {@link FlavorDescriptor} elements.
030 *
031 * @since 5.5
032 */
033public class FlavorRegistry extends ContributionFragmentRegistry<FlavorDescriptor> {
034
035    protected Map<String, FlavorDescriptor> themePageFlavors = new HashMap<String, FlavorDescriptor>();
036
037    @Override
038    public String getContributionId(FlavorDescriptor contrib) {
039        return contrib.getName();
040    }
041
042    @Override
043    public void contributionUpdated(String id, FlavorDescriptor contrib, FlavorDescriptor newOrigContrib) {
044        themePageFlavors.put(id, contrib);
045    }
046
047    @Override
048    public synchronized void removeContribution(FlavorDescriptor contrib) {
049        removeContribution(contrib, true);
050    }
051
052    @Override
053    public void contributionRemoved(String id, FlavorDescriptor origContrib) {
054        themePageFlavors.remove(id);
055    }
056
057    @Override
058    public FlavorDescriptor clone(FlavorDescriptor orig) {
059        if (orig == null) {
060            return null;
061        }
062        return orig.clone();
063    }
064
065    @Override
066    public void merge(FlavorDescriptor src, FlavorDescriptor dst) {
067        dst.merge(src);
068    }
069
070    public FlavorDescriptor getFlavor(String id) {
071        return themePageFlavors.get(id);
072    }
073
074    public List<FlavorDescriptor> getFlavorsExtending(String flavor) {
075        List<FlavorDescriptor> res = new ArrayList<FlavorDescriptor>();
076        for (FlavorDescriptor f : themePageFlavors.values()) {
077            if (f != null) {
078                String extendsFlavor = f.getExtendsFlavor();
079                if (!StringUtils.isBlank(extendsFlavor) && extendsFlavor.equals(flavor)) {
080                    res.add(f);
081                }
082            }
083        }
084        return res;
085    }
086
087}