001/*
002 * (C) Copyright 2010-2018 Nuxeo (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.lang3.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(
061                    String.format("Putting contribution %s with id %s in suggestion handler descriptors", contrib, id));
062            suggestionHandlerDescriptors.put(id, contrib);
063        } else {
064            log.trace(
065                    String.format("Removing disabled contribution with id %s from suggestion handler descriptors", id));
066            suggestionHandlerDescriptors.remove(id);
067        }
068    }
069
070    @Override
071    public SuggestionHandlerDescriptor clone(SuggestionHandlerDescriptor suggester) {
072        try {
073            return (SuggestionHandlerDescriptor) suggester.clone();
074        } catch (CloneNotSupportedException e) {
075            // this should never occur since clone implements Cloneable
076            throw new RuntimeException(e);
077        }
078    }
079
080    @Override
081    public void merge(SuggestionHandlerDescriptor src, SuggestionHandlerDescriptor dst) {
082        log.trace(String.format("Merging contribution with id %s to contribution with id %s", src.getName(),
083                dst.getName()));
084        // Enabled
085        if (src.isEnabled() != dst.isEnabled()) {
086            dst.setEnabled(src.isEnabled());
087        }
088        // Type
089        if (!StringUtils.isEmpty(src.getType()) && !src.getType().equals(dst.getType())) {
090            dst.setType(src.getType());
091        }
092        // Suggester group
093        if (!StringUtils.isEmpty(src.getSuggesterGroup()) && !src.getSuggesterGroup().equals(dst.getSuggesterGroup())) {
094            dst.setSuggesterGroup(src.getSuggesterGroup());
095        }
096        // Operation
097        if (!StringUtils.isEmpty(src.getOperation()) && !src.getOperation().equals(dst.getOperation())) {
098            dst.setOperation(src.getOperation());
099        }
100        // Operation chain
101        if (!StringUtils.isEmpty(src.getOperationChain()) && !src.getOperationChain().equals(dst.getOperationChain())) {
102            dst.setOperationChain(src.getOperationChain());
103        }
104    }
105}