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