001/*
002 * (C) Copyright 2006-2011 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 *     Thomas Roger <troger@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.platform.content.template.service;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.runtime.model.ContributionFragmentRegistry;
031
032/**
033 * Registry of {@link PostContentCreationHandler}s.
034 *
035 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
036 * @since 5.5
037 */
038public class PostContentCreationHandlerRegistry extends
039        ContributionFragmentRegistry<PostContentCreationHandlerDescriptor> {
040
041    private static final Log log = LogFactory.getLog(PostContentCreationHandlerRegistry.class);
042
043    protected Map<String, PostContentCreationHandlerDescriptor> postContentCreationHandlerDescriptors = new HashMap<String, PostContentCreationHandlerDescriptor>();
044
045    @Override
046    public String getContributionId(PostContentCreationHandlerDescriptor contrib) {
047        return contrib.getName();
048    }
049
050    @Override
051    public void contributionUpdated(String id, PostContentCreationHandlerDescriptor contrib,
052            PostContentCreationHandlerDescriptor newOrigContrib) {
053        if (contrib.isEnabled()) {
054            postContentCreationHandlerDescriptors.put(id, contrib);
055        } else {
056            postContentCreationHandlerDescriptors.remove(id);
057        }
058    }
059
060    @Override
061    public void contributionRemoved(String id, PostContentCreationHandlerDescriptor contrib) {
062        postContentCreationHandlerDescriptors.remove(id);
063    }
064
065    @Override
066    public PostContentCreationHandlerDescriptor clone(PostContentCreationHandlerDescriptor postContentCreationHandler) {
067        try {
068            return (PostContentCreationHandlerDescriptor) postContentCreationHandler.clone();
069        } catch (CloneNotSupportedException e) {
070            // this should never occur since clone implements Cloneable
071            throw new RuntimeException(e);
072        }
073    }
074
075    @Override
076    public void merge(PostContentCreationHandlerDescriptor src, PostContentCreationHandlerDescriptor dst) {
077        if (src.getClazz() != null) {
078            dst.setClazz(src.getClazz());
079        }
080        if (src.isEnabled() != dst.isEnabled()) {
081            dst.setEnabled(src.isEnabled());
082        }
083        int order = src.getOrder();
084        if (order > 0 && order != dst.getOrder()) {
085            dst.setOrder(src.getOrder());
086        }
087    }
088
089    public List<PostContentCreationHandler> getOrderedHandlers() {
090        List<PostContentCreationHandlerDescriptor> descs = new ArrayList<PostContentCreationHandlerDescriptor>(
091                postContentCreationHandlerDescriptors.values());
092        Collections.sort(descs);
093
094        List<PostContentCreationHandler> handlers = new ArrayList<PostContentCreationHandler>();
095        for (PostContentCreationHandlerDescriptor desc : descs) {
096            try {
097                handlers.add(desc.getClazz().newInstance());
098            } catch (ReflectiveOperationException e) {
099                log.error("Unable to instantiate class for handler: " + desc.getName(), e);
100            }
101        }
102        return handlers;
103    }
104
105}