001/*
002 * (C) Copyright 2012 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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.core.lifecycle.impl;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.lifecycle.extensions.LifeCycleTypesDescriptor;
029import org.nuxeo.runtime.model.ContributionFragmentRegistry;
030
031/**
032 * Registry for lifecycle <-> types association
033 *
034 * @since 5.6
035 */
036public class LifeCycleTypeRegistry extends ContributionFragmentRegistry<LifeCycleTypesDescriptor> {
037
038    private static final Log log = LogFactory.getLog(LifeCycleTypeRegistry.class);
039
040    /** Type name -> life cycle name. */
041    protected Map<String, String> typesMapping = new HashMap<String, String>();
042
043    /**
044     * a mapping from doc type -> list of transitions that should not recurse.
045     */
046    protected Map<String, List<String>> docTypeToNonRecursiveTransition = new HashMap<String, List<String>>();
047
048    @Override
049    public String getContributionId(LifeCycleTypesDescriptor contrib) {
050        return contrib.getDocumentType();
051    }
052
053    @Override
054    public void contributionUpdated(String id, LifeCycleTypesDescriptor contrib, LifeCycleTypesDescriptor newOrigContrib) {
055        log.info("Registering lifecycle types mapping: " + contrib.getDocumentType() + "-" + contrib.getLifeCycleName());
056        typesMapping.put(contrib.getDocumentType(), contrib.getLifeCycleName());
057        String transitionArray = contrib.getNoRecursionForTransitions();
058        List<String> transitions = new ArrayList<String>();
059        if (transitionArray != null && !transitionArray.isEmpty()) {
060            transitions = Arrays.asList(contrib.getNoRecursionForTransitions().split(","));
061        }
062        docTypeToNonRecursiveTransition.put(contrib.getDocumentType(), transitions);
063    }
064
065    @Override
066    public void contributionRemoved(String id, LifeCycleTypesDescriptor origContrib) {
067        typesMapping.remove(id);
068        docTypeToNonRecursiveTransition.remove(id);
069    }
070
071    @Override
072    public boolean isSupportingMerge() {
073        return false;
074    }
075
076    @Override
077    public LifeCycleTypesDescriptor clone(LifeCycleTypesDescriptor orig) {
078        throw new UnsupportedOperationException();
079    }
080
081    @Override
082    public void merge(LifeCycleTypesDescriptor src, LifeCycleTypesDescriptor dst) {
083        throw new UnsupportedOperationException();
084    }
085
086    // API
087
088    public String getLifeCycleNameForType(String docType) {
089        return typesMapping.get(docType);
090    }
091
092    public Collection<String> getTypesFor(String lifeCycleName) {
093        Collection<String> types = new ArrayList<String>();
094        for (String typeName : typesMapping.keySet()) {
095            if (typesMapping.get(typeName).equals(lifeCycleName)) {
096                types.add(typeName);
097            }
098        }
099        return types;
100    }
101
102    public Map<String, String> getTypesMapping() {
103        return typesMapping;
104    }
105
106    public List<String> getNonRecursiveTransitionForDocType(String docTypeName) {
107        return docTypeToNonRecursiveTransition.get(docTypeName);
108    }
109
110}