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 *     Julien Anguenot
018 *     Florent Guillaume
019 */
020
021package org.nuxeo.ecm.core.lifecycle.impl;
022
023import java.util.Collection;
024import java.util.Collections;
025
026import org.nuxeo.ecm.core.lifecycle.LifeCycle;
027import org.nuxeo.ecm.core.lifecycle.LifeCycleState;
028import org.nuxeo.ecm.core.lifecycle.LifeCycleTransition;
029
030/**
031 * Life cycle implementation.
032 *
033 * @see org.nuxeo.ecm.core.lifecycle.LifeCycle
034 * @author Julien Anguenot
035 * @author Florent Guillaume
036 */
037public class LifeCycleImpl implements LifeCycle {
038
039    private final String name;
040
041    private final String defaultInitialStateName;
042
043    private final Collection<String> initialStateNames;
044
045    private final Collection<LifeCycleState> states;
046
047    private final Collection<LifeCycleTransition> transitions;
048
049    public LifeCycleImpl(String name, String defaultInitialStateName, Collection<String> initialStateNames,
050            Collection<LifeCycleState> states, Collection<LifeCycleTransition> transitions) {
051        this.name = name;
052        this.defaultInitialStateName = defaultInitialStateName;
053        this.initialStateNames = initialStateNames;
054        this.states = states;
055        this.transitions = transitions;
056    }
057
058    @Override
059    public String getDefaultInitialStateName() {
060        return defaultInitialStateName;
061    }
062
063    @Override
064    public Collection<String> getInitialStateNames() {
065        return initialStateNames;
066    }
067
068    @Override
069    public String getName() {
070        return name;
071    }
072
073    @Override
074    public Collection<LifeCycleState> getStates() {
075        return states;
076    }
077
078    @Override
079    public LifeCycleState getStateByName(String stateName) {
080        LifeCycleState lifeCycleState = null;
081        for (LifeCycleState state : states) {
082            if (state.getName().equals(stateName)) {
083                lifeCycleState = state;
084                break;
085            }
086        }
087        return lifeCycleState;
088    }
089
090    @Override
091    public Collection<String> getAllowedStateTransitionsFrom(String stateName) {
092        LifeCycleState lifeCycleState = getStateByName(stateName);
093        if (lifeCycleState != null) {
094            return lifeCycleState.getAllowedStateTransitions();
095        } else {
096            return Collections.emptyList();
097        }
098    }
099
100    @Override
101    public Collection<LifeCycleTransition> getTransitions() {
102        return transitions;
103    }
104
105    @Override
106    public LifeCycleTransition getTransitionByName(String transitionName) {
107        LifeCycleTransition lifeCycleTransition = null;
108        for (LifeCycleTransition itransition : transitions) {
109            if (itransition.getName().equals(transitionName)) {
110                lifeCycleTransition = itransition;
111                break;
112            }
113        }
114        return lifeCycleTransition;
115    }
116
117}