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