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.Collection;
020import java.util.HashMap;
021import java.util.HashSet;
022import java.util.Map;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.lifecycle.LifeCycle;
027import org.nuxeo.ecm.core.lifecycle.LifeCycleState;
028import org.nuxeo.ecm.core.lifecycle.extensions.LifeCycleDescriptor;
029import org.nuxeo.runtime.model.ContributionFragmentRegistry;
030
031/**
032 * Registry for life cycles
033 *
034 * @since 5.6
035 */
036public class LifeCycleRegistry extends ContributionFragmentRegistry<LifeCycleDescriptor> {
037
038    private static final Log log = LogFactory.getLog(LifeCycleRegistry.class);
039
040    protected Map<String, LifeCycle> lifeCycles = new HashMap<String, LifeCycle>();
041
042    @Override
043    public String getContributionId(LifeCycleDescriptor contrib) {
044        return contrib.getName();
045    }
046
047    @Override
048    public void contributionUpdated(String id, LifeCycleDescriptor contrib, LifeCycleDescriptor newOrigContrib) {
049        log.info("Registering lifecycle: " + contrib.getName());
050        lifeCycles.put(contrib.getName(), getLifeCycle(contrib));
051    }
052
053    @Override
054    public void contributionRemoved(String id, LifeCycleDescriptor lifeCycleDescriptor) {
055        log.info("Unregistering lifecycle: " + lifeCycleDescriptor.getName());
056        lifeCycles.remove(lifeCycleDescriptor.getName());
057    }
058
059    @Override
060    public boolean isSupportingMerge() {
061        return false;
062    }
063
064    @Override
065    public LifeCycleDescriptor clone(LifeCycleDescriptor orig) {
066        throw new UnsupportedOperationException();
067    }
068
069    @Override
070    public void merge(LifeCycleDescriptor src, LifeCycleDescriptor dst) {
071        throw new UnsupportedOperationException();
072    }
073
074    // API
075
076    public LifeCycle getLifeCycle(String name) {
077        return lifeCycles.get(name);
078    }
079
080    public Collection<LifeCycle> getLifeCycles() {
081        return lifeCycles.values();
082    }
083
084    /**
085     * Returns a life cycle instance out of the life cycle configuration.
086     */
087    public LifeCycle getLifeCycle(LifeCycleDescriptor desc) {
088        String name = desc.getName();
089        String initialStateName = desc.getInitialStateName();
090        String defaultInitialStateName = desc.getDefaultInitialStateName();
091        if (initialStateName != null) {
092            defaultInitialStateName = initialStateName;
093            log.warn(String.format("Lifecycle registration of default initial"
094                    + " state has changed, change initial=\"%s\" to "
095                    + "defaultInitial=\"%s\" in lifecyle '%s' definition", defaultInitialStateName,
096                    defaultInitialStateName, name));
097        }
098        boolean defaultInitialStateFound = false;
099        Collection<String> initialStateNames = new HashSet<String>();
100        Collection<LifeCycleState> states = desc.getStates();
101        for (LifeCycleState state : states) {
102            String stateName = state.getName();
103            if (defaultInitialStateName.equals(stateName)) {
104                defaultInitialStateFound = true;
105                initialStateNames.add(stateName);
106            }
107            if (state.isInitial()) {
108                initialStateNames.add(stateName);
109            }
110        }
111        if (!defaultInitialStateFound) {
112            log.error(String.format("Default initial state %s not found on lifecycle %s", defaultInitialStateName, name));
113        }
114        return new LifeCycleImpl(name, defaultInitialStateName, initialStateNames, states, desc.getTransitions());
115    }
116
117}