001/*
002 * (C) Copyright 2010-2013 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 *     Olivier Grisel
018 */
019package org.nuxeo.ecm.platform.suggestbox.service.registries;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.platform.suggestbox.service.ComponentInitializationException;
026import org.nuxeo.ecm.platform.suggestbox.service.descriptors.SuggesterGroupDescriptor;
027import org.nuxeo.runtime.model.ContributionFragmentRegistry;
028
029public class SuggesterGroupRegistry extends ContributionFragmentRegistry<SuggesterGroupDescriptor> {
030
031    protected final Map<String, SuggesterGroupDescriptor> suggesterGroupDescriptors = new HashMap<String, SuggesterGroupDescriptor>();
032
033    public SuggesterGroupDescriptor getSuggesterGroupDescriptor(String name) {
034        return suggesterGroupDescriptors.get(name);
035    }
036
037    @Override
038    public SuggesterGroupDescriptor clone(SuggesterGroupDescriptor suggestGroup) {
039        try {
040            return (SuggesterGroupDescriptor) suggestGroup.clone();
041        } catch (CloneNotSupportedException e) {
042            // this should never occur since clone implements Cloneable
043            throw new RuntimeException(e);
044        }
045    }
046
047    @Override
048    public void contributionRemoved(String id, SuggesterGroupDescriptor arg1) {
049        suggesterGroupDescriptors.remove(id);
050    }
051
052    @Override
053    public void contributionUpdated(String id, SuggesterGroupDescriptor contrib, SuggesterGroupDescriptor newOrigContrib) {
054        suggesterGroupDescriptors.put(id, contrib);
055    }
056
057    @Override
058    public String getContributionId(SuggesterGroupDescriptor suggestGroup) {
059        return suggestGroup.getName();
060    }
061
062    @Override
063    public void merge(SuggesterGroupDescriptor src, SuggesterGroupDescriptor dst) {
064        try {
065            dst.mergeFrom(src);
066        } catch (ComponentInitializationException e) {
067            throw new NuxeoException(e);
068        }
069    }
070
071}