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