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