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.Collection;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import org.apache.commons.lang.StringUtils;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.platform.suggestbox.service.descriptors.SuggestionHandlerDescriptor;
027import org.nuxeo.runtime.model.ContributionFragmentRegistry;
028
029public class SuggestionHandlerRegistry extends ContributionFragmentRegistry<SuggestionHandlerDescriptor> {
030
031    private static final Log log = LogFactory.getLog(SuggestionHandlerRegistry.class);
032
033    protected final Map<String, SuggestionHandlerDescriptor> suggestionHandlerDescriptors = new LinkedHashMap<String, SuggestionHandlerDescriptor>();
034
035    public Collection<SuggestionHandlerDescriptor> getHandlers() {
036        return suggestionHandlerDescriptors.values();
037    }
038
039    public SuggestionHandlerDescriptor getSuggestionHandlerDescriptor(String name) {
040        return suggestionHandlerDescriptors.get(name);
041    }
042
043    @Override
044    public void contributionRemoved(String id, SuggestionHandlerDescriptor descriptor) {
045        log.trace(String.format("Removing contribution with id %s from suggestion handler descriptors", id));
046        suggestionHandlerDescriptors.remove(id);
047    }
048
049    @Override
050    public String getContributionId(SuggestionHandlerDescriptor descriptor) {
051        return descriptor.getName();
052    }
053
054    @Override
055    public void contributionUpdated(String id, SuggestionHandlerDescriptor contrib,
056            SuggestionHandlerDescriptor newOrigContrib) {
057        if (contrib.isEnabled()) {
058            log.trace(String.format("Putting contribution %s with id %s in suggestion handler descriptors", contrib, id));
059            suggestionHandlerDescriptors.put(id, contrib);
060        } else {
061            log.trace(String.format("Removing disabled contribution with id %s from suggestion handler descriptors", id));
062            suggestionHandlerDescriptors.remove(id);
063        }
064    }
065
066    @Override
067    public SuggestionHandlerDescriptor clone(SuggestionHandlerDescriptor suggester) {
068        try {
069            return (SuggestionHandlerDescriptor) suggester.clone();
070        } catch (CloneNotSupportedException e) {
071            // this should never occur since clone implements Cloneable
072            throw new RuntimeException(e);
073        }
074    }
075
076    @Override
077    public void merge(SuggestionHandlerDescriptor src, SuggestionHandlerDescriptor dst) {
078        log.trace(String.format("Merging contribution with id %s to contribution with id %s", src.getName(),
079                dst.getName()));
080        // Enabled
081        if (src.isEnabled() != dst.isEnabled()) {
082            dst.setEnabled(src.isEnabled());
083        }
084        // Type
085        if (!StringUtils.isEmpty(src.getType()) && !src.getType().equals(dst.getType())) {
086            dst.setType(src.getType());
087        }
088        // Suggester group
089        if (!StringUtils.isEmpty(src.getSuggesterGroup()) && !src.getSuggesterGroup().equals(dst.getSuggesterGroup())) {
090            dst.setSuggesterGroup(src.getSuggesterGroup());
091        }
092        // Operation
093        if (!StringUtils.isEmpty(src.getOperation()) && !src.getOperation().equals(dst.getOperation())) {
094            dst.setOperation(src.getOperation());
095        }
096        // Operation chain
097        if (!StringUtils.isEmpty(src.getOperationChain()) && !src.getOperationChain().equals(dst.getOperationChain())) {
098            dst.setOperationChain(src.getOperationChain());
099        }
100    }
101}